-
Notifications
You must be signed in to change notification settings - Fork 742
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 - 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
f1f508e
commit 03009f3
Showing
10 changed files
with
230 additions
and
4 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
66 changes: 66 additions & 0 deletions
66
packages/workers-shared/router-worker/__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,66 @@ | ||
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"); | ||
}, | ||
}, | ||
} 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"); | ||
}); | ||
}); |
15 changes: 15 additions & 0 deletions
15
packages/workers-shared/router-worker/__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,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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.