-
Notifications
You must be signed in to change notification settings - Fork 724
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify router-worker logic to allow for running user-worker ahead of …
…assets (#7303) - Adds logic to run user-worker ahead of assets - Moves vitest from worker-shared root into asset-worker - Creates new vitest unit suite for router-worker
- Loading branch information
1 parent
6fe9533
commit 0d314ed
Showing
12 changed files
with
242 additions
and
6 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 @@ | ||
--- | ||
"@cloudflare/workers-shared": minor | ||
--- | ||
|
||
Option to invoke user worker ahead of assets |
4 changes: 2 additions & 2 deletions
4
packages/workers-shared/vitest.config.mts → ...ers-shared/asset-worker/vitest.config.mts
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
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,74 @@ | ||
import { createExecutionContext } from "cloudflare:test"; | ||
import { describe, expect, it } from "vitest"; | ||
import { default as worker } from "../src/index"; | ||
|
||
describe("unit tests", async () => { | ||
it("fails if specify running user worker ahead of assets, without user worker", async () => { | ||
const request = new Request("https://example.com"); | ||
const ctx = createExecutionContext(); | ||
|
||
const env = { | ||
CONFIG: { | ||
invoke_user_worker_ahead_of_assets: true, | ||
has_user_worker: false, | ||
}, | ||
} as typeof env; | ||
|
||
void expect( | ||
async () => await worker.fetch(request, env, ctx) | ||
).rejects.toThrowError( | ||
"Fetch for user worker without having a user worker binding" | ||
); | ||
}); | ||
|
||
it("it returns fetch from user worker when invoke_user_worker_ahead_of_assets true", async () => { | ||
const request = new Request("https://example.com"); | ||
const ctx = createExecutionContext(); | ||
|
||
const env = { | ||
CONFIG: { | ||
invoke_user_worker_ahead_of_assets: true, | ||
has_user_worker: true, | ||
}, | ||
USER_WORKER: { | ||
async fetch(_: Request): Promise<Response> { | ||
return new Response("hello from user worker"); | ||
}, | ||
}, | ||
ASSET_WORKER: { | ||
async fetch(_: Request): Promise<Response> { | ||
return new Response("hello from asset worker"); | ||
}, | ||
async unstable_canFetch(_: Request): Promise<boolean> { | ||
return true; | ||
}, | ||
}, | ||
} as typeof env; | ||
|
||
const response = await worker.fetch(request, env, ctx); | ||
expect(await response.text()).toEqual("hello from user worker"); | ||
}); | ||
|
||
it("it returns fetch from asset worker when matching existing asset path", async () => { | ||
const request = new Request("https://example.com"); | ||
const ctx = createExecutionContext(); | ||
|
||
const env = { | ||
CONFIG: { | ||
invoke_user_worker_ahead_of_assets: false, | ||
has_user_worker: false, | ||
}, | ||
ASSET_WORKER: { | ||
async fetch(_: Request): Promise<Response> { | ||
return new Response("hello from asset worker"); | ||
}, | ||
async unstable_canFetch(_: Request): Promise<boolean> { | ||
return true; | ||
}, | ||
}, | ||
} as typeof env; | ||
|
||
const response = await worker.fetch(request, env, ctx); | ||
expect(await response.text()).toEqual("hello from asset worker"); | ||
}); | ||
}); |
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,15 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"module": "ES2020", | ||
"lib": ["ES2020"], | ||
"types": [ | ||
"@cloudflare/workers-types/experimental", | ||
"@cloudflare/vitest-pool-workers" | ||
], | ||
"moduleResolution": "bundler", | ||
"noEmit": true, | ||
"skipLibCheck": true | ||
}, | ||
"include": ["**/*.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 @@ | ||
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config"; | ||
|
||
export default defineWorkersConfig({ | ||
test: { | ||
poolOptions: { | ||
workers: { | ||
wrangler: { | ||
configPath: "./wrangler.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
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.