Skip to content

Commit

Permalink
Setup puppeteer-core fork
Browse files Browse the repository at this point in the history
Part two of two, this gets the initial changes in
  • Loading branch information
Phil Syme authored and edevil committed Apr 26, 2023
1 parent cddab39 commit c80de5c
Show file tree
Hide file tree
Showing 34 changed files with 4,024 additions and 2,591 deletions.
2 changes: 1 addition & 1 deletion install.js
Expand Up @@ -32,7 +32,7 @@ async function download() {
const {
downloadBrowser,
logPolitely,
} = require('./lib/cjs/puppeteer/node/install.js');
} = require('./test/build/node/install.js');

if (process.env.PUPPETEER_SKIP_DOWNLOAD) {
logPolitely(
Expand Down
5,815 changes: 3,674 additions & 2,141 deletions package-lock.json

Large diffs are not rendered by default.

43 changes: 22 additions & 21 deletions package.json
@@ -1,20 +1,21 @@
{
"name": "puppeteer",
"version": "17.0.0",
"description": "A high-level API to control headless Chrome over the DevTools Protocol",
"name": "@cloudflare/puppeteer",
"version": "0.0.0-alpha.6",
"description": "A high-level API to control headless Chrome over the DevTools Protocol for use in Workers",
"keywords": [
"puppeteer",
"chrome",
"headless",
"automation"
"automation",
"workers"
],
"type": "commonjs",
"main": "./lib/cjs/puppeteer/puppeteer.js",
"main": "./lib/cjs/puppeteer/puppeteer-core.js",
"exports": {
".": {
"types": "./lib/types.d.ts",
"import": "./lib/esm/puppeteer/puppeteer.js",
"require": "./lib/cjs/puppeteer/puppeteer.js"
"import": "./lib/esm/puppeteer/puppeteer-core.js",
"require": "./lib/cjs/puppeteer/puppeteer-core.js"
},
"./*": {
"import": "./*",
Expand All @@ -36,7 +37,7 @@
"test:chrome:headless-chrome": "cross-env HEADLESS=chrome PUPPETEER_DEFERRED_PROMISE_DEBUG_TIMEOUT=20000 mocha",
"test:chrome:headful": "cross-env HEADLESS=false PUPPETEER_DEFERRED_PROMISE_DEBUG_TIMEOUT=20000 mocha",
"prepublishOnly": "npm run build",
"prepare": "node typescript-if-required.js && husky install",
"prepare": "node typescript-if-required.js",
"lint": "run-s lint:prettier lint:eslint",
"lint:prettier": "prettier --check .",
"lint:eslint": "([ \"$CI\" = true ] && eslint --ext js --ext ts --quiet -f codeframe . || eslint --ext js --ext ts .)",
Expand All @@ -58,7 +59,6 @@
"build": "run-s generate:sources build:tsc generate:types generate:esm-package-json",
"build:tsc": "tsc --version && run-p build:tsc:*",
"build:tsc:esm": "tsc -b src/tsconfig.esm.json",
"build:tsc:cjs": "tsc -b src/tsconfig.cjs.json",
"build:tsc:test": "tsc -b test"
},
"files": [
Expand All @@ -70,17 +70,11 @@
"author": "The Chromium Authors",
"license": "Apache-2.0",
"dependencies": {
"cross-fetch": "3.1.5",
"debug": "4.3.4",
"devtools-protocol": "0.0.1019158",
"extract-zip": "2.0.1",
"https-proxy-agent": "5.0.1",
"progress": "2.0.3",
"proxy-from-env": "1.1.0",
"rimraf": "3.0.2",
"tar-fs": "2.1.1",
"unbzip2-stream": "1.4.3",
"ws": "8.8.1"
"events": "^3.3.0",
"stream": "^0.0.2",
"url": "^0.11.0",
"util": "^0.12.5"
},
"devDependencies": {
"@commitlint/cli": "17.0.3",
Expand Down Expand Up @@ -121,8 +115,9 @@
"eslint-plugin-unused-imports": "2.0.0",
"esprima": "4.0.1",
"expect": "25.2.7",
"extract-zip": "^2.0.1",
"gts": "4.0.0",
"husky": "8.0.1",
"https-proxy-agent": "^5.0.1",
"jpeg-js": "0.4.4",
"mime": "3.0.0",
"minimist": "1.2.6",
Expand All @@ -132,12 +127,18 @@
"pixelmatch": "5.3.0",
"pngjs": "6.0.0",
"prettier": "2.7.1",
"progress": "^2.0.3",
"proxy-from-env": "^1.1.0",
"rimraf": "3.0.2",
"semver": "7.3.7",
"sinon": "14.0.0",
"source-map-support": "0.5.21",
"tar-fs": "^2.1.1",
"text-diff": "1.0.1",
"tsd": "0.22.0",
"tsx": "3.8.2",
"typescript": "4.7.4"
"typescript": "4.7.4",
"unbzip2-stream": "^1.4.3",
"ws": "^8.8.1"
}
}
19 changes: 16 additions & 3 deletions src/common/Browser.ts
Expand Up @@ -229,7 +229,8 @@ export class Browser extends EventEmitter {
process?: ChildProcess,
closeCallback?: BrowserCloseCallback,
targetFilterCallback?: TargetFilterCallback,
isPageTargetCallback?: IsPageTargetCallback
isPageTargetCallback?: IsPageTargetCallback,
sessionId?: string
): Promise<Browser> {
const browser = new Browser(
product,
Expand All @@ -240,7 +241,8 @@ export class Browser extends EventEmitter {
process,
closeCallback,
targetFilterCallback,
isPageTargetCallback
isPageTargetCallback,
sessionId,
);
await browser._attach();
return browser;
Expand All @@ -256,6 +258,7 @@ export class Browser extends EventEmitter {
#contexts: Map<string, BrowserContext>;
#screenshotTaskQueue: TaskQueue;
#targetManager: TargetManager;
#sessionId: string;

/**
* @internal
Expand All @@ -276,7 +279,8 @@ export class Browser extends EventEmitter {
process?: ChildProcess,
closeCallback?: BrowserCloseCallback,
targetFilterCallback?: TargetFilterCallback,
isPageTargetCallback?: IsPageTargetCallback
isPageTargetCallback?: IsPageTargetCallback,
sessionId?: string
) {
super();
product = product || 'chrome';
Expand Down Expand Up @@ -313,6 +317,7 @@ export class Browser extends EventEmitter {
new BrowserContext(this.#connection, this, contextId)
);
}
this.#sessionId = sessionId || 'unknown'
}

#emitDisconnected = () => {
Expand Down Expand Up @@ -745,9 +750,17 @@ export class Browser extends EventEmitter {
return !this.#connection._closed;
}

/**
* Get the BISO session ID associated with this browser
*/
sessionId(): string {
return this.#sessionId
}

#getVersion(): Promise<Protocol.Browser.GetVersionResponse> {
return this.#connection.send('Browser.getVersion');
}

}
/**
* @public
Expand Down
12 changes: 6 additions & 6 deletions src/common/BrowserConnector.ts
Expand Up @@ -16,7 +16,6 @@

import {debugError} from './util.js';
import {isErrorLike} from '../util/ErrorLike.js';
import {isNode} from '../environment.js';
import {assert} from '../util/assert.js';
import {
Browser,
Expand All @@ -25,6 +24,7 @@ import {
} from './Browser.js';
import {Connection} from './Connection.js';
import {ConnectionTransport} from './ConnectionTransport.js';
import {BrowserWebSocketTransport} from './BrowserWebSocketTransport.js';
import {getFetch} from './fetch.js';
import {Viewport} from './PuppeteerViewport.js';
/**
Expand Down Expand Up @@ -58,10 +58,7 @@ export interface BrowserConnectOptions {
}

const getWebSocketTransportClass = async () => {
return isNode
? (await import('../node/NodeWebSocketTransport.js')).NodeWebSocketTransport
: (await import('./BrowserWebSocketTransport.js'))
.BrowserWebSocketTransport;
return BrowserWebSocketTransport;
};

/**
Expand All @@ -75,6 +72,7 @@ export async function _connectToBrowser(
browserWSEndpoint?: string;
browserURL?: string;
transport?: ConnectionTransport;
sessionId?: string;
}
): Promise<Browser> {
const {
Expand All @@ -86,6 +84,7 @@ export async function _connectToBrowser(
slowMo = 0,
targetFilter,
_isPageTarget: isPageTarget,
sessionId = "unknown",
} = options;

assert(
Expand Down Expand Up @@ -129,7 +128,8 @@ export async function _connectToBrowser(
return connection.send('Browser.close').catch(debugError);
},
targetFilter,
isPageTarget
isPageTarget,
sessionId
);
await browser.pages();
return browser;
Expand Down
46 changes: 27 additions & 19 deletions src/common/ExecutionContext.ts
Expand Up @@ -249,25 +249,33 @@ export class ExecutionContext {
: createJSHandle(this, remoteObject);
}

let functionText = pageFunction.toString();
try {
new Function('(' + functionText + ')');
} catch (error) {
// This means we might have a function shorthand. Try another
// time prefixing 'function '.
if (functionText.startsWith('async ')) {
functionText =
'async function ' + functionText.substring('async '.length);
} else {
functionText = 'function ' + functionText;
}
try {
new Function('(' + functionText + ')');
} catch (error) {
// We tried hard to serialize, but there's a weird beast here.
throw new Error('Passed function is not well-serializable!');
}
}
const functionText = pageFunction.toString();

/**
* We remove the check for the ability for dynamic javascript to be
* serializable, because the Workers runtime does not allow dynamic
* javascript to be executed, as a security precaution. In the
* future, we can consider the serialization check in the back end
* but before the message gets to the remote browser.
* try {
* new Function('(' + functionText + ')');
* } catch (error) {
* // This means we might have a function shorthand. Try another
* // time prefixing 'function '.
* if (functionText.startsWith('async ')) {
* functionText =
* 'async function ' + functionText.substring('async '.length);
* } else {
* functionText = 'function ' + functionText;
* }
* try {
* new Function('(' + functionText + ')');
* } catch (error) {
* // We tried hard to serialize, but there's a weird beast here.
* throw new Error('Passed function is not well-serializable!');
* }
* }
*/
let callFunctionOnPromise;
try {
callFunctionOnPromise = this._client.send('Runtime.callFunctionOn', {
Expand Down
47 changes: 24 additions & 23 deletions src/common/IsolatedWorld.ts
Expand Up @@ -34,7 +34,6 @@ import {EvaluateFunc, HandleFor, NodeFor} from './types.js';
import {
createJSHandle,
debugError,
importFS,
isNumber,
isString,
makePredicateString,
Expand Down Expand Up @@ -447,7 +446,7 @@ export class IsolatedWorld {
path?: string;
content?: string;
}): Promise<ElementHandle<HTMLStyleElement | HTMLLinkElement>> {
const {url = null, path = null, content = null} = options;
const {url = null, content = null} = options;
if (url !== null) {
try {
const context = await this.executionContext();
Expand All @@ -460,27 +459,29 @@ export class IsolatedWorld {
}
}

if (path !== null) {
let fs: typeof import('fs').promises;
try {
fs = (await importFS()).promises;
} catch (error) {
if (error instanceof TypeError) {
throw new Error(
'Cannot pass a filepath to addStyleTag in the browser environment.'
);
}
throw error;
}

let contents = await fs.readFile(path, 'utf8');
contents += '/*# sourceURL=' + path.replace(/\n/g, '') + '*/';
const context = await this.executionContext();
return (await context.evaluateHandle(
addStyleContent,
contents
)) as ElementHandle<HTMLStyleElement>;
}
/**
* if (path !== null) {
* let fs: typeof import('fs').promises;
* try {
* fs = (await importFS()).promises;
* } catch (error) {
* if (error instanceof TypeError) {
* throw new Error(
* 'Cannot pass a filepath to addStyleTag in the browser environment.'
* );
* }
* throw error;
* }
*
* let contents = await fs.readFile(path, 'utf8');
* contents += '/ _# sourceURL=' + path.replace(/\n/g, '') + '_ /';
* const context = await this.executionContext();
* return (await context.evaluateHandle(
* addStyleContent,
* contents
* )) as ElementHandle<HTMLStyleElement>;
* }
*/

if (content !== null) {
const context = await this.executionContext();
Expand Down

0 comments on commit c80de5c

Please sign in to comment.