From a7e36d503f442a8225ffdedef30b569a8a396663 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Tue, 23 Apr 2024 14:01:09 +0100 Subject: [PATCH] [wrangler] fix: fix local Durable Object proxies not working (#5669) --- .changeset/odd-guests-beam.md | 11 +++++++++++ .../tests/get-platform-proxy.env.test.ts | 2 +- packages/wrangler/src/dev/miniflare.ts | 12 ++++++------ 3 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 .changeset/odd-guests-beam.md diff --git a/.changeset/odd-guests-beam.md b/.changeset/odd-guests-beam.md new file mode 100644 index 00000000000..470b55fd6dc --- /dev/null +++ b/.changeset/odd-guests-beam.md @@ -0,0 +1,11 @@ +--- +"wrangler": patch +--- + +fix: fix broken Durable Object local proxying (when no `cf` property is present) + +A regression was introduced in wrangler 3.46.0 (https://github.com/cloudflare/workers-sdk/pull/5215) +which made it so that missing `Request#cf` properties are serialized as `"undefined"`, this in turn +throws a syntax parse error when such values are parsed via `JSON.parse` breaking the communication +with Durable Object local proxies. Fix such issue by serializing missing `Request#cf` properties as +`"{}"` instead. diff --git a/fixtures/get-platform-proxy/tests/get-platform-proxy.env.test.ts b/fixtures/get-platform-proxy/tests/get-platform-proxy.env.test.ts index 20bf1d09317..e553c237018 100644 --- a/fixtures/get-platform-proxy/tests/get-platform-proxy.env.test.ts +++ b/fixtures/get-platform-proxy/tests/get-platform-proxy.env.test.ts @@ -207,7 +207,7 @@ describe("getPlatformProxy - bindings", () => { await dispose(); }); - it.skip("correctly obtains functioning DO bindings (provided by external local workers)", async () => { + it("correctly obtains functioning DO bindings (provided by external local workers)", async () => { const { env, dispose } = await getPlatformProxy({ configPath: wranglerTomlFilePath, }); diff --git a/packages/wrangler/src/dev/miniflare.ts b/packages/wrangler/src/dev/miniflare.ts index 41d28741026..96460959856 100644 --- a/packages/wrangler/src/dev/miniflare.ts +++ b/packages/wrangler/src/dev/miniflare.ts @@ -97,10 +97,10 @@ function createProxyPrototypeClass(handlerSuperKlass, getUnknownPrototypeKey) { return getUnknownPrototypeKey(key); } }); - + return Reflect.construct(handlerSuperKlass, [ctx, env], klass); } - + Reflect.setPrototypeOf(klass.prototype, handlerSuperKlass.prototype); Reflect.setPrototypeOf(klass, handlerSuperKlass); @@ -111,7 +111,7 @@ function createDurableObjectClass({ className, proxyUrl }) { const klass = createProxyPrototypeClass(DurableObject, (key) => { throw new Error(\`Cannot access \\\`\${className}#\${key}\\\` as Durable Object RPC is not yet supported between multiple \\\`wrangler dev\\\` sessions.\`); }); - + // Forward regular HTTP requests to the other "wrangler dev" session klass.prototype.fetch = function(request) { if (proxyUrl === undefined) { @@ -121,7 +121,7 @@ function createDurableObjectClass({ className, proxyUrl }) { proxyRequest.headers.set(HEADER_URL, request.url); proxyRequest.headers.set(HEADER_NAME, className); proxyRequest.headers.set(HEADER_ID, this.ctx.id.toString()); - proxyRequest.headers.set(HEADER_CF_BLOB, JSON.stringify(request.cf)); + proxyRequest.headers.set(HEADER_CF_BLOB, JSON.stringify(request.cf ?? {})); return fetch(proxyRequest); }; @@ -147,7 +147,7 @@ export default { const originalUrl = request.headers.get(HEADER_URL); const className = request.headers.get(HEADER_NAME); const idString = request.headers.get(HEADER_ID); - const cfBlobString = request.headers.get(HEADER_CF_BLOB); + const cf = JSON.parse(request.headers.get(HEADER_CF_BLOB)); if (originalUrl === null || className === null || idString === null) { return new Response("[wrangler] Received Durable Object proxy request with missing headers", { status: 400 }); } @@ -159,7 +159,7 @@ export default { const ns = env[className]; const id = ns.idFromString(idString); const stub = ns.get(id); - return stub.fetch(request, { cf: JSON.parse(cfBlobString ?? "{}") }); + return stub.fetch(request, { cf }); } } `;