refactor(http): extract mergeInjectedHeaders pure helper
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
3 files changed
+60
-21
No files matched your search
@@ -0,0 +1,32 @@
|
|||||||
|
import { mergeVary } from '../security';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Merges a handler's response headers with authoritative `injected` headers
|
||||||
|
* (security + CORS). Injected keys win case-insensitively; `vary` is merged.
|
||||||
|
* Extracted from the Node writeHead patch so app.fetch shares the exact logic.
|
||||||
|
*/
|
||||||
|
export function mergeInjectedHeaders(
|
||||||
|
handlerHeaders: Record<string, any> | undefined,
|
||||||
|
injected: Record<string, string>,
|
||||||
|
): Record<string, any> {
|
||||||
|
const merged: Record<string, any> = {};
|
||||||
|
const injectedLower = new Set(Object.keys(injected).map((key) => key.toLowerCase()));
|
||||||
|
let handlerVary: string | undefined;
|
||||||
|
|
||||||
|
if (handlerHeaders)
|
||||||
|
for (const key of Object.keys(handlerHeaders)) {
|
||||||
|
const lowerKey = key.toLowerCase();
|
||||||
|
|
||||||
|
if (lowerKey === 'vary') {
|
||||||
|
handlerVary = handlerHeaders[key];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!injectedLower.has(lowerKey)) merged[key] = handlerHeaders[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.assign(merged, injected);
|
||||||
|
if (injected['vary']) merged['vary'] = mergeVary(handlerVary, injected['vary']);
|
||||||
|
else if (handlerVary !== undefined) merged['vary'] = handlerVary;
|
||||||
|
return merged;
|
||||||
|
}
|
||||||
+3
-21
@@ -4,12 +4,13 @@ import { renderError, type ErrorRequest, type ErrorRenderer } from '../transform
|
|||||||
import { isHttpError, HttpError, NotFound } from '../signals';
|
import { isHttpError, HttpError, NotFound } from '../signals';
|
||||||
import { isStreamResult, type PipelineResult, type ResponseShape } from '../pipeline';
|
import { isStreamResult, type PipelineResult, type ResponseShape } from '../pipeline';
|
||||||
import type { Bus } from '../bus';
|
import type { Bus } from '../bus';
|
||||||
import { buildSecurityHeaders, mergeVary, resolveCors, corsPreflightHeaders } from '../security';
|
import { buildSecurityHeaders, resolveCors, corsPreflightHeaders } from '../security';
|
||||||
import { parseMultipart, extractBoundary, collapseDuplicates } from '../multipart';
|
import { parseMultipart, extractBoundary, collapseDuplicates } from '../multipart';
|
||||||
import { matchRoute, allowedMethods, parseQuery } from './router';
|
import { matchRoute, allowedMethods, parseQuery } from './router';
|
||||||
import { readBody, deriveSecure, deriveIp } from './request';
|
import { readBody, deriveSecure, deriveIp } from './request';
|
||||||
import { pickEncoder, pipeStream } from './stream';
|
import { pickEncoder, pipeStream } from './stream';
|
||||||
import { attachWs } from './ws';
|
import { attachWs } from './ws';
|
||||||
|
import { mergeInjectedHeaders } from './headers';
|
||||||
import type { RouteDef, WsRouteDef, MatchedRoute, MeshControl, HttpOptions } from './types';
|
import type { RouteDef, WsRouteDef, MatchedRoute, MeshControl, HttpOptions } from './types';
|
||||||
|
|
||||||
interface HandlerConfig {
|
interface HandlerConfig {
|
||||||
@@ -185,27 +186,8 @@ function patchResponseHeaders(res: http.ServerResponse, injected: Record<string,
|
|||||||
const origWriteHead = res.writeHead.bind(res);
|
const origWriteHead = res.writeHead.bind(res);
|
||||||
|
|
||||||
(res as any).writeHead = (status: number, arg2?: any, arg3?: any) => {
|
(res as any).writeHead = (status: number, arg2?: any, arg3?: any) => {
|
||||||
// normalize (statusMessage?, headers?) overloads
|
|
||||||
const handlerHeaders = (typeof arg2 === 'string' ? arg3 : arg2) as Record<string, any> | undefined;
|
const handlerHeaders = (typeof arg2 === 'string' ? arg3 : arg2) as Record<string, any> | undefined;
|
||||||
const merged: Record<string, any> = {};
|
const merged = mergeInjectedHeaders(handlerHeaders, injected);
|
||||||
const injectedLower = new Set(Object.keys(injected).map((key) => key.toLowerCase()));
|
|
||||||
let handlerVary: string | undefined;
|
|
||||||
|
|
||||||
if (handlerHeaders)
|
|
||||||
for (const key of Object.keys(handlerHeaders)) {
|
|
||||||
const lowerKey = key.toLowerCase();
|
|
||||||
|
|
||||||
if (lowerKey === 'vary') {
|
|
||||||
handlerVary = handlerHeaders[key];
|
|
||||||
continue;
|
|
||||||
} // merged separately below
|
|
||||||
|
|
||||||
if (!injectedLower.has(lowerKey)) merged[key] = handlerHeaders[key];
|
|
||||||
}
|
|
||||||
|
|
||||||
Object.assign(merged, injected);
|
|
||||||
if (injected['vary']) merged['vary'] = mergeVary(handlerVary, injected['vary']);
|
|
||||||
else if (handlerVary !== undefined) merged['vary'] = handlerVary; // preserve handler Vary when no CORS
|
|
||||||
return typeof arg2 === 'string' ? origWriteHead(status, arg2, merged) : origWriteHead(status, merged);
|
return typeof arg2 === 'string' ? origWriteHead(status, arg2, merged) : origWriteHead(status, merged);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import { mergeInjectedHeaders } from '../src/http/headers';
|
||||||
|
|
||||||
|
describe('mergeInjectedHeaders', () => {
|
||||||
|
it('makes injected keys authoritative regardless of handler casing', () => {
|
||||||
|
const out = mergeInjectedHeaders(
|
||||||
|
{ 'Content-Type': 'text/html', 'X-Content-Type-Options': 'sniffme' },
|
||||||
|
{ 'x-content-type-options': 'nosniff' },
|
||||||
|
);
|
||||||
|
expect(out['x-content-type-options']).toBe('nosniff');
|
||||||
|
expect(Object.keys(out).filter((k) => k.toLowerCase() === 'x-content-type-options')).toHaveLength(1);
|
||||||
|
expect(out['Content-Type']).toBe('text/html'); // untouched handler header kept
|
||||||
|
});
|
||||||
|
|
||||||
|
it('merges Vary from handler and injected', () => {
|
||||||
|
const out = mergeInjectedHeaders({ Vary: 'Accept' }, { vary: 'Origin' });
|
||||||
|
expect(out['vary'].toLowerCase()).toContain('accept');
|
||||||
|
expect(out['vary'].toLowerCase()).toContain('origin');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('preserves handler Vary when no injected Vary', () => {
|
||||||
|
const out = mergeInjectedHeaders({ Vary: 'Accept' }, {});
|
||||||
|
expect(out['vary']).toBe('Accept');
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user