Skip to content

Commit

Permalink
feat: updated merging of headers
Browse files Browse the repository at this point in the history
Signed-off-by: Logan McAnsh <logan@mcan.sh>
  • Loading branch information
mcansh committed Aug 18, 2024
1 parent 1d46df2 commit 11f48d6
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions packages/remix-fastify/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,36 @@ export async function sendRemixResponse<Server extends HttpServer>(
): Promise<void> {
reply.status(nodeResponse.status);

let appliedHeaders = new Map<string, Set<string>>();

for (let [key, value] of nodeResponse.headers.entries()) {
let currentHeaders = reply.getHeader(key);
if (currentHeaders) {
if (Array.isArray(currentHeaders)) {
reply.headers({ [key]: [...currentHeaders, value] });
continue;
let currentFastifyHeader = reply.getHeader(key);
let currentValues = appliedHeaders.get(key) || new Set();

if (currentFastifyHeader) {
if (Array.isArray(currentFastifyHeader)) {
for (let header of currentFastifyHeader) {
currentValues.add(value);
currentValues.add(header);
}
} else {
currentValues.add(value);
currentValues.add(currentFastifyHeader as string);
}
reply.headers({ [key]: [String(currentHeaders), value] });
} else {
currentValues.add(value);
}

appliedHeaders.set(key, currentValues);
}

for (let [key, values] of appliedHeaders.entries()) {
if (values.size === 1) {
reply.header(key, values.values().next().value);
continue;
}
reply.headers({ [key]: value });

reply.header(key, [...values]);
}

if (nodeResponse.body) {
Expand Down

0 comments on commit 11f48d6

Please sign in to comment.