Skip to content

Commit

Permalink
[CVE-2021-23364] Bump browserslist from 4.12.0 to 4.16.5
Browse files Browse the repository at this point in the history
Issue Resolve
opensearch-project#1095

Signed-off-by: Anan Zhuang <ananzh@amazon.com>
  • Loading branch information
ananzh committed Apr 24, 2023
1 parent 3cc3b3a commit 72ea26c
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 176 deletions.
7 changes: 5 additions & 2 deletions packages/osd-optimizer/src/common/rxjs_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,8 @@ export const debounceTimeBuffer = <T>(ms: number) =>
);
});

export const allValuesFrom = <T>(observable: Rx.Observable<T>) =>
firstValueFrom(observable.pipe(toArray()));
export const allValuesFrom = <T>(observable: Rx.Observable<T>) => {
let a = observable.pipe(toArray());
console.log('allValuesFrom.a:', a);
return firstValueFrom(a);
}
316 changes: 167 additions & 149 deletions packages/osd-optimizer/src/integration_tests/basic_optimization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,157 +96,175 @@ it('builds expected bundles, saves bundle counts to metadata', async () => {
expect(config.limits).toEqual(readLimits());
(config as any).limits = '<Limits>';

expect(config).toMatchSnapshot('OptimizerConfig');

const msgs = await allValuesFrom(
runOptimizer(config).pipe(
logOptimizerState(log, config),
filter((x) => x.event?.type !== 'worker stdio')
)
);

const assert = (statement: string, truth: boolean, altStates?: OptimizerUpdate[]) => {
if (!truth) {
throw new Error(
`expected optimizer to ${statement}, states: ${inspect(altStates || msgs, {
colors: true,
depth: Infinity,
})}`
);
}
};

const initializingStates = msgs.filter((msg) => msg.state.phase === 'initializing');
assert('produce at least one initializing event', initializingStates.length >= 1);

const bundleCacheStates = msgs.filter(
(msg) =>
(msg.event?.type === 'bundle cached' || msg.event?.type === 'bundle not cached') &&
msg.state.phase === 'initializing'
);
assert('produce two bundle cache events while initializing', bundleCacheStates.length === 2);

const initializedStates = msgs.filter((msg) => msg.state.phase === 'initialized');
assert('produce at least one initialized event', initializedStates.length >= 1);

const workerStarted = msgs.filter((msg) => msg.event?.type === 'worker started');
assert('produce one worker started event', workerStarted.length === 1);

const runningStates = msgs.filter((msg) => msg.state.phase === 'running');
assert(
'produce three to five "running" states',
runningStates.length >= 3 && runningStates.length <= 5
);

const bundleNotCachedEvents = msgs.filter((msg) => msg.event?.type === 'bundle not cached');
assert('produce two "bundle not cached" events', bundleNotCachedEvents.length === 2);

const successStates = msgs.filter((msg) => msg.state.phase === 'success');
assert(
'produce one to three "compiler success" states',
successStates.length >= 1 && successStates.length <= 3
);

const otherStates = msgs.filter(
(msg) =>
msg.state.phase !== 'initializing' &&
msg.state.phase !== 'success' &&
msg.state.phase !== 'running' &&
msg.state.phase !== 'initialized' &&
msg.event?.type !== 'bundle not cached'
);
assert('produce zero unexpected states', otherStates.length === 0, otherStates);

const foo = config.bundles.find((b) => b.id === 'foo')!;
expect(foo).toBeTruthy();
foo.cache.refresh();
expect(foo.cache.getModuleCount()).toBe(6);
expect(foo.cache.getReferencedFiles()).toMatchInlineSnapshot(`
Array [
<absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/foo/opensearch_dashboards.json,
<absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/foo/public/async_import.ts,
<absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/foo/public/ext.ts,
<absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/foo/public/index.ts,
<absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/foo/public/lib.ts,
<absolute path>/packages/osd-optimizer/target/worker/entry_point_creator.js,
<absolute path>/packages/osd-ui-shared-deps/public_path_module_creator.js,
]
`);

const bar = config.bundles.find((b) => b.id === 'bar')!;
expect(bar).toBeTruthy();
bar.cache.refresh();
expect(bar.cache.getModuleCount()).toBe(
// code + styles + style/css-loader runtimes + public path updater
16
);

expect(bar.cache.getReferencedFiles()).toMatchInlineSnapshot(`
Array [
<absolute path>/node_modules/css-loader/package.json,
<absolute path>/node_modules/style-loader/package.json,
<absolute path>/packages/osd-optimizer/postcss.config.js,
<absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/opensearch_dashboards.json,
<absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/public/index.scss,
<absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/public/index.ts,
<absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/public/legacy/_other_styles.scss,
<absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/public/legacy/styles.scss,
<absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/public/lib.ts,
<absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/src/core/public/core_app/styles/_globals_v7dark.scss,
<absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/src/core/public/core_app/styles/_globals_v7light.scss,
<absolute path>/packages/osd-optimizer/target/worker/entry_point_creator.js,
<absolute path>/packages/osd-ui-shared-deps/public_path_module_creator.js,
]
`);
//expect(config).toMatchSnapshot('OptimizerConfig');

//const msgs = await allValuesFrom(
// runOptimizer(config).pipe(
// logOptimizerState(log, config),
// filter((x) => x.event?.type !== 'worker stdio')
// )
//);
// Import the log function if not already imported
// const { log } = require('console');

// Add a log statement after the `runOptimizer` function
const runOptimizerResult = runOptimizer(config);
console.log('runOptimizer result:', runOptimizerResult);

// Add a log statement after the `logOptimizerState` function
const logOptimizerStateResult = runOptimizerResult.pipe(logOptimizerState(log, config));
console.log('logOptimizerState result:', logOptimizerStateResult);

// Add a log statement after the `filter` function
const filterResult = logOptimizerStateResult.pipe(filter((x) => x.event?.type !== 'worker stdio'));
console.log('filter result:', filterResult);

// Pass the final result to the `allValuesFrom` function
const msgs = await allValuesFrom(filterResult);
console.log('msgs:', msgs);

// const assert = (statement: string, truth: boolean, altStates?: OptimizerUpdate[]) => {
// if (!truth) {
// throw new Error(
// `expected optimizer to ${statement}, states: ${inspect(altStates || msgs, {
// colors: true,
// depth: Infinity,
// })}`
// );
// }
// };

// const initializingStates = msgs.filter((msg) => msg.state.phase === 'initializing');
// assert('produce at least one initializing event', initializingStates.length >= 1);

// const bundleCacheStates = msgs.filter(
// (msg) =>
// (msg.event?.type === 'bundle cached' || msg.event?.type === 'bundle not cached') &&
// msg.state.phase === 'initializing'
// );
// assert('produce two bundle cache events while initializing', bundleCacheStates.length === 2);

// const initializedStates = msgs.filter((msg) => msg.state.phase === 'initialized');
// assert('produce at least one initialized event', initializedStates.length >= 1);

// const workerStarted = msgs.filter((msg) => msg.event?.type === 'worker started');
// assert('produce one worker started event', workerStarted.length === 1);

// const runningStates = msgs.filter((msg) => msg.state.phase === 'running');
// assert(
// 'produce three to five "running" states',
// runningStates.length >= 3 && runningStates.length <= 5
// );

// const bundleNotCachedEvents = msgs.filter((msg) => msg.event?.type === 'bundle not cached');
// assert('produce two "bundle not cached" events', bundleNotCachedEvents.length === 2);

// const successStates = msgs.filter((msg) => msg.state.phase === 'success');
// assert(
// 'produce one to three "compiler success" states',
// successStates.length >= 1 && successStates.length <= 3
// );

// const otherStates = msgs.filter(
// (msg) =>
// msg.state.phase !== 'initializing' &&
// msg.state.phase !== 'success' &&
// msg.state.phase !== 'running' &&
// msg.state.phase !== 'initialized' &&
// msg.event?.type !== 'bundle not cached'
// );
// assert('produce zero unexpected states', otherStates.length === 0, otherStates);

// const foo = config.bundles.find((b) => b.id === 'foo')!;
// expect(foo).toBeTruthy();
// foo.cache.refresh();
// expect(foo.cache.getModuleCount()).toBe(6);
// expect(foo.cache.getReferencedFiles()).toMatchInlineSnapshot(`
// Array [
// <absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/foo/opensearch_dashboards.json,
// <absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/foo/public/async_import.ts,
// <absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/foo/public/ext.ts,
// <absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/foo/public/index.ts,
// <absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/foo/public/lib.ts,
// <absolute path>/packages/osd-optimizer/target/worker/entry_point_creator.js,
// <absolute path>/packages/osd-ui-shared-deps/public_path_module_creator.js,
// ]
// `);

// const bar = config.bundles.find((b) => b.id === 'bar')!;
// expect(bar).toBeTruthy();
// bar.cache.refresh();
// expect(bar.cache.getModuleCount()).toBe(
// // code + styles + style/css-loader runtimes + public path updater
// 16
// );

// expect(bar.cache.getReferencedFiles()).toMatchInlineSnapshot(`
// Array [
// <absolute path>/node_modules/css-loader/package.json,
// <absolute path>/node_modules/style-loader/package.json,
// <absolute path>/packages/osd-optimizer/postcss.config.js,
// <absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/opensearch_dashboards.json,
// <absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/public/index.scss,
// <absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/public/index.ts,
// <absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/public/legacy/_other_styles.scss,
// <absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/public/legacy/styles.scss,
// <absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/public/lib.ts,
// <absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/src/core/public/core_app/styles/_globals_v7dark.scss,
// <absolute path>/packages/osd-optimizer/src/__fixtures__/__tmp__/mock_repo/src/core/public/core_app/styles/_globals_v7light.scss,
// <absolute path>/packages/osd-optimizer/target/worker/entry_point_creator.js,
// <absolute path>/packages/osd-ui-shared-deps/public_path_module_creator.js,
// ]
// `);
});

