Skip to content

Commit 07df95a

Browse files
committed
fix: prevent unnecessary flattening of headers during convertRes
1 parent fd95b22 commit 07df95a

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

packages/open-next/src/core/routing/util.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,10 @@ export function convertRes(res: OpenNextNodeResponse): InternalResult {
102102
// When using HEAD requests, it seems that flushHeaders is not called, not sure why
103103
// Probably some kind of race condition
104104
const headers = parseHeaders(res.getFixedHeaders());
105+
const contentType = headers["content-type"];
105106
const isBase64Encoded =
106-
isBinaryContentType(headers["content-type"]) ||
107-
!!headers["content-encoding"];
107+
typeof contentType === "string" &&
108+
(isBinaryContentType(contentType) || !!headers["content-encoding"]);
108109
// We cannot convert the OpenNextNodeResponse to a ReadableStream directly
109110
// You can look in the `aws-lambda.ts` file for some context
110111
const body = Readable.toWeb(Readable.from(res.getBody()));

packages/open-next/src/http/openNextResponse.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Transform } from "node:stream";
1010

1111
import type { StreamCreator } from "types/open-next";
1212
import { debug } from "../adapters/logger";
13-
import { parseHeaders, parseSetCookieHeader } from "./util";
13+
import { flattenHeaders, parseSetCookieHeader } from "./util";
1414

1515
const SET_COOKIE_HEADER = "set-cookie";
1616
const CANNOT_BE_USED = "This cannot be used in OpenNext";
@@ -184,7 +184,7 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
184184
// We need to fix the set-cookie header here
185185
this.headers[SET_COOKIE_HEADER] = this._cookies;
186186

187-
const parsedHeaders = parseHeaders(this.headers);
187+
const parsedHeaders = flattenHeaders(this.headers);
188188

189189
// We need to remove the set-cookie header from the parsed headers because
190190
// it does not handle multiple set-cookie headers properly

packages/open-next/src/http/util.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type http from "node:http";
33
export const parseHeaders = (
44
headers?: http.OutgoingHttpHeader[] | http.OutgoingHttpHeaders,
55
) => {
6-
const result: Record<string, string> = {};
6+
const result: Record<string, string | string[]> = {};
77
if (!headers) {
88
return result;
99
}
@@ -12,20 +12,31 @@ export const parseHeaders = (
1212
if (value === undefined) {
1313
continue;
1414
}
15-
result[key.toLowerCase()] = convertHeader(value);
15+
result[key.toLowerCase()] =
16+
typeof value === "number" ? String(value) : value;
1617
}
1718

1819
return result;
1920
};
2021

21-
export const convertHeader = (header: http.OutgoingHttpHeader) => {
22-
if (typeof header === "string") {
23-
return header;
22+
export const flattenHeaders = (
23+
headers?: http.OutgoingHttpHeader[] | http.OutgoingHttpHeaders,
24+
) => {
25+
const result: Record<string, string> = {};
26+
if (!headers) {
27+
return result;
2428
}
25-
if (Array.isArray(header)) {
26-
return header.join(",");
29+
30+
for (const [key, value] of Object.entries(headers)) {
31+
if (value === undefined) {
32+
continue;
33+
}
34+
result[key.toLowerCase()] = Array.isArray(value)
35+
? value.join(",")
36+
: String(value);
2737
}
28-
return String(header);
38+
39+
return result;
2940
};
3041

3142
/**

0 commit comments

Comments
 (0)