Skip to content
Open
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
3 changes: 2 additions & 1 deletion packages/open-next/src/core/routing/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ export function convertRes(res: OpenNextNodeResponse): InternalResult {
// When using HEAD requests, it seems that flushHeaders is not called, not sure why
// Probably some kind of race condition
const headers = parseHeaders(res.getFixedHeaders());
const contentType = headers["content-type"];
const isBase64Encoded =
isBinaryContentType(headers["content-type"]) ||
(typeof contentType === "string" && isBinaryContentType(contentType)) ||
!!headers["content-encoding"];
// We cannot convert the OpenNextNodeResponse to a ReadableStream directly
// You can look in the `aws-lambda.ts` file for some context
Expand Down
4 changes: 2 additions & 2 deletions packages/open-next/src/http/openNextResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Transform } from "node:stream";

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

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

const parsedHeaders = parseHeaders(this.headers);
const parsedHeaders = flattenHeaders(this.headers);

// We need to remove the set-cookie header from the parsed headers because
// it does not handle multiple set-cookie headers properly
Expand Down
27 changes: 19 additions & 8 deletions packages/open-next/src/http/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type http from "node:http";
export const parseHeaders = (
headers?: http.OutgoingHttpHeader[] | http.OutgoingHttpHeaders,
) => {
const result: Record<string, string> = {};
const result: Record<string, string | string[]> = {};
if (!headers) {
return result;
}
Expand All @@ -12,20 +12,31 @@ export const parseHeaders = (
if (value === undefined) {
continue;
}
result[key.toLowerCase()] = convertHeader(value);
result[key.toLowerCase()] =
typeof value === "number" ? String(value) : value;
}

return result;
};

export const convertHeader = (header: http.OutgoingHttpHeader) => {
if (typeof header === "string") {
return header;
export const flattenHeaders = (
headers?: http.OutgoingHttpHeader[] | http.OutgoingHttpHeaders,
) => {
const result: Record<string, string> = {};
if (!headers) {
return result;
}
if (Array.isArray(header)) {
return header.join(",");

for (const [key, value] of Object.entries(headers)) {
if (value === undefined) {
continue;
}
result[key.toLowerCase()] = Array.isArray(value)
? value.join(",")
: String(value);
}
return String(header);

return result;
};

/**
Expand Down
Loading