rekwest 2.4.0 → 3.0.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 +29 -20
- package/dist/errors.js +8 -4
- package/dist/helpers.js +117 -69
- package/dist/index.js +115 -59
- package/package.json +7 -7
- package/src/ackn.mjs +33 -33
- package/src/cookies.mjs +24 -24
- package/src/errors.mjs +4 -2
- package/src/file.mjs +40 -40
- package/src/formdata.mjs +224 -224
- package/src/helpers.mjs +375 -331
- package/src/index.mjs +103 -61
package/src/helpers.mjs
CHANGED
|
@@ -1,331 +1,375 @@
|
|
|
1
|
-
import { Blob } from 'buffer';
|
|
2
|
-
import http2 from 'http2';
|
|
3
|
-
import {
|
|
4
|
-
PassThrough,
|
|
5
|
-
Readable,
|
|
6
|
-
} from 'stream';
|
|
7
|
-
import {
|
|
8
|
-
promisify,
|
|
9
|
-
types,
|
|
10
|
-
} from 'util';
|
|
11
|
-
import zlib from 'zlib';
|
|
12
|
-
import { Cookies } from './cookies.mjs';
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
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
|
-
const
|
|
41
|
-
const
|
|
42
|
-
const
|
|
43
|
-
const
|
|
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
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
};
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
[
|
|
295
|
-
[
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
}
|
|
1
|
+
import { Blob } from 'buffer';
|
|
2
|
+
import http2 from 'http2';
|
|
3
|
+
import {
|
|
4
|
+
PassThrough,
|
|
5
|
+
Readable,
|
|
6
|
+
} from 'stream';
|
|
7
|
+
import {
|
|
8
|
+
promisify,
|
|
9
|
+
types,
|
|
10
|
+
} from 'util';
|
|
11
|
+
import zlib from 'zlib';
|
|
12
|
+
import { Cookies } from './cookies.mjs';
|
|
13
|
+
import { TimeoutError } from './errors.mjs';
|
|
14
|
+
import { File } from './file.mjs';
|
|
15
|
+
import { FormData } from './formdata.mjs';
|
|
16
|
+
import {
|
|
17
|
+
APPLICATION_FORM_URLENCODED,
|
|
18
|
+
APPLICATION_JSON,
|
|
19
|
+
APPLICATION_OCTET_STREAM,
|
|
20
|
+
TEXT_PLAIN,
|
|
21
|
+
WILDCARD,
|
|
22
|
+
} from './mediatypes.mjs';
|
|
23
|
+
|
|
24
|
+
const {
|
|
25
|
+
HTTP2_HEADER_ACCEPT,
|
|
26
|
+
HTTP2_HEADER_ACCEPT_ENCODING,
|
|
27
|
+
HTTP2_HEADER_AUTHORITY,
|
|
28
|
+
HTTP2_HEADER_CONTENT_ENCODING,
|
|
29
|
+
HTTP2_HEADER_CONTENT_LENGTH,
|
|
30
|
+
HTTP2_HEADER_CONTENT_TYPE,
|
|
31
|
+
HTTP2_HEADER_COOKIE,
|
|
32
|
+
HTTP2_HEADER_METHOD,
|
|
33
|
+
HTTP2_HEADER_PATH,
|
|
34
|
+
HTTP2_HEADER_SCHEME,
|
|
35
|
+
HTTP2_HEADER_STATUS,
|
|
36
|
+
HTTP2_METHOD_GET,
|
|
37
|
+
HTTP2_METHOD_HEAD,
|
|
38
|
+
} = http2.constants;
|
|
39
|
+
|
|
40
|
+
const brotliCompress = promisify(zlib.brotliCompress);
|
|
41
|
+
const brotliDecompress = promisify(zlib.brotliDecompress);
|
|
42
|
+
const gzip = promisify(zlib.gzip);
|
|
43
|
+
const gunzip = promisify(zlib.gunzip);
|
|
44
|
+
const deflate = promisify(zlib.deflate);
|
|
45
|
+
const inflate = promisify(zlib.inflate);
|
|
46
|
+
|
|
47
|
+
export const admix = (res, headers, options) => {
|
|
48
|
+
const { h2 } = options;
|
|
49
|
+
|
|
50
|
+
if (h2) {
|
|
51
|
+
Reflect.defineProperty(res, 'headers', {
|
|
52
|
+
enumerable: true,
|
|
53
|
+
value: headers,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
Reflect.defineProperty(res, 'httpVersion', {
|
|
57
|
+
enumerable: true,
|
|
58
|
+
value: `${ h2 + 1 }.0`,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
Reflect.defineProperty(res, 'statusCode', {
|
|
62
|
+
enumerable: true,
|
|
63
|
+
value: headers[HTTP2_HEADER_STATUS],
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
Reflect.defineProperty(res, 'ok', {
|
|
68
|
+
enumerable: true,
|
|
69
|
+
value: /^2\d{2}$/.test(res.statusCode),
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
Reflect.defineProperty(res, 'redirected', {
|
|
73
|
+
enumerable: true,
|
|
74
|
+
value: !!options.redirected,
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export const affix = (client, req, options) => {
|
|
79
|
+
req.once('end', () => client?.close());
|
|
80
|
+
req.once('timeout', () => req.destroy(new TimeoutError(`Timed out after ${ options.timeout } ms.`)));
|
|
81
|
+
req.once('trailers', (trailers) => {
|
|
82
|
+
Reflect.defineProperty(req, 'trailers', {
|
|
83
|
+
enumerable: true,
|
|
84
|
+
value: trailers,
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export const compress = (buf, encoding, { async = false } = {}) => {
|
|
90
|
+
encoding &&= encoding.match(/(?<encoding>\bbr\b|\bdeflate\b|\bgzip\b)/i)?.groups.encoding.toLowerCase();
|
|
91
|
+
const compressor = {
|
|
92
|
+
br: async ? brotliCompress : zlib.brotliCompressSync,
|
|
93
|
+
deflate: async ? deflate : zlib.deflateSync,
|
|
94
|
+
gzip: async ? gzip : zlib.gzipSync,
|
|
95
|
+
}[encoding];
|
|
96
|
+
|
|
97
|
+
return compressor?.(buf) ?? (async ? Promise.resolve(buf) : buf);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export const decompress = (buf, encoding, { async = false } = {}) => {
|
|
101
|
+
encoding &&= encoding.match(/(?<encoding>\bbr\b|\bdeflate\b|\bgzip\b)/i)?.groups.encoding.toLowerCase();
|
|
102
|
+
const decompressor = {
|
|
103
|
+
br: async ? brotliDecompress : zlib.brotliDecompressSync,
|
|
104
|
+
deflate: async ? inflate : zlib.inflateSync,
|
|
105
|
+
gzip: async ? gunzip : zlib.gunzipSync,
|
|
106
|
+
}[encoding];
|
|
107
|
+
|
|
108
|
+
return decompressor?.(buf) ?? (async ? Promise.resolve(buf) : buf);
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export const dispatch = (req, { body, headers }) => {
|
|
112
|
+
if (types.isUint8Array(body)) {
|
|
113
|
+
return req.end(body);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (body === Object(body) && !Buffer.isBuffer(body)) {
|
|
117
|
+
if (body.pipe?.constructor !== Function
|
|
118
|
+
&& (Reflect.has(body, Symbol.asyncIterator) || Reflect.has(body, Symbol.iterator))) {
|
|
119
|
+
body = Readable.from(body);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const compressor = {
|
|
123
|
+
br: zlib.createBrotliCompress,
|
|
124
|
+
deflate: zlib.createDeflate,
|
|
125
|
+
gzip: zlib.createGzip,
|
|
126
|
+
}[headers[HTTP2_HEADER_CONTENT_ENCODING]] ?? PassThrough;
|
|
127
|
+
|
|
128
|
+
body.pipe(compressor()).pipe(req);
|
|
129
|
+
} else {
|
|
130
|
+
req.end(body);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export const merge = (target = {}, ...rest) => {
|
|
135
|
+
target = JSON.parse(JSON.stringify(target));
|
|
136
|
+
if (!rest.length) {
|
|
137
|
+
return target;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
rest.filter((it) => it === Object(it)).forEach((it) => {
|
|
141
|
+
Object.entries(it).reduce((acc, [key, val]) => {
|
|
142
|
+
if ([
|
|
143
|
+
acc[key]?.constructor,
|
|
144
|
+
val?.constructor,
|
|
145
|
+
].every((it) => [
|
|
146
|
+
Array,
|
|
147
|
+
Object,
|
|
148
|
+
].includes(it))) {
|
|
149
|
+
if (acc[key]?.constructor === val.constructor) {
|
|
150
|
+
acc[key] = merge(acc[key], val);
|
|
151
|
+
} else {
|
|
152
|
+
acc[key] = val;
|
|
153
|
+
}
|
|
154
|
+
} else {
|
|
155
|
+
acc[key] = val;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return acc;
|
|
159
|
+
}, target);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
return target;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
export const mixin = (res, { digest = false, parse = false } = {}) => {
|
|
166
|
+
if (!digest) {
|
|
167
|
+
Object.defineProperties(res, {
|
|
168
|
+
arrayBuffer: {
|
|
169
|
+
enumerable: true,
|
|
170
|
+
value: function () {
|
|
171
|
+
parse &&= false;
|
|
172
|
+
|
|
173
|
+
return this.body().then(({ buffer, byteLength, byteOffset }) => buffer.slice(
|
|
174
|
+
byteOffset,
|
|
175
|
+
byteOffset + byteLength,
|
|
176
|
+
));
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
blob: {
|
|
180
|
+
enumerable: true,
|
|
181
|
+
value: function () {
|
|
182
|
+
return this.arrayBuffer().then((res) => new Blob([res]));
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
json: {
|
|
186
|
+
enumerable: true,
|
|
187
|
+
value: function () {
|
|
188
|
+
return this.text().then((res) => JSON.parse(res));
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
text: {
|
|
192
|
+
enumerable: true,
|
|
193
|
+
value: function () {
|
|
194
|
+
return this.blob().then((blob) => blob.text());
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return Object.defineProperties(res, {
|
|
201
|
+
body: {
|
|
202
|
+
enumerable: true,
|
|
203
|
+
value: async function () {
|
|
204
|
+
if (this.bodyUsed) {
|
|
205
|
+
throw new TypeError('Response stream already read.');
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
let spool = [];
|
|
209
|
+
|
|
210
|
+
for await (const chunk of this) {
|
|
211
|
+
spool.push(chunk);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
spool = Buffer.concat(spool);
|
|
215
|
+
|
|
216
|
+
if (spool.length) {
|
|
217
|
+
spool = await decompress(spool, this.headers[HTTP2_HEADER_CONTENT_ENCODING], { async: true });
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (spool.length && parse) {
|
|
221
|
+
const contentType = this.headers[HTTP2_HEADER_CONTENT_TYPE] ?? '';
|
|
222
|
+
const charset = contentType.split(';')
|
|
223
|
+
.find((it) => /charset=/i.test(it))
|
|
224
|
+
?.toLowerCase()
|
|
225
|
+
.replace('charset=', '')
|
|
226
|
+
.replace('iso-8859-1', 'latin1')
|
|
227
|
+
.trim() || 'utf-8';
|
|
228
|
+
|
|
229
|
+
if (/\bjson\b/i.test(contentType)) {
|
|
230
|
+
spool = JSON.parse(spool.toString(charset));
|
|
231
|
+
} else if (/\b(text|xml)\b/i.test(contentType)) {
|
|
232
|
+
if (/\b(latin1|ucs-2|utf-(8|16le))\b/.test(charset)) {
|
|
233
|
+
spool = spool.toString(charset);
|
|
234
|
+
} else {
|
|
235
|
+
spool = new TextDecoder(charset).decode(spool);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return spool;
|
|
241
|
+
},
|
|
242
|
+
writable: true,
|
|
243
|
+
},
|
|
244
|
+
bodyUsed: {
|
|
245
|
+
enumerable: true,
|
|
246
|
+
get: function () {
|
|
247
|
+
return this.readableEnded;
|
|
248
|
+
},
|
|
249
|
+
},
|
|
250
|
+
});
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
export const preflight = (options) => {
|
|
254
|
+
const url = options.url = new URL(options.url);
|
|
255
|
+
const { cookies, h2 = false, method = HTTP2_METHOD_GET, headers, redirected } = options;
|
|
256
|
+
|
|
257
|
+
if (h2) {
|
|
258
|
+
options.endStream = [
|
|
259
|
+
HTTP2_METHOD_GET,
|
|
260
|
+
HTTP2_METHOD_HEAD,
|
|
261
|
+
].includes(method);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (cookies !== false) {
|
|
265
|
+
let cookie = Cookies.jar.get(url.origin);
|
|
266
|
+
|
|
267
|
+
if (cookies === Object(cookies) && !redirected) {
|
|
268
|
+
if (cookie) {
|
|
269
|
+
new Cookies(cookies).forEach(function (val, key) {
|
|
270
|
+
this.set(key, val);
|
|
271
|
+
}, cookie);
|
|
272
|
+
} else {
|
|
273
|
+
cookie = new Cookies(cookies);
|
|
274
|
+
Cookies.jar.set(url.origin, cookie);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
options.headers = {
|
|
279
|
+
...cookie && { [HTTP2_HEADER_COOKIE]: cookie },
|
|
280
|
+
...headers,
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
options.digest ??= true;
|
|
285
|
+
options.follow ??= 20;
|
|
286
|
+
options.h2 ??= h2;
|
|
287
|
+
options.headers = {
|
|
288
|
+
[HTTP2_HEADER_ACCEPT]: `${ APPLICATION_JSON }, ${ TEXT_PLAIN }, ${ WILDCARD }`,
|
|
289
|
+
[HTTP2_HEADER_ACCEPT_ENCODING]: 'br, deflate, gzip, identity',
|
|
290
|
+
...Object.entries(options.headers ?? {})
|
|
291
|
+
.reduce((acc, [key, val]) => (acc[key.toLowerCase()] = val, acc), {}),
|
|
292
|
+
...h2 && {
|
|
293
|
+
[HTTP2_HEADER_AUTHORITY]: url.host,
|
|
294
|
+
[HTTP2_HEADER_METHOD]: method,
|
|
295
|
+
[HTTP2_HEADER_PATH]: `${ url.pathname }${ url.search }`,
|
|
296
|
+
[HTTP2_HEADER_SCHEME]: url.protocol.replace(/\p{Punctuation}/gu, ''),
|
|
297
|
+
},
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
options.method ??= method;
|
|
301
|
+
options.parse ??= true;
|
|
302
|
+
options.redirect ??= redirects.follow;
|
|
303
|
+
|
|
304
|
+
if (!Object.values(redirects).includes(options.redirect)) {
|
|
305
|
+
options.createConnection?.().destroy();
|
|
306
|
+
throw new TypeError(`Failed to read the 'redirect' property from 'options': The provided value '${
|
|
307
|
+
options.redirect
|
|
308
|
+
}' is not a valid enum value.`);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
options.redirected ??= false;
|
|
312
|
+
options.thenable ??= false;
|
|
313
|
+
|
|
314
|
+
return options;
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
export const redirects = {
|
|
318
|
+
error: 'error',
|
|
319
|
+
follow: 'follow',
|
|
320
|
+
manual: 'manual',
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
export async function* tap(value) {
|
|
324
|
+
if (Reflect.has(value, Symbol.asyncIterator)) {
|
|
325
|
+
yield* value;
|
|
326
|
+
} else if (value.stream) {
|
|
327
|
+
yield* value.stream();
|
|
328
|
+
} else {
|
|
329
|
+
yield await value.arrayBuffer();
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export const transform = (body, options) => {
|
|
334
|
+
let headers = {};
|
|
335
|
+
|
|
336
|
+
if (File.alike(body)) {
|
|
337
|
+
headers = {
|
|
338
|
+
[HTTP2_HEADER_CONTENT_LENGTH]: body.size,
|
|
339
|
+
[HTTP2_HEADER_CONTENT_TYPE]: body.type || APPLICATION_OCTET_STREAM,
|
|
340
|
+
};
|
|
341
|
+
body = body.stream?.() ?? Readable.from(tap(body));
|
|
342
|
+
} else if (FormData.alike(body)) {
|
|
343
|
+
body = FormData.actuate(body);
|
|
344
|
+
headers = { [HTTP2_HEADER_CONTENT_TYPE]: body.contentType };
|
|
345
|
+
} else if (body === Object(body) && !Reflect.has(body, Symbol.asyncIterator)) {
|
|
346
|
+
if (body.constructor === URLSearchParams) {
|
|
347
|
+
headers = { [HTTP2_HEADER_CONTENT_TYPE]: APPLICATION_FORM_URLENCODED };
|
|
348
|
+
body = body.toString();
|
|
349
|
+
} else if (!Buffer.isBuffer(body)
|
|
350
|
+
&& !(!Array.isArray(body) && Reflect.has(body, Symbol.iterator))) {
|
|
351
|
+
headers = { [HTTP2_HEADER_CONTENT_TYPE]: APPLICATION_JSON };
|
|
352
|
+
body = JSON.stringify(body);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (types.isUint8Array(body) || Buffer.isBuffer(body) || body !== Object(body)) {
|
|
356
|
+
if (options.headers[HTTP2_HEADER_CONTENT_ENCODING]) {
|
|
357
|
+
body = compress(body, options.headers[HTTP2_HEADER_CONTENT_ENCODING]);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
headers = {
|
|
361
|
+
...headers,
|
|
362
|
+
[HTTP2_HEADER_CONTENT_LENGTH]: Buffer.byteLength(body),
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
Object.assign(options.headers, {
|
|
368
|
+
...headers,
|
|
369
|
+
...options.headers[HTTP2_HEADER_CONTENT_TYPE] && {
|
|
370
|
+
[HTTP2_HEADER_CONTENT_TYPE]: options.headers[HTTP2_HEADER_CONTENT_TYPE],
|
|
371
|
+
},
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
return body;
|
|
375
|
+
};
|