Skip to content

Commit

Permalink
[wrangler] fix: fix local Durable Object proxies not working (#5669)
Browse files Browse the repository at this point in the history
  • Loading branch information
dario-piotrowicz committed Apr 23, 2024
1 parent 6424f0a commit a7e36d5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
11 changes: 11 additions & 0 deletions .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.
Expand Up @@ -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<Env>({
configPath: wranglerTomlFilePath,
});
Expand Down
12 changes: 6 additions & 6 deletions packages/wrangler/src/dev/miniflare.ts
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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);
};
Expand All @@ -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 });
}
Expand All @@ -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 });
}
}
`;
Expand Down

0 comments on commit a7e36d5

Please sign in to comment.