@philiprehberger/safe-timeout 0.1.5
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/LICENSE +21 -0
- package/README.md +85 -0
- package/dist/index.cjs +80 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +14 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +75 -0
- package/dist/index.js.map +1 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 philiprehberger
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# @philiprehberger/safe-timeout
|
|
2
|
+
|
|
3
|
+
[](https://github.com/philiprehberger/ts-safe-timeout/actions/workflows/ci.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/@philiprehberger/safe-timeout)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
Reliable timeout wrapper for async operations with AbortController support
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @philiprehberger/safe-timeout
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### Timeout with Error
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { withTimeout, TimeoutError } from '@philiprehberger/safe-timeout';
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
const data = await withTimeout(fetch('/api/data'), 5000);
|
|
24
|
+
} catch (error) {
|
|
25
|
+
if (error instanceof TimeoutError) {
|
|
26
|
+
console.log('Request timed out');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Timeout with Fallback
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { withTimeoutFallback } from '@philiprehberger/safe-timeout';
|
|
35
|
+
|
|
36
|
+
const data = await withTimeoutFallback(fetch('/api/data'), 5000, cachedData);
|
|
37
|
+
// Returns cachedData if the fetch takes longer than 5 seconds
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Timeout Signal
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { createTimeoutSignal } from '@philiprehberger/safe-timeout';
|
|
44
|
+
|
|
45
|
+
const signal = createTimeoutSignal(5000);
|
|
46
|
+
const response = await fetch('/api/data', { signal });
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### With Options
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
const data = await withTimeout(fetch('/api'), 5000, {
|
|
53
|
+
signal: existingAbortSignal, // Also abort on external signal
|
|
54
|
+
onTimeout: () => console.warn('Operation timed out'),
|
|
55
|
+
});
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## API
|
|
59
|
+
|
|
60
|
+
| Export | Description |
|
|
61
|
+
|--------|-------------|
|
|
62
|
+
| `withTimeout(promise, ms, options?)` | Race promise against timeout, throws `TimeoutError` on timeout |
|
|
63
|
+
| `withTimeoutFallback(promise, ms, fallback)` | Returns fallback value on timeout instead of throwing |
|
|
64
|
+
| `createTimeoutSignal(ms)` | Creates an `AbortSignal` that aborts after `ms` |
|
|
65
|
+
| `TimeoutError` | Error class thrown on timeout, has `ms` property |
|
|
66
|
+
|
|
67
|
+
### `TimeoutOptions`
|
|
68
|
+
|
|
69
|
+
| Option | Type | Description |
|
|
70
|
+
|--------|------|-------------|
|
|
71
|
+
| `signal` | `AbortSignal` | External abort signal |
|
|
72
|
+
| `onTimeout` | `() => void` | Callback when timeout fires |
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
## Development
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
npm install
|
|
79
|
+
npm run build
|
|
80
|
+
npm test
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/types.ts
|
|
4
|
+
var TimeoutError = class extends Error {
|
|
5
|
+
constructor(ms) {
|
|
6
|
+
super(`Operation timed out after ${ms}ms`);
|
|
7
|
+
this.name = "TimeoutError";
|
|
8
|
+
this.ms = ms;
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// src/timeout.ts
|
|
13
|
+
function withTimeout(promise, ms, options) {
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
var _a;
|
|
16
|
+
let settled = false;
|
|
17
|
+
const timer = setTimeout(() => {
|
|
18
|
+
var _a2;
|
|
19
|
+
if (!settled) {
|
|
20
|
+
settled = true;
|
|
21
|
+
(_a2 = options == null ? void 0 : options.onTimeout) == null ? void 0 : _a2.call(options);
|
|
22
|
+
reject(new TimeoutError(ms));
|
|
23
|
+
}
|
|
24
|
+
}, ms);
|
|
25
|
+
if (options == null ? void 0 : options.signal) {
|
|
26
|
+
if (options.signal.aborted) {
|
|
27
|
+
settled = true;
|
|
28
|
+
clearTimeout(timer);
|
|
29
|
+
reject((_a = options.signal.reason) != null ? _a : new DOMException("Aborted", "AbortError"));
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
options.signal.addEventListener("abort", () => {
|
|
33
|
+
var _a2;
|
|
34
|
+
if (!settled) {
|
|
35
|
+
settled = true;
|
|
36
|
+
clearTimeout(timer);
|
|
37
|
+
reject((_a2 = options.signal.reason) != null ? _a2 : new DOMException("Aborted", "AbortError"));
|
|
38
|
+
}
|
|
39
|
+
}, { once: true });
|
|
40
|
+
}
|
|
41
|
+
promise.then(
|
|
42
|
+
(value) => {
|
|
43
|
+
if (!settled) {
|
|
44
|
+
settled = true;
|
|
45
|
+
clearTimeout(timer);
|
|
46
|
+
resolve(value);
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
(error) => {
|
|
50
|
+
if (!settled) {
|
|
51
|
+
settled = true;
|
|
52
|
+
clearTimeout(timer);
|
|
53
|
+
reject(error);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
async function withTimeoutFallback(promise, ms, fallback) {
|
|
60
|
+
try {
|
|
61
|
+
return await withTimeout(promise, ms);
|
|
62
|
+
} catch (error) {
|
|
63
|
+
if (error instanceof TimeoutError) {
|
|
64
|
+
return fallback;
|
|
65
|
+
}
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function createTimeoutSignal(ms) {
|
|
70
|
+
const controller = new AbortController();
|
|
71
|
+
setTimeout(() => controller.abort(new TimeoutError(ms)), ms);
|
|
72
|
+
return controller.signal;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
exports.TimeoutError = TimeoutError;
|
|
76
|
+
exports.createTimeoutSignal = createTimeoutSignal;
|
|
77
|
+
exports.withTimeout = withTimeout;
|
|
78
|
+
exports.withTimeoutFallback = withTimeoutFallback;
|
|
79
|
+
//# sourceMappingURL=index.cjs.map
|
|
80
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/types.ts","../src/timeout.ts"],"names":["_a"],"mappings":";;;AAKO,IAAM,YAAA,GAAN,cAA2B,KAAA,CAAM;AAAA,EAGtC,YAAY,EAAA,EAAY;AACtB,IAAA,KAAA,CAAM,CAAA,0BAAA,EAA6B,EAAE,CAAA,EAAA,CAAI,CAAA;AACzC,IAAA,IAAA,CAAK,IAAA,GAAO,cAAA;AACZ,IAAA,IAAA,CAAK,EAAA,GAAK,EAAA;AAAA,EACZ;AACF;;;ACVO,SAAS,WAAA,CACd,OAAA,EACA,EAAA,EACA,OAAA,EACY;AACZ,EAAA,OAAO,IAAI,OAAA,CAAW,CAAC,OAAA,EAAS,MAAA,KAAW;AAR7C,IAAA,IAAA,EAAA;AASI,IAAA,IAAI,OAAA,GAAU,KAAA;AAEd,IAAA,MAAM,KAAA,GAAQ,WAAW,MAAM;AAXnC,MAAA,IAAAA,GAAAA;AAYM,MAAA,IAAI,CAAC,OAAA,EAAS;AACZ,QAAA,OAAA,GAAU,IAAA;AACV,QAAA,CAAAA,GAAAA,GAAA,OAAA,IAAA,IAAA,GAAA,MAAA,GAAA,OAAA,CAAS,SAAA,KAAT,IAAA,GAAA,MAAA,GAAAA,GAAAA,CAAA,IAAA,CAAA,OAAA,CAAA;AACA,QAAA,MAAA,CAAO,IAAI,YAAA,CAAa,EAAE,CAAC,CAAA;AAAA,MAC7B;AAAA,IACF,GAAG,EAAE,CAAA;AAEL,IAAA,IAAI,mCAAS,MAAA,EAAQ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ACY;AACZ,EAAA,IAAI;AACF,IAAA,OAAO,MAAM,WAAA,CAAY,OAAA,EAAS,EAAE,CAAA;AAAA,EACtC,SAAS,KAAA,EAAO;AACd,IAAA,IAAI,iBAAiB,YAAA,EAAc;AACjC,MAAA,OAAO,QAAA;AAAA,IACT;AACA,IAAA,MAAM,KAAA;AAAA,EACR;AACF;AAEO,SAAS,oBAAoB,EAAA,EAAyB;AAC3D,EAAA,MAAM,UAAA,GAAa,IAAI,eAAA,EAAgB;AACvC,EAAA,UAAA,CAAW,MAAM,WAAW,KAAA,CAAM,IAAI,aAAa,EAAE,CAAC,GAAG,EAAE,CAAA;AAC3D,EAAA,OAAO,UAAA,CAAW,MAAA;AACpB","file":"index.cjs","sourcesContent":["export interface TimeoutOptions {\n signal?: AbortSignal;\n onTimeout?: () => void;\n}\n\nexport class TimeoutError extends Error {\n public readonly ms: number;\n\n constructor(ms: number) {\n super(`Operation timed out after ${ms}ms`);\n this.name = 'TimeoutError';\n this.ms = ms;\n }\n}\n","import { TimeoutError } from './types.js';\nimport type { TimeoutOptions } from './types.js';\n\nexport function withTimeout<T>(\n promise: Promise<T>,\n ms: number,\n options?: TimeoutOptions,\n): Promise<T> {\n return new Promise<T>((resolve, reject) => {\n let settled = false;\n\n const timer = setTimeout(() => {\n if (!settled) {\n settled = true;\n options?.onTimeout?.();\n reject(new TimeoutError(ms));\n }\n }, ms);\n\n if (options?.signal) {\n if (options.signal.aborted) {\n settled = true;\n clearTimeout(timer);\n reject(options.signal.reason ?? new DOMException('Aborted', 'AbortError'));\n return;\n }\n\n options.signal.addEventListener('abort', () => {\n if (!settled) {\n settled = true;\n clearTimeout(timer);\n reject(options.signal!.reason ?? new DOMException('Aborted', 'AbortError'));\n }\n }, { once: true });\n }\n\n promise.then(\n (value) => {\n if (!settled) {\n settled = true;\n clearTimeout(timer);\n resolve(value);\n }\n },\n (error) => {\n if (!settled) {\n settled = true;\n clearTimeout(timer);\n reject(error);\n }\n },\n );\n });\n}\n\nexport async function withTimeoutFallback<T>(\n promise: Promise<T>,\n ms: number,\n fallback: T,\n): Promise<T> {\n try {\n return await withTimeout(promise, ms);\n } catch (error) {\n if (error instanceof TimeoutError) {\n return fallback;\n }\n throw error;\n }\n}\n\nexport function createTimeoutSignal(ms: number): AbortSignal {\n const controller = new AbortController();\n setTimeout(() => controller.abort(new TimeoutError(ms)), ms);\n return controller.signal;\n}\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface TimeoutOptions {
|
|
2
|
+
signal?: AbortSignal;
|
|
3
|
+
onTimeout?: () => void;
|
|
4
|
+
}
|
|
5
|
+
declare class TimeoutError extends Error {
|
|
6
|
+
readonly ms: number;
|
|
7
|
+
constructor(ms: number);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
declare function withTimeout<T>(promise: Promise<T>, ms: number, options?: TimeoutOptions): Promise<T>;
|
|
11
|
+
declare function withTimeoutFallback<T>(promise: Promise<T>, ms: number, fallback: T): Promise<T>;
|
|
12
|
+
declare function createTimeoutSignal(ms: number): AbortSignal;
|
|
13
|
+
|
|
14
|
+
export { TimeoutError, type TimeoutOptions, createTimeoutSignal, withTimeout, withTimeoutFallback };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface TimeoutOptions {
|
|
2
|
+
signal?: AbortSignal;
|
|
3
|
+
onTimeout?: () => void;
|
|
4
|
+
}
|
|
5
|
+
declare class TimeoutError extends Error {
|
|
6
|
+
readonly ms: number;
|
|
7
|
+
constructor(ms: number);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
declare function withTimeout<T>(promise: Promise<T>, ms: number, options?: TimeoutOptions): Promise<T>;
|
|
11
|
+
declare function withTimeoutFallback<T>(promise: Promise<T>, ms: number, fallback: T): Promise<T>;
|
|
12
|
+
declare function createTimeoutSignal(ms: number): AbortSignal;
|
|
13
|
+
|
|
14
|
+
export { TimeoutError, type TimeoutOptions, createTimeoutSignal, withTimeout, withTimeoutFallback };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// src/types.ts
|
|
2
|
+
var TimeoutError = class extends Error {
|
|
3
|
+
constructor(ms) {
|
|
4
|
+
super(`Operation timed out after ${ms}ms`);
|
|
5
|
+
this.name = "TimeoutError";
|
|
6
|
+
this.ms = ms;
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
// src/timeout.ts
|
|
11
|
+
function withTimeout(promise, ms, options) {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
var _a;
|
|
14
|
+
let settled = false;
|
|
15
|
+
const timer = setTimeout(() => {
|
|
16
|
+
var _a2;
|
|
17
|
+
if (!settled) {
|
|
18
|
+
settled = true;
|
|
19
|
+
(_a2 = options == null ? void 0 : options.onTimeout) == null ? void 0 : _a2.call(options);
|
|
20
|
+
reject(new TimeoutError(ms));
|
|
21
|
+
}
|
|
22
|
+
}, ms);
|
|
23
|
+
if (options == null ? void 0 : options.signal) {
|
|
24
|
+
if (options.signal.aborted) {
|
|
25
|
+
settled = true;
|
|
26
|
+
clearTimeout(timer);
|
|
27
|
+
reject((_a = options.signal.reason) != null ? _a : new DOMException("Aborted", "AbortError"));
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
options.signal.addEventListener("abort", () => {
|
|
31
|
+
var _a2;
|
|
32
|
+
if (!settled) {
|
|
33
|
+
settled = true;
|
|
34
|
+
clearTimeout(timer);
|
|
35
|
+
reject((_a2 = options.signal.reason) != null ? _a2 : new DOMException("Aborted", "AbortError"));
|
|
36
|
+
}
|
|
37
|
+
}, { once: true });
|
|
38
|
+
}
|
|
39
|
+
promise.then(
|
|
40
|
+
(value) => {
|
|
41
|
+
if (!settled) {
|
|
42
|
+
settled = true;
|
|
43
|
+
clearTimeout(timer);
|
|
44
|
+
resolve(value);
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
(error) => {
|
|
48
|
+
if (!settled) {
|
|
49
|
+
settled = true;
|
|
50
|
+
clearTimeout(timer);
|
|
51
|
+
reject(error);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
async function withTimeoutFallback(promise, ms, fallback) {
|
|
58
|
+
try {
|
|
59
|
+
return await withTimeout(promise, ms);
|
|
60
|
+
} catch (error) {
|
|
61
|
+
if (error instanceof TimeoutError) {
|
|
62
|
+
return fallback;
|
|
63
|
+
}
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function createTimeoutSignal(ms) {
|
|
68
|
+
const controller = new AbortController();
|
|
69
|
+
setTimeout(() => controller.abort(new TimeoutError(ms)), ms);
|
|
70
|
+
return controller.signal;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export { TimeoutError, createTimeoutSignal, withTimeout, withTimeoutFallback };
|
|
74
|
+
//# sourceMappingURL=index.js.map
|
|
75
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/types.ts","../src/timeout.ts"],"names":["_a"],"mappings":";AAKO,IAAM,YAAA,GAAN,cAA2B,KAAA,CAAM;AAAA,EAGtC,YAAY,EAAA,EAAY;AACtB,IAAA,KAAA,CAAM,CAAA,0BAAA,EAA6B,EAAE,CAAA,EAAA,CAAI,CAAA;AACzC,IAAA,IAAA,CAAK,IAAA,GAAO,cAAA;AACZ,IAAA,IAAA,CAAK,EAAA,GAAK,EAAA;AAAA,EACZ;AACF;;;ACVO,SAAS,WAAA,CACd,OAAA,EACA,EAAA,EACA,OAAA,EACY;AACZ,EAAA,OAAO,IAAI,OAAA,CAAW,CAAC,OAAA,EAAS,MAAA,KAAW;AAR7C,IAAA,IAAA,EAAA;AASI,IAAA,IAAI,OAAA,GAAU,KAAA;AAEd,IAAA,MAAM,KAAA,GAAQ,WAAW,MAAM;AAXnC,MAAA,IAAAA,GAAAA;AAY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K,CAAA;AAClB,UAAA,OAAA,CAAQ,KAAK,CAAA;AAAA,QACf;AAAA,MACF,CAAA;AAAA,MACA,CAAC,KAAA,KAAU;AACT,QAAA,IAAI,CAAC,OAAA,EAAS;AACZ,UAAA,OAAA,GAAU,IAAA;AACV,UAAA,YAAA,CAAa,KAAK,CAAA;AAClB,UAAA,MAAA,CAAO,KAAK,CAAA;AAAA,QACd;AAAA,MACF;AAAA,KACF;AAAA,EACF,CAAC,CAAA;AACH;AAEA,eAAsB,mBAAA,CACpB,OAAA,EACA,EAAA,EACA,QAAA,EACY;AACZ,EAAA,IAAI;AACF,IAAA,OAAO,MAAM,WAAA,CAAY,OAAA,EAAS,EAAE,CAAA;AAAA,EACtC,SAAS,KAAA,EAAO;AACd,IAAA,IAAI,iBAAiB,YAAA,EAAc;AACjC,MAAA,OAAO,QAAA;AAAA,IACT;AACA,IAAA,MAAM,KAAA;AAAA,EACR;AACF;AAEO,SAAS,oBAAoB,EAAA,EAAyB;AAC3D,EAAA,MAAM,UAAA,GAAa,IAAI,eAAA,EAAgB;AACvC,EAAA,UAAA,CAAW,MAAM,WAAW,KAAA,CAAM,IAAI,aAAa,EAAE,CAAC,GAAG,EAAE,CAAA;AAC3D,EAAA,OAAO,UAAA,CAAW,MAAA;AACpB","file":"index.js","sourcesContent":["export interface TimeoutOptions {\n signal?: AbortSignal;\n onTimeout?: () => void;\n}\n\nexport class TimeoutError extends Error {\n public readonly ms: number;\n\n constructor(ms: number) {\n super(`Operation timed out after ${ms}ms`);\n this.name = 'TimeoutError';\n this.ms = ms;\n }\n}\n","import { TimeoutError } from './types.js';\nimport type { TimeoutOptions } from './types.js';\n\nexport function withTimeout<T>(\n promise: Promise<T>,\n ms: number,\n options?: TimeoutOptions,\n): Promise<T> {\n return new Promise<T>((resolve, reject) => {\n let settled = false;\n\n const timer = setTimeout(() => {\n if (!settled) {\n settled = true;\n options?.onTimeout?.();\n reject(new TimeoutError(ms));\n }\n }, ms);\n\n if (options?.signal) {\n if (options.signal.aborted) {\n settled = true;\n clearTimeout(timer);\n reject(options.signal.reason ?? new DOMException('Aborted', 'AbortError'));\n return;\n }\n\n options.signal.addEventListener('abort', () => {\n if (!settled) {\n settled = true;\n clearTimeout(timer);\n reject(options.signal!.reason ?? new DOMException('Aborted', 'AbortError'));\n }\n }, { once: true });\n }\n\n promise.then(\n (value) => {\n if (!settled) {\n settled = true;\n clearTimeout(timer);\n resolve(value);\n }\n },\n (error) => {\n if (!settled) {\n settled = true;\n clearTimeout(timer);\n reject(error);\n }\n },\n );\n });\n}\n\nexport async function withTimeoutFallback<T>(\n promise: Promise<T>,\n ms: number,\n fallback: T,\n): Promise<T> {\n try {\n return await withTimeout(promise, ms);\n } catch (error) {\n if (error instanceof TimeoutError) {\n return fallback;\n }\n throw error;\n }\n}\n\nexport function createTimeoutSignal(ms: number): AbortSignal {\n const controller = new AbortController();\n setTimeout(() => controller.abort(new TimeoutError(ms)), ms);\n return controller.signal;\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@philiprehberger/safe-timeout",
|
|
3
|
+
"version": "0.1.5",
|
|
4
|
+
"description": "Reliable timeout wrapper for async operations with AbortController support",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsup",
|
|
26
|
+
"dev": "tsup --watch",
|
|
27
|
+
"typecheck": "tsc --noEmit",
|
|
28
|
+
"prepublishOnly": "npm run build",
|
|
29
|
+
"test": "node --test"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"tsup": "^8.0.0",
|
|
33
|
+
"typescript": "^5.0.0"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"timeout",
|
|
37
|
+
"async",
|
|
38
|
+
"abort",
|
|
39
|
+
"cancel",
|
|
40
|
+
"promise"
|
|
41
|
+
],
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "git+https://github.com/philiprehberger/ts-safe-timeout.git"
|
|
46
|
+
},
|
|
47
|
+
"homepage": "https://github.com/philiprehberger/ts-safe-timeout#readme",
|
|
48
|
+
"bugs": {
|
|
49
|
+
"url": "https://github.com/philiprehberger/ts-safe-timeout/issues"
|
|
50
|
+
},
|
|
51
|
+
"author": "Philip Rehberger",
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": ">=18.0.0"
|
|
54
|
+
},
|
|
55
|
+
"sideEffects": false
|
|
56
|
+
}
|