Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix headers in wfp dev #6904

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default {
fetch(request, env) {
return fetch("https://example.com");
return fetch(request);
},
};
2 changes: 1 addition & 1 deletion fixtures/external-dispatch-namespace-app/outbound/index.ts
Original file line number Diff line number Diff line change
@@ -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 },
});
},
Expand Down
23 changes: 16 additions & 7 deletions fixtures/external-dispatch-namespace-app/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response> => {
const waitUntilReady = async (input: RequestInfo, init?: RequestInit): Promise<Response> => {
let response: Response | undefined = undefined;

while (response === undefined) {
await setTimeout(500);

try {
response = await fetch(url);
response = await fetch(input, init);
} catch (e) {}
}

Expand All @@ -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(
Expand Down Expand Up @@ -74,7 +75,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")
Expand All @@ -83,13 +88,17 @@ describe("external-dispatch-namespace-app", () => {
pagesDispatcherResponse.headers.get("parameter2")
).toMatchInlineSnapshot(`"p2"`);
expect(await pagesDispatcherResponse.text()).toMatchInlineSnapshot(
`"intercepted"`
`"{\"accept\":\"*/*\",\"accept-encoding\":\"br, gzip\",\"accept-language\":\"*\",\"host\":\"localhost:${outbound.port}\",\"sec-fetch-mode\":\"cors\",\"user-agent\":\"undici\",\"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"`
Expand All @@ -98,7 +107,7 @@ describe("external-dispatch-namespace-app", () => {
`"p2"`
);
expect(await dispatcherResponse.text()).toMatchInlineSnapshot(
`"intercepted"`
`"{\"accept\":\"*/*\",\"accept-encoding\":\"br, gzip\",\"accept-language\":\"*\",\"host\":\"localhost:${outbound.port}\",\"sec-fetch-mode\":\"cors\",\"user-agent\":\"undici\",\"x-foo\":\"bar\"}"`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if host here should be preserved or overwritten. Need to check what prod runtime behavior is.

);
});
});
Expand Down
3 changes: 1 addition & 2 deletions packages/wrangler/src/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/wrangler/src/dispatch-namespaces/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
GregBrimble marked this conversation as resolved.
Show resolved Hide resolved

request.headers.delete(HEADER_URL);
request.headers.delete(HEADER_OUTBOUND_PROXY_URL);
Expand All @@ -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);
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading