-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add a ctx field to the getBindingsProxy result
#4922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
11c8e12
split get-bindings-proxy fixture tests in multiple files
dario-piotrowicz 3701302
add ctx to getBindingsProxy
dario-piotrowicz e3096a9
add ctx tests to get-bindings-proxy fixture
dario-piotrowicz f3f7427
add changeset
dario-piotrowicz 06eee31
Update packages/wrangler/src/api/integrations/bindings/executionConte…
dario-piotrowicz 47f64e7
run prettify
dario-piotrowicz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,16 @@ | ||
| --- | ||
| "wrangler": minor | ||
| --- | ||
|
|
||
| feature: add a `ctx` field to the `getBindingsProxy` result | ||
|
|
||
| Add a new `ctx` filed to the `getBindingsProxy` result that people can use to mock the production | ||
| `ExecutionContext` object. | ||
|
|
||
| Example: | ||
|
|
||
| ```ts | ||
| const { ctx } = await getBindingsProxy(); | ||
| // ... | ||
| ctx.waitUntil(myPromise); | ||
| ``` |
This file contains hidden or 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
34 changes: 34 additions & 0 deletions
34
fixtures/get-bindings-proxy/tests/get-bindings-proxy.caches.test.ts
This file contains hidden or 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,34 @@ | ||
| import { Request, Response } from "undici"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { getBindingsProxy } from "./shared"; | ||
|
|
||
| describe("getBindingsProxy - caches", () => { | ||
| (["default", "named"] as const).forEach((cacheType) => | ||
| it(`correctly obtains a no-op ${cacheType} cache`, async () => { | ||
| const { caches, dispose } = await getBindingsProxy(); | ||
| try { | ||
| const cache = | ||
| cacheType === "default" | ||
| ? caches.default | ||
| : await caches.open("my-cache"); | ||
| testNoOpCache(cache); | ||
| } finally { | ||
| await dispose(); | ||
| } | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| async function testNoOpCache( | ||
| cache: Awaited<ReturnType<typeof getBindingsProxy>>["caches"]["default"] | ||
| ) { | ||
| let match = await cache.match("http://0.0.0.0/test"); | ||
| expect(match).toBeUndefined(); | ||
|
|
||
| const req = new Request("http://0.0.0.0/test"); | ||
| await cache.put(req, new Response("test")); | ||
| const resp = await cache.match(req); | ||
| expect(resp).toBeUndefined(); | ||
| const deleted = await cache.delete(req); | ||
| expect(deleted).toBe(false); | ||
| } |
55 changes: 55 additions & 0 deletions
55
fixtures/get-bindings-proxy/tests/get-bindings-proxy.ctx.test.ts
This file contains hidden or 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,55 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { getBindingsProxy } from "./shared"; | ||
|
|
||
| describe("getBindingsProxy - ctx", () => { | ||
| it("should provide a no-op waitUntil method", async () => { | ||
| const { ctx, dispose } = await getBindingsProxy(); | ||
| try { | ||
| let value = 4; | ||
| ctx.waitUntil( | ||
| new Promise((resolve) => { | ||
| value++; | ||
| resolve(value); | ||
| }) | ||
| ); | ||
| expect(value).toBe(5); | ||
| } finally { | ||
| await dispose(); | ||
| } | ||
| }); | ||
|
|
||
| it("should provide a no-op passThroughOnException method", async () => { | ||
| const { ctx, dispose } = await getBindingsProxy(); | ||
| try { | ||
| expect(ctx.passThroughOnException()).toBe(undefined); | ||
| } finally { | ||
| await dispose(); | ||
| } | ||
| }); | ||
|
|
||
| it("should match the production runtime ctx object", async () => { | ||
| const { ctx, dispose } = await getBindingsProxy(); | ||
| try { | ||
| expect(ctx.constructor.name).toBe("ExecutionContext"); | ||
| expect(typeof ctx.waitUntil).toBe("function"); | ||
| expect(typeof ctx.passThroughOnException).toBe("function"); | ||
|
|
||
| ctx.waitUntil = ((str: string) => `- ${str} -`) as any; | ||
| expect(ctx.waitUntil("waitUntil can be overridden" as any)).toBe( | ||
| "- waitUntil can be overridden -" | ||
| ); | ||
|
|
||
| ctx.passThroughOnException = ((str: string) => `_ ${str} _`) as any; | ||
| expect( | ||
| (ctx.passThroughOnException as any)( | ||
| "passThroughOnException can be overridden" | ||
| ) | ||
| ).toBe("_ passThroughOnException can be overridden _"); | ||
|
|
||
| (ctx as any).text = "the ExecutionContext can be extended"; | ||
| expect((ctx as any).text).toBe("the ExecutionContext can be extended"); | ||
| } finally { | ||
| await dispose(); | ||
| } | ||
| }); | ||
| }); | ||
This file contains hidden or 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 { getBindingsProxy as originalGetBindingsProxy } from "wrangler"; | ||
| import type { GetBindingsProxyOptions } from "wrangler"; | ||
|
|
||
| // Here we wrap the actual original getBindingsProxy function and disable its persistance, this is to make sure | ||
| // that we don't implement any persistance during these tests (which would add unnecessary extra complexity) | ||
| export function getBindingsProxy<T>( | ||
| options: Omit<GetBindingsProxyOptions, "persist"> = {} | ||
| ): ReturnType<typeof originalGetBindingsProxy<T>> { | ||
| return originalGetBindingsProxy({ | ||
| ...options, | ||
| persist: false, | ||
| }); | ||
| } |
5 changes: 5 additions & 0 deletions
5
packages/wrangler/src/api/integrations/bindings/executionContext.ts
This file contains hidden or 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 @@ | ||
| export class ExecutionContext { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any, unused-imports/no-unused-vars | ||
| waitUntil(promise: Promise<any>): void {} | ||
mrbbot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| passThroughOnException(): void {} | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.