diff --git a/README.md b/README.md index 900705237..d5451aa66 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Workers version of Puppeteer Core This repo is a fork of main puppeteer project. It creates a version of -puppeteer core specialized for use in Cloudflare workers. +puppeteer core specialized for use in Cloudflare workers [Browser Rendering](https://developers.cloudflare.com/browser-rendering) API. The goals of the fork are: @@ -14,6 +14,13 @@ Note that the main branch in this repo is branched off of version 17.0.0 of the library, to match the currently deployed version of Chromium on the edge. +### Requirements + +- You need [Wrangler](https://github.com/cloudflare/workers-sdk) 3.0.0 or higher. +- Puppeteer requires a few Node.js APIs to work. Add `compatibility_flags = [ "nodejs_compat" ]` to your [wrangler.toml](https://developers.cloudflare.com/workers/wrangler/configuration/) configuration. + +Read our [Developer Documentation](https://developers.cloudflare.com/browser-rendering/platform/puppeteer/) for more information. + Original README follows... # Puppeteer diff --git a/compat/cjs/compat.ts b/compat/cjs/compat.ts index 782727e00..394abfc11 100644 --- a/compat/cjs/compat.ts +++ b/compat/cjs/compat.ts @@ -1,4 +1,4 @@ -import {dirname} from 'path'; +import {dirname} from 'node:path'; /** * @internal diff --git a/package-lock.json b/package-lock.json index b9919f341..0b1231ff7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@cloudflare/puppeteer", - "version": "0.0.4", + "version": "0.0.5", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/package.json b/package.json index 18707c397..0af2c0ae9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cloudflare/puppeteer", - "version": "0.0.4", + "version": "0.0.5", "description": "A high-level API to control headless Chrome over the DevTools Protocol for use in Workers", "keywords": [ "puppeteer", diff --git a/src/common/ElementHandle.ts b/src/common/ElementHandle.ts index ed80b69bd..fea1dcf1f 100644 --- a/src/common/ElementHandle.ts +++ b/src/common/ElementHandle.ts @@ -764,9 +764,9 @@ export class ElementHandle< ); // Locate all files and confirm that they exist. - let path: typeof import('path'); + let path: typeof import('node:path'); try { - path = await import('path'); + path = await import('node:path'); } catch (error) { if (error instanceof TypeError) { throw new Error( diff --git a/src/common/IsolatedWorld.ts b/src/common/IsolatedWorld.ts index 64068fdf9..4d78f6bd3 100644 --- a/src/common/IsolatedWorld.ts +++ b/src/common/IsolatedWorld.ts @@ -366,21 +366,9 @@ export class IsolatedWorld { } if (path !== null) { - let fs; - try { - fs = (await import('fs')).promises; - } catch (error) { - if (error instanceof TypeError) { - throw new Error( - 'Can only pass a filepath to addScriptTag in a Node-like 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(addScriptContent, contents, id, type); + throw new Error( + 'Can only pass a filepath to addScriptTag in a Node-like environment.' + ); } if (content !== null) { diff --git a/src/constants.ts b/src/constants.ts index d8e562cce..be2ae6bd9 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,4 +1,4 @@ -import {dirname} from 'path'; +import {dirname} from 'node:path'; import {puppeteerDirname} from './compat.js'; /** diff --git a/src/util/getPackageDirectory.ts b/src/util/getPackageDirectory.ts index 94c49535a..799045ff1 100644 --- a/src/util/getPackageDirectory.ts +++ b/src/util/getPackageDirectory.ts @@ -1,15 +1,5 @@ -import {existsSync} from 'fs'; -import {dirname, join, parse} from 'path'; - export const getPackageDirectory = (from: string): string => { - let found = existsSync(join(from, 'package.json')); - const root = parse(from).root; - while (!found) { - if (from === root) { - throw new Error('Cannot find package directory'); - } - from = dirname(from); - found = existsSync(join(from, 'package.json')); - } - return from; + throw new Error( + `Can only use the filesystem in a Node-like environment (${from}).` + ); };