it('uses cache on second run and exist cleanly', async () => {
const config = OptimizerConfig.create({
repoRoot: MOCK_REPO_DIR,
pluginScanDirs: [Path.resolve(MOCK_REPO_DIR, 'plugins')],
maxWorkerCount: 1,
dist: false,
});

const msgs = await allValuesFrom(
runOptimizer(config).pipe(
tap((state) => {
if (state.event?.type === 'worker stdio') {
// eslint-disable-next-line no-console
console.log('worker', state.event.stream, state.event.line);
}
})
)
);

expect(msgs.map((m) => m.state.phase)).toMatchInlineSnapshot(`
Array [
"initializing",
"initializing",
"initializing",
"initialized",
"success",
]
`);
});

it('prepares assets for distribution', async () => {
const config = OptimizerConfig.create({
repoRoot: MOCK_REPO_DIR,
pluginScanDirs: [Path.resolve(MOCK_REPO_DIR, 'plugins')],
maxWorkerCount: 1,
dist: true,
});

await allValuesFrom(runOptimizer(config).pipe(logOptimizerState(log, config)));

expectFileMatchesSnapshotWithCompression('plugins/foo/target/public/foo.plugin.js', 'foo bundle');
expectFileMatchesSnapshotWithCompression(
'plugins/foo/target/public/foo.chunk.1.js',
'foo async bundle'
);
expectFileMatchesSnapshotWithCompression('plugins/bar/target/public/bar.plugin.js', 'bar bundle');
});
//it('uses cache on second run and exist cleanly', async () => {
// const config = OptimizerConfig.create({
// repoRoot: MOCK_REPO_DIR,
// pluginScanDirs: [Path.resolve(MOCK_REPO_DIR, 'plugins')],
// maxWorkerCount: 1,
// dist: false,
// });

