@common.js/p-retry 5.1.2 → 6.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
@@ -2,4 +2,4 @@
2
2
 
3
3
  The [p-retry](https://www.npmjs.com/package/p-retry) package exported as CommonJS modules.
4
4
 
5
- Exported from [p-retry@5.1.2](https://www.npmjs.com/package/p-retry/v/5.1.2) using https://github.com/etienne-martin/common.js.
5
+ Exported from [p-retry@6.1.0](https://www.npmjs.com/package/p-retry/v/6.1.0) using https://github.com/etienne-martin/common.js.
package/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import {OperationOptions} from 'retry';
1
+ import {type OperationOptions} from 'retry';
2
2
 
3
3
  export class AbortError extends Error {
4
4
  readonly name: 'AbortError';
@@ -12,12 +12,12 @@ export class AbortError extends Error {
12
12
  constructor(message: string | Error);
13
13
  }
14
14
 
15
- export interface FailedAttemptError extends Error {
15
+ export type FailedAttemptError = {
16
16
  readonly attemptNumber: number;
17
17
  readonly retriesLeft: number;
18
- }
18
+ } & Error;
19
19
 
20
- export interface Options extends OperationOptions {
20
+ export type Options = {
21
21
  /**
22
22
  Callback invoked on each retry. Receives the error thrown by `input` as the first argument with properties `attemptNumber` and `retriesLeft` which indicate the current attempt number and the number of attempts left, respectively.
23
23
 
@@ -44,10 +44,6 @@ export interface Options extends OperationOptions {
44
44
  /**
45
45
  You can abort retrying using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
46
46
 
47
- When `AbortController.abort(reason)` is called, the promise will be rejected with `reason` as the error message.
48
-
49
- *Requires Node.js 16 or later.*
50
-
51
47
  ```
52
48
  import pRetry from 'p-retry';
53
49
 
@@ -55,7 +51,7 @@ export interface Options extends OperationOptions {
55
51
  const controller = new AbortController();
56
52
 
57
53
  cancelButton.addEventListener('click', () => {
58
- controller.abort('User clicked cancel button');
54
+ controller.abort(new Error('User clicked cancel button'));
59
55
  });
60
56
 
61
57
  try {
@@ -67,7 +63,7 @@ export interface Options extends OperationOptions {
67
63
  ```
68
64
  */
69
65
  readonly signal?: AbortSignal;
70
- }
66
+ } & OperationOptions;
71
67
 
72
68
  /**
73
69
  Returns a `Promise` that is fulfilled when calling `input` returns a fulfilled promise. If calling `input` returns a rejected promise, `input` is called again until the max retries are reached, it then rejects with the last rejection reason.
package/index.js CHANGED
@@ -17,6 +17,7 @@ _export(exports, {
17
17
  }
18
18
  });
19
19
  var _retry = /*#__PURE__*/ _interopRequireDefault(require("retry"));
20
+ var _isNetworkError = /*#__PURE__*/ _interopRequireDefault(require("is-network-error"));
20
21
  function _assertThisInitialized(self) {
21
22
  if (self === void 0) {
22
23
  throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
@@ -309,13 +310,6 @@ var __generator = (void 0) && (void 0).__generator || function(thisArg, body) {
309
310
  };
310
311
  }
311
312
  };
312
- var networkErrorMsgs = new Set([
313
- "Failed to fetch",
314
- "NetworkError when attempting to fetch resource.",
315
- "The Internet connection appears to be offline.",
316
- "Network request failed",
317
- "fetch failed"
318
- ]);
319
313
  var AbortError = /*#__PURE__*/ function(Error1) {
320
314
  "use strict";
321
315
  _inherits(AbortError, Error1);
@@ -344,12 +338,6 @@ var decorateErrorWithCounts = function(error, attemptNumber, options) {
344
338
  error.retriesLeft = retriesLeft;
345
339
  return error;
346
340
  };
347
- var isNetworkError = function(errorMessage) {
348
- return networkErrorMsgs.has(errorMessage);
349
- };
350
- var getDOMException = function(errorMessage) {
351
- return globalThis.DOMException === undefined ? new Error(errorMessage) : new DOMException(errorMessage);
352
- };
353
341
  function pRetry(input, options) {
354
342
  return _pRetry.apply(this, arguments);
355
343
  }
@@ -364,9 +352,24 @@ function _pRetry() {
364
352
  retries: 10
365
353
  }, options);
366
354
  var operation = _retry.default.operation(options);
355
+ var abortHandler = function() {
356
+ var ref;
357
+ operation.stop();
358
+ reject((ref = options.signal) === null || ref === void 0 ? void 0 : ref.reason);
359
+ };
360
+ if (options.signal && !options.signal.aborted) {
361
+ options.signal.addEventListener("abort", abortHandler, {
362
+ once: true
363
+ });
364
+ }
365
+ var cleanUp = function() {
366
+ var ref;
367
+ (ref = options.signal) === null || ref === void 0 ? void 0 : ref.removeEventListener("abort", abortHandler);
368
+ operation.stop();
369
+ };
367
370
  operation.attempt(function() {
368
371
  var _ref = _asyncToGenerator(function(attemptNumber) {
369
- var error, error1;
372
+ var result, error, finalError;
370
373
  return __generator(this, function(_state) {
371
374
  switch(_state.label){
372
375
  case 0:
@@ -374,86 +377,67 @@ function _pRetry() {
374
377
  0,
375
378
  2,
376
379
  ,
377
- 10
380
+ 7
378
381
  ]);
379
382
  return [
380
383
  4,
381
384
  input(attemptNumber)
382
385
  ];
383
386
  case 1:
384
- resolve.apply(void 0, [
385
- _state.sent()
386
- ]);
387
+ result = _state.sent();
388
+ cleanUp();
389
+ resolve(result);
387
390
  return [
388
391
  3,
389
- 10
392
+ 7
390
393
  ];
391
394
  case 2:
392
395
  error = _state.sent();
393
- if (!_instanceof(error, Error)) {
394
- reject(new TypeError('Non-error was thrown: "'.concat(error, '". You should only throw errors.')));
395
- return [
396
- 2
397
- ];
398
- }
399
- if (!_instanceof(error, AbortError)) return [
400
- 3,
401
- 3
402
- ];
403
- operation.stop();
404
- reject(error.originalError);
405
- return [
406
- 3,
407
- 9
408
- ];
396
+ _state.label = 3;
409
397
  case 3:
410
- if (!(_instanceof(error, TypeError) && !isNetworkError(error.message))) return [
411
- 3,
412
- 4
413
- ];
414
- operation.stop();
415
- reject(error);
416
- return [
417
- 3,
418
- 9
419
- ];
420
- case 4:
421
- decorateErrorWithCounts(error, attemptNumber, options);
422
- _state.label = 5;
423
- case 5:
424
398
  _state.trys.push([
399
+ 3,
425
400
  5,
426
- 7,
427
401
  ,
428
- 8
402
+ 6
429
403
  ]);
404
+ if (!_instanceof(error, Error)) {
405
+ throw new TypeError('Non-error was thrown: "'.concat(error, '". You should only throw errors.'));
406
+ }
407
+ if (_instanceof(error, AbortError)) {
408
+ throw error.originalError;
409
+ }
410
+ if (_instanceof(error, TypeError) && !(0, _isNetworkError.default)(error)) {
411
+ throw error;
412
+ }
430
413
  return [
431
414
  4,
432
- options.onFailedAttempt(error)
415
+ options.onFailedAttempt(decorateErrorWithCounts(error, attemptNumber, options))
433
416
  ];
434
- case 6:
417
+ case 4:
435
418
  _state.sent();
419
+ if (!operation.retry(error)) {
420
+ throw operation.mainError();
421
+ }
436
422
  return [
437
423
  3,
438
- 8
424
+ 6
439
425
  ];
440
- case 7:
441
- error1 = _state.sent();
442
- reject(error1);
426
+ case 5:
427
+ finalError = _state.sent();
428
+ decorateErrorWithCounts(finalError, attemptNumber, options);
429
+ cleanUp();
430
+ reject(finalError);
443
431
  return [
444
- 2
432
+ 3,
433
+ 6
445
434
  ];
446
- case 8:
447
- if (!operation.retry(error)) {
448
- reject(operation.mainError());
449
- }
450
- _state.label = 9;
451
- case 9:
435
+ case 6:
452
436
  return [
453
437
  3,
454
- 10
438
+ 7
455
439
  ];
456
- case 10:
440
+ case 7:
457
441
  return [
458
442
  2
459
443
  ];
@@ -464,15 +448,6 @@ function _pRetry() {
464
448
  return _ref.apply(this, arguments);
465
449
  };
466
450
  }());
467
- if (options.signal && !options.signal.aborted) {
468
- options.signal.addEventListener("abort", function() {
469
- operation.stop();
470
- var reason = options.signal.reason === undefined ? getDOMException("The operation was aborted.") : options.signal.reason;
471
- reject(_instanceof(reason, Error) ? reason : getDOMException(reason));
472
- }, {
473
- once: true
474
- });
475
- }
476
451
  })
477
452
  ];
478
453
  });
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@common.js/p-retry",
3
- "version": "5.1.2",
3
+ "version": "6.1.0",
4
4
  "description": "p-retry package exported as CommonJS modules",
5
5
  "license": "MIT",
6
6
  "repository": "etienne-martin/common.js",
7
7
  "funding": "https://github.com/sponsors/sindresorhus",
8
8
  "type": "commonjs",
9
9
  "engines": {
10
- "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
10
+ "node": ">=16.17"
11
11
  },
12
12
  "scripts": {
13
13
  "test": "xo && ava && tsd"
@@ -17,14 +17,15 @@
17
17
  "index.d.ts"
18
18
  ],
19
19
  "dependencies": {
20
- "@types/retry": "0.12.1",
20
+ "@types/retry": "0.12.2",
21
+ "@common.js/is-network-error": "^1.0.0",
21
22
  "retry": "^0.13.1"
22
23
  },
23
24
  "devDependencies": {
24
- "ava": "^4.1.0",
25
- "delay": "^5.0.0",
26
- "tsd": "^0.19.1",
27
- "xo": "^0.48.0"
25
+ "ava": "^5.3.1",
26
+ "delay": "^6.0.0",
27
+ "tsd": "^0.28.1",
28
+ "xo": "^0.56.0"
28
29
  },
29
30
  "homepage": "https://github.com/etienne-martin/common.js#readme",
30
31
  "main": "./index.js"