Skip to content

Commit

Permalink
perf(headers): use object.keys + reduce instead of entries
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Jun 7, 2023
1 parent f72967a commit 41339c6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 50 deletions.
29 changes: 14 additions & 15 deletions src/adapters/aws/lambda-edge.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,13 @@ export class LambdaEdgeAdapter
protected getFlattenedHeadersFromCloudfrontRequest(
cloudFrontRequest: CloudFrontRequest,
): SingleValueHeaders {
const headers: SingleValueHeaders = {};
const headerValuePairs = Object.entries(cloudFrontRequest.headers);
return Object.keys(cloudFrontRequest.headers).reduce((acc, headerKey) => {
const headerValue = cloudFrontRequest.headers[headerKey];

for (const [headerKey, headerValue] of headerValuePairs)
headers[headerKey] = headerValue.map(header => header.value).join(',');
acc[headerKey] = headerValue.map(header => header.value).join(',');

return headers;
return acc;
}, {} as SingleValueHeaders);
}

/**
Expand Down Expand Up @@ -430,32 +430,31 @@ export class LambdaEdgeAdapter
protected getHeadersForCloudfrontResponse(
originalHeaders: BothValueHeaders,
): CloudFrontHeaders {
const originalHeadersEntries = Object.entries(originalHeaders);
const headers: CloudFrontHeaders = {};
return Object.keys(originalHeaders).reduce((acc, headerKey) => {
if (this.shouldStripHeader(headerKey)) return acc;

for (const [headerKey, headerValue] of originalHeadersEntries) {
if (this.shouldStripHeader(headerKey)) continue;
if (!acc[headerKey]) acc[headerKey] = [];

if (!headers[headerKey]) headers[headerKey] = [];
const headerValue = originalHeaders[headerKey];

if (!Array.isArray(headerValue)) {
headers[headerKey].push({
acc[headerKey].push({
key: headerKey,
value: headerValue || '',
});

continue;
return acc;
}

const headersArray = headerValue.map(value => ({
key: headerKey,
value: value,
}));

headers[headerKey].push(...headersArray);
}
acc[headerKey].push(...headersArray);

return headers;
return acc;
}, {} as CloudFrontHeaders);
}

/**
Expand Down
68 changes: 33 additions & 35 deletions src/core/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,15 @@ export function getFlattenedHeadersMap(
separator: string = ',',
lowerCaseKey: boolean = false,
): Record<string, string> {
const commaDelimitedHeaders: Record<string, string> = {};
const headersMapEntries = Object.entries(headersMap);

for (const [headerKey, headerValue] of headersMapEntries) {
return Object.keys(headersMap).reduce((acc, headerKey) => {
const newKey = lowerCaseKey ? headerKey.toLowerCase() : headerKey;
const headerValue = headersMap[headerKey];

if (Array.isArray(headerValue))
commaDelimitedHeaders[newKey] = headerValue.join(separator);
else commaDelimitedHeaders[newKey] = String(headerValue ?? '');
}
if (Array.isArray(headerValue)) acc[newKey] = headerValue.join(separator);
else acc[newKey] = (headerValue ?? '') + '';

return commaDelimitedHeaders;
return acc;
}, {});
}

/**
Expand All @@ -60,16 +57,15 @@ export function getFlattenedHeadersMap(
export function getMultiValueHeadersMap(
headersMap: BothValueHeaders,
): Record<string, string[]> {
const multiValueHeaders: Record<string, string[]> = {};
const headersMapEntries = Object.entries(headersMap);
return Object.keys(headersMap).reduce((acc, headerKey) => {
const headerValue = headersMap[headerKey];

for (const [headerKey, headerValue] of headersMapEntries) {
multiValueHeaders[headerKey.toLowerCase()] = Array.isArray(headerValue)
acc[headerKey.toLowerCase()] = Array.isArray(headerValue)
? headerValue.map(String)
: [String(headerValue)];
}

return multiValueHeaders;
return acc;
}, {});
}

/**
Expand Down Expand Up @@ -101,26 +97,28 @@ export type FlattenedHeadersAndCookies = {
export function getFlattenedHeadersMapAndCookies(
headersMap: BothValueHeaders,
): FlattenedHeadersAndCookies {
const headers: FlattenedHeadersAndCookies = {
cookies: [],
headers: {},
};

for (const [headerKey, headerValue] of Object.entries(headersMap)) {
const lowerHeaderKey = headerKey.toLowerCase();

if (Array.isArray(headerValue)) {
if (lowerHeaderKey !== 'set-cookie')
headers.headers[headerKey] = headerValue.join(',');
else headers.cookies.push(...headerValue);
} else {
if (lowerHeaderKey === 'set-cookie' && headerValue !== undefined)
headers.cookies.push(headerValue ?? '');
else headers.headers[headerKey] = String(headerValue ?? '');
}
}

return headers;
return Object.keys(headersMap).reduce(
(acc, headerKey) => {
const headerValue = headersMap[headerKey];
const lowerHeaderKey = headerKey.toLowerCase();

if (Array.isArray(headerValue)) {
if (lowerHeaderKey !== 'set-cookie')
acc.headers[headerKey] = headerValue.join(',');
else acc.cookies.push(...headerValue);
} else {
if (lowerHeaderKey === 'set-cookie' && headerValue !== undefined)
acc.cookies.push(headerValue ?? '');
else acc.headers[headerKey] = String(headerValue ?? '');
}

return acc;
},
{
cookies: [],
headers: {},
} as FlattenedHeadersAndCookies,
);
}

/**
Expand Down

0 comments on commit 41339c6

Please sign in to comment.