-
Notifications
You must be signed in to change notification settings - Fork 756
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wranger dev support with experimental_serve_directly (#7445)
- Loading branch information
1 parent
8c873ed
commit f4ae6ee
Showing
13 changed files
with
236 additions
and
5 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,6 @@ | ||
--- | ||
"@cloudflare/workers-shared": minor | ||
"wrangler": minor | ||
--- | ||
|
||
Support for `assets.experimental_serve_directly` with `wrangler dev` |
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,17 @@ | ||
# workers-assets-with-user-worker" | ||
|
||
`workers-assets-with-user-worker-serve-directly"` is a test fixture that showcases Workers with Assets using the serve_directly option. This particular fixture forces a user Worker to be invoked, even if it otherwise would have matched an assets request. | ||
|
||
## dev | ||
|
||
To start a dev session you can run | ||
|
||
``` | ||
wrangler dev | ||
``` | ||
|
||
## Run tests | ||
|
||
``` | ||
npm run test | ||
``` |
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,20 @@ | ||
{ | ||
"name": "workers-assets-with-user-worker-serve-directly", | ||
"private": true, | ||
"scripts": { | ||
"check:type": "tsc", | ||
"dev": "npx wrangler dev", | ||
"test:ci": "vitest run", | ||
"test:watch": "vitest", | ||
"type:tests": "tsc -p ./tests/tsconfig.json" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/workers-tsconfig": "workspace:*", | ||
"@cloudflare/workers-types": "^4.20241106.0", | ||
"undici": "catalog:default", | ||
"wrangler": "workspace:*" | ||
}, | ||
"volta": { | ||
"extends": "../../package.json" | ||
} | ||
} |
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 @@ | ||
<h1>Hello Workers + Assets World 🚀!</h1> |
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,22 @@ | ||
export interface Env { | ||
// Example binding to a Service. Learn more at https://developers.cloudflare.com/workers/runtime-apis/service-bindings/ | ||
ASSETS: Fetcher; | ||
} | ||
|
||
export default { | ||
async fetch(request: Request, env: Env): Promise<Response> { | ||
const auth = request.headers.get("Authorization"); | ||
|
||
if (!auth) { | ||
return new Response("Forbidden", { | ||
status: 403, | ||
statusText: "Forbidden", | ||
headers: { | ||
"Content-Type": "text/plain", | ||
}, | ||
}); | ||
} | ||
|
||
return await env.ASSETS.fetch(request); | ||
}, | ||
}; |
37 changes: 37 additions & 0 deletions
37
fixtures/workers-with-assets-serve-directly/tests/index.test.ts
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,37 @@ | ||
import { resolve } from "node:path"; | ||
import { fetch } from "undici"; | ||
import { afterAll, beforeAll, describe, it } from "vitest"; | ||
import { runWranglerDev } from "../../shared/src/run-wrangler-long-lived"; | ||
|
||
describe("[Workers + Assets] serve_directly false", () => { | ||
let ip: string, port: number, stop: (() => Promise<unknown>) | undefined; | ||
|
||
beforeAll(async () => { | ||
({ ip, port, stop } = await runWranglerDev(resolve(__dirname, ".."), [ | ||
"--port=0", | ||
"--inspector-port=0", | ||
])); | ||
}); | ||
|
||
afterAll(async () => { | ||
await stop?.(); | ||
}); | ||
|
||
it("should return a 403 without an Authorization header", async ({ | ||
expect, | ||
}) => { | ||
let response = await fetch(`http://${ip}:${port}/index.html`); | ||
let text = await response.text(); | ||
expect(response.status).toBe(403); | ||
}); | ||
|
||
it("should return a 200 with an Authorization header", async ({ expect }) => { | ||
let response = await fetch(`http://${ip}:${port}/index.html`, { | ||
headers: { Authorization: "some auth header" }, | ||
}); | ||
let text = await response.text(); | ||
|
||
expect(response.status).toBe(200); | ||
expect(text).toContain(`<h1>Hello Workers + Assets World 🚀!</h1>`); | ||
}); | ||
}); |
7 changes: 7 additions & 0 deletions
7
fixtures/workers-with-assets-serve-directly/tests/tsconfig.json
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 @@ | ||
{ | ||
"extends": "@cloudflare/workers-tsconfig/tsconfig.json", | ||
"compilerOptions": { | ||
"types": ["node"] | ||
}, | ||
"include": ["**/*.ts", "../../../node-types.d.ts"] | ||
} |
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 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"module": "CommonJS", | ||
"lib": ["ES2020"], | ||
"types": ["@cloudflare/workers-types"], | ||
"moduleResolution": "node", | ||
"noEmit": true, | ||
"skipLibCheck": true | ||
}, | ||
"include": ["**/*.ts"], | ||
"exclude": ["tests"] | ||
} |
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 @@ | ||
#:schema node_modules/wrangler/config-schema.json | ||
|
||
name = "worker-with-assets-serve-directly" | ||
main = "src/index.ts" | ||
compatibility_date = "2024-01-01" | ||
|
||
[assets] | ||
directory = "./public" | ||
binding="ASSETS" | ||
experimental_serve_directly = false |
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
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.