UNPKG

1.07 kBJavaScriptView Raw
1const CONCAT_SEQ = ', ';
2const XE = /(Mon|Tue|Wed|Thu|Fri|Sat|Sun), /;
3const XR = '$1,�';
4const YE = /(Mon|Tue|Wed|Thu|Fri|Sat|Sun),�/;
5const YR = '$1, ';
6/**
7 * Fixes the iteration of the `Headers` class with respect to `set-cookie` header:
8 *
9 * By default, multiple `set-cookie` headers will be concatenated by the `Headers` class implementation.
10 * However, the HTTP protocol/browsers expect multiple `Set-Cookie` headers,
11 * and do not recognize concatenated `Set-Cookie` headers.
12 *
13 * This helper function fixes this behavior, yielding multiple `set-cookie` key-value tuples,
14 * provided that no cookie value contains the concatenation sequence `', '` (comma empty-space).
15 */
16export function iterHeadersSetCookieFix(headers) {
17 return [...headers].flatMap(([h, v]) => h === 'set-cookie'
18 ? v.replace(new RegExp(XE, 'g'), XR)
19 .split(CONCAT_SEQ)
20 .map(x => [h, x.replace(new RegExp(YE, 'g'), YR)])
21 : [[h, v]]);
22}
23export { iterHeadersSetCookieFix as headersSetCookieFix };
24//# sourceMappingURL=headers-set-cookie-fix.js.map
\No newline at end of file