-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: resolve raw file bindings correctly in
wrangler dev
local mode
For `wasm_modules`/`text_blobs`/`data_blobs` in local mode, we need to rewrite the paths as absolute so that they're resolved correctly by miniflare. This also expands some coverage for local mode `wrangler dev`. Fixes #740 Fixes #416
- Loading branch information
1 parent
b933641
commit 3869d2c
Showing
14 changed files
with
219 additions
and
14 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,10 @@ | ||
--- | ||
"wrangler": patch | ||
--- | ||
|
||
fix: resolve raw file bindings correctly in `wrangler dev` local mode | ||
|
||
For `wasm_modules`/`text_blobs`/`data_blobs` in local mode, we need to rewrite the paths as absolute so that they're resolved correctly by miniflare. This also expands some coverage for local mode `wrangler dev`. | ||
|
||
Fixes https://github.com/cloudflare/wrangler2/issues/740 | ||
Fixes https://github.com/cloudflare/wrangler2/issues/416 |
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
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 @@ | ||
Here be some data |
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 @@ | ||
Here be some text |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// @ts-expect-error non standard module | ||
import data from "../some-data.bin"; | ||
// @ts-expect-error non standard module | ||
import text from "../some-text.txt"; | ||
|
||
export default { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
async fetch(_request: Request, env: any): Promise<Response> { | ||
return new Response( | ||
JSON.stringify( | ||
{ | ||
VAR1: env.VAR1, | ||
VAR2: env.VAR2, | ||
VAR3: env.VAR3, | ||
text, | ||
data: new TextDecoder().decode(data), | ||
}, | ||
null, | ||
2 | ||
) | ||
); | ||
}, | ||
}; |
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,31 @@ | ||
// @ts-expect-error non standard module | ||
import data from "../some-data.bin"; | ||
// @ts-expect-error non standard module | ||
import text from "../some-text.txt"; | ||
|
||
addEventListener("fetch", (event: FetchEvent) => { | ||
event.respondWith(handleRequest(event.request)); | ||
}); | ||
|
||
async function handleRequest(_req: Request): Promise<Response> { | ||
return new Response( | ||
JSON.stringify( | ||
{ | ||
// @ts-expect-error binding | ||
VAR1, | ||
// @ts-expect-error binding | ||
VAR2, | ||
// @ts-expect-error binding | ||
VAR3, | ||
text, | ||
data: new TextDecoder().decode(data), | ||
// @ts-expect-error binding | ||
TEXT, | ||
// @ts-expect-error binding | ||
DATA: new TextDecoder().decode(DATA), | ||
}, | ||
null, | ||
2 | ||
) | ||
); | ||
} |
6 changes: 5 additions & 1 deletion
6
examples/local-mode-tests/wrangler.toml → ...local-mode-tests/src/wrangler.module.toml
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
name = "local-mode-tests" | ||
main = "src/index.ts" | ||
compatibility_date = "2022-03-27" | ||
|
||
[vars] | ||
VAR1 = "value1" | ||
VAR2 = 123 | ||
VAR3 = {abc = "def"} |
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,13 @@ | ||
name = "local-mode-tests" | ||
compatibility_date = "2022-03-27" | ||
|
||
[vars] | ||
VAR1 = "value1" | ||
VAR2 = 123 | ||
VAR3 = {abc = "def"} | ||
|
||
[text_blobs] | ||
TEXT = "../some-text.txt" | ||
|
||
[data_blobs] | ||
DATA = "../some-data.bin" |
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
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,72 @@ | ||
import { spawn } from "child_process"; | ||
import { fetch } from "undici"; | ||
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, 100)); | ||
|
||
try { | ||
response = await fetch(url); | ||
} catch {} | ||
} | ||
|
||
return response as Response; | ||
}; | ||
const isWindows = process.platform === "win32"; | ||
|
||
let wranglerProcess: ChildProcess; | ||
|
||
beforeAll(async () => { | ||
wranglerProcess = spawn( | ||
"npx", | ||
[ | ||
"wrangler", | ||
"dev", | ||
"src/sw.ts", | ||
"--local", | ||
"--config", | ||
"src/wrangler.sw.toml", | ||
"--port", | ||
"9002", | ||
], | ||
{ | ||
shell: isWindows, | ||
stdio: "inherit", | ||
} | ||
); | ||
}); | ||
|
||
afterAll(async () => { | ||
await new Promise((resolve, reject) => { | ||
wranglerProcess.once("exit", (code) => { | ||
if (!code) { | ||
resolve(code); | ||
} else { | ||
reject(code); | ||
} | ||
}); | ||
wranglerProcess.kill(); | ||
}); | ||
}); | ||
|
||
it("renders", async () => { | ||
const response = await waitUntilReady("http://localhost:9002/"); | ||
const text = await response.text(); | ||
expect(text).toMatchInlineSnapshot(` | ||
"{ | ||
\\"VAR1\\": \\"value1\\", | ||
\\"VAR2\\": 123, | ||
\\"VAR3\\": { | ||
\\"abc\\": \\"def\\" | ||
}, | ||
\\"text\\": \\"Here be some text\\", | ||
\\"data\\": \\"Here be some data\\", | ||
\\"TEXT\\": \\"Here be some text\\", | ||
\\"DATA\\": \\"Here be some data\\" | ||
}" | ||
`); | ||
}); |
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
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