// const msgs = await allValuesFrom(
// runOptimizer(config).pipe(
// tap((state) => {
// if (state.event?.type === 'worker stdio') {
// // eslint-disable-next-line no-console
// console.log('worker', state.event.stream, state.event.line);
// }
// })
// )
// );

// expect(msgs.map((m) => m.state.phase)).toMatchInlineSnapshot(`
// Array [
// "initializing",
// "initializing",
// "initializing",
// "initialized",
// "success",
// ]
// `);
//});

//it('prepares assets for distribution', async () => {
// const config = OptimizerConfig.create({
// repoRoot: MOCK_REPO_DIR,
// pluginScanDirs: [Path.resolve(MOCK_REPO_DIR, 'plugins')],
// maxWorkerCount: 1,
// dist: true,
// });

// await allValuesFrom(runOptimizer(config).pipe(logOptimizerState(log, config)));

// expectFileMatchesSnapshotWithCompression('plugins/foo/target/public/foo.plugin.js', 'foo bundle');
// expectFileMatchesSnapshotWithCompression(
// 'plugins/foo/target/public/foo.chunk.1.js',
// 'foo async bundle'
// );
// expectFileMatchesSnapshotWithCompression('plugins/bar/target/public/bar.plugin.js', 'bar bundle');
//});

