forked from cloudflare/workers-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: disallow imports in _worker.js (cloudflare#1629)
* fix: disallow imports in _worker.js Fixes: cloudflare#1214 This does not actually prevent the user from importing things (could be modified to do so) It currently just logs an error to console. * chore: prettier * draft: start to add tests * apply changes * Update packages/wrangler/src/pages/dev.tsx Co-authored-by: Greg Brimble <[email protected]> * mark test as todo Co-authored-by: Greg Brimble <[email protected]>
- Loading branch information
1 parent
f1c97c8
commit 06915ff
Showing
8 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"wrangler": patch | ||
--- | ||
|
||
fix: disallow imports in \_worker.js (https://github.com/cloudflare/wrangler2/issues/1214) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "pages-workerjs-app", | ||
"version": "0.0.0", | ||
"private": true, | ||
"sideEffects": false, | ||
"scripts": { | ||
"dev": "npx wrangler pages dev ./workerjs-test --port 8792", | ||
"test": "npx jest --forceExit --verbose", | ||
"test:ci": "npx jest --forceExit" | ||
}, | ||
"jest": { | ||
"restoreMocks": true, | ||
"testRegex": ".*.(test|spec)\\.[jt]sx?$", | ||
"testTimeout": 30000, | ||
"transform": { | ||
"^.+\\.c?(t|j)sx?$": [ | ||
"esbuild-jest", | ||
{ | ||
"sourcemap": true | ||
} | ||
] | ||
}, | ||
"transformIgnorePatterns": [ | ||
"node_modules/(?!find-up|locate-path|p-locate|p-limit|yocto-queue|path-exists|execa|strip-final-newline|npm-run-path|path-key|onetime|mimic-fn|human-signals|is-stream)" | ||
] | ||
}, | ||
"devDependencies": { | ||
"undici": "^5.5.1" | ||
}, | ||
"engines": { | ||
"node": ">=14" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { spawn } from "child_process"; | ||
import * as path from "path"; | ||
import patchConsole from "patch-console"; | ||
import { fetch } from "undici"; | ||
// import { mockConsoleMethods } from "../../../packages/wrangler/src/__tests__/helpers/mock-console"; | ||
import type { ChildProcess } from "child_process"; | ||
import type { Response } from "undici"; | ||
|
||
const waitUntilReady = async (url: string): Promise<Response> => { | ||
let response: Response | undefined = undefined; | ||
|
||
while (response === undefined) { | ||
await new Promise((resolvePromise) => setTimeout(resolvePromise, 500)); | ||
|
||
try { | ||
response = await fetch(url); | ||
} catch (err) {} | ||
} | ||
|
||
return response as Response; | ||
}; | ||
|
||
const isWindows = process.platform === "win32"; | ||
|
||
describe("Pages _worker.js", () => { | ||
let wranglerProcess: ChildProcess; | ||
|
||
// const std = mockConsoleMethods(); | ||
beforeEach(() => { | ||
wranglerProcess = spawn("npm", ["run", "dev"], { | ||
shell: isWindows, | ||
cwd: path.resolve(__dirname, "../"), | ||
env: { BROWSER: "none", ...process.env }, | ||
}); | ||
wranglerProcess.stdout?.on("data", (chunk) => { | ||
console.log(chunk.toString()); | ||
}); | ||
wranglerProcess.stderr?.on("data", (chunk) => { | ||
console.log(chunk.toString()); | ||
}); | ||
}); | ||
|
||
afterEach(async () => { | ||
patchConsole(() => {}); | ||
|
||
await new Promise((resolve, reject) => { | ||
wranglerProcess.once("exit", (code) => { | ||
if (!code) { | ||
resolve(code); | ||
} else { | ||
reject(code); | ||
} | ||
}); | ||
wranglerProcess.kill("SIGTERM"); | ||
}); | ||
}); | ||
|
||
it("renders static pages", async () => { | ||
const response = await waitUntilReady("http://127.0.0.1:8792/"); | ||
const text = await response.text(); | ||
expect(text).toContain("test"); | ||
}); | ||
|
||
it.todo("shows an error for the import in _worker.js"); | ||
// it("shows an error for the import in _worker.js", async () => { | ||
// const _ = await waitUntilReady("http://127.0.0.1:8792/"); | ||
// expect(std.err).toMatchInlineSnapshot(`""`); | ||
// }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"esModuleInterop": true, | ||
"module": "CommonJS", | ||
"lib": ["ES2020"], | ||
"types": ["jest"], | ||
"moduleResolution": "node" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import text from "./other-script"; | ||
|
||
export default { | ||
async fetch() { | ||
return new Response(text); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default "test"; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters