Skip to content

Commit

Permalink
upgrade puppeteer version
Browse files Browse the repository at this point in the history
Summary: upgrade puppeteer dependency to the latest version, which has less security vulnerabilities

Reviewed By: twobassdrum

Differential Revision: D59416041

fbshipit-source-id: d697c13b7457953a6b04f102eedb835e9db44f4c
  • Loading branch information
JacksonGL authored and facebook-github-bot committed Jul 8, 2024
1 parent 30d6be2 commit 7ee364d
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 35 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"chalk": "^4.0.0",
"fs-extra": "^4.0.2",
"minimist": "^1.2.0",
"puppeteer": "^21.0.3",
"puppeteer": "^22.12.1",
"string-width": "^4.2.0",
"util.promisify": "^1.1.1",
"xvfb": "^0.4.0"
Expand All @@ -21,7 +21,6 @@
"@types/jest": "^27.4.1",
"@types/minimist": "^1.2.2",
"@types/node": "^12.16.3",
"@types/puppeteer": "^5.4.4",
"jest": "^29.6.2",
"typescript": "^4.6.3"
},
Expand Down
5 changes: 2 additions & 3 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
"chalk": "^4.0.0",
"fs-extra": "^4.0.2",
"minimist": "^1.2.8",
"puppeteer": "^21.0.3",
"puppeteer-core": "^21.0.3",
"puppeteer": "^22.12.1",
"puppeteer-core": "^22.12.1",
"string-width": "^4.2.0",
"util.promisify": "^1.1.1",
"xvfb": "^0.4.0"
Expand All @@ -46,7 +46,6 @@
"@types/jest": "^27.4.1",
"@types/minimist": "^1.2.5",
"@types/node": "^12.16.3",
"@types/puppeteer": "^5.4.4",
"jest": "^29.6.2",
"ts-jest": "^29.1.1",
"typescript": "^4.6.3"
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/__tests__/heap/examples/example-5.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ test('memory test', async () => {
heap = await takeNodeMinimalHeap();
// if rabbitHole does not add new references, the obj can be GCed
expect(heap.hasObjectWithClassName('TestObject')).toBe(false);
}, 30000);
}, 60000);
2 changes: 1 addition & 1 deletion packages/api/src/__tests__/heap/examples/example-7.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ test('memory test', async () => {

// expect object with marker "memlab-mark-2" can be GCed
expect(heap.hasObjectWithTag('memlab-mark-2')).toBe(false);
}, 30000);
}, 60000);
5 changes: 2 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
"chalk": "^4.0.0",
"fs-extra": "^4.0.2",
"minimist": "^1.2.8",
"puppeteer": "^21.0.3",
"puppeteer-core": "^21.0.3",
"puppeteer": "^22.12.1",
"puppeteer-core": "^22.12.1",
"string-width": "^4.2.0",
"util.promisify": "^1.1.1",
"xvfb": "^0.4.0"
Expand All @@ -48,7 +48,6 @@
"@types/jest": "^27.4.1",
"@types/minimist": "^1.2.5",
"@types/node": "^12.16.3",
"@types/puppeteer": "^5.4.4",
"jest": "^29.6.2",
"ts-jest": "^29.1.1",
"typescript": "^4.6.3"
Expand Down
5 changes: 2 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
"chalk": "^4.0.0",
"fs-extra": "^4.0.2",
"minimist": "^1.2.8",
"puppeteer": "^21.0.3",
"puppeteer-core": "^21.0.3",
"puppeteer": "^22.12.1",
"puppeteer-core": "^22.12.1",
"string-width": "^4.2.0",
"util.promisify": "^1.1.1",
"xvfb": "^0.4.0"
Expand All @@ -47,7 +47,6 @@
"@types/jest": "^27.4.1",
"@types/minimist": "^1.2.5",
"@types/node": "^12.16.3",
"@types/puppeteer": "^5.4.4",
"jest": "^29.6.2",
"ts-jest": "^29.1.1",
"typescript": "^4.6.3"
Expand Down
17 changes: 15 additions & 2 deletions packages/core/src/lib/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
PuppeteerConfig,
} from './Types';

