resilient-exec 1.0.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/LICENSE +21 -0
- package/README.md +234 -0
- package/dist/index.cjs +382 -0
- package/dist/index.d.cts +40 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.js +355 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 GBB26
|
|
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,234 @@
|
|
|
1
|
+
# resilient-exec
|
|
2
|
+
|
|
3
|
+
A tiny, deterministic utility for executing async functions with
|
|
4
|
+
**retries, backoff, timeouts, abort signals, and fallbacks** --- without
|
|
5
|
+
hidden magic.
|
|
6
|
+
|
|
7
|
+
Built for Node.js & TypeScript. Works with both ESM and CommonJS.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## ✨ Features
|
|
12
|
+
|
|
13
|
+
- ✅ Deterministic retries
|
|
14
|
+
- ✅ Configurable backoff strategies (fixed, exponential, fibonacci,
|
|
15
|
+
custom)
|
|
16
|
+
- ✅ Per-attempt timeouts
|
|
17
|
+
- ✅ AbortSignal support
|
|
18
|
+
- ✅ Fallback execution
|
|
19
|
+
- ✅ Strong TypeScript typings
|
|
20
|
+
- ✅ Works with ESM & CommonJS
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## 📦 Install
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm install resilient-exec
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## 🚀 Basic Usage
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { attempt } from "resilient-exec";
|
|
36
|
+
|
|
37
|
+
const result = await attempt(async () => {
|
|
38
|
+
return "hello";
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
if (result.ok) {
|
|
42
|
+
console.log("Success:", result.value);
|
|
43
|
+
} else {
|
|
44
|
+
console.error("Failed:", result.error);
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## 🔁 Retry
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
await attempt(
|
|
54
|
+
async () => {
|
|
55
|
+
return fetchData();
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
retry: { retries: 3 },
|
|
59
|
+
},
|
|
60
|
+
);
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
## ⏱ Timeout
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
await attempt(
|
|
69
|
+
async () => {
|
|
70
|
+
return slowTask();
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
timeout: 500, // milliseconds
|
|
74
|
+
},
|
|
75
|
+
);
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## 🧯 Fallback
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
await attempt(primaryTask, {
|
|
84
|
+
retry: { retries: 2 },
|
|
85
|
+
fallback: async () => {
|
|
86
|
+
return "fallback-result";
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
## 🛑 Abort
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
const controller = new AbortController();
|
|
97
|
+
|
|
98
|
+
setTimeout(() => controller.abort(), 100);
|
|
99
|
+
|
|
100
|
+
const result = await attempt(
|
|
101
|
+
async () => {
|
|
102
|
+
return longRunningTask();
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
signal: controller.signal,
|
|
106
|
+
},
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
if (result.cancelled) {
|
|
110
|
+
console.log("Execution was cancelled");
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
## 🧠 Backoff Strategies
|
|
117
|
+
|
|
118
|
+
### Fixed Backoff
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
await attempt(fn, {
|
|
122
|
+
retry: { retries: 3 },
|
|
123
|
+
backoff: { type: "fixed", delay: 200 },
|
|
124
|
+
});
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Exponential Backoff
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
await attempt(fn, {
|
|
131
|
+
retry: { retries: 3 },
|
|
132
|
+
backoff: { type: "exponential", base: 100, max: 2000 },
|
|
133
|
+
});
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Fibonacci Backoff
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
await attempt(fn, {
|
|
140
|
+
retry: { retries: 3 },
|
|
141
|
+
backoff: { type: "fibonacci", base: 100 },
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Custom Backoff
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
await attempt(fn, {
|
|
149
|
+
retry: { retries: 3 },
|
|
150
|
+
backoff: {
|
|
151
|
+
type: "custom",
|
|
152
|
+
getDelay: (attempt) => attempt * 150,
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
## 📚 API
|
|
160
|
+
|
|
161
|
+
### `attempt(fn, options?)`
|
|
162
|
+
|
|
163
|
+
Executes the provided function with retry, timeout, abort, and fallback
|
|
164
|
+
logic.
|
|
165
|
+
|
|
166
|
+
#### Return Type
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
type AttemptResult<T> =
|
|
170
|
+
| {
|
|
171
|
+
ok: true;
|
|
172
|
+
value: T;
|
|
173
|
+
attempts: number;
|
|
174
|
+
usedFallback: boolean;
|
|
175
|
+
duration: number;
|
|
176
|
+
}
|
|
177
|
+
| {
|
|
178
|
+
ok: false;
|
|
179
|
+
error?: unknown;
|
|
180
|
+
attempts: number;
|
|
181
|
+
usedFallback: boolean;
|
|
182
|
+
cancelled?: boolean;
|
|
183
|
+
duration: number;
|
|
184
|
+
};
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
#### Options
|
|
188
|
+
|
|
189
|
+
```ts
|
|
190
|
+
type AttemptOptions = {
|
|
191
|
+
retry?: {
|
|
192
|
+
retries: number;
|
|
193
|
+
retryIf?: (error: unknown) => boolean;
|
|
194
|
+
};
|
|
195
|
+
backoff?: {
|
|
196
|
+
type: "fixed" | "exponential" | "fibonacci" | "custom";
|
|
197
|
+
delay?: number;
|
|
198
|
+
base?: number;
|
|
199
|
+
max?: number;
|
|
200
|
+
getDelay?: (attempt: number) => number;
|
|
201
|
+
};
|
|
202
|
+
timeout?: number;
|
|
203
|
+
fallback?: () => Promise<unknown> | unknown;
|
|
204
|
+
signal?: AbortSignal;
|
|
205
|
+
throwOnFail?: boolean;
|
|
206
|
+
};
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
---
|
|
210
|
+
|
|
211
|
+
## 🎯 Design Philosophy
|
|
212
|
+
|
|
213
|
+
- No hidden retries
|
|
214
|
+
- No global state
|
|
215
|
+
- No monkey-patching
|
|
216
|
+
- Explicit behavior
|
|
217
|
+
- Predictable control flow
|
|
218
|
+
- Works with real-world async edge cases
|
|
219
|
+
|
|
220
|
+
---
|
|
221
|
+
|
|
222
|
+
## 🧪 Testing
|
|
223
|
+
|
|
224
|
+
This package is fully covered by:
|
|
225
|
+
|
|
226
|
+
- Unit tests
|
|
227
|
+
- Integration tests
|
|
228
|
+
- Timeout & abort edge cases
|
|
229
|
+
|
|
230
|
+
---
|
|
231
|
+
|
|
232
|
+
## 📄 License
|
|
233
|
+
|
|
234
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
attempt: () => attempt
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
|
|
27
|
+
// src/context/ExecutionContext.ts
|
|
28
|
+
var ExecutionContext = class {
|
|
29
|
+
constructor() {
|
|
30
|
+
/** Total number of execution attempts (primary + retries) */
|
|
31
|
+
this.attemptCount = 0;
|
|
32
|
+
/** Number of retries performed */
|
|
33
|
+
this.retryCount = 0;
|
|
34
|
+
/** Index of the current fallback being executed */
|
|
35
|
+
this.fallbackIndex = 0;
|
|
36
|
+
/** Whether a fallback was used */
|
|
37
|
+
this.usedFallback = false;
|
|
38
|
+
/** Whether execution was cancelled */
|
|
39
|
+
this.cancelled = false;
|
|
40
|
+
/** Last encountered error */
|
|
41
|
+
this.lastError = null;
|
|
42
|
+
this.startTime = Date.now();
|
|
43
|
+
}
|
|
44
|
+
/** Mark that an execution attempt occurred */
|
|
45
|
+
recordAttempt() {
|
|
46
|
+
this.attemptCount++;
|
|
47
|
+
}
|
|
48
|
+
/** Mark that a retry occurred */
|
|
49
|
+
recordRetry() {
|
|
50
|
+
this.retryCount++;
|
|
51
|
+
}
|
|
52
|
+
/** Mark that fallback execution has started */
|
|
53
|
+
markFallbackUsed() {
|
|
54
|
+
this.usedFallback = true;
|
|
55
|
+
}
|
|
56
|
+
/** Mark execution as cancelled */
|
|
57
|
+
markCancelled() {
|
|
58
|
+
this.cancelled = true;
|
|
59
|
+
}
|
|
60
|
+
/** Store the last error */
|
|
61
|
+
setError(error) {
|
|
62
|
+
this.lastError = error;
|
|
63
|
+
}
|
|
64
|
+
/** Total execution duration in milliseconds */
|
|
65
|
+
get duration() {
|
|
66
|
+
return Date.now() - this.startTime;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// src/utils/timeout.ts
|
|
71
|
+
async function withTimeout(fn, timeoutMs, signal) {
|
|
72
|
+
if (timeoutMs <= 0) {
|
|
73
|
+
return fn();
|
|
74
|
+
}
|
|
75
|
+
let timeoutId;
|
|
76
|
+
return Promise.race([
|
|
77
|
+
fn(),
|
|
78
|
+
new Promise((_, reject) => {
|
|
79
|
+
timeoutId = setTimeout(() => {
|
|
80
|
+
reject(createTimeoutError(timeoutMs));
|
|
81
|
+
}, timeoutMs);
|
|
82
|
+
if (signal) {
|
|
83
|
+
signal.addEventListener(
|
|
84
|
+
"abort",
|
|
85
|
+
() => reject(createAbortError()),
|
|
86
|
+
{ once: true }
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
]).finally(() => {
|
|
91
|
+
clearTimeout(timeoutId);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
function createTimeoutError(ms) {
|
|
95
|
+
const error = new Error(`Operation timed out after ${ms}ms`);
|
|
96
|
+
error.name = "TimeoutError";
|
|
97
|
+
return error;
|
|
98
|
+
}
|
|
99
|
+
function createAbortError() {
|
|
100
|
+
const error = new Error("Operation aborted");
|
|
101
|
+
error.name = "AbortError";
|
|
102
|
+
return error;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/utils/abort.ts
|
|
106
|
+
function isAbortError(error) {
|
|
107
|
+
return error instanceof Error && (error.name === "AbortError" || error.message.toLowerCase().includes("aborted"));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// src/engine/ExecutionEngine.ts
|
|
111
|
+
var ExecutionEngine = class {
|
|
112
|
+
async execute(fn, context, options) {
|
|
113
|
+
if (options.signal?.aborted) {
|
|
114
|
+
context.markCancelled();
|
|
115
|
+
throw this.createAbortError();
|
|
116
|
+
}
|
|
117
|
+
context.recordAttempt();
|
|
118
|
+
try {
|
|
119
|
+
const result = await withTimeout(
|
|
120
|
+
() => Promise.resolve().then(fn),
|
|
121
|
+
options.timeout ?? 0,
|
|
122
|
+
options.signal
|
|
123
|
+
);
|
|
124
|
+
return result;
|
|
125
|
+
} catch (error) {
|
|
126
|
+
if (isAbortError(error)) {
|
|
127
|
+
context.markCancelled();
|
|
128
|
+
}
|
|
129
|
+
context.setError(error);
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
createAbortError() {
|
|
134
|
+
const error = new Error("Operation aborted");
|
|
135
|
+
error.name = "AbortError";
|
|
136
|
+
return error;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
// src/retry/RetryController.ts
|
|
141
|
+
var RetryController = class {
|
|
142
|
+
constructor(options) {
|
|
143
|
+
this.retries = options?.retries ?? 0;
|
|
144
|
+
this.retryIf = options?.retryIf ?? (() => true);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Decide whether a retry should be attempted
|
|
148
|
+
*/
|
|
149
|
+
shouldRetry(error, context) {
|
|
150
|
+
if (context.cancelled) {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
if (context.retryCount >= this.retries) {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
if (this.retryIf && !this.retryIf(error)) {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Record that a retry is happening
|
|
163
|
+
*/
|
|
164
|
+
recordRetry(context) {
|
|
165
|
+
context.recordRetry();
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
// src/retry/BackoffCalculator.ts
|
|
170
|
+
var BackoffCalculator = class {
|
|
171
|
+
constructor(strategy) {
|
|
172
|
+
this.strategy = strategy ?? null;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Calculate delay (in ms) before the next retry
|
|
176
|
+
*/
|
|
177
|
+
getDelay(attempt2) {
|
|
178
|
+
if (!this.strategy) {
|
|
179
|
+
return 0;
|
|
180
|
+
}
|
|
181
|
+
switch (this.strategy.type) {
|
|
182
|
+
case "fixed":
|
|
183
|
+
return this.strategy.delay;
|
|
184
|
+
case "exponential": {
|
|
185
|
+
const delay2 = this.strategy.base * Math.pow(2, attempt2);
|
|
186
|
+
return this.strategy.max ? Math.min(delay2, this.strategy.max) : delay2;
|
|
187
|
+
}
|
|
188
|
+
case "fibonacci":
|
|
189
|
+
return this.fibonacciDelay(this.strategy.base, attempt2);
|
|
190
|
+
case "custom":
|
|
191
|
+
return Math.max(0, this.strategy.getDelay(attempt2));
|
|
192
|
+
default:
|
|
193
|
+
return 0;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
fibonacciDelay(base, attempt2) {
|
|
197
|
+
if (attempt2 <= 1) {
|
|
198
|
+
return base;
|
|
199
|
+
}
|
|
200
|
+
let prev = 0;
|
|
201
|
+
let curr = base;
|
|
202
|
+
for (let i = 2; i <= attempt2; i++) {
|
|
203
|
+
const next = prev + curr;
|
|
204
|
+
prev = curr;
|
|
205
|
+
curr = next;
|
|
206
|
+
}
|
|
207
|
+
return curr;
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
// src/fallback/FallbackController.ts
|
|
212
|
+
var FallbackController = class {
|
|
213
|
+
constructor(fallback) {
|
|
214
|
+
if (!fallback) {
|
|
215
|
+
this.fallbacks = [];
|
|
216
|
+
} else if (Array.isArray(fallback)) {
|
|
217
|
+
this.fallbacks = fallback;
|
|
218
|
+
} else {
|
|
219
|
+
this.fallbacks = [fallback];
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
hasFallback() {
|
|
223
|
+
return this.fallbacks.length > 0;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Execute fallback operations sequentially.
|
|
227
|
+
* Stops at the first successful fallback.
|
|
228
|
+
*/
|
|
229
|
+
async execute(engine, context, options) {
|
|
230
|
+
context.markFallbackUsed();
|
|
231
|
+
for (let i = 0; i < this.fallbacks.length; i++) {
|
|
232
|
+
context.fallbackIndex = i;
|
|
233
|
+
try {
|
|
234
|
+
const result = await engine.execute(
|
|
235
|
+
this.fallbacks[i],
|
|
236
|
+
context,
|
|
237
|
+
options
|
|
238
|
+
);
|
|
239
|
+
return result;
|
|
240
|
+
} catch (error) {
|
|
241
|
+
context.setError(error);
|
|
242
|
+
if (context.cancelled) {
|
|
243
|
+
throw error;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
throw context.lastError;
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
// src/result/ResultAggregator.ts
|
|
252
|
+
var ResultAggregator = class {
|
|
253
|
+
buildSuccess(value, context) {
|
|
254
|
+
return {
|
|
255
|
+
ok: true,
|
|
256
|
+
value,
|
|
257
|
+
attempts: context.attemptCount,
|
|
258
|
+
usedFallback: context.usedFallback,
|
|
259
|
+
duration: context.duration
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
buildFailure(error, context) {
|
|
263
|
+
return {
|
|
264
|
+
ok: false,
|
|
265
|
+
error,
|
|
266
|
+
attempts: context.attemptCount,
|
|
267
|
+
usedFallback: context.usedFallback,
|
|
268
|
+
duration: context.duration,
|
|
269
|
+
cancelled: context.cancelled
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
buildCancelled(context) {
|
|
273
|
+
return {
|
|
274
|
+
ok: false,
|
|
275
|
+
attempts: context.attemptCount,
|
|
276
|
+
usedFallback: context.usedFallback,
|
|
277
|
+
duration: context.duration,
|
|
278
|
+
cancelled: true
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
finalize(result, throwOnFail) {
|
|
282
|
+
if (!result.ok && throwOnFail) {
|
|
283
|
+
throw result.error;
|
|
284
|
+
}
|
|
285
|
+
return result;
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
// src/utils/delay.ts
|
|
290
|
+
function delay(ms, signal) {
|
|
291
|
+
return new Promise((resolve, reject) => {
|
|
292
|
+
if (signal?.aborted) {
|
|
293
|
+
return reject(createAbortError2());
|
|
294
|
+
}
|
|
295
|
+
const timer = setTimeout(() => {
|
|
296
|
+
cleanup();
|
|
297
|
+
resolve();
|
|
298
|
+
}, ms);
|
|
299
|
+
const onAbort = () => {
|
|
300
|
+
cleanup();
|
|
301
|
+
reject(createAbortError2());
|
|
302
|
+
};
|
|
303
|
+
const cleanup = () => {
|
|
304
|
+
clearTimeout(timer);
|
|
305
|
+
signal?.removeEventListener("abort", onAbort);
|
|
306
|
+
};
|
|
307
|
+
if (signal) {
|
|
308
|
+
signal.addEventListener("abort", onAbort);
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
function createAbortError2() {
|
|
313
|
+
const error = new Error("Operation aborted");
|
|
314
|
+
error.name = "AbortError";
|
|
315
|
+
return error;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// src/attempt.ts
|
|
319
|
+
async function attempt(fn, options = {}) {
|
|
320
|
+
const context = new ExecutionContext();
|
|
321
|
+
const engine = new ExecutionEngine();
|
|
322
|
+
const retryController = new RetryController(options.retry);
|
|
323
|
+
const backoffCalculator = new BackoffCalculator(options.backoff);
|
|
324
|
+
const fallbackController = new FallbackController(options.fallback);
|
|
325
|
+
const resultAggregator = new ResultAggregator();
|
|
326
|
+
const execOptions = {};
|
|
327
|
+
if (options.timeout !== void 0) execOptions.timeout = options.timeout;
|
|
328
|
+
if (options.signal !== void 0) execOptions.signal = options.signal;
|
|
329
|
+
try {
|
|
330
|
+
while (true) {
|
|
331
|
+
try {
|
|
332
|
+
const value = await engine.execute(fn, context, execOptions);
|
|
333
|
+
return resultAggregator.finalize(
|
|
334
|
+
resultAggregator.buildSuccess(value, context),
|
|
335
|
+
options.throwOnFail
|
|
336
|
+
);
|
|
337
|
+
} catch (error) {
|
|
338
|
+
context.setError(error);
|
|
339
|
+
if (retryController.shouldRetry(error, context)) {
|
|
340
|
+
retryController.recordRetry(context);
|
|
341
|
+
const waitTime = backoffCalculator.getDelay(context.retryCount);
|
|
342
|
+
if (waitTime > 0) {
|
|
343
|
+
await delay(waitTime, options.signal);
|
|
344
|
+
}
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
break;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
if (fallbackController.hasFallback()) {
|
|
351
|
+
const value = await fallbackController.execute(
|
|
352
|
+
engine,
|
|
353
|
+
context,
|
|
354
|
+
execOptions
|
|
355
|
+
);
|
|
356
|
+
return resultAggregator.finalize(
|
|
357
|
+
resultAggregator.buildSuccess(value, context),
|
|
358
|
+
options.throwOnFail
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
return resultAggregator.finalize(
|
|
362
|
+
resultAggregator.buildFailure(context.lastError, context),
|
|
363
|
+
options.throwOnFail
|
|
364
|
+
);
|
|
365
|
+
} catch (error) {
|
|
366
|
+
context.setError(error);
|
|
367
|
+
if (context.cancelled) {
|
|
368
|
+
return resultAggregator.finalize(
|
|
369
|
+
resultAggregator.buildCancelled(context),
|
|
370
|
+
options.throwOnFail
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
return resultAggregator.finalize(
|
|
374
|
+
resultAggregator.buildFailure(error, context),
|
|
375
|
+
options.throwOnFail
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
380
|
+
0 && (module.exports = {
|
|
381
|
+
attempt
|
|
382
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
type AttemptFn<T = any> = () => T | Promise<T>;
|
|
2
|
+
interface RetryOptions {
|
|
3
|
+
retries?: number;
|
|
4
|
+
retryIf?: (error: unknown) => boolean;
|
|
5
|
+
}
|
|
6
|
+
type BackoffStrategy = {
|
|
7
|
+
type: "fixed";
|
|
8
|
+
delay: number;
|
|
9
|
+
} | {
|
|
10
|
+
type: "exponential";
|
|
11
|
+
base: number;
|
|
12
|
+
max?: number;
|
|
13
|
+
} | {
|
|
14
|
+
type: "fibonacci";
|
|
15
|
+
base: number;
|
|
16
|
+
} | {
|
|
17
|
+
type: "custom";
|
|
18
|
+
getDelay: (attempt: number) => number;
|
|
19
|
+
};
|
|
20
|
+
interface AttemptOptions {
|
|
21
|
+
retry?: RetryOptions;
|
|
22
|
+
backoff?: BackoffStrategy;
|
|
23
|
+
timeout?: number;
|
|
24
|
+
fallback?: AttemptFn | AttemptFn[];
|
|
25
|
+
signal?: AbortSignal;
|
|
26
|
+
throwOnFail?: boolean;
|
|
27
|
+
}
|
|
28
|
+
interface AttemptResult<T = any> {
|
|
29
|
+
ok: boolean;
|
|
30
|
+
value?: T;
|
|
31
|
+
error?: unknown;
|
|
32
|
+
attempts: number;
|
|
33
|
+
usedFallback: boolean;
|
|
34
|
+
duration: number;
|
|
35
|
+
cancelled?: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
declare function attempt<T>(fn: AttemptFn<T>, options?: AttemptOptions): Promise<AttemptResult<unknown>>;
|
|
39
|
+
|
|
40
|
+
export { type AttemptFn, type AttemptOptions, type AttemptResult, type BackoffStrategy, type RetryOptions, attempt };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
type AttemptFn<T = any> = () => T | Promise<T>;
|
|
2
|
+
interface RetryOptions {
|
|
3
|
+
retries?: number;
|
|
4
|
+
retryIf?: (error: unknown) => boolean;
|
|
5
|
+
}
|
|
6
|
+
type BackoffStrategy = {
|
|
7
|
+
type: "fixed";
|
|
8
|
+
delay: number;
|
|
9
|
+
} | {
|
|
10
|
+
type: "exponential";
|
|
11
|
+
base: number;
|
|
12
|
+
max?: number;
|
|
13
|
+
} | {
|
|
14
|
+
type: "fibonacci";
|
|
15
|
+
base: number;
|
|
16
|
+
} | {
|
|
17
|
+
type: "custom";
|
|
18
|
+
getDelay: (attempt: number) => number;
|
|
19
|
+
};
|
|
20
|
+
interface AttemptOptions {
|
|
21
|
+
retry?: RetryOptions;
|
|
22
|
+
backoff?: BackoffStrategy;
|
|
23
|
+
timeout?: number;
|
|
24
|
+
fallback?: AttemptFn | AttemptFn[];
|
|
25
|
+
signal?: AbortSignal;
|
|
26
|
+
throwOnFail?: boolean;
|
|
27
|
+
}
|
|
28
|
+
interface AttemptResult<T = any> {
|
|
29
|
+
ok: boolean;
|
|
30
|
+
value?: T;
|
|
31
|
+
error?: unknown;
|
|
32
|
+
attempts: number;
|
|
33
|
+
usedFallback: boolean;
|
|
34
|
+
duration: number;
|
|
35
|
+
cancelled?: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
declare function attempt<T>(fn: AttemptFn<T>, options?: AttemptOptions): Promise<AttemptResult<unknown>>;
|
|
39
|
+
|
|
40
|
+
export { type AttemptFn, type AttemptOptions, type AttemptResult, type BackoffStrategy, type RetryOptions, attempt };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
// src/context/ExecutionContext.ts
|
|
2
|
+
var ExecutionContext = class {
|
|
3
|
+
constructor() {
|
|
4
|
+
/** Total number of execution attempts (primary + retries) */
|
|
5
|
+
this.attemptCount = 0;
|
|
6
|
+
/** Number of retries performed */
|
|
7
|
+
this.retryCount = 0;
|
|
8
|
+
/** Index of the current fallback being executed */
|
|
9
|
+
this.fallbackIndex = 0;
|
|
10
|
+
/** Whether a fallback was used */
|
|
11
|
+
this.usedFallback = false;
|
|
12
|
+
/** Whether execution was cancelled */
|
|
13
|
+
this.cancelled = false;
|
|
14
|
+
/** Last encountered error */
|
|
15
|
+
this.lastError = null;
|
|
16
|
+
this.startTime = Date.now();
|
|
17
|
+
}
|
|
18
|
+
/** Mark that an execution attempt occurred */
|
|
19
|
+
recordAttempt() {
|
|
20
|
+
this.attemptCount++;
|
|
21
|
+
}
|
|
22
|
+
/** Mark that a retry occurred */
|
|
23
|
+
recordRetry() {
|
|
24
|
+
this.retryCount++;
|
|
25
|
+
}
|
|
26
|
+
/** Mark that fallback execution has started */
|
|
27
|
+
markFallbackUsed() {
|
|
28
|
+
this.usedFallback = true;
|
|
29
|
+
}
|
|
30
|
+
/** Mark execution as cancelled */
|
|
31
|
+
markCancelled() {
|
|
32
|
+
this.cancelled = true;
|
|
33
|
+
}
|
|
34
|
+
/** Store the last error */
|
|
35
|
+
setError(error) {
|
|
36
|
+
this.lastError = error;
|
|
37
|
+
}
|
|
38
|
+
/** Total execution duration in milliseconds */
|
|
39
|
+
get duration() {
|
|
40
|
+
return Date.now() - this.startTime;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// src/utils/timeout.ts
|
|
45
|
+
async function withTimeout(fn, timeoutMs, signal) {
|
|
46
|
+
if (timeoutMs <= 0) {
|
|
47
|
+
return fn();
|
|
48
|
+
}
|
|
49
|
+
let timeoutId;
|
|
50
|
+
return Promise.race([
|
|
51
|
+
fn(),
|
|
52
|
+
new Promise((_, reject) => {
|
|
53
|
+
timeoutId = setTimeout(() => {
|
|
54
|
+
reject(createTimeoutError(timeoutMs));
|
|
55
|
+
}, timeoutMs);
|
|
56
|
+
if (signal) {
|
|
57
|
+
signal.addEventListener(
|
|
58
|
+
"abort",
|
|
59
|
+
() => reject(createAbortError()),
|
|
60
|
+
{ once: true }
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
]).finally(() => {
|
|
65
|
+
clearTimeout(timeoutId);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
function createTimeoutError(ms) {
|
|
69
|
+
const error = new Error(`Operation timed out after ${ms}ms`);
|
|
70
|
+
error.name = "TimeoutError";
|
|
71
|
+
return error;
|
|
72
|
+
}
|
|
73
|
+
function createAbortError() {
|
|
74
|
+
const error = new Error("Operation aborted");
|
|
75
|
+
error.name = "AbortError";
|
|
76
|
+
return error;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// src/utils/abort.ts
|
|
80
|
+
function isAbortError(error) {
|
|
81
|
+
return error instanceof Error && (error.name === "AbortError" || error.message.toLowerCase().includes("aborted"));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// src/engine/ExecutionEngine.ts
|
|
85
|
+
var ExecutionEngine = class {
|
|
86
|
+
async execute(fn, context, options) {
|
|
87
|
+
if (options.signal?.aborted) {
|
|
88
|
+
context.markCancelled();
|
|
89
|
+
throw this.createAbortError();
|
|
90
|
+
}
|
|
91
|
+
context.recordAttempt();
|
|
92
|
+
try {
|
|
93
|
+
const result = await withTimeout(
|
|
94
|
+
() => Promise.resolve().then(fn),
|
|
95
|
+
options.timeout ?? 0,
|
|
96
|
+
options.signal
|
|
97
|
+
);
|
|
98
|
+
return result;
|
|
99
|
+
} catch (error) {
|
|
100
|
+
if (isAbortError(error)) {
|
|
101
|
+
context.markCancelled();
|
|
102
|
+
}
|
|
103
|
+
context.setError(error);
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
createAbortError() {
|
|
108
|
+
const error = new Error("Operation aborted");
|
|
109
|
+
error.name = "AbortError";
|
|
110
|
+
return error;
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// src/retry/RetryController.ts
|
|
115
|
+
var RetryController = class {
|
|
116
|
+
constructor(options) {
|
|
117
|
+
this.retries = options?.retries ?? 0;
|
|
118
|
+
this.retryIf = options?.retryIf ?? (() => true);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Decide whether a retry should be attempted
|
|
122
|
+
*/
|
|
123
|
+
shouldRetry(error, context) {
|
|
124
|
+
if (context.cancelled) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
if (context.retryCount >= this.retries) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
if (this.retryIf && !this.retryIf(error)) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Record that a retry is happening
|
|
137
|
+
*/
|
|
138
|
+
recordRetry(context) {
|
|
139
|
+
context.recordRetry();
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
// src/retry/BackoffCalculator.ts
|
|
144
|
+
var BackoffCalculator = class {
|
|
145
|
+
constructor(strategy) {
|
|
146
|
+
this.strategy = strategy ?? null;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Calculate delay (in ms) before the next retry
|
|
150
|
+
*/
|
|
151
|
+
getDelay(attempt2) {
|
|
152
|
+
if (!this.strategy) {
|
|
153
|
+
return 0;
|
|
154
|
+
}
|
|
155
|
+
switch (this.strategy.type) {
|
|
156
|
+
case "fixed":
|
|
157
|
+
return this.strategy.delay;
|
|
158
|
+
case "exponential": {
|
|
159
|
+
const delay2 = this.strategy.base * Math.pow(2, attempt2);
|
|
160
|
+
return this.strategy.max ? Math.min(delay2, this.strategy.max) : delay2;
|
|
161
|
+
}
|
|
162
|
+
case "fibonacci":
|
|
163
|
+
return this.fibonacciDelay(this.strategy.base, attempt2);
|
|
164
|
+
case "custom":
|
|
165
|
+
return Math.max(0, this.strategy.getDelay(attempt2));
|
|
166
|
+
default:
|
|
167
|
+
return 0;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
fibonacciDelay(base, attempt2) {
|
|
171
|
+
if (attempt2 <= 1) {
|
|
172
|
+
return base;
|
|
173
|
+
}
|
|
174
|
+
let prev = 0;
|
|
175
|
+
let curr = base;
|
|
176
|
+
for (let i = 2; i <= attempt2; i++) {
|
|
177
|
+
const next = prev + curr;
|
|
178
|
+
prev = curr;
|
|
179
|
+
curr = next;
|
|
180
|
+
}
|
|
181
|
+
return curr;
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
// src/fallback/FallbackController.ts
|
|
186
|
+
var FallbackController = class {
|
|
187
|
+
constructor(fallback) {
|
|
188
|
+
if (!fallback) {
|
|
189
|
+
this.fallbacks = [];
|
|
190
|
+
} else if (Array.isArray(fallback)) {
|
|
191
|
+
this.fallbacks = fallback;
|
|
192
|
+
} else {
|
|
193
|
+
this.fallbacks = [fallback];
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
hasFallback() {
|
|
197
|
+
return this.fallbacks.length > 0;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Execute fallback operations sequentially.
|
|
201
|
+
* Stops at the first successful fallback.
|
|
202
|
+
*/
|
|
203
|
+
async execute(engine, context, options) {
|
|
204
|
+
context.markFallbackUsed();
|
|
205
|
+
for (let i = 0; i < this.fallbacks.length; i++) {
|
|
206
|
+
context.fallbackIndex = i;
|
|
207
|
+
try {
|
|
208
|
+
const result = await engine.execute(
|
|
209
|
+
this.fallbacks[i],
|
|
210
|
+
context,
|
|
211
|
+
options
|
|
212
|
+
);
|
|
213
|
+
return result;
|
|
214
|
+
} catch (error) {
|
|
215
|
+
context.setError(error);
|
|
216
|
+
if (context.cancelled) {
|
|
217
|
+
throw error;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
throw context.lastError;
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
// src/result/ResultAggregator.ts
|
|
226
|
+
var ResultAggregator = class {
|
|
227
|
+
buildSuccess(value, context) {
|
|
228
|
+
return {
|
|
229
|
+
ok: true,
|
|
230
|
+
value,
|
|
231
|
+
attempts: context.attemptCount,
|
|
232
|
+
usedFallback: context.usedFallback,
|
|
233
|
+
duration: context.duration
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
buildFailure(error, context) {
|
|
237
|
+
return {
|
|
238
|
+
ok: false,
|
|
239
|
+
error,
|
|
240
|
+
attempts: context.attemptCount,
|
|
241
|
+
usedFallback: context.usedFallback,
|
|
242
|
+
duration: context.duration,
|
|
243
|
+
cancelled: context.cancelled
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
buildCancelled(context) {
|
|
247
|
+
return {
|
|
248
|
+
ok: false,
|
|
249
|
+
attempts: context.attemptCount,
|
|
250
|
+
usedFallback: context.usedFallback,
|
|
251
|
+
duration: context.duration,
|
|
252
|
+
cancelled: true
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
finalize(result, throwOnFail) {
|
|
256
|
+
if (!result.ok && throwOnFail) {
|
|
257
|
+
throw result.error;
|
|
258
|
+
}
|
|
259
|
+
return result;
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
// src/utils/delay.ts
|
|
264
|
+
function delay(ms, signal) {
|
|
265
|
+
return new Promise((resolve, reject) => {
|
|
266
|
+
if (signal?.aborted) {
|
|
267
|
+
return reject(createAbortError2());
|
|
268
|
+
}
|
|
269
|
+
const timer = setTimeout(() => {
|
|
270
|
+
cleanup();
|
|
271
|
+
resolve();
|
|
272
|
+
}, ms);
|
|
273
|
+
const onAbort = () => {
|
|
274
|
+
cleanup();
|
|
275
|
+
reject(createAbortError2());
|
|
276
|
+
};
|
|
277
|
+
const cleanup = () => {
|
|
278
|
+
clearTimeout(timer);
|
|
279
|
+
signal?.removeEventListener("abort", onAbort);
|
|
280
|
+
};
|
|
281
|
+
if (signal) {
|
|
282
|
+
signal.addEventListener("abort", onAbort);
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
function createAbortError2() {
|
|
287
|
+
const error = new Error("Operation aborted");
|
|
288
|
+
error.name = "AbortError";
|
|
289
|
+
return error;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// src/attempt.ts
|
|
293
|
+
async function attempt(fn, options = {}) {
|
|
294
|
+
const context = new ExecutionContext();
|
|
295
|
+
const engine = new ExecutionEngine();
|
|
296
|
+
const retryController = new RetryController(options.retry);
|
|
297
|
+
const backoffCalculator = new BackoffCalculator(options.backoff);
|
|
298
|
+
const fallbackController = new FallbackController(options.fallback);
|
|
299
|
+
const resultAggregator = new ResultAggregator();
|
|
300
|
+
const execOptions = {};
|
|
301
|
+
if (options.timeout !== void 0) execOptions.timeout = options.timeout;
|
|
302
|
+
if (options.signal !== void 0) execOptions.signal = options.signal;
|
|
303
|
+
try {
|
|
304
|
+
while (true) {
|
|
305
|
+
try {
|
|
306
|
+
const value = await engine.execute(fn, context, execOptions);
|
|
307
|
+
return resultAggregator.finalize(
|
|
308
|
+
resultAggregator.buildSuccess(value, context),
|
|
309
|
+
options.throwOnFail
|
|
310
|
+
);
|
|
311
|
+
} catch (error) {
|
|
312
|
+
context.setError(error);
|
|
313
|
+
if (retryController.shouldRetry(error, context)) {
|
|
314
|
+
retryController.recordRetry(context);
|
|
315
|
+
const waitTime = backoffCalculator.getDelay(context.retryCount);
|
|
316
|
+
if (waitTime > 0) {
|
|
317
|
+
await delay(waitTime, options.signal);
|
|
318
|
+
}
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
if (fallbackController.hasFallback()) {
|
|
325
|
+
const value = await fallbackController.execute(
|
|
326
|
+
engine,
|
|
327
|
+
context,
|
|
328
|
+
execOptions
|
|
329
|
+
);
|
|
330
|
+
return resultAggregator.finalize(
|
|
331
|
+
resultAggregator.buildSuccess(value, context),
|
|
332
|
+
options.throwOnFail
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
return resultAggregator.finalize(
|
|
336
|
+
resultAggregator.buildFailure(context.lastError, context),
|
|
337
|
+
options.throwOnFail
|
|
338
|
+
);
|
|
339
|
+
} catch (error) {
|
|
340
|
+
context.setError(error);
|
|
341
|
+
if (context.cancelled) {
|
|
342
|
+
return resultAggregator.finalize(
|
|
343
|
+
resultAggregator.buildCancelled(context),
|
|
344
|
+
options.throwOnFail
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
return resultAggregator.finalize(
|
|
348
|
+
resultAggregator.buildFailure(error, context),
|
|
349
|
+
options.throwOnFail
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
export {
|
|
354
|
+
attempt
|
|
355
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "resilient-exec",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Deterministic retry, timeout, and fallback execution utility",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
23
|
+
"test": "vitest run",
|
|
24
|
+
"clean": "rimraf dist",
|
|
25
|
+
"prepublishOnly": "npm run clean && npm run build && npm test"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"retry",
|
|
29
|
+
"fallback",
|
|
30
|
+
"resilience",
|
|
31
|
+
"timeout",
|
|
32
|
+
"error-handling",
|
|
33
|
+
"typescript",
|
|
34
|
+
"async"
|
|
35
|
+
],
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"tsup": "^8.0.0",
|
|
39
|
+
"typescript": "^5.0.0",
|
|
40
|
+
"vitest": "^4.0.18",
|
|
41
|
+
"rimraf": "^5.0.5"
|
|
42
|
+
}
|
|
43
|
+
}
|