From f743084ec920f2e74c38b2239ad328df3f1efd6d Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Mon, 5 Feb 2024 18:55:19 +0000 Subject: [PATCH 01/10] 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(), From a2497b81e9dd8ab76a21d3d60af5f4aa6d625883 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Tue, 6 Feb 2024 17:55:58 +0000 Subject: [PATCH 02/10] deepFreeze cf object --- .../tests/get-bindings-proxy.cf.test.ts | 5 +++++ .../src/api/integrations/bindings/index.ts | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) 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 index d52d379559d5..b22e360b1a39 100644 --- a/fixtures/get-bindings-proxy/tests/get-bindings-proxy.cf.test.ts +++ b/fixtures/get-bindings-proxy/tests/get-bindings-proxy.cf.test.ts @@ -31,6 +31,11 @@ describe("getBindingsProxy - cf", () => { cf.newField = "test new field"; }).toThrowError("Cannot add property newField, object is not extensible"); expect("newField" in cf).toBe(false); + + expect(cf.botManagement).toMatchObject({ + score: 99, + }); + expect(Object.isFrozen(cf.botManagement)).toBe(true); } finally { await dispose(); } diff --git a/packages/wrangler/src/api/integrations/bindings/index.ts b/packages/wrangler/src/api/integrations/bindings/index.ts index 20a9ce137bfb..8e4cc00c9ddd 100644 --- a/packages/wrangler/src/api/integrations/bindings/index.ts +++ b/packages/wrangler/src/api/integrations/bindings/index.ts @@ -93,12 +93,15 @@ export async function getBindingsProxy>( const vars = getVarsForDev(rawConfig, env); + const cf = await mf.getCf(); + deepFreeze(cf); + return { bindings: { ...vars, ...bindings, }, - cf: Object.freeze(await mf.getCf()), + cf, ctx: new ExecutionContext(), caches: new CacheStorage(), dispose: () => mf.dispose(), @@ -183,3 +186,14 @@ function getMiniflarePersistOptions( d1Persist: `${persistPath}/d1`, }; } + +function deepFreeze>( + obj: T +): void { + Object.freeze(obj); + Object.entries(obj).forEach(([, prop]) => { + if (prop !== null && typeof prop === "object" && !Object.isFrozen(prop)) { + deepFreeze(prop as Record); + } + }); +} From 6bb6eee818fd0ccfc76aa8041f12142add7771a0 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Tue, 6 Feb 2024 19:13:32 +0000 Subject: [PATCH 03/10] Apply suggestions from code review Co-authored-by: James Culveyhouse --- .changeset/strong-otters-hope.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.changeset/strong-otters-hope.md b/.changeset/strong-otters-hope.md index 2040912bf94a..bf0f8a52513a 100644 --- a/.changeset/strong-otters-hope.md +++ b/.changeset/strong-otters-hope.md @@ -4,7 +4,7 @@ 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 +Add a new `cf` field to the `getBindingsProxy` result that people can use to mock the production `cf` (`IncomingRequestCfProperties`) object. Example: @@ -12,5 +12,5 @@ Example: ```ts const { cf } = await getBindingsProxy(); -console.log(`country = ${cf.country} ; colo = ${cf.colo}`); // logs 'country = GB ; colo = LHR' +console.log(`country = ${cf.country}; colo = ${cf.colo}`); // logs 'country = GB ; colo = LHR' ``` From 091db6677a4ecf20345de74071729c2cfd53d8cc Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Wed, 7 Feb 2024 10:27:12 +0000 Subject: [PATCH 04/10] use IncomingRequestCfProperties type of cf --- packages/wrangler/package.json | 8 + .../src/api/integrations/bindings/index.ts | 5 +- packages/wrangler/tsconfig.json | 2 +- pnpm-lock.yaml | 207 +++++++++--------- 4 files changed, 111 insertions(+), 111 deletions(-) diff --git a/packages/wrangler/package.json b/packages/wrangler/package.json index 61da11023647..1c64b7dd27f9 100644 --- a/packages/wrangler/package.json +++ b/packages/wrangler/package.json @@ -211,6 +211,14 @@ "optionalDependencies": { "fsevents": "~2.3.2" }, + "peerDependencies": { + "@cloudflare/workers-types": "^4.20230914.0" + }, + "peerDependenciesMeta": { + "@cloudflare/workers-types": { + "optional": true + } + }, "engines": { "node": ">=16.17.0" }, diff --git a/packages/wrangler/src/api/integrations/bindings/index.ts b/packages/wrangler/src/api/integrations/bindings/index.ts index 8e4cc00c9ddd..620243cce3dd 100644 --- a/packages/wrangler/src/api/integrations/bindings/index.ts +++ b/packages/wrangler/src/api/integrations/bindings/index.ts @@ -44,8 +44,7 @@ export type BindingsProxy> = { /** * 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; + cf: IncomingRequestCfProperties; /** * Mock of the context object that Workers received in their request handler, all the object's methods are no-op */ @@ -101,7 +100,7 @@ export async function getBindingsProxy>( ...vars, ...bindings, }, - cf, + cf: cf as IncomingRequestCfProperties, ctx: new ExecutionContext(), caches: new CacheStorage(), dispose: () => mf.dispose(), diff --git a/packages/wrangler/tsconfig.json b/packages/wrangler/tsconfig.json index ddef0d63ac23..fda57a0833c9 100644 --- a/packages/wrangler/tsconfig.json +++ b/packages/wrangler/tsconfig.json @@ -2,7 +2,7 @@ "extends": "@cloudflare/workers-tsconfig/tsconfig.json", "compilerOptions": { "module": "CommonJS", - "types": ["node"], + "types": ["node", "@cloudflare/workers-types"], "jsx": "react" }, "include": ["**/*.ts", "**/*.tsx", "**/*.js"], diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 440cd1e66a07..2639bbe84106 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1063,7 +1063,7 @@ importers: version: 5.28.2 wrangler: specifier: ^3.24.0 - version: link:../wrangler + version: 3.27.0 packages/prerelease-registry: dependencies: @@ -1394,7 +1394,7 @@ importers: version: 3.0.0 '@microsoft/api-extractor': specifier: ^7.28.3 - version: 7.28.3 + version: 7.40.0(@types/node@20.1.7) '@sentry/node': specifier: ^7.86.0 version: 7.87.0(supports-color@9.2.2) @@ -1643,7 +1643,7 @@ importers: version: 6.5.1 wrangler: specifier: ^3.0.0 - version: link:../wrangler + version: 3.27.0 packages: @@ -3854,7 +3854,6 @@ packages: resolution: {integrity: sha512-MVbXLbTcAotOPUj0pAMhVtJ+3/kFkwJqc5qNOleOZTv6QkZZABDMS21dSrSlVswEHwrpWC03e4fWytjqKvuE2A==} dependencies: mime: 3.0.0 - dev: false /@cloudflare/kv-asset-handler@0.3.0: resolution: {integrity: sha512-9CB/MKf/wdvbfkUdfrj+OkEwZ5b7rws0eogJ4293h+7b6KX5toPwym+VQKmILafNB9YiehqY0DlNrDcDhdWHSQ==} @@ -3962,7 +3961,6 @@ packages: cpu: [x64] os: [darwin] requiresBuild: true - dev: false optional: true /@cloudflare/workerd-darwin-arm64@1.20240129.0: @@ -3971,7 +3969,6 @@ packages: cpu: [arm64] os: [darwin] requiresBuild: true - dev: false optional: true /@cloudflare/workerd-linux-64@1.20240129.0: @@ -3980,7 +3977,6 @@ packages: cpu: [x64] os: [linux] requiresBuild: true - dev: false optional: true /@cloudflare/workerd-linux-arm64@1.20240129.0: @@ -3989,7 +3985,6 @@ packages: cpu: [arm64] os: [linux] requiresBuild: true - dev: false optional: true /@cloudflare/workerd-windows-64@1.20240129.0: @@ -3998,7 +3993,6 @@ packages: cpu: [x64] os: [win32] requiresBuild: true - dev: false optional: true /@cloudflare/workers-types@3.18.0: @@ -4069,7 +4063,6 @@ packages: esbuild: '*' dependencies: esbuild: 0.17.19 - dev: false /@esbuild-plugins/node-modules-polyfill@0.2.2(esbuild@0.17.19): resolution: {integrity: sha512-LXV7QsWJxRuMYvKbiznh+U1ilIop3g2TeKRzUxOG5X3YITc8JyyTa90BmLwqqv0YnX4v32CSlG+vsziZp9dMvA==} @@ -4079,7 +4072,6 @@ packages: esbuild: 0.17.19 escape-string-regexp: 4.0.0 rollup-plugin-node-polyfills: 0.2.1 - dev: false /@esbuild/android-arm64@0.16.17: resolution: {integrity: sha512-MIGl6p5sc3RDTLLkYL1MyL8BMRN4tLMRCn+yRJJmEDvYZ2M7tmAf80hx1kbNEUX2KJ50RRtxZ4JHLvCfuB6kBg==} @@ -5525,14 +5517,6 @@ packages: read-yaml-file: 1.1.0 dev: false - /@microsoft/api-extractor-model@7.21.0: - resolution: {integrity: sha512-NN4mXzoQWTuzznIcnLWeV6tGyn6Os9frDK6M/mmTXZ73vUYOvSWoKQ5SYzyzP7HF3YtvTmr1Rs+DsBb0HRx7WQ==} - dependencies: - '@microsoft/tsdoc': 0.14.1 - '@microsoft/tsdoc-config': 0.16.1 - '@rushstack/node-core-library': 3.49.0 - dev: true - /@microsoft/api-extractor-model@7.28.2(@types/node@18.16.10): resolution: {integrity: sha512-vkojrM2fo3q4n4oPh4uUZdjJ2DxQ2+RnDQL/xhTWSRUNPF6P4QyrvY357HBxbnltKcYu+nNNolVqc6TIGQ73Ig==} dependencies: @@ -5543,22 +5527,14 @@ packages: - '@types/node' dev: true - /@microsoft/api-extractor@7.28.3: - resolution: {integrity: sha512-lkDHPyln8MNEy1QHjmGwedRquclGKU0qL0gHplfnHuSTXSoNQ86UYaPmhG77/GiNehXzGNKMYSIfTsuoQb69jA==} - hasBin: true + /@microsoft/api-extractor-model@7.28.8(@types/node@20.1.7): + resolution: {integrity: sha512-/q6ds8XQVqs4Tq0/HueFiMk0wwJH8RaXHm+Z7XJ9ffeZ+6/oQUh6E0++uVfNoMD0JmZvLTV8++UgQ4dXMRQFWA==} dependencies: - '@microsoft/api-extractor-model': 7.21.0 - '@microsoft/tsdoc': 0.14.1 + '@microsoft/tsdoc': 0.14.2 '@microsoft/tsdoc-config': 0.16.1 - '@rushstack/node-core-library': 3.49.0 - '@rushstack/rig-package': 0.3.13 - '@rushstack/ts-command-line': 4.12.1 - colors: 1.2.5 - lodash: 4.17.21 - resolve: 1.17.0 - semver: 7.3.8 - source-map: 0.6.1 - typescript: 4.6.4 + '@rushstack/node-core-library': 3.65.0(@types/node@20.1.7) + transitivePeerDependencies: + - '@types/node' dev: true /@microsoft/api-extractor@7.38.2(@types/node@18.16.10): @@ -5581,6 +5557,26 @@ packages: - '@types/node' dev: true + /@microsoft/api-extractor@7.40.0(@types/node@20.1.7): + resolution: {integrity: sha512-U4yTHabfut6WuYUnSM2+FWUsNIJ+w8ZfQGqZWLjH5I/MZvCyDBFyPDIhZAnndd4Vd3pwl4eSBpeMDe8etkCxpA==} + hasBin: true + dependencies: + '@microsoft/api-extractor-model': 7.28.8(@types/node@20.1.7) + '@microsoft/tsdoc': 0.14.2 + '@microsoft/tsdoc-config': 0.16.1 + '@rushstack/node-core-library': 3.65.0(@types/node@20.1.7) + '@rushstack/rig-package': 0.5.1 + '@rushstack/ts-command-line': 4.17.1 + colors: 1.2.5 + lodash: 4.17.21 + resolve: 1.22.8 + semver: 7.5.4 + source-map: 0.6.1 + typescript: 5.3.3 + transitivePeerDependencies: + - '@types/node' + dev: true + /@microsoft/tsdoc-config@0.16.1: resolution: {integrity: sha512-2RqkwiD4uN6MLnHFljqBlZIXlt/SaUT6cuogU1w2ARw4nKuuppSmR0+s+NC+7kXBQykd9zzu0P4HtBpZT5zBpQ==} dependencies: @@ -6222,61 +6218,49 @@ packages: resolution: {integrity: sha512-sXo/qW2/pAcmT43VoRKOJbDOfV3cYpq3szSVfIThQXNt+E4DfKj361vaAt3c88U5tPUxzEswam7GW48PJqtKAg==} dev: true - /@rushstack/node-core-library@3.49.0: - resolution: {integrity: sha512-yBJRzGgUNFwulVrwwBARhbGaHsxVMjsZ9JwU1uSBbqPYCdac+t2HYdzi4f4q/Zpgb0eNbwYj2yxgHYpJORNEaw==} + /@rushstack/node-core-library@3.61.0(@types/node@18.16.10): + resolution: {integrity: sha512-tdOjdErme+/YOu4gPed3sFS72GhtWCgNV9oDsHDnoLY5oDfwjKUc9Z+JOZZ37uAxcm/OCahDHfuu2ugqrfWAVQ==} + peerDependencies: + '@types/node': '*' + peerDependenciesMeta: + '@types/node': + optional: true dependencies: - '@types/node': 12.20.24 + '@types/node': 18.16.10 colors: 1.2.5 fs-extra: 7.0.1 import-lazy: 4.0.0 jju: 1.4.0 - resolve: 1.17.0 - semver: 7.3.8 - timsort: 0.3.0 + resolve: 1.22.2 + semver: 7.5.4 z-schema: 5.0.3 dev: true - /@rushstack/node-core-library@3.61.0(@types/node@18.16.10): - resolution: {integrity: sha512-tdOjdErme+/YOu4gPed3sFS72GhtWCgNV9oDsHDnoLY5oDfwjKUc9Z+JOZZ37uAxcm/OCahDHfuu2ugqrfWAVQ==} + /@rushstack/node-core-library@3.65.0(@types/node@20.1.7): + resolution: {integrity: sha512-4AistGV/26JjSMrBuCc0bh13ayQ5mZo/SpnJjETkmkoKNaqIQpZdWr/T04Sa3DLBc4U2e61cx5ZpDzvTVCo+pQ==} peerDependencies: '@types/node': '*' peerDependenciesMeta: '@types/node': optional: true dependencies: - '@types/node': 18.16.10 + '@types/node': 20.1.7 colors: 1.2.5 fs-extra: 7.0.1 import-lazy: 4.0.0 jju: 1.4.0 - resolve: 1.22.2 + resolve: 1.22.8 semver: 7.5.4 z-schema: 5.0.3 dev: true - /@rushstack/rig-package@0.3.13: - resolution: {integrity: sha512-4/2+yyA/uDl7LQvtYtFs1AkhSWuaIGEKhP9/KK2nNARqOVc5eCXmu1vyOqr5mPvNq7sHoIR+sG84vFbaKYGaDA==} - dependencies: - resolve: 1.17.0 - strip-json-comments: 3.1.1 - dev: true - /@rushstack/rig-package@0.5.1: resolution: {integrity: sha512-pXRYSe29TjRw7rqxD4WS3HN/sRSbfr+tJs4a9uuaSIBAITbUggygdhuG0VrO0EO+QqH91GhYMN4S6KRtOEmGVA==} dependencies: - resolve: 1.22.2 + resolve: 1.22.8 strip-json-comments: 3.1.1 dev: true - /@rushstack/ts-command-line@4.12.1: - resolution: {integrity: sha512-S1Nev6h/kNnamhHeGdp30WgxZTA+B76SJ/P721ctP7DrnC+rrjAc6h/R80I4V0cA2QuEEcMdVOQCtK2BTjsOiQ==} - dependencies: - '@types/argparse': 1.0.38 - argparse: 1.0.10 - colors: 1.2.5 - string-argv: 0.3.1 - dev: true - /@rushstack/ts-command-line@4.17.1: resolution: {integrity: sha512-2jweO1O57BYP5qdBGl6apJLB+aRIn5ccIRTPDyULh0KMwVzFqWtw6IZWt1qtUoZD/pD2RNkIOosH6Cq45rIYeg==} dependencies: @@ -6866,10 +6850,6 @@ packages: resolution: {integrity: sha512-Tfx3TU/PBK8vW/BG1TK793EHlVpGnoHUj+DGxOwNOYwZiueLeu7FgksvDdpEyFSw4+AKKiEuiMm8EGUHUR4o6g==} dev: false - /@types/node@12.20.24: - resolution: {integrity: sha512-yxDeaQIAJlMav7fH5AQqPH1u8YIuhYJXYBzxaQ4PifsU0GDO38MSdmEDeRlIxrKbC6NbEaaEHDanWb+y30U8SQ==} - dev: true - /@types/node@12.20.47: resolution: {integrity: sha512-BzcaRsnFuznzOItW1WpQrDHM7plAa7GIDMZ6b5pnMbkqEtM/6WCOhvZar39oeMQP79gwvFUWjjptE7/KGcNqFg==} dev: false @@ -8352,7 +8332,6 @@ packages: resolution: {integrity: sha512-xvsWESUJn0JN421Xb9MQw6AsMHRCUknCe0Wjlxvjud80mU4E6hQf1A6NzQKcYNmYw62MfzEtXc+badstZP3JpQ==} dependencies: printable-characters: 1.0.42 - dev: false /assert@2.0.0: resolution: {integrity: sha512-se5Cd+js9dXJnu6Ag2JFc00t+HmHOen+8Q+L7O9zI0PqQXr20uk2J0XQqMxZEeo5U50o8Nvmmx7dZrl+Ufr35A==} @@ -8749,7 +8728,6 @@ packages: /blake3-wasm@2.1.5: resolution: {integrity: sha512-F1+K8EbfOZE49dtoPtmxUQrpXaBIl3ICvasLh+nJta0xkz+9kF/7uet9fLnwKqhDrmj6g+6K3Tw9yQPUg2ka5g==} - dev: false /blueimp-md5@2.19.0: resolution: {integrity: sha512-DRQrD6gJyy8FbiE4s+bDoXS9hiW3Vbx5uCdwvcCf3zLHL+Iv7LtGHLpr+GZV8rHG8tK766FGYBwRbu8pELTt+w==} @@ -9053,7 +9031,6 @@ packages: tslib: 2.5.3 transitivePeerDependencies: - supports-color - dev: false patched: true /capnpc-ts@0.7.0: @@ -9810,7 +9787,6 @@ packages: /data-uri-to-buffer@2.0.2: resolution: {integrity: sha512-ND9qDTLc6diwj+Xe5cdAgVTbLVdXbtxTJRXRhli8Mowuaan+0EJOtdqJ0QCHNSSPyoXGx9HX2/VMnKeC34AChA==} - dev: false /data-uri-to-buffer@3.0.1: resolution: {integrity: sha512-WboRycPNsVw3B3TL559F7kuBUM4d8CgMEvk6xEJlOp7OBPjt6G7z8WMWlD2rOFZLk6OYfFIUGsCOWzcQH9K2og==} @@ -11402,7 +11378,6 @@ packages: /estree-walker@0.6.1: resolution: {integrity: sha512-SqmZANLWS0mnatqbSfRP5g8OXZC12Fgg1IwNtLsyHDzJizORW4khDfjPqJZsemPWBB2uqykUah5YpQ6epsqC/w==} - dev: false /estree-walker@2.0.2: resolution: {integrity: sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==} @@ -12100,7 +12075,6 @@ packages: dependencies: data-uri-to-buffer: 2.0.2 source-map: 0.6.1 - dev: false /get-stream@4.1.0: resolution: {integrity: sha512-GMat4EJ5161kIy2HevLlr4luNjBgvmj413KaQA7jt4V8B4RDsfpHk7WQ9GVqfYyyx8OS/L66Kox+rJRNklLK7w==} @@ -14447,7 +14421,6 @@ packages: resolution: {integrity: sha512-RmF0AsMzgt25qzqqLc1+MbHmhdx0ojF2Fvs4XnOqz2ZOBXzzkEwc/dJQZCYHAn7v1jbVOjAZfK8msRn4BxO4VQ==} dependencies: sourcemap-codec: 1.4.8 - dev: false /magic-string@0.30.3: resolution: {integrity: sha512-B7xGbll2fG/VjP+SWg4sX3JynwIU0mjoTc6MPpKNuIvftk6u6vqhDnk1R80b8C2GBR6ywqy+1DcKBrevBg+bmw==} @@ -15081,6 +15054,29 @@ packages: engines: {node: '>=4'} dev: false + /miniflare@3.20240129.1: + resolution: {integrity: sha512-GfqclPxbTnam4S8GKHRkFyr+s+szELK/ORtQ3ZFUiGBO4HNJsaeA6RhBMKBH7iHqn5ng035cyPsLZvH35lwtsA==} + engines: {node: '>=16.13'} + hasBin: true + dependencies: + '@cspotcode/source-map-support': 0.8.1 + acorn: 8.10.0 + acorn-walk: 8.2.0 + capnp-ts: 0.7.0(patch_hash=l4yimnxyvkiyj6alnps2ec3sii) + exit-hook: 2.2.1 + glob-to-regexp: 0.4.1 + stoppable: 1.1.0 + undici: 5.28.2 + workerd: 1.20240129.0 + ws: 8.14.2 + youch: 3.2.3 + zod: 3.22.3 + transitivePeerDependencies: + - bufferutil + - supports-color + - utf-8-validate + dev: true + /minimatch@3.1.2: resolution: {integrity: sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==} dependencies: @@ -15406,7 +15402,6 @@ packages: /node-forge@1.3.0: resolution: {integrity: sha512-08ARB91bUi6zNKzVmaj3QO7cr397uiDT2nJ63cHjyNtCTWIgvS47j3eT0WfzUwS9+6Z5YshRaoasFkXCKrIYbA==} engines: {node: '>= 6.13.0'} - dev: false /node-gyp@10.0.1: resolution: {integrity: sha512-gg3/bHehQfZivQVfqIyy8wTdSymF9yTyP4CJifK73imyNMU8AIGQE2pUa7dNWfmMeG9cDVF2eehiRMv0LC1iAg==} @@ -16428,7 +16423,6 @@ packages: /printable-characters@1.0.42: resolution: {integrity: sha512-dKp+C4iXWK4vVYZmYSd0KBH5F/h1HoZRsbJ82AVKRO3PEo8L4lBS/vLwhVtpwwuYcoIsVY+1JYKR268yn480uQ==} - dev: false /prism-react-renderer@1.3.5(react@18.2.0): resolution: {integrity: sha512-IJ+MSwBWKG+SM3b2SUfdrhC+gu01QkV2KmRQgREThBfSQRoufqRfxfHUxpG1WcaFjP+kojcFyO9Qqtpgt3qLCg==} @@ -17217,12 +17211,6 @@ packages: resolution: {integrity: sha512-X2UW6Nw3n/aMgDVy+0rSqgHlv39WZAlZrXCdnbyEiKm17DSqHX4MmQMaST3FbeWR5FTuRcUwYAziZajji0Y7mg==} engines: {node: '>=10'} - /resolve@1.17.0: - resolution: {integrity: sha512-ic+7JYiV8Vi2yzQGFWOkiZD5Z9z7O2Zhm9XMaTxdJExKasieFCr+yXZ/WmXsckHiKl12ar0y6XiXDx3m4RHn1w==} - dependencies: - path-parse: 1.0.7 - dev: true - /resolve@1.19.0: resolution: {integrity: sha512-rArEXAgsBG4UgRGcynxWIWKFvh/XZCcS8UJdHhwy91zwAvCZIbcs+vAbflgBnNjYMs/i/i+/Ux6IZhML1yPvxg==} dependencies: @@ -17336,19 +17324,16 @@ packages: estree-walker: 0.6.1 magic-string: 0.25.9 rollup-pluginutils: 2.8.2 - dev: false /rollup-plugin-node-polyfills@0.2.1: resolution: {integrity: sha512-4kCrKPTJ6sK4/gLL/U5QzVT8cxJcofO0OU74tnB19F40cmuAKSzH5/siithxlofFEjwvw1YAhPmbvGNA6jEroA==} dependencies: rollup-plugin-inject: 3.0.2 - dev: false /rollup-pluginutils@2.8.2: resolution: {integrity: sha512-EEp9NhnUkwY8aif6bxgovPHMoMoNr2FulJziTndpt5H9RdwC47GSGuII9XxpSdzVGM0GWrNPHV6ie1LTNJPaLQ==} dependencies: estree-walker: 0.6.1 - dev: false /rollup@3.25.1: resolution: {integrity: sha512-tywOR+rwIt5m2ZAWSe5AIJcTat8vGlnPFAv15ycCrw33t6iFsXZ6mzHVFh2psSjxQPmI+xgzMZZizUAukBI4aQ==} @@ -17487,7 +17472,6 @@ packages: engines: {node: '>=10'} dependencies: node-forge: 1.3.0 - dev: false /semiver@1.1.0: resolution: {integrity: sha512-QNI2ChmuioGC1/xjyYwyZYADILWyW6AmS1UH6gDj/SFUUUS4MBAWs/7mxnkRPc/F4iHezDP+O8t0dO8WHiEOdg==} @@ -17506,14 +17490,6 @@ packages: resolution: {integrity: sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==} hasBin: true - /semver@7.3.8: - resolution: {integrity: sha512-NB1ctGL5rlHrPJtFDVIVzTyQylMLu9N9VICA6HSFJo8MCGVTMW6gfpicwKmmK/dAjTOrqu5l63JJOpDSrAis3A==} - engines: {node: '>=10'} - hasBin: true - dependencies: - lru-cache: 6.0.0 - dev: true - /semver@7.5.1: resolution: {integrity: sha512-Wvss5ivl8TMRZXXESstBA4uR5iXgEN/VC5/sOcuXdVLzcdkz4HWetIoRfG5gb5X+ij/G9rw9YoGn3QoQ8OCSpw==} engines: {node: '>=10'} @@ -17870,7 +17846,6 @@ packages: /sourcemap-codec@1.4.8: resolution: {integrity: sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==} deprecated: Please use @jridgewell/sourcemap-codec instead - dev: false /space-separated-tokens@2.0.2: resolution: {integrity: sha512-PEGlAwrG8yXGXRjW32fGbg66JAlOAwbObuqVoJpv/mRgoWDQfgH1wDPvtzWyUSNAXBGSk8h755YDbbcEy3SH2Q==} @@ -17958,7 +17933,6 @@ packages: dependencies: as-table: 1.0.55 get-source: 2.0.12 - dev: false /standard-as-callback@2.1.0: resolution: {integrity: sha512-qoRRSyROncaz1z0mvYqIE4lCd9p2R90i6GxW3uZv5ucSu8tU7B5HXUP1gG8pVZsYNVaXjk8ClXHPttLyxAL48A==} @@ -17984,7 +17958,6 @@ packages: /stoppable@1.1.0: resolution: {integrity: sha512-KXDYZ9dszj6bzvnEMRYvxgeTHU74QBFL54XKtP3nyMuJ81CFYtABZ3bAzL2EdFUaEwJOBOgENyFj3R7oTzDyyw==} engines: {node: '>=4', npm: '>=6'} - dev: false /stream-shift@1.0.1: resolution: {integrity: sha512-AiisoFqQ0vbGcZgQPY1cdP2I76glaVA/RauYR4G4thNFgkTqr90yXTo4LYX60Jl+sIlPNHHdGSwo01AvbKUSVQ==} @@ -18340,10 +18313,6 @@ packages: resolution: {integrity: sha512-a7wPxPdVlQL7lqvitHGGRsofhdwtkoSXPGATFuSOA2i1ZNQEPLrGnj68vOp2sOJTCFAQVXPeNMX/GctBaO9L2w==} dev: true - /timsort@0.3.0: - resolution: {integrity: sha512-qsdtZH+vMoCARQtyod4imc2nIJwg9Cc7lPRrw9CzF8ZKR0khdr8+2nX80PBhET3tcyTtJDxAffGh2rXH4tyU8A==} - dev: true - /tinybench@2.5.0: resolution: {integrity: sha512-kRwSG8Zx4tjF9ZiyH4bhaebu+EDz1BOx9hOigYHlUW4xxI/wKIUQUqo018UlU4ar6ATPBsaMrdbKZ+tmPdohFA==} dev: false @@ -18832,12 +18801,6 @@ packages: hasBin: true dev: true - /typescript@4.6.4: - resolution: {integrity: sha512-9ia/jWHIEbo49HfjrLGfKbZSuWo9iTMwXO+Ca3pRsSpbsMbc7/IU8NKdCZVRRBafVPGnoJeFL76ZOAA84I9fEg==} - engines: {node: '>=4.2.0'} - hasBin: true - dev: true - /typescript@4.9.5: resolution: {integrity: sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g==} engines: {node: '>=4.2.0'} @@ -18855,6 +18818,12 @@ packages: hasBin: true dev: true + /typescript@5.3.3: + resolution: {integrity: sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==} + engines: {node: '>=14.17'} + hasBin: true + dev: true + /ufo@1.3.0: resolution: {integrity: sha512-bRn3CsoojyNStCZe0BG0Mt4Nr/4KF+rhFlnNXybgqt5pXHNFRlqinSoQaTrGyzE4X8aHplSb+TorH+COin9Yxw==} @@ -19587,7 +19556,33 @@ packages: '@cloudflare/workerd-linux-64': 1.20240129.0 '@cloudflare/workerd-linux-arm64': 1.20240129.0 '@cloudflare/workerd-windows-64': 1.20240129.0 - dev: false + + /wrangler@3.27.0: + resolution: {integrity: sha512-VV2QXH+4OfNwWRR2cgMw5Xh6WQXec8h1cPjgKMWuUTR1q0Ha8TiEhU1rBOraRYTZbUuM4tqqO4GYeOE0AX7oVQ==} + engines: {node: '>=16.17.0'} + hasBin: true + dependencies: + '@cloudflare/kv-asset-handler': 0.2.0 + '@esbuild-plugins/node-globals-polyfill': 0.2.3(esbuild@0.17.19) + '@esbuild-plugins/node-modules-polyfill': 0.2.2(esbuild@0.17.19) + blake3-wasm: 2.1.5 + chokidar: 3.5.3 + esbuild: 0.17.19 + miniflare: 3.20240129.1 + nanoid: 3.3.6 + path-to-regexp: 6.2.0 + resolve: 1.22.8 + resolve.exports: 2.0.2 + selfsigned: 2.1.1 + source-map: 0.6.1 + xxhash-wasm: 1.0.1 + optionalDependencies: + fsevents: 2.3.2 + transitivePeerDependencies: + - bufferutil + - supports-color + - utf-8-validate + dev: true /wrap-ansi@6.2.0: resolution: {integrity: sha512-r6lPcBGxZXlIcymEu7InxDMhdW0KDxpLgoFLcguasxCaJ/SOIZwINatK9KY/tf+ZrlywOKU0UDj3ATXUBfxJXA==} @@ -19751,7 +19746,6 @@ packages: /xxhash-wasm@1.0.1: resolution: {integrity: sha512-Lc9CTvDrH2vRoiaUzz25q7lRaviMhz90pkx6YxR9EPYtF99yOJnv2cB+CQ0hp/TLoqrUsk8z/W2EN31T568Azw==} - dev: false /y18n@4.0.3: resolution: {integrity: sha512-JKhqTOwSrqNA1NY5lSztJ1GrBiUodLMmIZuLiDaMRJ+itFd+ABVE8XBjOvIWL+rSqNDC74LCSFmlb/U4UZ4hJQ==} @@ -19870,7 +19864,6 @@ packages: cookie: 0.5.0 mustache: 4.2.0 stacktracey: 2.1.8 - dev: false /z-schema@5.0.3: resolution: {integrity: sha512-sGvEcBOTNum68x9jCpCVGPFJ6mWnkD0YxOcddDlJHRx3tKdB2q8pCHExMVZo/AV/6geuVJXG7hljDaWG8+5GDw==} From 20037dcecdbf706f5ab6e7fa8964fcc747bce2f9 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Wed, 7 Feb 2024 10:59:48 +0000 Subject: [PATCH 05/10] don't add workers-types to tsconfig types --- packages/wrangler/scripts/emit-types.ts | 73 ++++++++++++------- .../src/api/integrations/bindings/index.ts | 1 + packages/wrangler/tsconfig.json | 2 +- 3 files changed, 50 insertions(+), 26 deletions(-) diff --git a/packages/wrangler/scripts/emit-types.ts b/packages/wrangler/scripts/emit-types.ts index 8e055936f054..3651b906ddf8 100644 --- a/packages/wrangler/scripts/emit-types.ts +++ b/packages/wrangler/scripts/emit-types.ts @@ -16,29 +16,52 @@ const configObject = ExtractorConfig.loadFile(configObjectFullPath); // include the dependencies we want to bundle configObject.bundledPackages = BUNDLED_DEPENDENCIES; -const extractorConfig = ExtractorConfig.prepare({ - configObject, - configObjectFullPath, - packageJsonFullPath, - packageJson, -}); - -// Invoke API Extractor -const extractorResult = Extractor.invoke(extractorConfig, { - // Equivalent to the "--local" command-line parameter - localBuild: true, - - // Equivalent to the "--verbose" command-line parameter - showVerboseMessages: true, -}); - -if (extractorResult.succeeded) { - console.log(`API Extractor completed successfully`); - process.exitCode = 0; -} else { - console.error( - `API Extractor completed with ${extractorResult.errorCount} errors` + - ` and ${extractorResult.warningCount} warnings` - ); - process.exitCode = 1; +const pkgRoot = path.resolve(__dirname, ".."); + +// `api-extractor` doesn't know to load `index.ts` instead of `index.d.ts` when +// resolving imported types, so copy `index.ts` to `index.d.ts`, bundle types, +// then restore the original contents. We need the original `index.d.ts` for +// typing the `packages/miniflare/src/workers` directory. +const workersTypesExperimental = path.join( + pkgRoot, + "node_modules", + "@cloudflare", + "workers-types", + "experimental" +); +const indexTsPath = path.join(workersTypesExperimental, "index.ts"); +const indexDtsPath = path.join(workersTypesExperimental, "index.d.ts"); +const originalDtsContent = fs.readFileSync(indexDtsPath); + +fs.copyFileSync(indexTsPath, indexDtsPath); + +try { + const extractorConfig = ExtractorConfig.prepare({ + configObject, + configObjectFullPath, + packageJsonFullPath, + packageJson, + }); + + // Invoke API Extractor + const extractorResult = Extractor.invoke(extractorConfig, { + // Equivalent to the "--local" command-line parameter + localBuild: true, + + // Equivalent to the "--verbose" command-line parameter + showVerboseMessages: true, + }); + + if (extractorResult.succeeded) { + console.log(`API Extractor completed successfully`); + process.exitCode = 0; + } else { + console.error( + `API Extractor completed with ${extractorResult.errorCount} errors` + + ` and ${extractorResult.warningCount} warnings` + ); + process.exitCode = 1; + } +} finally { + fs.writeFileSync(indexDtsPath, originalDtsContent); } diff --git a/packages/wrangler/src/api/integrations/bindings/index.ts b/packages/wrangler/src/api/integrations/bindings/index.ts index 620243cce3dd..2bdc5b1939be 100644 --- a/packages/wrangler/src/api/integrations/bindings/index.ts +++ b/packages/wrangler/src/api/integrations/bindings/index.ts @@ -9,6 +9,7 @@ import { ExecutionContext } from "./executionContext"; import { getServiceBindings } from "./services"; import type { Config } from "../../../config"; import type { MiniflareOptions } from "miniflare"; +import type { IncomingRequestCfProperties } from "@cloudflare/workers-types/experimental"; /** * Options for the `getBindingsProxy` utility diff --git a/packages/wrangler/tsconfig.json b/packages/wrangler/tsconfig.json index fda57a0833c9..ddef0d63ac23 100644 --- a/packages/wrangler/tsconfig.json +++ b/packages/wrangler/tsconfig.json @@ -2,7 +2,7 @@ "extends": "@cloudflare/workers-tsconfig/tsconfig.json", "compilerOptions": { "module": "CommonJS", - "types": ["node", "@cloudflare/workers-types"], + "types": ["node"], "jsx": "react" }, "include": ["**/*.ts", "**/*.tsx", "**/*.js"], From 2784cd2562220e996bdc8013b015f3b752035dca Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Wed, 7 Feb 2024 11:09:39 +0000 Subject: [PATCH 06/10] run prettify --- packages/wrangler/src/api/integrations/bindings/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/wrangler/src/api/integrations/bindings/index.ts b/packages/wrangler/src/api/integrations/bindings/index.ts index 2bdc5b1939be..0f723db21cae 100644 --- a/packages/wrangler/src/api/integrations/bindings/index.ts +++ b/packages/wrangler/src/api/integrations/bindings/index.ts @@ -8,8 +8,8 @@ import { CacheStorage } from "./caches"; import { ExecutionContext } from "./executionContext"; import { getServiceBindings } from "./services"; import type { Config } from "../../../config"; -import type { MiniflareOptions } from "miniflare"; import type { IncomingRequestCfProperties } from "@cloudflare/workers-types/experimental"; +import type { MiniflareOptions } from "miniflare"; /** * Options for the `getBindingsProxy` utility From fdc5b2f9494734b73cb04f5fc4390647f5154b0d Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Wed, 7 Feb 2024 11:51:05 +0000 Subject: [PATCH 07/10] remove workers-types optional peer dependency --- packages/wrangler/package.json | 8 -------- 1 file changed, 8 deletions(-) diff --git a/packages/wrangler/package.json b/packages/wrangler/package.json index 1c64b7dd27f9..61da11023647 100644 --- a/packages/wrangler/package.json +++ b/packages/wrangler/package.json @@ -211,14 +211,6 @@ "optionalDependencies": { "fsevents": "~2.3.2" }, - "peerDependencies": { - "@cloudflare/workers-types": "^4.20230914.0" - }, - "peerDependenciesMeta": { - "@cloudflare/workers-types": { - "optional": true - } - }, "engines": { "node": ">=16.17.0" }, From 97afdf5625c4c7eb9d9801b0f898b5222c15f6d6 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Wed, 7 Feb 2024 12:06:02 +0000 Subject: [PATCH 08/10] Revert "remove workers-types optional peer dependency" This reverts commit fdc5b2f9494734b73cb04f5fc4390647f5154b0d. --- packages/wrangler/package.json | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/wrangler/package.json b/packages/wrangler/package.json index 61da11023647..1c64b7dd27f9 100644 --- a/packages/wrangler/package.json +++ b/packages/wrangler/package.json @@ -211,6 +211,14 @@ "optionalDependencies": { "fsevents": "~2.3.2" }, + "peerDependencies": { + "@cloudflare/workers-types": "^4.20230914.0" + }, + "peerDependenciesMeta": { + "@cloudflare/workers-types": { + "optional": true + } + }, "engines": { "node": ">=16.17.0" }, From 9ab96edd3a9ba02c4a49141550e1de509c68aca4 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Wed, 7 Feb 2024 12:33:53 +0000 Subject: [PATCH 09/10] make sure workers-types doesn't get bundled in d.ts file --- packages/wrangler/scripts/deps.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/wrangler/scripts/deps.ts b/packages/wrangler/scripts/deps.ts index 493a7c313070..22187e1754bd 100644 --- a/packages/wrangler/scripts/deps.ts +++ b/packages/wrangler/scripts/deps.ts @@ -15,6 +15,9 @@ export const EXTERNAL_DEPENDENCIES = [ "@esbuild-plugins/node-globals-polyfill", "@esbuild-plugins/node-modules-polyfill", "chokidar", + // @cloudflare/workers-types is an optional peer dependency of wrangler, so users can + // get the types by installing the package (to what version they prefer) themselves + "@cloudflare/workers-types", ]; const pathToPackageJson = path.resolve(__dirname, "..", "package.json"); From fb1421b1645c5b1a2bb87cc0e47d1dec4137402b Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Wed, 7 Feb 2024 15:57:47 +0000 Subject: [PATCH 10/10] add new `CfProperties` type argument --- .../src/api/integrations/bindings/index.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/wrangler/src/api/integrations/bindings/index.ts b/packages/wrangler/src/api/integrations/bindings/index.ts index 0f723db21cae..aefabfa24906 100644 --- a/packages/wrangler/src/api/integrations/bindings/index.ts +++ b/packages/wrangler/src/api/integrations/bindings/index.ts @@ -37,7 +37,10 @@ export type GetBindingsProxyOptions = { /** * Result of the `getBindingsProxy` utility */ -export type BindingsProxy> = { +export type BindingsProxy< + Bindings = Record, + CfProperties extends Record = IncomingRequestCfProperties +> = { /** * Object containing the various proxies */ @@ -45,7 +48,7 @@ export type BindingsProxy> = { /** * Mock of the context object that Workers received in their request handler, all the object's methods are no-op */ - cf: IncomingRequestCfProperties; + cf: CfProperties; /** * Mock of the context object that Workers received in their request handler, all the object's methods are no-op */ @@ -67,9 +70,12 @@ export type BindingsProxy> = { * @param options The various options that can tweak this function's behavior * @returns An Object containing the generated proxies alongside other related utilities */ -export async function getBindingsProxy>( +export async function getBindingsProxy< + Bindings = Record, + CfProperties extends Record = IncomingRequestCfProperties +>( options: GetBindingsProxyOptions = {} -): Promise> { +): Promise> { const rawConfig = readConfig(options.configPath, { experimentalJsonConfig: options.experimentalJsonConfig, }); @@ -101,7 +107,7 @@ export async function getBindingsProxy>( ...vars, ...bindings, }, - cf: cf as IncomingRequestCfProperties, + cf: cf as CfProperties, ctx: new ExecutionContext(), caches: new CacheStorage(), dispose: () => mf.dispose(),