rekwest 4.0.0 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -3
- package/dist/ackn.js +1 -1
- package/dist/constants.js +27 -0
- package/dist/cookies.js +1 -1
- package/dist/defaults.js +41 -0
- package/dist/formdata.js +12 -12
- package/dist/index.js +27 -185
- package/dist/postflight.js +110 -0
- package/dist/preflight.js +66 -0
- package/dist/utils.js +138 -95
- package/package.json +3 -3
- package/src/ackn.mjs +1 -1
- package/src/constants.mjs +29 -0
- package/src/cookies.mjs +2 -2
- package/src/defaults.mjs +44 -0
- package/src/formdata.mjs +16 -13
- package/src/index.mjs +84 -282
- package/src/postflight.mjs +135 -0
- package/src/preflight.mjs +70 -0
- package/src/utils.mjs +159 -99
package/src/index.mjs
CHANGED
|
@@ -1,282 +1,84 @@
|
|
|
1
|
-
import http from 'node:http';
|
|
2
|
-
import http2 from 'node:http2';
|
|
3
|
-
import https from 'node:https';
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
export
|
|
22
|
-
|
|
23
|
-
export * from './
|
|
24
|
-
export * from './
|
|
25
|
-
export * from './
|
|
26
|
-
export * from './
|
|
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
|
-
throw new TypeError(`Request with ${ HTTP2_METHOD_GET }/${ HTTP2_METHOD_HEAD } method cannot have body.`);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (options.follow === 0) {
|
|
89
|
-
throw new RequestError(`Maximum redirect reached at: ${ url.href }`);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (url.protocol === 'https:') {
|
|
93
|
-
options = await ackn(options);
|
|
94
|
-
} else if (Reflect.has(options, 'alpnProtocol')) {
|
|
95
|
-
[
|
|
96
|
-
'alpnProtocol',
|
|
97
|
-
'createConnection',
|
|
98
|
-
'h2',
|
|
99
|
-
'protocol',
|
|
100
|
-
].forEach((it) => Reflect.deleteProperty(options, it));
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
options = await transform(preflight(options));
|
|
104
|
-
|
|
105
|
-
const { cookies, digest, follow, h2, redirect, redirected, thenable } = options;
|
|
106
|
-
const { request } = (url.protocol === 'http:' ? http : https);
|
|
107
|
-
|
|
108
|
-
const promise = new Promise((resolve, reject) => {
|
|
109
|
-
let client, req;
|
|
110
|
-
|
|
111
|
-
if (h2) {
|
|
112
|
-
client = http2.connect(url.origin, options);
|
|
113
|
-
req = client.request(options.headers, options);
|
|
114
|
-
} else {
|
|
115
|
-
req = request(url, options);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
affix(client, req, options);
|
|
119
|
-
req.once('error', reject);
|
|
120
|
-
req.once('frameError', reject);
|
|
121
|
-
req.once('goaway', reject);
|
|
122
|
-
req.once('response', (res) => {
|
|
123
|
-
let headers;
|
|
124
|
-
|
|
125
|
-
if (h2) {
|
|
126
|
-
headers = res;
|
|
127
|
-
res = req;
|
|
128
|
-
} else {
|
|
129
|
-
res.once('error', reject);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
admix(res, headers, options);
|
|
133
|
-
|
|
134
|
-
if (cookies !== false && res.headers[HTTP2_HEADER_SET_COOKIE]) {
|
|
135
|
-
if (Cookies.jar.has(url.origin)) {
|
|
136
|
-
new Cookies(res.headers[HTTP2_HEADER_SET_COOKIE]).forEach(function (val, key) {
|
|
137
|
-
this.set(key, val);
|
|
138
|
-
}, Cookies.jar.get(url.origin));
|
|
139
|
-
} else {
|
|
140
|
-
Cookies.jar.set(url.origin, new Cookies(res.headers[HTTP2_HEADER_SET_COOKIE]));
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
Reflect.defineProperty(res, 'cookies', {
|
|
145
|
-
enumerable: true,
|
|
146
|
-
value: cookies !== false && Cookies.jar.has(url.origin)
|
|
147
|
-
? Cookies.jar.get(url.origin)
|
|
148
|
-
: void 0,
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
if (follow && /^3\d{2}$/.test(res.statusCode) && res.headers[HTTP2_HEADER_LOCATION]) {
|
|
152
|
-
if (redirect === redirects.error) {
|
|
153
|
-
return res.emit('error', new RequestError(`Unexpected redirect, redirect mode is set to '${ redirect }'.`));
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
if (redirect === redirects.follow) {
|
|
157
|
-
options.url = new URL(res.headers[HTTP2_HEADER_LOCATION], url).href;
|
|
158
|
-
|
|
159
|
-
if (res.statusCode !== HTTP_STATUS_SEE_OTHER && options?.body?.pipe?.constructor === Function) {
|
|
160
|
-
return res.emit('error', new RequestError(`Unable to ${ redirect } redirect with streamable body.`));
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
options.follow--;
|
|
164
|
-
|
|
165
|
-
if (res.statusCode === HTTP_STATUS_SEE_OTHER) {
|
|
166
|
-
Reflect.deleteProperty(options.headers, HTTP2_HEADER_CONTENT_LENGTH);
|
|
167
|
-
options.method = HTTP2_METHOD_GET;
|
|
168
|
-
options.body = null;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
Reflect.set(options, 'redirected', true);
|
|
172
|
-
|
|
173
|
-
if (res.statusCode === HTTP_STATUS_MOVED_PERMANENTLY && res.headers[HTTP2_HEADER_RETRY_AFTER]) {
|
|
174
|
-
let interval = res.headers[HTTP2_HEADER_RETRY_AFTER];
|
|
175
|
-
|
|
176
|
-
interval = Number(interval) * 1000 || new Date(interval) - Date.now();
|
|
177
|
-
|
|
178
|
-
if (interval > options.maxRetryAfter) {
|
|
179
|
-
return res.emit('error', maxRetryAfterError(interval, { cause: mixin(res, options) }));
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return setTimeoutPromise(interval).then(() => rekwest(options.url, options).then(resolve, reject));
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
return rekwest(options.url, options).then(resolve, reject);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
if (res.statusCode >= HTTP_STATUS_BAD_REQUEST) {
|
|
190
|
-
return reject(mixin(res, options));
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
resolve(mixin(res, options));
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
dispatch(options, req);
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
try {
|
|
200
|
-
const res = await promise;
|
|
201
|
-
|
|
202
|
-
if (digest && !redirected) {
|
|
203
|
-
res.body = await res.body();
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return res;
|
|
207
|
-
} catch (ex) {
|
|
208
|
-
const { maxRetryAfter, retry } = options;
|
|
209
|
-
|
|
210
|
-
if (retry?.attempts && retry?.statusCodes.includes(ex.statusCode)) {
|
|
211
|
-
let { interval } = retry;
|
|
212
|
-
|
|
213
|
-
if (retry.retryAfter && ex.headers[HTTP2_HEADER_RETRY_AFTER]) {
|
|
214
|
-
interval = ex.headers[HTTP2_HEADER_RETRY_AFTER];
|
|
215
|
-
interval = Number(interval) * 1000 || new Date(interval) - Date.now();
|
|
216
|
-
if (interval > maxRetryAfter) {
|
|
217
|
-
throw maxRetryAfterError(interval, { cause: ex });
|
|
218
|
-
}
|
|
219
|
-
} else {
|
|
220
|
-
interval = new Function('interval', `return Math.ceil(${ retry.backoffStrategy });`)(interval);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
retry.attempts--;
|
|
224
|
-
retry.interval = interval;
|
|
225
|
-
|
|
226
|
-
return setTimeoutPromise(interval).then(() => rekwest(url, options));
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
if (digest && !redirected && ex.body) {
|
|
230
|
-
ex.body = await ex.body();
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
if (!thenable) {
|
|
234
|
-
throw ex;
|
|
235
|
-
} else {
|
|
236
|
-
return ex;
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
Reflect.defineProperty(rekwest, 'stream', {
|
|
242
|
-
enumerable: true,
|
|
243
|
-
value(...args) {
|
|
244
|
-
const options = preflight({
|
|
245
|
-
...merge(rekwest.defaults, {
|
|
246
|
-
headers: { [HTTP2_HEADER_CONTENT_TYPE]: APPLICATION_OCTET_STREAM },
|
|
247
|
-
}, sanitize(...args)),
|
|
248
|
-
redirect: redirects.manual,
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
const { h2, url } = options;
|
|
252
|
-
const { request } = (url.protocol === 'http:' ? http : https);
|
|
253
|
-
let client, req;
|
|
254
|
-
|
|
255
|
-
if (h2) {
|
|
256
|
-
client = http2.connect(url.origin, options);
|
|
257
|
-
req = client.request(options.headers, options);
|
|
258
|
-
} else {
|
|
259
|
-
req = request(url, options);
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
affix(client, req, options);
|
|
263
|
-
req.once('response', (res) => {
|
|
264
|
-
let headers;
|
|
265
|
-
|
|
266
|
-
if (h2) {
|
|
267
|
-
headers = res;
|
|
268
|
-
res = req;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
admix(res, headers, options);
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
return req;
|
|
275
|
-
},
|
|
276
|
-
});
|
|
277
|
-
|
|
278
|
-
Reflect.defineProperty(rekwest, 'defaults', {
|
|
279
|
-
enumerable: true,
|
|
280
|
-
get() { return defaults; },
|
|
281
|
-
set(value) { defaults = merge(defaults, value); },
|
|
282
|
-
});
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import http2 from 'node:http2';
|
|
3
|
+
import https from 'node:https';
|
|
4
|
+
import { requestRedirect } from './constants.mjs';
|
|
5
|
+
import defaults from './defaults.mjs';
|
|
6
|
+
import { APPLICATION_OCTET_STREAM } from './mediatypes.mjs';
|
|
7
|
+
import { preflight } from './preflight.mjs';
|
|
8
|
+
import {
|
|
9
|
+
admix,
|
|
10
|
+
affix,
|
|
11
|
+
merge,
|
|
12
|
+
sanitize,
|
|
13
|
+
transfer,
|
|
14
|
+
validation,
|
|
15
|
+
} from './utils.mjs';
|
|
16
|
+
|
|
17
|
+
export { constants } from 'node:http2';
|
|
18
|
+
|
|
19
|
+
export * from './ackn.mjs';
|
|
20
|
+
export * from './constants.mjs';
|
|
21
|
+
export * from './cookies.mjs';
|
|
22
|
+
export * from './errors.mjs';
|
|
23
|
+
export * from './file.mjs';
|
|
24
|
+
export * from './formdata.mjs';
|
|
25
|
+
export * as mediatypes from './mediatypes.mjs';
|
|
26
|
+
export * from './utils.mjs';
|
|
27
|
+
|
|
28
|
+
const {
|
|
29
|
+
HTTP2_HEADER_CONTENT_TYPE,
|
|
30
|
+
} = http2.constants;
|
|
31
|
+
|
|
32
|
+
export default function rekwest(...args) {
|
|
33
|
+
let options = sanitize(...args);
|
|
34
|
+
|
|
35
|
+
if (!options.redirected) {
|
|
36
|
+
options = merge(rekwest.defaults, options);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return transfer(validation(options));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
Reflect.defineProperty(rekwest, 'stream', {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
value(...args) {
|
|
45
|
+
const options = preflight({
|
|
46
|
+
...validation(merge(rekwest.defaults, {
|
|
47
|
+
headers: { [HTTP2_HEADER_CONTENT_TYPE]: APPLICATION_OCTET_STREAM },
|
|
48
|
+
}, sanitize(...args))),
|
|
49
|
+
redirect: requestRedirect.manual,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const { h2, url } = options;
|
|
53
|
+
const { request } = (url.protocol === 'http:' ? http : https);
|
|
54
|
+
let client, req;
|
|
55
|
+
|
|
56
|
+
if (h2) {
|
|
57
|
+
client = http2.connect(url.origin, options);
|
|
58
|
+
req = client.request(options.headers, options);
|
|
59
|
+
} else {
|
|
60
|
+
req = request(url, options);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
affix(client, req, options);
|
|
64
|
+
|
|
65
|
+
req.once('response', (res) => {
|
|
66
|
+
let headers;
|
|
67
|
+
|
|
68
|
+
if (h2) {
|
|
69
|
+
headers = res;
|
|
70
|
+
res = req;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
admix(res, headers, options);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return req;
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
Reflect.defineProperty(rekwest, 'defaults', {
|
|
81
|
+
enumerable: true,
|
|
82
|
+
get() { return defaults.stash; },
|
|
83
|
+
set(value) { defaults.stash = merge(defaults.stash, value); },
|
|
84
|
+
});
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import http2 from 'node:http2';
|
|
2
|
+
import { setTimeout as setTimeoutPromise } from 'node:timers/promises';
|
|
3
|
+
import {
|
|
4
|
+
requestCredentials,
|
|
5
|
+
requestRedirect,
|
|
6
|
+
requestRedirectCodes,
|
|
7
|
+
} from './constants.mjs';
|
|
8
|
+
import { Cookies } from './cookies.mjs';
|
|
9
|
+
import { RequestError } from './errors.mjs';
|
|
10
|
+
import rekwest from './index.mjs';
|
|
11
|
+
import {
|
|
12
|
+
admix,
|
|
13
|
+
maxRetryAfterError,
|
|
14
|
+
mixin,
|
|
15
|
+
sameOrigin,
|
|
16
|
+
} from './utils.mjs';
|
|
17
|
+
|
|
18
|
+
const {
|
|
19
|
+
HTTP2_HEADER_AUTHORIZATION,
|
|
20
|
+
HTTP2_HEADER_LOCATION,
|
|
21
|
+
HTTP2_HEADER_RETRY_AFTER,
|
|
22
|
+
HTTP2_HEADER_SET_COOKIE,
|
|
23
|
+
HTTP2_METHOD_GET,
|
|
24
|
+
HTTP2_METHOD_HEAD,
|
|
25
|
+
HTTP2_METHOD_POST,
|
|
26
|
+
HTTP_STATUS_BAD_REQUEST,
|
|
27
|
+
HTTP_STATUS_FOUND,
|
|
28
|
+
HTTP_STATUS_MOVED_PERMANENTLY,
|
|
29
|
+
HTTP_STATUS_SEE_OTHER,
|
|
30
|
+
} = http2.constants;
|
|
31
|
+
|
|
32
|
+
export const postflight = (req, res, options, { reject, resolve }) => {
|
|
33
|
+
const { cookies, credentials, follow, h2, redirect, url } = options;
|
|
34
|
+
let headers;
|
|
35
|
+
|
|
36
|
+
if (h2) {
|
|
37
|
+
headers = res;
|
|
38
|
+
res = req;
|
|
39
|
+
} else {
|
|
40
|
+
res.once('error', reject);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
admix(res, headers, options);
|
|
44
|
+
|
|
45
|
+
if (cookies !== false && res.headers[HTTP2_HEADER_SET_COOKIE]) {
|
|
46
|
+
if (Cookies.jar.has(url.origin)) {
|
|
47
|
+
new Cookies(res.headers[HTTP2_HEADER_SET_COOKIE]).forEach(function (val, key) {
|
|
48
|
+
this.set(key, val);
|
|
49
|
+
}, Cookies.jar.get(url.origin));
|
|
50
|
+
} else {
|
|
51
|
+
Cookies.jar.set(url.origin, new Cookies(res.headers[HTTP2_HEADER_SET_COOKIE]));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
Reflect.defineProperty(res, 'cookies', {
|
|
56
|
+
enumerable: true,
|
|
57
|
+
value: cookies !== false && Cookies.jar.has(url.origin)
|
|
58
|
+
? Cookies.jar.get(url.origin)
|
|
59
|
+
: void 0,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const { statusCode } = res;
|
|
63
|
+
|
|
64
|
+
if (follow && /3\d{2}/.test(statusCode) && res.headers[HTTP2_HEADER_LOCATION]) {
|
|
65
|
+
if (!requestRedirectCodes.includes(statusCode)) {
|
|
66
|
+
return res.emit('error', new RangeError(`Invalid status code: ${ statusCode }`));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (redirect === requestRedirect.error) {
|
|
70
|
+
return res.emit('error', new RequestError(`Unexpected redirect, redirect mode is set to '${ redirect }'.`));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (redirect === requestRedirect.follow) {
|
|
74
|
+
const location = new URL(res.headers[HTTP2_HEADER_LOCATION], url);
|
|
75
|
+
|
|
76
|
+
if (!/^https?:/i.test(location.protocol)) {
|
|
77
|
+
return res.emit('error', new RequestError('URL scheme must be "http" or "https".'));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (!sameOrigin(location, url) && [
|
|
81
|
+
requestCredentials.omit,
|
|
82
|
+
requestCredentials.sameOrigin,
|
|
83
|
+
].includes(credentials)) {
|
|
84
|
+
Reflect.deleteProperty(options.headers, HTTP2_HEADER_AUTHORIZATION);
|
|
85
|
+
location.password = location.username = '';
|
|
86
|
+
if (credentials === requestCredentials.omit) {
|
|
87
|
+
options.cookies = false;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
options.url = location;
|
|
92
|
+
|
|
93
|
+
if (statusCode !== HTTP_STATUS_SEE_OTHER && options.body?.pipe?.constructor === Function) {
|
|
94
|
+
return res.emit('error', new RequestError(`Unable to ${ redirect } redirect with streamable body.`));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
options.follow--;
|
|
98
|
+
|
|
99
|
+
if (([
|
|
100
|
+
HTTP_STATUS_MOVED_PERMANENTLY,
|
|
101
|
+
HTTP_STATUS_FOUND,
|
|
102
|
+
].includes(statusCode) && options.method === HTTP2_METHOD_POST) || (statusCode === HTTP_STATUS_SEE_OTHER && ![
|
|
103
|
+
HTTP2_METHOD_GET,
|
|
104
|
+
HTTP2_METHOD_HEAD,
|
|
105
|
+
].includes(options.method))) {
|
|
106
|
+
Object.keys(options.headers).filter((it) => /^content-/i.test(it))
|
|
107
|
+
.forEach((it) => Reflect.deleteProperty(options.headers, it));
|
|
108
|
+
options.body = null;
|
|
109
|
+
options.method = HTTP2_METHOD_GET;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
Reflect.set(options, 'redirected', true);
|
|
113
|
+
|
|
114
|
+
if (statusCode === HTTP_STATUS_MOVED_PERMANENTLY && res.headers[HTTP2_HEADER_RETRY_AFTER]) {
|
|
115
|
+
let interval = res.headers[HTTP2_HEADER_RETRY_AFTER];
|
|
116
|
+
|
|
117
|
+
interval = Number(interval) * 1000 || new Date(interval) - Date.now();
|
|
118
|
+
|
|
119
|
+
if (interval > options.maxRetryAfter) {
|
|
120
|
+
return res.emit('error', maxRetryAfterError(interval, { cause: mixin(res, options) }));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return setTimeoutPromise(interval).then(() => rekwest(options.url, options).then(resolve, reject));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return rekwest(options.url, options).then(resolve, reject);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (statusCode >= HTTP_STATUS_BAD_REQUEST) {
|
|
131
|
+
return reject(mixin(res, options));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
resolve(mixin(res, options));
|
|
135
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import http2 from 'node:http2';
|
|
2
|
+
import { requestCredentials } from './constants.mjs';
|
|
3
|
+
import { Cookies } from './cookies.mjs';
|
|
4
|
+
import {
|
|
5
|
+
APPLICATION_JSON,
|
|
6
|
+
TEXT_PLAIN,
|
|
7
|
+
WILDCARD,
|
|
8
|
+
} from './mediatypes.mjs';
|
|
9
|
+
|
|
10
|
+
const {
|
|
11
|
+
HTTP2_HEADER_ACCEPT,
|
|
12
|
+
HTTP2_HEADER_ACCEPT_ENCODING,
|
|
13
|
+
HTTP2_HEADER_AUTHORITY,
|
|
14
|
+
HTTP2_HEADER_COOKIE,
|
|
15
|
+
HTTP2_HEADER_METHOD,
|
|
16
|
+
HTTP2_HEADER_PATH,
|
|
17
|
+
HTTP2_HEADER_SCHEME,
|
|
18
|
+
HTTP2_METHOD_GET,
|
|
19
|
+
HTTP2_METHOD_HEAD,
|
|
20
|
+
} = http2.constants;
|
|
21
|
+
|
|
22
|
+
export const preflight = (options) => {
|
|
23
|
+
const { cookies, credentials, h2 = false, headers, method, url } = options;
|
|
24
|
+
|
|
25
|
+
if (h2) {
|
|
26
|
+
options.endStream = [
|
|
27
|
+
HTTP2_METHOD_GET,
|
|
28
|
+
HTTP2_METHOD_HEAD,
|
|
29
|
+
].includes(method);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (cookies !== false) {
|
|
33
|
+
let cookie = Cookies.jar.get(url.origin);
|
|
34
|
+
|
|
35
|
+
if (cookies === Object(cookies) && [
|
|
36
|
+
requestCredentials.include,
|
|
37
|
+
requestCredentials.sameOrigin,
|
|
38
|
+
].includes(credentials)) {
|
|
39
|
+
if (cookie) {
|
|
40
|
+
new Cookies(cookies).forEach(function (val, key) {
|
|
41
|
+
this.set(key, val);
|
|
42
|
+
}, cookie);
|
|
43
|
+
} else {
|
|
44
|
+
cookie = new Cookies(cookies);
|
|
45
|
+
Cookies.jar.set(url.origin, cookie);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
options.headers = {
|
|
50
|
+
...cookie && { [HTTP2_HEADER_COOKIE]: cookie },
|
|
51
|
+
...headers,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
options.h2 ??= h2;
|
|
56
|
+
options.headers = {
|
|
57
|
+
[HTTP2_HEADER_ACCEPT]: `${ APPLICATION_JSON }, ${ TEXT_PLAIN }, ${ WILDCARD }`,
|
|
58
|
+
[HTTP2_HEADER_ACCEPT_ENCODING]: 'br, deflate, deflate-raw, gzip, identity',
|
|
59
|
+
...Object.entries(options.headers ?? {})
|
|
60
|
+
.reduce((acc, [key, val]) => (acc[key.toLowerCase()] = val, acc), {}),
|
|
61
|
+
...h2 && {
|
|
62
|
+
[HTTP2_HEADER_AUTHORITY]: url.host,
|
|
63
|
+
[HTTP2_HEADER_METHOD]: method,
|
|
64
|
+
[HTTP2_HEADER_PATH]: `${ url.pathname }${ url.search }`,
|
|
65
|
+
[HTTP2_HEADER_SCHEME]: url.protocol.replace(/\p{Punctuation}/gu, ''),
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
return options;
|
|
70
|
+
};
|