rekwest 3.0.0 → 3.1.1
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 +3 -3
- package/dist/helpers.js +7 -5
- package/dist/index.js +12 -8
- package/package.json +1 -1
- package/src/helpers.mjs +7 -5
- package/src/index.mjs +9 -8
package/README.md
CHANGED
|
@@ -121,11 +121,11 @@ console.log(res.body);
|
|
|
121
121
|
& [http2.ClientSessionRequestOptions](https://nodejs.org/api/http2.html#http2_clienthttp2session_request_headers_options)
|
|
122
122
|
and [tls.ConnectionOptions](https://nodejs.org/api/tls.html#tls_tls_connect_options_callback)
|
|
123
123
|
for HTTP/2 attunes
|
|
124
|
-
* `body` **{string | Array |
|
|
125
|
-
|
|
124
|
+
* `body` **{string | Array | ArrayBuffer | ArrayBufferView | AsyncIterator | Blob | Buffer | DataView | File |
|
|
125
|
+
FormData | Iterator | Object | Readable | SharedArrayBuffer | URLSearchParams}** The body to send with the request
|
|
126
126
|
* `cookies` **{boolean | Array<[k, v]> | Cookies | Object | URLSearchParams}** `Default: true` The cookies to add to
|
|
127
127
|
the request
|
|
128
|
-
* `digest` **{boolean}** `Default: true` Controls whether to read the response stream or
|
|
128
|
+
* `digest` **{boolean}** `Default: true` Controls whether to read the response stream or simply add a mixin
|
|
129
129
|
* `follow` **{number}** `Default: 20` The number of redirects to follow
|
|
130
130
|
* `h2` **{boolean}** `Default: false` Forces the use of HTTP/2 protocol
|
|
131
131
|
* `headers` **{Object}** The headers to add to the request
|
package/dist/helpers.js
CHANGED
|
@@ -126,10 +126,6 @@ const dispatch = (req, {
|
|
|
126
126
|
body,
|
|
127
127
|
headers
|
|
128
128
|
}) => {
|
|
129
|
-
if (_util.types.isUint8Array(body)) {
|
|
130
|
-
return req.end(body);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
129
|
if (body === Object(body) && !Buffer.isBuffer(body)) {
|
|
134
130
|
if (body.pipe?.constructor !== Function && (Reflect.has(body, Symbol.asyncIterator) || Reflect.has(body, Symbol.iterator))) {
|
|
135
131
|
body = _stream.Readable.from(body);
|
|
@@ -350,6 +346,12 @@ async function* tap(value) {
|
|
|
350
346
|
const transform = (body, options) => {
|
|
351
347
|
let headers = {};
|
|
352
348
|
|
|
349
|
+
if (_util.types.isAnyArrayBuffer(body) && !Buffer.isBuffer(body)) {
|
|
350
|
+
body = Buffer.from(body);
|
|
351
|
+
} else if (_util.types.isArrayBufferView(body) && !Buffer.isBuffer(body)) {
|
|
352
|
+
body = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
|
|
353
|
+
}
|
|
354
|
+
|
|
353
355
|
if (_file.File.alike(body)) {
|
|
354
356
|
headers = {
|
|
355
357
|
[HTTP2_HEADER_CONTENT_LENGTH]: body.size,
|
|
@@ -374,7 +376,7 @@ const transform = (body, options) => {
|
|
|
374
376
|
body = JSON.stringify(body);
|
|
375
377
|
}
|
|
376
378
|
|
|
377
|
-
if (
|
|
379
|
+
if (Buffer.isBuffer(body) || body !== Object(body)) {
|
|
378
380
|
if (options.headers[HTTP2_HEADER_CONTENT_ENCODING]) {
|
|
379
381
|
body = compress(body, options.headers[HTTP2_HEADER_CONTENT_ENCODING]);
|
|
380
382
|
}
|
package/dist/index.js
CHANGED
|
@@ -81,7 +81,6 @@ const {
|
|
|
81
81
|
HTTP2_HEADER_LOCATION,
|
|
82
82
|
HTTP2_HEADER_RETRY_AFTER,
|
|
83
83
|
HTTP2_HEADER_SET_COOKIE,
|
|
84
|
-
HTTP2_HEADER_STATUS,
|
|
85
84
|
HTTP2_METHOD_GET,
|
|
86
85
|
HTTP2_METHOD_HEAD,
|
|
87
86
|
HTTP_STATUS_BAD_REQUEST,
|
|
@@ -257,26 +256,31 @@ async function rekwest(url, options = {}) {
|
|
|
257
256
|
|
|
258
257
|
return res;
|
|
259
258
|
} catch (ex) {
|
|
260
|
-
|
|
259
|
+
const {
|
|
260
|
+
maxRetryAfter,
|
|
261
|
+
retry
|
|
262
|
+
} = options;
|
|
263
|
+
|
|
264
|
+
if (retry?.attempts && retry?.statusCodes.includes(ex.statusCode)) {
|
|
261
265
|
let {
|
|
262
266
|
interval
|
|
263
|
-
} =
|
|
267
|
+
} = retry;
|
|
264
268
|
|
|
265
|
-
if (
|
|
269
|
+
if (retry.retryAfter && ex.headers[HTTP2_HEADER_RETRY_AFTER]) {
|
|
266
270
|
interval = ex.headers[HTTP2_HEADER_RETRY_AFTER];
|
|
267
271
|
interval = Number(interval) * 1000 || new Date(interval) - Date.now();
|
|
268
272
|
|
|
269
|
-
if (interval >
|
|
273
|
+
if (interval > maxRetryAfter) {
|
|
270
274
|
throw maxRetryAfterError(interval, {
|
|
271
275
|
cause: ex
|
|
272
276
|
});
|
|
273
277
|
}
|
|
274
278
|
} else {
|
|
275
|
-
interval = new Function('interval', `return Math.ceil(${
|
|
279
|
+
interval = new Function('interval', `return Math.ceil(${retry.backoffStrategy});`)(interval);
|
|
276
280
|
}
|
|
277
281
|
|
|
278
|
-
|
|
279
|
-
|
|
282
|
+
retry.attempts--;
|
|
283
|
+
retry.interval = interval;
|
|
280
284
|
return (0, _promises.setTimeout)(interval).then(() => rekwest(url, options));
|
|
281
285
|
}
|
|
282
286
|
|
package/package.json
CHANGED
package/src/helpers.mjs
CHANGED
|
@@ -109,10 +109,6 @@ export const decompress = (buf, encoding, { async = false } = {}) => {
|
|
|
109
109
|
};
|
|
110
110
|
|
|
111
111
|
export const dispatch = (req, { body, headers }) => {
|
|
112
|
-
if (types.isUint8Array(body)) {
|
|
113
|
-
return req.end(body);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
112
|
if (body === Object(body) && !Buffer.isBuffer(body)) {
|
|
117
113
|
if (body.pipe?.constructor !== Function
|
|
118
114
|
&& (Reflect.has(body, Symbol.asyncIterator) || Reflect.has(body, Symbol.iterator))) {
|
|
@@ -333,6 +329,12 @@ export async function* tap(value) {
|
|
|
333
329
|
export const transform = (body, options) => {
|
|
334
330
|
let headers = {};
|
|
335
331
|
|
|
332
|
+
if (types.isAnyArrayBuffer(body) && !Buffer.isBuffer(body)) {
|
|
333
|
+
body = Buffer.from(body);
|
|
334
|
+
} else if (types.isArrayBufferView(body) && !Buffer.isBuffer(body)) {
|
|
335
|
+
body = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
|
|
336
|
+
}
|
|
337
|
+
|
|
336
338
|
if (File.alike(body)) {
|
|
337
339
|
headers = {
|
|
338
340
|
[HTTP2_HEADER_CONTENT_LENGTH]: body.size,
|
|
@@ -352,7 +354,7 @@ export const transform = (body, options) => {
|
|
|
352
354
|
body = JSON.stringify(body);
|
|
353
355
|
}
|
|
354
356
|
|
|
355
|
-
if (
|
|
357
|
+
if (Buffer.isBuffer(body) || body !== Object(body)) {
|
|
356
358
|
if (options.headers[HTTP2_HEADER_CONTENT_ENCODING]) {
|
|
357
359
|
body = compress(body, options.headers[HTTP2_HEADER_CONTENT_ENCODING]);
|
|
358
360
|
}
|
package/src/index.mjs
CHANGED
|
@@ -32,7 +32,6 @@ const {
|
|
|
32
32
|
HTTP2_HEADER_LOCATION,
|
|
33
33
|
HTTP2_HEADER_RETRY_AFTER,
|
|
34
34
|
HTTP2_HEADER_SET_COOKIE,
|
|
35
|
-
HTTP2_HEADER_STATUS,
|
|
36
35
|
HTTP2_METHOD_GET,
|
|
37
36
|
HTTP2_METHOD_HEAD,
|
|
38
37
|
HTTP_STATUS_BAD_REQUEST,
|
|
@@ -206,21 +205,23 @@ export default async function rekwest(url, options = {}) {
|
|
|
206
205
|
|
|
207
206
|
return res;
|
|
208
207
|
} catch (ex) {
|
|
209
|
-
|
|
210
|
-
let { interval } = options.retry;
|
|
208
|
+
const { maxRetryAfter, retry } = options;
|
|
211
209
|
|
|
212
|
-
|
|
210
|
+
if (retry?.attempts && retry?.statusCodes.includes(ex.statusCode)) {
|
|
211
|
+
let { interval } = retry;
|
|
212
|
+
|
|
213
|
+
if (retry.retryAfter && ex.headers[HTTP2_HEADER_RETRY_AFTER]) {
|
|
213
214
|
interval = ex.headers[HTTP2_HEADER_RETRY_AFTER];
|
|
214
215
|
interval = Number(interval) * 1000 || new Date(interval) - Date.now();
|
|
215
|
-
if (interval >
|
|
216
|
+
if (interval > maxRetryAfter) {
|
|
216
217
|
throw maxRetryAfterError(interval, { cause: ex });
|
|
217
218
|
}
|
|
218
219
|
} else {
|
|
219
|
-
interval = new Function('interval', `return Math.ceil(${
|
|
220
|
+
interval = new Function('interval', `return Math.ceil(${ retry.backoffStrategy });`)(interval);
|
|
220
221
|
}
|
|
221
222
|
|
|
222
|
-
|
|
223
|
-
|
|
223
|
+
retry.attempts--;
|
|
224
|
+
retry.interval = interval;
|
|
224
225
|
|
|
225
226
|
return setTimeoutPromise(interval).then(() => rekwest(url, options));
|
|
226
227
|
}
|