diff --git a/packages/playwright-cloudflare/examples/todomvc/package-lock.json b/packages/playwright-cloudflare/examples/todomvc/package-lock.json index 75417d15c..feb97a6e7 100644 --- a/packages/playwright-cloudflare/examples/todomvc/package-lock.json +++ b/packages/playwright-cloudflare/examples/todomvc/package-lock.json @@ -9,7 +9,7 @@ "version": "1.0.0", "license": "ISC", "devDependencies": { - "@cloudflare/playwright": "^0.0.9", + "@cloudflare/playwright": "^0.0.10", "typescript": "^5.8.3", "wrangler": "^4.13.0" } @@ -28,9 +28,9 @@ } }, "node_modules/@cloudflare/playwright": { - "version": "0.0.9", - "resolved": "https://registry.npmjs.org/@cloudflare/playwright/-/playwright-0.0.9.tgz", - "integrity": "sha512-ytAeVK4ZM5TClIWB5gtT8deleFfz+rQ63fdW7t/ROFtqDBq3ziKMXIwJN5GVwq3hLtdOOzEp0nkgz1NTukhVRQ==", + "version": "0.0.10", + "resolved": "https://registry.npmjs.org/@cloudflare/playwright/-/playwright-0.0.10.tgz", + "integrity": "sha512-phlOzpO5FLgXFbx20C2GyQonffpe+qdL/MSMOf7ryc3+WjEsRrr6zqVLVJJq9ZW4CEypee1e/wcUQ473srC2dA==", "dev": true, "license": "Apache-2.0" }, diff --git a/packages/playwright-cloudflare/examples/todomvc/package.json b/packages/playwright-cloudflare/examples/todomvc/package.json index 95d819b59..8761af53e 100644 --- a/packages/playwright-cloudflare/examples/todomvc/package.json +++ b/packages/playwright-cloudflare/examples/todomvc/package.json @@ -10,7 +10,7 @@ "deploy": "wrangler deploy" }, "devDependencies": { - "@cloudflare/playwright": "^0.0.9", + "@cloudflare/playwright": "^0.0.10", "typescript": "^5.8.3", "wrangler": "^4.13.0" } diff --git a/packages/playwright-cloudflare/index.d.ts b/packages/playwright-cloudflare/index.d.ts index d92dacf4a..e5b922775 100644 --- a/packages/playwright-cloudflare/index.d.ts +++ b/packages/playwright-cloudflare/index.d.ts @@ -16,6 +16,206 @@ declare module './types/types' { } } +/** + * @public + */ +export type Location = + | 'AF' + | 'AL' + | 'DZ' + | 'AD' + | 'AO' + | 'AG' + | 'AR' + | 'AM' + | 'AU' + | 'AT' + | 'AZ' + | 'BH' + | 'BD' + | 'BB' + | 'BY' + | 'BE' + | 'BZ' + | 'BJ' + | 'BM' + | 'BT' + | 'BO' + | 'BA' + | 'BW' + | 'BR' + | 'BN' + | 'BG' + | 'BF' + | 'BI' + | 'KH' + | 'CM' + | 'CA' + | 'CV' + | 'KY' + | 'CF' + | 'TD' + | 'CL' + | 'CN' + | 'CO' + | 'KM' + | 'CG' + | 'CR' + | 'CI' + | 'HR' + | 'CU' + | 'CY' + | 'CZ' + | 'CD' + | 'DK' + | 'DJ' + | 'DM' + | 'DO' + | 'EC' + | 'EG' + | 'SV' + | 'GQ' + | 'ER' + | 'EE' + | 'SZ' + | 'ET' + | 'FJ' + | 'FI' + | 'FR' + | 'GA' + | 'GE' + | 'DE' + | 'GH' + | 'GR' + | 'GL' + | 'GD' + | 'GT' + | 'GN' + | 'GW' + | 'GY' + | 'HT' + | 'HN' + | 'HU' + | 'IS' + | 'IN' + | 'ID' + | 'IR' + | 'IQ' + | 'IE' + | 'IL' + | 'IT' + | 'JM' + | 'JP' + | 'JO' + | 'KZ' + | 'KE' + | 'KI' + | 'KW' + | 'KG' + | 'LA' + | 'LV' + | 'LB' + | 'LS' + | 'LR' + | 'LY' + | 'LI' + | 'LT' + | 'LU' + | 'MO' + | 'MG' + | 'MW' + | 'MY' + | 'MV' + | 'ML' + | 'MR' + | 'MU' + | 'MX' + | 'FM' + | 'MD' + | 'MC' + | 'MN' + | 'MS' + | 'MA' + | 'MZ' + | 'MM' + | 'NA' + | 'NR' + | 'NP' + | 'NL' + | 'NZ' + | 'NI' + | 'NE' + | 'NG' + | 'KP' + | 'MK' + | 'NO' + | 'OM' + | 'PK' + | 'PS' + | 'PA' + | 'PG' + | 'PY' + | 'PE' + | 'PH' + | 'PL' + | 'PT' + | 'QA' + | 'RO' + | 'RU' + | 'RW' + | 'SH' + | 'KN' + | 'LC' + | 'VC' + | 'WS' + | 'SM' + | 'ST' + | 'SA' + | 'SN' + | 'RS' + | 'SC' + | 'SL' + | 'SK' + | 'SI' + | 'SB' + | 'SO' + | 'ZA' + | 'KR' + | 'SS' + | 'ES' + | 'LK' + | 'SD' + | 'SR' + | 'SE' + | 'CH' + | 'SY' + | 'TW' + | 'TJ' + | 'TZ' + | 'TH' + | 'BS' + | 'GM' + | 'TL' + | 'TG' + | 'TO' + | 'TT' + | 'TN' + | 'TR' + | 'TM' + | 'UG' + | 'UA' + | 'AE' + | 'GB' + | 'US' + | 'UY' + | 'UZ' + | 'VU' + | 'VE' + | 'VN' + | 'YE' + | 'ZM' + | 'ZW'; + /** * @public */ @@ -86,6 +286,7 @@ export interface LimitsResponse { */ export interface WorkersLaunchOptions { keep_alive?: number; // milliseconds to keep browser alive even if it has no activity (from 10_000ms to 600_000ms, default is 60_000) + location?: Location; } // Extracts the keys whose values match a specified type `ValueType` diff --git a/packages/playwright-cloudflare/src/index.ts b/packages/playwright-cloudflare/src/index.ts index a972491cf..78b301ca3 100644 --- a/packages/playwright-cloudflare/src/index.ts +++ b/packages/playwright-cloudflare/src/index.ts @@ -1,4 +1,4 @@ -import './patch'; +import './polyfills/process'; import { createInProcessPlaywright } from 'playwright-core/lib/inProcessFactory'; import { kBrowserCloseMessageId } from 'playwright-core/lib/server/chromium/crConnection'; @@ -133,9 +133,11 @@ export async function launch(endpoint: BrowserEndpoint, launchOptions?: WorkersL export async function acquire(endpoint: BrowserEndpoint, options?: WorkersLaunchOptions): Promise { options = { ...extractOptions(endpoint), ...options }; - let acquireUrl = `${HTTP_FAKE_HOST}/v1/acquire`; + const acquireUrl = new URL(`${HTTP_FAKE_HOST}/v1/acquire`); if (options?.keep_alive) - acquireUrl = `${acquireUrl}?keep_alive=${options.keep_alive}`; + acquireUrl.searchParams.append('keep_alive', String(options.keep_alive)); + if (options?.location) + acquireUrl.searchParams.set('location', options.location); const res = await getBrowserBinding(endpoint).fetch(acquireUrl); const status = res.status; diff --git a/packages/playwright-cloudflare/src/internal.ts b/packages/playwright-cloudflare/src/internal.ts index 4fe8955a6..710fe5a04 100644 --- a/packages/playwright-cloudflare/src/internal.ts +++ b/packages/playwright-cloudflare/src/internal.ts @@ -1,3 +1,5 @@ +import './polyfills/process'; + import fs from 'fs'; import { asLocator, currentZone, isString, ManualPromise, setTimeOrigin, timeOrigin } from 'playwright-core/lib/utils'; diff --git a/packages/playwright-cloudflare/src/patch.ts b/packages/playwright-cloudflare/src/patch.ts deleted file mode 100644 index 9549d9cc8..000000000 --- a/packages/playwright-cloudflare/src/patch.ts +++ /dev/null @@ -1,3 +0,0 @@ -import process from 'process'; - -process.versions.node = '20.0.0'; diff --git a/packages/playwright-cloudflare/src/polyfills/process.ts b/packages/playwright-cloudflare/src/polyfills/process.ts new file mode 100644 index 000000000..461b64377 --- /dev/null +++ b/packages/playwright-cloudflare/src/polyfills/process.ts @@ -0,0 +1,36 @@ +if (!process || !process.env) + throw new Error('process is not defined, ensure cloudflare worker has nodejs_compat compatibility flag enabled'); + +const noop = () => {}; + +const polyfillProcess = { + versions: { + node: '22.15.0', + }, + version: 'v22.15.0', + argv: [], + stdout: { + isTTY: false, + }, + on: noop, + addListener: noop, + once: noop, + off: noop, + removeListener: noop, + removeAllListeners: noop, + emit: noop, + prependListener: noop, + prependOnceListener: noop, +}; + +function merge(target: any, source: any) { + for (const key in source) { + if (typeof source[key] === 'object' && !Array.isArray(source[key])) + target[key] = merge(target[key] || {}, source[key]); + else if (!target[key]) + target[key] = source[key]; + } + return target; +} + +merge(process, polyfillProcess); diff --git a/packages/playwright-cloudflare/src/test.ts b/packages/playwright-cloudflare/src/test.ts index 67ce0d4ce..ecad3e47d 100644 --- a/packages/playwright-cloudflare/src/test.ts +++ b/packages/playwright-cloudflare/src/test.ts @@ -1,2 +1,4 @@ +import './polyfills/process'; + export * from './index'; export { expect, mergeExpects } from 'playwright/lib/matchers/expect'; diff --git a/packages/playwright-cloudflare/utils/generate_worker_tests.js b/packages/playwright-cloudflare/utils/generate_worker_tests.js index 8ce29d5e8..7d4b03a3b 100644 --- a/packages/playwright-cloudflare/utils/generate_worker_tests.js +++ b/packages/playwright-cloudflare/utils/generate_worker_tests.js @@ -1,4 +1,3 @@ -import fs from 'fs'; import path from "path"; import { fileURLToPath } from "url"; import { build } from "vite"; @@ -167,35 +166,7 @@ ${[...testFiles, ...cloudflareTestFiles].map(file => `import ${JSON.stringify(fi chunkFileNames: '[name].js', }, external: [ - 'node:async_hooks', - 'node:assert', - 'node:buffer', - 'node:child_process', - 'node:constants', - 'node:crypto', - 'node:dns', - 'node:domain', - 'node:events', - 'node:http', - 'node:http2', - 'node:https', - 'node:inspector', - 'node:module', - 'node:net', - 'node:os', - 'node:path', - 'node:querystring', - 'node:process', - 'node:readline', - 'node:stream', - 'node:string_decoder', - 'node:timers', - 'node:tls', - 'node:url', - 'node:util', - 'node:vm', - 'node:zlib', - + /^node:/, 'cloudflare:workers', /^@cloudflare\/playwright.*/, ] diff --git a/packages/playwright-cloudflare/vite.config.ts b/packages/playwright-cloudflare/vite.config.ts index 073abc98d..afeebf448 100644 --- a/packages/playwright-cloudflare/vite.config.ts +++ b/packages/playwright-cloudflare/vite.config.ts @@ -105,31 +105,7 @@ export default defineConfig({ }, ], external: [ - 'node:async_hooks', - 'node:assert', - 'node:browser', - 'node:buffer', - 'node:child_process', - 'node:constants', - 'node:crypto', - 'node:dns', - 'node:events', - 'node:http', - 'node:http2', - 'node:https', - 'node:inspector', - 'node:module', - 'node:net', - 'node:os', - 'node:path', - 'node:process', - 'node:readline', - 'node:stream', - 'node:timers', - 'node:tls', - 'node:url', - 'node:util', - 'node:zlib', + /^node:/, 'cloudflare:workers', ] },