Skip to content

Commit

Permalink
fix headers object to accept duplicated keys like set-cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
guitavano committed Jul 23, 2024
1 parent 11b802f commit 7faea45
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{}
{}
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[![JSR](https://jsr.io/badges/@deco/warp)](https://jsr.io/@deco/warp)
[![JSR Score](https://jsr.io/badges/@deco/warp/score)](https://jsr.io/@deco/warp)


# Warp

**Warp** is a simple tool that allows your locally running HTTP(s) servers to
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@deco/warp",
"version": "0.3.0",
"version": "0.3.1",
"exports": "./mod.ts",
"tasks": {
"check": "deno fmt && deno lint && deno check mod.ts"
Expand Down
11 changes: 10 additions & 1 deletion handlers.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,22 @@ async function doFetch(
signal,
},
);

const headers: Record<string, Array<string>> = {};

for (const [key, value] of response.headers.entries()) {
headers[key] ??= [];
headers[key].push(value);
}

await clientCh.send({
type: "response-start",
id: request.id,
statusCode: response.status,
statusMessage: response.statusText,
headers: Object.fromEntries(response.headers.entries()),
headers,
});

const body = response?.body;
const stream = body ? makeChanStream(body) : undefined;
for await (const chunk of stream?.recv(signal) ?? []) {
Expand Down
14 changes: 11 additions & 3 deletions handlers.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,17 @@ const onResponseStart: ClientMessageHandler<ResponseStartMessage> = (
return;
}
const headers = new Headers();
Object.entries(message.headers).forEach(([key, value]: [string, string]) => {
headers.set(key, value);
});
Object.entries(message.headers).forEach(
([key, value]: [string, string | Array<string>]) => {
if (typeof value === "string") {
headers.set(key, value);
} else if (Array.isArray(value)) {
value.forEach((v) => {
headers.append(key, v);
});
}
},
);
const shouldBeNullBody = NULL_BODIES.includes(message.statusCode);
const stream = !shouldBeNullBody && request.responseBodyChan
? makeReadableStream(request.responseBodyChan)
Expand Down
2 changes: 1 addition & 1 deletion messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface ResponseStartMessage {
id: string;
statusCode: number;
statusMessage: string;
headers: Record<string, string>;
headers: Record<string, Array<string>>;
}
export interface DataMessage {
type: "data";
Expand Down

0 comments on commit 7faea45

Please sign in to comment.