import os from 'os';
import path from 'path';
import modes from '../modes/RunningModes';
import info from './Console';
Expand Down Expand Up @@ -87,6 +88,7 @@ export class MemLabConfig {
_isHeadfulBrowser: boolean;
_disableWebSecurity: boolean;
_browser: string;
_chromeUserDataDir: string;

snapshotHasDetachedness: boolean;
specifiedEngine: boolean;
Expand All @@ -103,7 +105,6 @@ export class MemLabConfig {
workDir: string;
browserDir: string;
dataBaseDir: string;
userDataDir: string;
curDataDir: string;
webSourceDir: string;
debugDataDir: string;
Expand Down Expand Up @@ -263,6 +264,7 @@ export class MemLabConfig {
this.analysisMode = constant.unset;
this.focusFiberNodeId = -1;
this.isFB = constant.isFB;
this._chromeUserDataDir = os.tmpdir();
// assuming the Evn doesn't support Xvfb before checking
this.machineSupportsXVFB = false;
// by default we want to use Xvfb if the Env supports it
Expand All @@ -280,7 +282,7 @@ export class MemLabConfig {
ignoreHTTPSErrors: true,
// Support running on Windows
ignoreDefaultArgs: ['--disable-extensions'],
userDataDir: this.userDataDir,
userDataDir: this._chromeUserDataDir,
// Chromium in ContinuousTest needs this args
args: [
'--no-sandbox',
Expand Down Expand Up @@ -602,6 +604,17 @@ export class MemLabConfig {
this.targetTab = tab;
}

set userDataDir(chromeUserDataDir: string) {
this._chromeUserDataDir = chromeUserDataDir;
if (this.puppeteerConfig != null) {
this.puppeteerConfig.userDataDir = chromeUserDataDir;
}
}

get userDataDir(): string {
return this._chromeUserDataDir;
}

set scenario(scenario: Optional<IScenario>) {
this._scenario = scenario;
if (scenario == null) {
Expand Down
5 changes: 2 additions & 3 deletions packages/e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"chalk": "^4.0.0",
"fs-extra": "^4.0.2",
"minimist": "^1.2.8",
"puppeteer": "^21.0.3",
"puppeteer-core": "^21.0.3",
"puppeteer": "^22.12.1",
"puppeteer-core": "^22.12.1",
"string-width": "^4.2.0",
"util.promisify": "^1.1.1",
"xvfb": "^0.4.0"
Expand All @@ -44,7 +44,6 @@
"@types/jest": "^27.4.1",
"@types/minimist": "^1.2.5",
"@types/node": "^12.16.3",
"@types/puppeteer": "^5.4.4",
"jest": "^29.6.2",
"ts-jest": "^29.1.1",
"typescript": "^4.6.3"
Expand Down
48 changes: 40 additions & 8 deletions packages/e2e/src/E2EInteractionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ const {
getURLParameter,
} = E2EUtils;

type PuppeteerDataHandler = (data: {chunk: string}) => void;
type PuppeteerProgressHandler = (data: {
done: number;
total: number;
finished?: boolean;
}) => void;
type DataHandler = PuppeteerDataHandler | PuppeteerProgressHandler;

// some synced implementation of memlab based on this codebase
// may be using an old version of puppeteer that doesn't have page.$
interface SessionWithRemoveListenerFromAllVersions {
removeListener?: (type: string, dataHandler: DataHandler) => void;
removeAllListeners: (type: string) => void;
}

export default class E2EInteractionManager {
private mainThreadCdpsession: Optional<CDPSession>;
private page: Page;
Expand Down Expand Up @@ -389,16 +404,12 @@ export default class E2EInteractionManager {
) {
const writeStream = fs.createWriteStream(file, {encoding: 'UTF-8'});
let lastChunk = '';
const dataHandler = (data: {chunk: string}) => {
const dataHandler: PuppeteerDataHandler = data => {
writeStream.write(data.chunk);
lastChunk = data.chunk;
};

const progressHandler = (data: {
done: number;
total: number;
finished?: boolean;
}) => {
const progressHandler: PuppeteerProgressHandler = data => {
const percent = ((100 * data.done) / data.total) | 0;
if (!config.isContinuousTest) {
info.overwrite(`heap snapshot ${percent}% complete`);
Expand All @@ -415,14 +426,35 @@ export default class E2EInteractionManager {
});

checkLastSnapshotChunk(lastChunk);
session.removeListener('HeapProfiler.addHeapSnapshotChunk', dataHandler);
session.removeListener(
this.removeListener(
session,
'HeapProfiler.addHeapSnapshotChunk',
dataHandler,
);
this.removeListener(
session,
'HeapProfiler.reportHeapSnapshotProgress',
progressHandler,
);
writeStream.end();
}

// this implement may remove all dataHandler of the specified eventType
private removeListener(
session: SessionWithRemoveListenerFromAllVersions,
eventType: string,
dataHandler: DataHandler,
) {
if (typeof session.removeListener === 'function') {
session.removeListener(eventType, dataHandler);
return;
}
if (typeof session.removeAllListeners === 'function') {
session.removeAllListeners(eventType);
return;
}
}

private async saveHeapSnapshotToFile(file: string): Promise<void> {
info.beginSection('heap snapshot');
const start = Date.now();
Expand Down
57 changes: 54 additions & 3 deletions packages/e2e/src/lib/operations/InteractionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import {info, config} from '@memlab/core';
import path from 'path';

import type {Page, ElementHandle} from 'puppeteer';
import type {CheckPageLoadCallback, OperationArgs} from '@memlab/core';
import type {
CheckPageLoadCallback,
OperationArgs,
Optional,
} from '@memlab/core';

function waitFor(delay: number): Promise<void> {
return new Promise(resolve => {
Expand Down Expand Up @@ -213,8 +217,55 @@ async function getElementsContainingText(
text: string,
): Promise<ElementHandle<Element>[]> {
const xpath = `//*[not(self::script)][contains(text(), '${text}')]`;
const elements = await page.$x(xpath);
return elements as ElementHandle<Element>[];
return findBySelector(page, xpath);
}

// some synced implementation of memlab based on this codebase
// may be using an old version of puppeteer that doesn't have page.$
interface PageWithSelectorAllVersions {
$?: (xpath: string) => Promise<Optional<ElementHandle<Element>[]>>;
$x?: (xpath: string) => Promise<Optional<ElementHandle<Element>[]>>;
}

async function findBySelector(
page: Page,
xpath: string,
): Promise<ElementHandle<Element>[]> {
const _page = page as unknown as PageWithSelectorAllVersions;
if (typeof _page.$x === 'function') {
return ((await _page.$x(xpath)) ?? []) as ElementHandle<Element>[];
}

// evaluate in browser and return the result as a JSHandle
// to an array of elements in the browser's context
const elements = await page.evaluateHandle(xpath => {
const xpathResult = document.evaluate(
xpath,
document,
null,
XPathResult.ORDERED_NODE_ITERATOR_TYPE,
null,
);

const nodes = [];
let node = xpathResult.iterateNext();
while (node) {
nodes.push(node);
node = xpathResult.iterateNext();
}
return nodes;
}, xpath);

// Convert the elements to ElementHandle<Element>[]
const elementHandles = await elements.getProperties();
const ret = [];
for (const elementHandle of elementHandles.values()) {
const element = elementHandle.asElement();
if (element != null) {
ret.push(element as ElementHandle<Element>);
}
}
return ret;
}

export default {
Expand Down
5 changes: 2 additions & 3 deletions packages/heap-analysis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"chalk": "^4.0.0",
"fs-extra": "^4.0.2",
"minimist": "^1.2.8",
"puppeteer": "^21.0.3",
"puppeteer-core": "^21.0.3",
"puppeteer": "^22.12.1",
"puppeteer-core": "^22.12.1",
"string-width": "^4.2.0",
"util.promisify": "^1.1.1",
"xvfb": "^0.4.0"
Expand All @@ -38,7 +38,6 @@
"@types/jest": "^27.4.1",
"@types/minimist": "^1.2.5",
"@types/node": "^12.16.3",
"@types/puppeteer": "^5.4.4",
"jest": "^29.6.2",
"ts-jest": "^29.1.1",
"typescript": "^4.6.3"
Expand Down
5 changes: 2 additions & 3 deletions packages/memlab/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
"chalk": "^4.0.0",
"fs-extra": "^4.0.2",
"minimist": "^1.2.8",
"puppeteer": "^21.0.3",
"puppeteer-core": "^21.0.3",
"puppeteer": "^22.12.1",
"puppeteer-core": "^22.12.1",
"string-width": "^4.2.0",
"util.promisify": "^1.1.1",
"xvfb": "^0.4.0"
Expand All @@ -47,7 +47,6 @@
"@types/jest": "^27.4.1",
"@types/minimist": "^1.2.5",
"@types/node": "^12.16.3",
"@types/puppeteer": "^5.4.4",
"jest": "^29.6.2",
"ts-jest": "^29.1.1",
"typescript": "^4.6.3"
Expand Down

0 comments on commit 7ee364d

Please sign in to comment.