From 431af33dec8c0e22868696c659021636af9c4b82 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Mon, 5 Feb 2024 18:55:19 +0000 Subject: [PATCH] add a `cf` field to the `getBindingsProxy` result --- .changeset/strong-otters-hope.md | 16 ++++++++ .../tests/get-bindings-proxy.cf.test.ts | 38 +++++++++++++++++++ .../src/api/integrations/bindings/index.ts | 6 +++ 3 files changed, 60 insertions(+) create mode 100644 .changeset/strong-otters-hope.md create mode 100644 fixtures/get-bindings-proxy/tests/get-bindings-proxy.cf.test.ts diff --git a/.changeset/strong-otters-hope.md b/.changeset/strong-otters-hope.md new file mode 100644 index 000000000000..2040912bf94a --- /dev/null +++ b/.changeset/strong-otters-hope.md @@ -0,0 +1,16 @@ +--- +"wrangler": minor +--- + +feature: add a `cf` field to the `getBindingsProxy` result + +Add a new `cf` filed to the `getBindingsProxy` result that people can use to mock the production +`cf` (`IncomingRequestCfProperties`) object. + +Example: + +```ts +const { cf } = await getBindingsProxy(); + +console.log(`country = ${cf.country} ; colo = ${cf.colo}`); // logs 'country = GB ; colo = LHR' +``` diff --git a/fixtures/get-bindings-proxy/tests/get-bindings-proxy.cf.test.ts b/fixtures/get-bindings-proxy/tests/get-bindings-proxy.cf.test.ts new file mode 100644 index 000000000000..d52d379559d5 --- /dev/null +++ b/fixtures/get-bindings-proxy/tests/get-bindings-proxy.cf.test.ts @@ -0,0 +1,38 @@ +import { describe, expect, it } from "vitest"; +import { getBindingsProxy } from "./shared"; + +describe("getBindingsProxy - cf", () => { + it("should provide mock data", async () => { + const { cf, dispose } = await getBindingsProxy(); + try { + expect(cf).toMatchObject({ + colo: "DFW", + city: "Austin", + regionCode: "TX", + }); + } finally { + await dispose(); + } + }); + + it("should match the production runtime cf object", async () => { + const { cf, dispose } = await getBindingsProxy(); + try { + expect(cf.constructor.name).toBe("Object"); + + expect(() => { + cf.city = "test city"; + }).toThrowError( + "Cannot assign to read only property 'city' of object '#'" + ); + expect(cf.city).not.toBe("test city"); + + expect(() => { + cf.newField = "test new field"; + }).toThrowError("Cannot add property newField, object is not extensible"); + expect("newField" in cf).toBe(false); + } finally { + await dispose(); + } + }); +}); diff --git a/packages/wrangler/src/api/integrations/bindings/index.ts b/packages/wrangler/src/api/integrations/bindings/index.ts index 487dae15f810..20a9ce137bfb 100644 --- a/packages/wrangler/src/api/integrations/bindings/index.ts +++ b/packages/wrangler/src/api/integrations/bindings/index.ts @@ -41,6 +41,11 @@ export type BindingsProxy> = { * Object containing the various proxies */ bindings: Bindings; + /** + * Mock of the context object that Workers received in their request handler, all the object's methods are no-op + */ + // eslint-disable-next-line @typescript-eslint/no-explicit-any + cf: Record; /** * Mock of the context object that Workers received in their request handler, all the object's methods are no-op */ @@ -93,6 +98,7 @@ export async function getBindingsProxy>( ...vars, ...bindings, }, + cf: Object.freeze(await mf.getCf()), ctx: new ExecutionContext(), caches: new CacheStorage(), dispose: () => mf.dispose(),