From cc410e7043610f6b8161a677936e6f3cffb4133e Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Sun, 7 Jan 2024 15:20:55 +0100 Subject: [PATCH] add pages-dev-proxy-with-script fixture --- .../pages-dev-proxy-with-script/_worker.js | 5 ++ .../custom/script/path/index.js | 5 ++ .../pages-dev-proxy-with-script/package.json | 21 +++++ .../tests/index.test.ts | 78 +++++++++++++++++++ .../tests/tsconfig.json | 7 ++ .../pages-dev-proxy-with-script/tsconfig.json | 12 +++ .../vitest.config.ts | 10 +++ pnpm-lock.yaml | 15 ++++ 8 files changed, 153 insertions(+) create mode 100644 fixtures/pages-dev-proxy-with-script/_worker.js create mode 100644 fixtures/pages-dev-proxy-with-script/custom/script/path/index.js create mode 100644 fixtures/pages-dev-proxy-with-script/package.json create mode 100644 fixtures/pages-dev-proxy-with-script/tests/index.test.ts create mode 100644 fixtures/pages-dev-proxy-with-script/tests/tsconfig.json create mode 100644 fixtures/pages-dev-proxy-with-script/tsconfig.json create mode 100644 fixtures/pages-dev-proxy-with-script/vitest.config.ts diff --git a/fixtures/pages-dev-proxy-with-script/_worker.js b/fixtures/pages-dev-proxy-with-script/_worker.js new file mode 100644 index 000000000000..3fb22f4a8ae6 --- /dev/null +++ b/fixtures/pages-dev-proxy-with-script/_worker.js @@ -0,0 +1,5 @@ +export default { + fetch() { + return new Response("hello from _worker.js"); + }, +}; diff --git a/fixtures/pages-dev-proxy-with-script/custom/script/path/index.js b/fixtures/pages-dev-proxy-with-script/custom/script/path/index.js new file mode 100644 index 000000000000..a74d25b5b2e9 --- /dev/null +++ b/fixtures/pages-dev-proxy-with-script/custom/script/path/index.js @@ -0,0 +1,5 @@ +export default { + fetch() { + return new Response("hello from custom/script/path/index.js"); + }, +}; diff --git a/fixtures/pages-dev-proxy-with-script/package.json b/fixtures/pages-dev-proxy-with-script/package.json new file mode 100644 index 000000000000..5173a8d669f4 --- /dev/null +++ b/fixtures/pages-dev-proxy-with-script/package.json @@ -0,0 +1,21 @@ +{ + "name": "pages-dev-proxy-with-script", + "version": "0.0.0", + "private": true, + "sideEffects": false, + "scripts": { + "dev": "npx wrangler pages dev public", + "test": "vitest run", + "test:watch": "vitest", + "type:tests": "tsc -p ./tests/tsconfig.json" + }, + "devDependencies": { + "@cloudflare/workers-tsconfig": "workspace:*", + "@cloudflare/workers-types": "^4.20221111.1", + "undici": "^5.9.1", + "wrangler": "workspace:*" + }, + "engines": { + "node": ">=16.13" + } +} diff --git a/fixtures/pages-dev-proxy-with-script/tests/index.test.ts b/fixtures/pages-dev-proxy-with-script/tests/index.test.ts new file mode 100644 index 000000000000..d62829ac7b02 --- /dev/null +++ b/fixtures/pages-dev-proxy-with-script/tests/index.test.ts @@ -0,0 +1,78 @@ +import path from "node:path"; +import { Response, fetch } from "undici"; +import { describe, it, expect } from "vitest"; +import { ChildProcess, fork } from "node:child_process"; +import { setTimeout } from "node:timers/promises"; + +describe("Pages dev with proxy and a script file", () => { + it("should handle requests using a script from the default _worker.js path", async () => { + const { port, childProcess } = await startWranglerPagesDevProxy(); + const combinedResponse = await waitUntilReady(`http://127.0.0.1:${port}/`); + const respText = await combinedResponse.text(); + expect(respText).toMatchInlineSnapshot('"hello from _worker.js"'); + await terminateChildProcess(childProcess); + }); + + it("should handle requests using a script from a custom script path", async () => { + const { port, childProcess } = await startWranglerPagesDevProxy([ + "--script-path=custom/script/path/index.js", + ]); + const combinedResponse = await waitUntilReady(`http://127.0.0.1:${port}/`); + const respText = await combinedResponse.text(); + expect(respText).toMatchInlineSnapshot( + '"hello from custom/script/path/index.js"' + ); + await terminateChildProcess(childProcess); + }); +}); + +async function startWranglerPagesDevProxy(extraArgs: string[] = []): Promise<{ + childProcess: ChildProcess; + port: string; +}> { + return new Promise(async (resolve) => { + const childProcess = fork( + path.join("..", "..", "packages", "wrangler", "bin", "wrangler.js"), + ["pages", "dev", "--port=0", "--proxy=9999", ...extraArgs], + { + cwd: path.resolve(__dirname, ".."), + env: { BROWSER: "none", ...process.env }, + stdio: ["ignore", "ignore", "ignore", "ipc"], + } + ).on("message", (message) => { + const parsedMessage = JSON.parse(message.toString()); + resolve({ + childProcess, + port: parsedMessage.port, + }); + }); + debugger; + }); +} + +function terminateChildProcess(childProcess: ChildProcess): Promise { + return new Promise((resolve, reject) => { + childProcess.once("exit", (code) => { + if (!code) { + resolve(code); + } else { + reject(code); + } + }); + childProcess.kill("SIGTERM"); + }); +} + +async function waitUntilReady(url: string): Promise { + let response: Response | undefined = undefined; + + while (response === undefined) { + await setTimeout(500); + + try { + response = await fetch(url); + } catch (e) {} + } + + return response as Response; +} diff --git a/fixtures/pages-dev-proxy-with-script/tests/tsconfig.json b/fixtures/pages-dev-proxy-with-script/tests/tsconfig.json new file mode 100644 index 000000000000..d2ce7f144694 --- /dev/null +++ b/fixtures/pages-dev-proxy-with-script/tests/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "@cloudflare/workers-tsconfig/tsconfig.json", + "compilerOptions": { + "types": ["node"] + }, + "include": ["**/*.ts", "../../../node-types.d.ts"] +} diff --git a/fixtures/pages-dev-proxy-with-script/tsconfig.json b/fixtures/pages-dev-proxy-with-script/tsconfig.json new file mode 100644 index 000000000000..d270009565a2 --- /dev/null +++ b/fixtures/pages-dev-proxy-with-script/tsconfig.json @@ -0,0 +1,12 @@ +{ + "include": ["index.d.ts"], + "compilerOptions": { + "target": "ES2020", + "module": "CommonJS", + "lib": ["ES2020"], + "types": ["@cloudflare/workers-types"], + "moduleResolution": "node", + "noEmit": true, + "skipLibCheck": true + } +} diff --git a/fixtures/pages-dev-proxy-with-script/vitest.config.ts b/fixtures/pages-dev-proxy-with-script/vitest.config.ts new file mode 100644 index 000000000000..ed02453b1c87 --- /dev/null +++ b/fixtures/pages-dev-proxy-with-script/vitest.config.ts @@ -0,0 +1,10 @@ +import { defineConfig } from "vitest/config"; + +export default defineConfig({ + test: { + testTimeout: 10_000, + hookTimeout: 10_000, + teardownTimeout: 10_000, + useAtomics: true, + }, +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3debe9e3e8f8..1bda304d446c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -285,6 +285,21 @@ importers: specifier: workspace:* version: link:../../packages/wrangler + fixtures/pages-dev-proxy-with-script.js: + devDependencies: + '@cloudflare/workers-tsconfig': + specifier: workspace:* + version: link:../../packages/workers-tsconfig + '@cloudflare/workers-types': + specifier: ^4.20221111.1 + version: 4.20231025.0 + undici: + specifier: ^5.9.1 + version: 5.28.2 + wrangler: + specifier: workspace:* + version: link:../../packages/wrangler + fixtures/pages-functions-app: devDependencies: '@cloudflare/workers-tsconfig':