From 452873afdbcb4b830092b0b51afbdd0274ea21fa Mon Sep 17 00:00:00 2001 From: Greg Brimble Date: Sun, 6 Oct 2024 19:17:57 -0400 Subject: [PATCH 1/3] Fix headers in wfp dev --- .../dispatchee/index.ts | 2 +- .../outbound/index.ts | 2 +- .../tests/index.test.ts | 22 +++++++++++++------ .../src/dispatch-namespaces/fetcher.ts | 4 ++-- .../middleware/middleware-dispatchee.ts | 3 +-- .../middleware/middleware-outbound.ts | 2 +- 6 files changed, 21 insertions(+), 14 deletions(-) diff --git a/fixtures/external-dispatch-namespace-app/dispatchee/index.ts b/fixtures/external-dispatch-namespace-app/dispatchee/index.ts index a19be93e8355..1b8c03396d84 100644 --- a/fixtures/external-dispatch-namespace-app/dispatchee/index.ts +++ b/fixtures/external-dispatch-namespace-app/dispatchee/index.ts @@ -1,5 +1,5 @@ export default { fetch(request, env) { - return fetch("https://example.com"); + return fetch(request); }, }; diff --git a/fixtures/external-dispatch-namespace-app/outbound/index.ts b/fixtures/external-dispatch-namespace-app/outbound/index.ts index a8928195011f..6e4bb6dd0d78 100644 --- a/fixtures/external-dispatch-namespace-app/outbound/index.ts +++ b/fixtures/external-dispatch-namespace-app/outbound/index.ts @@ -1,6 +1,6 @@ export default { fetch(request, env) { - return new Response("intercepted", { + return new Response(JSON.stringify(Object.fromEntries([...request.headers.entries()])), { headers: { parameter1: env.parameter1, parameter2: env.parameter2 }, }); }, diff --git a/fixtures/external-dispatch-namespace-app/tests/index.test.ts b/fixtures/external-dispatch-namespace-app/tests/index.test.ts index 5241b663596c..892fc8779cb0 100644 --- a/fixtures/external-dispatch-namespace-app/tests/index.test.ts +++ b/fixtures/external-dispatch-namespace-app/tests/index.test.ts @@ -5,17 +5,17 @@ import { fetch } from "undici"; import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest"; import { unstable_dev } from "wrangler"; import type { ChildProcess } from "child_process"; -import type { Response } from "undici"; +import type { Response, RequestInfo, RequestInit } from "undici"; import type { UnstableDevWorker } from "wrangler"; -const waitUntilReady = async (url: string): Promise => { +const waitUntilReady = async (input: RequestInfo, init?: RequestInit): Promise => { let response: Response | undefined = undefined; while (response === undefined) { await setTimeout(500); try { - response = await fetch(url); + response = await fetch(input, init); } catch (e) {} } @@ -74,7 +74,11 @@ describe("external-dispatch-namespace-app", () => { it("dispatches from a Pages project", async () => { const pagesDispatcherResponse = await waitUntilReady( - `http://127.0.0.1:${pagesDispatcher.port}/` + `http://127.0.0.1:${pagesDispatcher.port}/`, { + headers: { + "x-foo": "bar" + } + } ); expect( pagesDispatcherResponse.headers.get("parameter1") @@ -83,13 +87,17 @@ describe("external-dispatch-namespace-app", () => { pagesDispatcherResponse.headers.get("parameter2") ).toMatchInlineSnapshot(`"p2"`); expect(await pagesDispatcherResponse.text()).toMatchInlineSnapshot( - `"intercepted"` + `"{\"x-foo\":\"bar\"}"` ); }); it("dispatches from a Worker", async () => { const dispatcherResponse = await waitUntilReady( - `http://127.0.0.1:${dispatcher.port}/` + `http://127.0.0.1:${dispatcher.port}/`, { + headers: { + "x-foo": "bar" + } + } ); expect(dispatcherResponse.headers.get("parameter1")).toMatchInlineSnapshot( `"p1"` @@ -98,7 +106,7 @@ describe("external-dispatch-namespace-app", () => { `"p2"` ); expect(await dispatcherResponse.text()).toMatchInlineSnapshot( - `"intercepted"` + `"{\"x-foo\":\"bar\"}"` ); }); }); diff --git a/packages/wrangler/src/dispatch-namespaces/fetcher.ts b/packages/wrangler/src/dispatch-namespaces/fetcher.ts index abbb5344bd16..ad07875c5098 100644 --- a/packages/wrangler/src/dispatch-namespaces/fetcher.ts +++ b/packages/wrangler/src/dispatch-namespaces/fetcher.ts @@ -29,8 +29,8 @@ export default function (env) { return { get(name, args, options) { return { - fetch(request) { - request = new Request(request); + fetch(input, init) { + const request = new Request(input, init); request.headers.set(HEADER_SCRIPT_NAME, name); request.headers.set(HEADER_URL, request.url); request.headers.set(HEADER_PARAMETERS, JSON.stringify(Object.fromEntries(Object.entries(options?.outbound ?? {}).filter(([key]) => ALLOWED_PARAMETERS.includes(key))))); diff --git a/packages/wrangler/templates/middleware/middleware-dispatchee.ts b/packages/wrangler/templates/middleware/middleware-dispatchee.ts index 49fec27bcf9f..90f6c98849e5 100644 --- a/packages/wrangler/templates/middleware/middleware-dispatchee.ts +++ b/packages/wrangler/templates/middleware/middleware-dispatchee.ts @@ -23,7 +23,7 @@ const dispatchee: Middleware = async (request, env, _ctx, middlewareCtx) => { const outboundProxyUrl = request.headers.get(HEADER_OUTBOUND_PROXY_URL); const parameters = request.headers.get(HEADER_PARAMETERS); - request = new Request(url, { ...request, cf }); + request = new Request(url, { ...request, cf, method: request.method, headers: request.headers }); request.headers.delete(HEADER_URL); request.headers.delete(HEADER_OUTBOUND_PROXY_URL); @@ -42,7 +42,6 @@ const dispatchee: Middleware = async (request, env, _ctx, middlewareCtx) => { if (parameters) { outboundRequest.headers.set(HEADER_PARAMETERS, parameters); } - console.log(outboundProxyUrl); return originalFetch(outboundProxyUrl, outboundRequest); }; } diff --git a/packages/wrangler/templates/middleware/middleware-outbound.ts b/packages/wrangler/templates/middleware/middleware-outbound.ts index ee41ec945914..908be2a064e4 100644 --- a/packages/wrangler/templates/middleware/middleware-outbound.ts +++ b/packages/wrangler/templates/middleware/middleware-outbound.ts @@ -23,7 +23,7 @@ const outbound: Middleware = async (request, env, _ctx, middlewareCtx) => { env[key] = value; } - request = new Request(url, { ...request, cf }); + request = new Request(url, { ...request, cf, method: request.method, headers: request.headers }); request.headers.delete(HEADER_OUTBOUND_URL); request.headers.delete(HEADER_CF_BLOB); From 4a33b7896005f13e1c52d7a3224120a67a9b7ace Mon Sep 17 00:00:00 2001 From: Greg Brimble Date: Sun, 6 Oct 2024 19:39:46 -0400 Subject: [PATCH 2/3] WIP Test --- fixtures/external-dispatch-namespace-app/tests/index.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fixtures/external-dispatch-namespace-app/tests/index.test.ts b/fixtures/external-dispatch-namespace-app/tests/index.test.ts index 892fc8779cb0..7d0a9d11b85c 100644 --- a/fixtures/external-dispatch-namespace-app/tests/index.test.ts +++ b/fixtures/external-dispatch-namespace-app/tests/index.test.ts @@ -40,6 +40,7 @@ describe("external-dispatch-namespace-app", () => { path.join(__dirname, "../dispatchee/index.ts"), { config: path.join(__dirname, "../dispatchee/wrangler.toml"), + dispatchNamespace: "my-namespace", } ); outbound = await unstable_dev( @@ -87,7 +88,7 @@ describe("external-dispatch-namespace-app", () => { pagesDispatcherResponse.headers.get("parameter2") ).toMatchInlineSnapshot(`"p2"`); expect(await pagesDispatcherResponse.text()).toMatchInlineSnapshot( - `"{\"x-foo\":\"bar\"}"` + `"{\"accept\":\"*/*\",\"accept-encoding\":\"br, gzip\",\"accept-language\":\"*\",\"host\":\"localhost:${outbound.port}\",\"sec-fetch-mode\":\"cors\",\"user-agent\":\"undici\",\"x-foo\":\"bar\"}"` ); }); @@ -106,7 +107,7 @@ describe("external-dispatch-namespace-app", () => { `"p2"` ); expect(await dispatcherResponse.text()).toMatchInlineSnapshot( - `"{\"x-foo\":\"bar\"}"` + `"{\"accept\":\"*/*\",\"accept-encoding\":\"br, gzip\",\"accept-language\":\"*\",\"host\":\"localhost:${outbound.port}\",\"sec-fetch-mode\":\"cors\",\"user-agent\":\"undici\",\"x-foo\":\"bar\"}"` ); }); }); From bfd9dd674387ffa108e9df1d29784839bf543b33 Mon Sep 17 00:00:00 2001 From: Greg Brimble Date: Sun, 6 Oct 2024 21:08:39 -0400 Subject: [PATCH 3/3] Fix Pages too? --- packages/wrangler/src/dev.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/wrangler/src/dev.tsx b/packages/wrangler/src/dev.tsx index fb275f6e90d9..fa95ee45b797 100644 --- a/packages/wrangler/src/dev.tsx +++ b/packages/wrangler/src/dev.tsx @@ -1040,7 +1040,6 @@ export function getBindings( data_blobs: configParam.data_blobs, // inheritable fields - dispatch_namespaces: configParam.dispatch_namespaces, logfwdr: configParam.logfwdr, // non-inheritable fields @@ -1064,7 +1063,7 @@ export function getBindings( browser: configParam.browser, ai: args.ai || configParam.ai, version_metadata: args.version_metadata || configParam.version_metadata, - dispatchNamespaces: mergedDispatchBindings, + dispatch_namespaces: mergedDispatchBindings, unsafe: { bindings: configParam.unsafe.bindings, metadata: configParam.unsafe.metadata,