Skip to content

Commit

Permalink
fix: ensure url and cf blob preserved across service bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbbot committed Mar 26, 2024
1 parent 4934a78 commit 8ae7dde
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changeset/afraid-jeans-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: ensure request `url` and `cf` properties preserved across service bindings

Previously, Wrangler could rewrite `url` and `cf` properties when sending requests via service bindings or Durable Object stubs. To match production behaviour, this change ensures these properties are preserved.
1 change: 1 addition & 0 deletions packages/miniflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import {
import {
Config,
Extension,
HttpOptions_Style,
Runtime,
RuntimeOptions,
Service,
Expand Down
13 changes: 11 additions & 2 deletions packages/wrangler/src/dev/miniflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from "node:path";
import * as esmLexer from "es-module-lexer";
import {
CoreHeaders,
HttpOptions_Style,
Log,
LogLevel,
Miniflare,
Expand Down Expand Up @@ -60,6 +61,7 @@ const EXTERNAL_DURABLE_OBJECTS_WORKER_SCRIPT = `
const HEADER_URL = "X-Miniflare-Durable-Object-URL";
const HEADER_NAME = "X-Miniflare-Durable-Object-Name";
const HEADER_ID = "X-Miniflare-Durable-Object-Id";
const HEADER_CF_BLOB = "X-Miniflare-Durable-Object-Cf-Blob";
function createClass({ className, proxyUrl }) {
return class {
Expand All @@ -74,6 +76,7 @@ function createClass({ className, proxyUrl }) {
proxyRequest.headers.set(HEADER_URL, request.url);
proxyRequest.headers.set(HEADER_NAME, className);
proxyRequest.headers.set(HEADER_ID, this.id);
proxyRequest.headers.set(HEADER_CF_BLOB, JSON.stringify(request.cf));
return fetch(proxyRequest);
}
}
Expand All @@ -84,17 +87,19 @@ 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);
if (originalUrl === null || className === null || idString === null) {
return new Response("[wrangler] Received Durable Object proxy request with missing headers", { status: 400 });
}
request = new Request(originalUrl, request);
request.headers.delete(HEADER_URL);
request.headers.delete(HEADER_NAME);
request.headers.delete(HEADER_ID);
request.headers.delete(HEADER_CF_BLOB);
const ns = env[className];
const id = ns.idFromString(idString);
const stub = ns.get(id);
return stub.fetch(request);
return stub.fetch(request, { cf: JSON.parse(cfBlobString ?? "{}") });
}
}
`;
Expand Down Expand Up @@ -351,6 +356,7 @@ export function buildMiniflareBindingOptions(config: MiniflareBindingsConfig): {
// script. They don't need to persist anything, and would end up using the
// incorrect unsafe unique key.
unsafeEphemeralDurableObjects: true,
compatibilityDate: "2024-01-01",
modules: true,
script:
EXTERNAL_DURABLE_OBJECTS_WORKER_SCRIPT +
Expand Down Expand Up @@ -459,7 +465,10 @@ export function buildMiniflareBindingOptions(config: MiniflareBindingsConfig): {
serviceBindings[service.binding] = {
external: {
address,
http: { cfBlobHeader: CoreHeaders.CF_BLOB }, // TODO(now): test cf blob/original URL passthrough
http: {
style: HttpOptions_Style.PROXY,
cfBlobHeader: CoreHeaders.CF_BLOB,
},
},
};
}
Expand Down

0 comments on commit 8ae7dde

Please sign in to comment.