rekwest 3.0.1 → 3.1.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 CHANGED
@@ -121,8 +121,8 @@ 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 | AsyncIterator | Blob | Buffer | File | FromData | Iterator | Object | Readable |
125
- Uint8Array | URLSearchParams}** The body to send with the request
124
+ * `body` **{string | Array | ArrayBuffer | ArrayBufferView | AsyncIterator | Blob | Buffer | DataView | File |
125
+ FromData | 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
128
  * `digest` **{boolean}** `Default: true` Controls whether to read the response stream or simply add a mixin
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 (_util.types.isUint8Array(body) || Buffer.isBuffer(body) || body !== Object(body)) {
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
@@ -256,26 +256,31 @@ async function rekwest(url, options = {}) {
256
256
 
257
257
  return res;
258
258
  } catch (ex) {
259
- if (options.retry?.attempts && options.retry?.statusCodes.includes(ex.statusCode)) {
259
+ const {
260
+ maxRetryAfter,
261
+ retry
262
+ } = options;
263
+
264
+ if (retry?.attempts && retry?.statusCodes.includes(ex.statusCode)) {
260
265
  let {
261
266
  interval
262
- } = options.retry;
267
+ } = retry;
263
268
 
264
- if (options.retry.retryAfter && ex.headers[HTTP2_HEADER_RETRY_AFTER]) {
269
+ if (retry.retryAfter && ex.headers[HTTP2_HEADER_RETRY_AFTER]) {
265
270
  interval = ex.headers[HTTP2_HEADER_RETRY_AFTER];
266
271
  interval = Number(interval) * 1000 || new Date(interval) - Date.now();
267
272
 
268
- if (interval > options.maxRetryAfter) {
273
+ if (interval > maxRetryAfter) {
269
274
  throw maxRetryAfterError(interval, {
270
275
  cause: ex
271
276
  });
272
277
  }
273
278
  } else {
274
- interval = new Function('interval', `return Math.ceil(${options.retry.backoffStrategy});`)(interval);
279
+ interval = new Function('interval', `return Math.ceil(${retry.backoffStrategy});`)(interval);
275
280
  }
276
281
 
277
- options.retry.attempts--;
278
- options.retry.interval = interval;
282
+ retry.attempts--;
283
+ retry.interval = interval;
279
284
  return (0, _promises.setTimeout)(interval).then(() => rekwest(url, options));
280
285
  }
281
286
 
package/package.json CHANGED
@@ -60,5 +60,5 @@
60
60
  "test:bail": "mocha --bail",
61
61
  "test:cover": "c8 --include=src --reporter=lcov --reporter=text npm test"
62
62
  },
63
- "version": "3.0.1"
63
+ "version": "3.1.0"
64
64
  }
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 (types.isUint8Array(body) || Buffer.isBuffer(body) || body !== Object(body)) {
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
@@ -205,21 +205,23 @@ export default async function rekwest(url, options = {}) {
205
205
 
206
206
  return res;
207
207
  } catch (ex) {
208
- if (options.retry?.attempts && options.retry?.statusCodes.includes(ex.statusCode)) {
209
- let { interval } = options.retry;
208
+ const { maxRetryAfter, retry } = options;
210
209
 
211
- if (options.retry.retryAfter && ex.headers[HTTP2_HEADER_RETRY_AFTER]) {
210
+ if (retry?.attempts && retry?.statusCodes.includes(ex.statusCode)) {
211
+ let { interval } = retry;
212
+
213
+ if (retry.retryAfter && ex.headers[HTTP2_HEADER_RETRY_AFTER]) {
212
214
  interval = ex.headers[HTTP2_HEADER_RETRY_AFTER];
213
215
  interval = Number(interval) * 1000 || new Date(interval) - Date.now();
214
- if (interval > options.maxRetryAfter) {
216
+ if (interval > maxRetryAfter) {
215
217
  throw maxRetryAfterError(interval, { cause: ex });
216
218
  }
217
219
  } else {
218
- interval = new Function('interval', `return Math.ceil(${ options.retry.backoffStrategy });`)(interval);
220
+ interval = new Function('interval', `return Math.ceil(${ retry.backoffStrategy });`)(interval);
219
221
  }
220
222
 
221
- options.retry.attempts--;
222
- options.retry.interval = interval;
223
+ retry.attempts--;
224
+ retry.interval = interval;
223
225
 
224
226
  return setTimeoutPromise(interval).then(() => rekwest(url, options));
225
227
  }