UNPKG

@worker-tools/middleware

Version:

A suite of standalone HTTP server middlewares for Worker Runtimes.

24 lines 1.07 kB
const CONCAT_SEQ = ', '; const XE = /(Mon|Tue|Wed|Thu|Fri|Sat|Sun), /; const XR = '$1,�'; const YE = /(Mon|Tue|Wed|Thu|Fri|Sat|Sun),�/; const YR = '$1, '; /** * Fixes the iteration of the `Headers` class with respect to `set-cookie` header: * * By default, multiple `set-cookie` headers will be concatenated by the `Headers` class implementation. * However, the HTTP protocol/browsers expect multiple `Set-Cookie` headers, * and do not recognize concatenated `Set-Cookie` headers. * * This helper function fixes this behavior, yielding multiple `set-cookie` key-value tuples, * provided that no cookie value contains the concatenation sequence `', '` (comma empty-space). */ export function iterHeadersSetCookieFix(headers) { return [...headers].flatMap(([h, v]) => h === 'set-cookie' ? v.replace(new RegExp(XE, 'g'), XR) .split(CONCAT_SEQ) .map(x => [h, x.replace(new RegExp(YE, 'g'), YR)]) : [[h, v]]); } export { iterHeadersSetCookieFix as headersSetCookieFix }; //# sourceMappingURL=headers-set-cookie-fix.js.map