-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreflight.mjs
91 lines (78 loc) · 2.66 KB
/
preflight.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import http2 from 'node:http2';
import { isZstdSupported } from './config.mjs';
import { requestCredentials } from './constants.mjs';
import { Cookies } from './cookies.mjs';
const {
HTTP2_HEADER_ACCEPT_ENCODING,
HTTP2_HEADER_AUTHORITY,
HTTP2_HEADER_AUTHORIZATION,
HTTP2_HEADER_COOKIE,
HTTP2_HEADER_METHOD,
HTTP2_HEADER_PATH,
HTTP2_HEADER_SCHEME,
HTTP2_METHOD_GET,
HTTP2_METHOD_HEAD,
} = http2.constants;
export const preflight = (options) => {
const { cookies, credentials, h2, headers, method, url } = options;
if (h2) {
options.endStream = [
HTTP2_METHOD_GET,
HTTP2_METHOD_HEAD,
].includes(method);
}
if (cookies !== false && credentials !== requestCredentials.omit) {
let cookie = Cookies.jar.has(url.origin);
if (cookies === Object(cookies) && [
requestCredentials.include,
requestCredentials.sameOrigin,
].includes(credentials)) {
if (cookie) {
cookie = new Cookies(cookies, options);
Cookies.jar.get(url.origin).forEach((val, key) => {
if (!cookie.has(key)) {
cookie.set(key, val);
}
});
Cookies.jar.set(url.origin, cookie);
} else {
cookie = new Cookies(cookies, options);
Cookies.jar.set(url.origin, cookie);
}
} else {
cookie &&= Cookies.jar.get(url.origin);
}
options.headers = {
...cookie && { [HTTP2_HEADER_COOKIE]: cookie },
...headers,
};
}
if (credentials === requestCredentials.omit) {
options.cookies = false;
for (const it of Object.keys(options.headers ?? {})
.filter((val) => new RegExp(`^(${
HTTP2_HEADER_AUTHORIZATION }|${ HTTP2_HEADER_COOKIE
})$`, 'i').test(val))) { Reflect.deleteProperty(options.headers, it); }
url.password = url.username = '';
}
options.headers = {
...Object.entries(options.headers ?? {})
.reduce((acc, [key, val]) => {
acc[key.toLowerCase()] = val;
if (acc[HTTP2_HEADER_ACCEPT_ENCODING]?.match(/\bzstd\b/i) && !isZstdSupported) {
acc[HTTP2_HEADER_ACCEPT_ENCODING] = val.replace(/\s?zstd,?/i, '').trim();
if (!acc[HTTP2_HEADER_ACCEPT_ENCODING]) {
Reflect.deleteProperty(acc, HTTP2_HEADER_ACCEPT_ENCODING);
}
}
return acc;
}, {}),
...h2 && {
[HTTP2_HEADER_AUTHORITY]: url.host,
[HTTP2_HEADER_METHOD]: method,
[HTTP2_HEADER_PATH]: `${ url.pathname }${ url.search }`,
[HTTP2_HEADER_SCHEME]: url.protocol.replace(/\p{Punctuation}/gu, ''),
},
};
return options;
};