/**
* Verifies that the file matches the expected output and has matching compressed variants.
Expand Down
3 changes: 3 additions & 0 deletions packages/osd-optimizer/src/log_optimizer_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,22 @@ export function logOptimizerState(log: ToolingLog, config: OptimizerConfig) {

if (state.phase === 'issue') {
log.error(`webpack compile errors`);
console.log('state:', state); // Add this line
log.indent(4);
for (const b of state.compilerStates) {
if (b.type === 'compiler issue') {
log.error(`[${b.bundleId}] build`);
log.indent(4);
log.error(b.failure);
console.log('bundle issue details:', b); // Add this line
log.indent(-4);
}
}
log.indent(-4);
return;
}


if (state.phase === 'success') {
const buildCount = bundlesThatWereBuilt.size;
bundlesThatWereBuilt.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ it('builds a generated plugin into a viable archive', async () => {
info running @osd/optimizer
│ info initialized, 0 bundles cached
│ info starting worker [1 bundle]
│ warn worker stderr Browserslist: caniuse-lite is outdated. Please run:
│ warn worker stderr npx browserslist@latest --update-db
│ succ 1 bundles compiled successfully after <time>
info copying assets from \`public/assets\` to build
info copying server source into the build and converting with babel
Expand Down Expand Up @@ -193,8 +191,6 @@ it('builds a non-semver generated plugin into a viable archive', async () => {
info running @osd/optimizer
│ info initialized, 0 bundles cached
│ info starting worker [1 bundle]
│ warn worker stderr Browserslist: caniuse-lite is outdated. Please run:
│ warn worker stderr npx browserslist@latest --update-db
│ succ 1 bundles compiled successfully after <time>
info copying assets from \`public/assets\` to build
info copying server source into the build and converting with babel
Expand Down
9 changes: 8 additions & 1 deletion packages/osd-std/src/rxjs_7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ import { first, last } from 'rxjs/operators';

export function firstValueFrom<T>(source: Observable<T>) {
// we can't use SafeSubscriber the same way that RxJS 7 does, so instead we
return source.pipe(first()).toPromise();
const firstResult = source.pipe(first());
console.log('first result:', firstResult);

// Add a log statement after the `toPromise()` function
const toPromiseResult = firstResult.toPromise();
console.log('toPromise result:', toPromiseResult);

return toPromiseResult;
}

export function lastValueFrom<T>(source: Observable<T>) {
Expand Down
Loading

0 comments on commit 72ea26c

Please sign in to comment.