@common.js/p-timeout 6.1.3 → 7.0.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 +1 -1
- package/index.d.ts +2 -4
- package/index.js +38 -202
- package/package.json +9 -9
package/README.md
CHANGED
|
@@ -2,4 +2,4 @@
|
|
|
2
2
|
|
|
3
3
|
The [p-timeout](https://www.npmjs.com/package/p-timeout) package exported as CommonJS modules.
|
|
4
4
|
|
|
5
|
-
Exported from [p-timeout@
|
|
5
|
+
Exported from [p-timeout@7.0.1](https://www.npmjs.com/package/p-timeout/v/7.0.1) using https://github.com/etienne-martin/common.js.
|
package/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export class TimeoutError extends Error {
|
|
2
2
|
readonly name: 'TimeoutError';
|
|
3
|
-
constructor(message?: string);
|
|
3
|
+
constructor(message?: string, options?: ErrorOptions);
|
|
4
4
|
}
|
|
5
5
|
|
|
6
6
|
export type ClearablePromise<T> = {
|
|
@@ -92,9 +92,7 @@ export type Options<ReturnType> = {
|
|
|
92
92
|
};
|
|
93
93
|
|
|
94
94
|
/**
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
_Requires Node.js 16 or later._
|
|
95
|
+
Abort the promise.
|
|
98
96
|
|
|
99
97
|
@example
|
|
100
98
|
```
|
package/index.js
CHANGED
|
@@ -12,9 +12,6 @@ _export(exports, {
|
|
|
12
12
|
TimeoutError: function() {
|
|
13
13
|
return TimeoutError;
|
|
14
14
|
},
|
|
15
|
-
AbortError: function() {
|
|
16
|
-
return AbortError;
|
|
17
|
-
},
|
|
18
15
|
default: function() {
|
|
19
16
|
return pTimeout;
|
|
20
17
|
}
|
|
@@ -25,35 +22,6 @@ function _assertThisInitialized(self) {
|
|
|
25
22
|
}
|
|
26
23
|
return self;
|
|
27
24
|
}
|
|
28
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
29
|
-
try {
|
|
30
|
-
var info = gen[key](arg);
|
|
31
|
-
var value = info.value;
|
|
32
|
-
} catch (error) {
|
|
33
|
-
reject(error);
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
if (info.done) {
|
|
37
|
-
resolve(value);
|
|
38
|
-
} else {
|
|
39
|
-
Promise.resolve(value).then(_next, _throw);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
function _asyncToGenerator(fn) {
|
|
43
|
-
return function() {
|
|
44
|
-
var self = this, args = arguments;
|
|
45
|
-
return new Promise(function(resolve, reject) {
|
|
46
|
-
var gen = fn.apply(self, args);
|
|
47
|
-
function _next(value) {
|
|
48
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
49
|
-
}
|
|
50
|
-
function _throw(err) {
|
|
51
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
52
|
-
}
|
|
53
|
-
_next(undefined);
|
|
54
|
-
});
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
25
|
function _classCallCheck(instance, Constructor) {
|
|
58
26
|
if (!(instance instanceof Constructor)) {
|
|
59
27
|
throw new TypeError("Cannot call a class as a function");
|
|
@@ -87,6 +55,19 @@ function _construct(Parent, args, Class) {
|
|
|
87
55
|
}
|
|
88
56
|
return _construct.apply(null, arguments);
|
|
89
57
|
}
|
|
58
|
+
function _defineProperty(obj, key, value) {
|
|
59
|
+
if (key in obj) {
|
|
60
|
+
Object.defineProperty(obj, key, {
|
|
61
|
+
value: value,
|
|
62
|
+
enumerable: true,
|
|
63
|
+
configurable: true,
|
|
64
|
+
writable: true
|
|
65
|
+
});
|
|
66
|
+
} else {
|
|
67
|
+
obj[key] = value;
|
|
68
|
+
}
|
|
69
|
+
return obj;
|
|
70
|
+
}
|
|
90
71
|
function _getPrototypeOf(o) {
|
|
91
72
|
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
|
|
92
73
|
return o.__proto__ || Object.getPrototypeOf(o);
|
|
@@ -183,170 +164,57 @@ function _createSuper(Derived) {
|
|
|
183
164
|
return _possibleConstructorReturn(this, result);
|
|
184
165
|
};
|
|
185
166
|
}
|
|
186
|
-
var __generator = (void 0) && (void 0).__generator || function(thisArg, body) {
|
|
187
|
-
var f, y, t, g, _ = {
|
|
188
|
-
label: 0,
|
|
189
|
-
sent: function() {
|
|
190
|
-
if (t[0] & 1) throw t[1];
|
|
191
|
-
return t[1];
|
|
192
|
-
},
|
|
193
|
-
trys: [],
|
|
194
|
-
ops: []
|
|
195
|
-
};
|
|
196
|
-
return g = {
|
|
197
|
-
next: verb(0),
|
|
198
|
-
"throw": verb(1),
|
|
199
|
-
"return": verb(2)
|
|
200
|
-
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
201
|
-
return this;
|
|
202
|
-
}), g;
|
|
203
|
-
function verb(n) {
|
|
204
|
-
return function(v) {
|
|
205
|
-
return step([
|
|
206
|
-
n,
|
|
207
|
-
v
|
|
208
|
-
]);
|
|
209
|
-
};
|
|
210
|
-
}
|
|
211
|
-
function step(op) {
|
|
212
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
213
|
-
while(_)try {
|
|
214
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
215
|
-
if (y = 0, t) op = [
|
|
216
|
-
op[0] & 2,
|
|
217
|
-
t.value
|
|
218
|
-
];
|
|
219
|
-
switch(op[0]){
|
|
220
|
-
case 0:
|
|
221
|
-
case 1:
|
|
222
|
-
t = op;
|
|
223
|
-
break;
|
|
224
|
-
case 4:
|
|
225
|
-
_.label++;
|
|
226
|
-
return {
|
|
227
|
-
value: op[1],
|
|
228
|
-
done: false
|
|
229
|
-
};
|
|
230
|
-
case 5:
|
|
231
|
-
_.label++;
|
|
232
|
-
y = op[1];
|
|
233
|
-
op = [
|
|
234
|
-
0
|
|
235
|
-
];
|
|
236
|
-
continue;
|
|
237
|
-
case 7:
|
|
238
|
-
op = _.ops.pop();
|
|
239
|
-
_.trys.pop();
|
|
240
|
-
continue;
|
|
241
|
-
default:
|
|
242
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
243
|
-
_ = 0;
|
|
244
|
-
continue;
|
|
245
|
-
}
|
|
246
|
-
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
247
|
-
_.label = op[1];
|
|
248
|
-
break;
|
|
249
|
-
}
|
|
250
|
-
if (op[0] === 6 && _.label < t[1]) {
|
|
251
|
-
_.label = t[1];
|
|
252
|
-
t = op;
|
|
253
|
-
break;
|
|
254
|
-
}
|
|
255
|
-
if (t && _.label < t[2]) {
|
|
256
|
-
_.label = t[2];
|
|
257
|
-
_.ops.push(op);
|
|
258
|
-
break;
|
|
259
|
-
}
|
|
260
|
-
if (t[2]) _.ops.pop();
|
|
261
|
-
_.trys.pop();
|
|
262
|
-
continue;
|
|
263
|
-
}
|
|
264
|
-
op = body.call(thisArg, _);
|
|
265
|
-
} catch (e) {
|
|
266
|
-
op = [
|
|
267
|
-
6,
|
|
268
|
-
e
|
|
269
|
-
];
|
|
270
|
-
y = 0;
|
|
271
|
-
} finally{
|
|
272
|
-
f = t = 0;
|
|
273
|
-
}
|
|
274
|
-
if (op[0] & 5) throw op[1];
|
|
275
|
-
return {
|
|
276
|
-
value: op[0] ? op[1] : void 0,
|
|
277
|
-
done: true
|
|
278
|
-
};
|
|
279
|
-
}
|
|
280
|
-
};
|
|
281
167
|
var TimeoutError = /*#__PURE__*/ function(Error1) {
|
|
282
168
|
"use strict";
|
|
283
169
|
_inherits(TimeoutError, Error1);
|
|
284
170
|
var _super = _createSuper(TimeoutError);
|
|
285
|
-
function TimeoutError(message) {
|
|
171
|
+
function TimeoutError(message, options) {
|
|
286
172
|
_classCallCheck(this, TimeoutError);
|
|
287
173
|
var _this;
|
|
288
|
-
|
|
289
|
-
_this
|
|
174
|
+
var ref;
|
|
175
|
+
_this = _super.call(this, message, options);
|
|
176
|
+
_defineProperty(_assertThisInitialized(_this), "name", "TimeoutError");
|
|
177
|
+
(ref = Error.captureStackTrace) === null || ref === void 0 ? void 0 : ref.call(Error, _assertThisInitialized(_this), TimeoutError);
|
|
290
178
|
return _this;
|
|
291
179
|
}
|
|
292
180
|
return TimeoutError;
|
|
293
181
|
}(_wrapNativeSuper(Error));
|
|
294
|
-
var
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
var _super = _createSuper(AbortError);
|
|
298
|
-
function AbortError(message) {
|
|
299
|
-
_classCallCheck(this, AbortError);
|
|
300
|
-
var _this;
|
|
301
|
-
_this = _super.call(this);
|
|
302
|
-
_this.name = "AbortError";
|
|
303
|
-
_this.message = message;
|
|
304
|
-
return _this;
|
|
305
|
-
}
|
|
306
|
-
return AbortError;
|
|
307
|
-
}(_wrapNativeSuper(Error));
|
|
308
|
-
/**
|
|
309
|
-
TODO: Remove AbortError and just throw DOMException when targeting Node 18.
|
|
310
|
-
*/ var getDOMException = function(errorMessage) {
|
|
311
|
-
return globalThis.DOMException === undefined ? new AbortError(errorMessage) : new DOMException(errorMessage);
|
|
312
|
-
};
|
|
313
|
-
/**
|
|
314
|
-
TODO: Remove below function and just 'reject(signal.reason)' when targeting Node 18.
|
|
315
|
-
*/ var getAbortedReason = function(signal) {
|
|
316
|
-
var reason = signal.reason === undefined ? getDOMException("This operation was aborted.") : signal.reason;
|
|
317
|
-
return _instanceof(reason, Error) ? reason : getDOMException(reason);
|
|
182
|
+
var _reason;
|
|
183
|
+
var getAbortedReason = function(signal) {
|
|
184
|
+
return (_reason = signal.reason) !== null && _reason !== void 0 ? _reason : new DOMException("This operation was aborted.", "AbortError");
|
|
318
185
|
};
|
|
319
186
|
function pTimeout(promise, options) {
|
|
320
187
|
var milliseconds = options.milliseconds, fallback = options.fallback, message = options.message, _customTimers = options.customTimers, customTimers = _customTimers === void 0 ? {
|
|
321
188
|
setTimeout: setTimeout,
|
|
322
189
|
clearTimeout: clearTimeout
|
|
323
|
-
} : _customTimers;
|
|
190
|
+
} : _customTimers, signal = options.signal;
|
|
324
191
|
var timer;
|
|
192
|
+
var abortHandler;
|
|
325
193
|
var wrappedPromise = new Promise(function(resolve, reject) {
|
|
326
194
|
if (typeof milliseconds !== "number" || Math.sign(milliseconds) !== 1) {
|
|
327
195
|
throw new TypeError("Expected `milliseconds` to be a positive number, got `".concat(milliseconds, "`"));
|
|
328
196
|
}
|
|
329
|
-
if (
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
197
|
+
if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
|
|
198
|
+
reject(getAbortedReason(signal));
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
if (signal) {
|
|
202
|
+
abortHandler = function() {
|
|
335
203
|
reject(getAbortedReason(signal));
|
|
336
204
|
};
|
|
337
205
|
signal.addEventListener("abort", abortHandler, {
|
|
338
206
|
once: true
|
|
339
207
|
});
|
|
340
|
-
promise.finally(function() {
|
|
341
|
-
signal.removeEventListener("abort", abortHandler);
|
|
342
|
-
});
|
|
343
208
|
}
|
|
209
|
+
// Use .then() instead of async IIFE to preserve stack traces
|
|
210
|
+
// eslint-disable-next-line promise/prefer-await-to-then, promise/prefer-catch
|
|
211
|
+
promise.then(resolve, reject);
|
|
344
212
|
if (milliseconds === Number.POSITIVE_INFINITY) {
|
|
345
|
-
promise.then(resolve, reject);
|
|
346
213
|
return;
|
|
347
214
|
}
|
|
348
215
|
// We create the error outside of `setTimeout` to preserve the stack trace.
|
|
349
216
|
var timeoutError = new TimeoutError();
|
|
217
|
+
// `.call(undefined, ...)` is needed for custom timers to avoid context issues
|
|
350
218
|
timer = customTimers.setTimeout.call(undefined, function() {
|
|
351
219
|
if (fallback) {
|
|
352
220
|
try {
|
|
@@ -368,48 +236,16 @@ function pTimeout(promise, options) {
|
|
|
368
236
|
reject(timeoutError);
|
|
369
237
|
}
|
|
370
238
|
}, milliseconds);
|
|
371
|
-
_asyncToGenerator(function() {
|
|
372
|
-
var error;
|
|
373
|
-
return __generator(this, function(_state) {
|
|
374
|
-
switch(_state.label){
|
|
375
|
-
case 0:
|
|
376
|
-
_state.trys.push([
|
|
377
|
-
0,
|
|
378
|
-
2,
|
|
379
|
-
,
|
|
380
|
-
3
|
|
381
|
-
]);
|
|
382
|
-
return [
|
|
383
|
-
4,
|
|
384
|
-
promise
|
|
385
|
-
];
|
|
386
|
-
case 1:
|
|
387
|
-
resolve.apply(void 0, [
|
|
388
|
-
_state.sent()
|
|
389
|
-
]);
|
|
390
|
-
return [
|
|
391
|
-
3,
|
|
392
|
-
3
|
|
393
|
-
];
|
|
394
|
-
case 2:
|
|
395
|
-
error = _state.sent();
|
|
396
|
-
reject(error);
|
|
397
|
-
return [
|
|
398
|
-
3,
|
|
399
|
-
3
|
|
400
|
-
];
|
|
401
|
-
case 3:
|
|
402
|
-
return [
|
|
403
|
-
2
|
|
404
|
-
];
|
|
405
|
-
}
|
|
406
|
-
});
|
|
407
|
-
})();
|
|
408
239
|
});
|
|
240
|
+
// eslint-disable-next-line promise/prefer-await-to-then
|
|
409
241
|
var cancelablePromise = wrappedPromise.finally(function() {
|
|
410
242
|
cancelablePromise.clear();
|
|
243
|
+
if (abortHandler && signal) {
|
|
244
|
+
signal.removeEventListener("abort", abortHandler);
|
|
245
|
+
}
|
|
411
246
|
});
|
|
412
247
|
cancelablePromise.clear = function() {
|
|
248
|
+
// `.call(undefined, ...)` is needed for custom timers to avoid context issues
|
|
413
249
|
customTimers.clearTimeout.call(undefined, timer);
|
|
414
250
|
timer = undefined;
|
|
415
251
|
};
|
package/package.json
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@common.js/p-timeout",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.1",
|
|
4
4
|
"description": "p-timeout 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
|
-
"types": "./index.d.ts",
|
|
10
9
|
"sideEffects": false,
|
|
11
10
|
"engines": {
|
|
12
|
-
"node": ">=
|
|
11
|
+
"node": ">=20"
|
|
13
12
|
},
|
|
14
13
|
"scripts": {
|
|
15
14
|
"test": "xo && ava && tsd"
|
|
@@ -19,16 +18,17 @@
|
|
|
19
18
|
"index.d.ts"
|
|
20
19
|
],
|
|
21
20
|
"devDependencies": {
|
|
22
|
-
"ava": "^4.
|
|
23
|
-
"delay": "^
|
|
21
|
+
"ava": "^6.4.1",
|
|
22
|
+
"delay": "^6.0.0",
|
|
24
23
|
"in-range": "^3.0.0",
|
|
25
24
|
"p-cancelable": "^4.0.1",
|
|
26
|
-
"sinon": "^
|
|
25
|
+
"sinon": "^21.0.0",
|
|
27
26
|
"time-span": "^5.1.0",
|
|
28
|
-
"tsd": "^0.
|
|
29
|
-
"xo": "^
|
|
27
|
+
"tsd": "^0.33.0",
|
|
28
|
+
"xo": "^1.2.2"
|
|
30
29
|
},
|
|
31
30
|
"homepage": "https://github.com/etienne-martin/common.js#readme",
|
|
32
31
|
"dependencies": {},
|
|
33
|
-
"main": "./index.js"
|
|
32
|
+
"main": "./index.js",
|
|
33
|
+
"types": "./index.d.ts"
|
|
34
34
|
}
|