@zap-studio/retry 0.3.2 → 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/CHANGELOG.md +50 -18
- package/LICENSE +1 -1
- package/README.md +88 -154
- package/dist/base-policy.d.ts +61 -53
- package/dist/base-policy.d.ts.map +1 -1
- package/dist/base-policy.js +306 -2
- package/dist/base-policy.js.map +1 -0
- package/dist/{errors-BVZjP1Q5.d.ts → errors-CS5UPJWs.d.ts} +25 -1
- package/dist/errors-CS5UPJWs.d.ts.map +1 -0
- package/dist/errors.d.ts +1 -1
- package/dist/errors.js +18 -0
- package/dist/errors.js.map +1 -1
- package/dist/exponential-backoff.d.ts +13 -28
- package/dist/exponential-backoff.d.ts.map +1 -1
- package/dist/exponential-backoff.js +10 -35
- package/dist/exponential-backoff.js.map +1 -1
- package/dist/fixed-delay.d.ts +9 -24
- package/dist/fixed-delay.d.ts.map +1 -1
- package/dist/fixed-delay.js +10 -30
- package/dist/fixed-delay.js.map +1 -1
- package/dist/index.d.ts +6 -7
- package/dist/index.js +5 -6
- package/dist/linear-backoff.d.ts +46 -0
- package/dist/linear-backoff.d.ts.map +1 -0
- package/dist/linear-backoff.js +35 -0
- package/dist/linear-backoff.js.map +1 -0
- package/dist/types.d.ts +51 -7
- package/dist/types.d.ts.map +1 -1
- package/package.json +9 -9
- package/dist/abort.d.ts +0 -29
- package/dist/abort.d.ts.map +0 -1
- package/dist/abort.js +0 -61
- package/dist/abort.js.map +0 -1
- package/dist/base-policy-Dn3TOJd3.js +0 -248
- package/dist/base-policy-Dn3TOJd3.js.map +0 -1
- package/dist/errors-BVZjP1Q5.d.ts.map +0 -1
- package/dist/sleep.d.ts +0 -17
- package/dist/sleep.d.ts.map +0 -1
- package/dist/sleep.js +0 -23
- package/dist/sleep.js.map +0 -1
|
@@ -1,248 +0,0 @@
|
|
|
1
|
-
import { RetryError } from "./errors.js";
|
|
2
|
-
import { sleepWithAbortSignal, throwIfAborted, toAbortError } from "./abort.js";
|
|
3
|
-
import { defaultSleep } from "./sleep.js";
|
|
4
|
-
//#region src/_result-mode.ts
|
|
5
|
-
/**
|
|
6
|
-
* Result-mode execution path for `BaseRetryPolicy.run` when
|
|
7
|
-
* `throwOnExhausted: false` is set.
|
|
8
|
-
*
|
|
9
|
-
* @module @zap-studio/retry/_result-mode (private)
|
|
10
|
-
*/
|
|
11
|
-
/**
|
|
12
|
-
* When `signal` is already aborted, builds the terminal `{ ok: false }` object
|
|
13
|
-
* with a normalized `AbortError` on `error`.
|
|
14
|
-
*
|
|
15
|
-
* @param signal - Optional abort signal; only acts when `aborted` is set.
|
|
16
|
-
* @param attempts - Number of finished attempts to report in the result.
|
|
17
|
-
* @returns Failure result or `undefined` if not aborted.
|
|
18
|
-
*/
|
|
19
|
-
const buildAbortResult = (signal, attempts) => {
|
|
20
|
-
if (signal?.aborted !== true) return;
|
|
21
|
-
return {
|
|
22
|
-
attempts,
|
|
23
|
-
error: toAbortError(signal.reason),
|
|
24
|
-
ok: false
|
|
25
|
-
};
|
|
26
|
-
};
|
|
27
|
-
/**
|
|
28
|
-
* Runs one `execute(attempt)` call and returns either a success value or a
|
|
29
|
-
* captured error without rethrowing.
|
|
30
|
-
*
|
|
31
|
-
* @param execute - User work callback.
|
|
32
|
-
* @param attempt - One-based attempt number passed to `execute`.
|
|
33
|
-
* @returns A tagged success with `value` or a tagged failure with `error`.
|
|
34
|
-
*/
|
|
35
|
-
const runAttempt = async (execute, attempt) => {
|
|
36
|
-
try {
|
|
37
|
-
return {
|
|
38
|
-
ok: true,
|
|
39
|
-
value: await execute(attempt)
|
|
40
|
-
};
|
|
41
|
-
} catch (error) {
|
|
42
|
-
return {
|
|
43
|
-
error,
|
|
44
|
-
ok: false
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
/**
|
|
49
|
-
* Awaits inter-attempt delay in result mode, mapping an abort during wait to
|
|
50
|
-
* a terminal result instead of throwing when `throwOnExhausted` is false.
|
|
51
|
-
*
|
|
52
|
-
* @param sleep - Custom or default sleep implementation.
|
|
53
|
-
* @param delayMs - Milliseconds to wait.
|
|
54
|
-
* @param signal - If set, `sleep` is raced with the abort signal.
|
|
55
|
-
* @param attempts - Attempt count to attach if the wait ends in abort.
|
|
56
|
-
* @returns A terminal result when canceled during the wait, otherwise
|
|
57
|
-
* `undefined`.
|
|
58
|
-
* @throws {Error} The underlying `sleep` rejection when it is not an abort.
|
|
59
|
-
*/
|
|
60
|
-
const waitForDelay = async (sleep, delayMs, signal, attempts) => {
|
|
61
|
-
if (signal === void 0) {
|
|
62
|
-
await sleep(delayMs);
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
try {
|
|
66
|
-
await sleepWithAbortSignal(sleep, delayMs, signal);
|
|
67
|
-
return;
|
|
68
|
-
} catch (error) {
|
|
69
|
-
const aborted = buildAbortResult(signal, attempts);
|
|
70
|
-
if (aborted !== void 0) return aborted;
|
|
71
|
-
throw error;
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
/**
|
|
75
|
-
* After a failed attempt, applies abort rules, `next`, optional delay, and
|
|
76
|
-
* either returns a terminal `RetryRunResult` or `undefined` to continue.
|
|
77
|
-
*
|
|
78
|
-
* @param policy - Retry policy hooks (`next`, `onExhausted`) matching
|
|
79
|
-
* `BaseRetryPolicy`.
|
|
80
|
-
* @param params - Failure context for the current attempt.
|
|
81
|
-
* @param params.attempt - Current attempt number.
|
|
82
|
-
* @param params.error - Error thrown by the attempt.
|
|
83
|
-
* @param params.sleep - Delay function between retries.
|
|
84
|
-
* @param params.signal - Optional abort signal.
|
|
85
|
-
* @returns Terminal non-throw result if the loop should stop, otherwise
|
|
86
|
-
* `undefined` to schedule another attempt.
|
|
87
|
-
* @throws {Error} Any error thrown by `next`, `onExhausted`, or a custom `sleep` when
|
|
88
|
-
* the error is not an abort.
|
|
89
|
-
*/
|
|
90
|
-
const handleFailure = async (policy, params) => {
|
|
91
|
-
const { attempt, error, sleep, signal } = params;
|
|
92
|
-
const abortResult = buildAbortResult(signal, attempt);
|
|
93
|
-
if (abortResult !== void 0) return abortResult;
|
|
94
|
-
const decision = policy.next({
|
|
95
|
-
attempt,
|
|
96
|
-
error
|
|
97
|
-
});
|
|
98
|
-
if (!decision.shouldRetry) return {
|
|
99
|
-
attempts: attempt,
|
|
100
|
-
error: policy.onExhausted({
|
|
101
|
-
attempts: attempt,
|
|
102
|
-
error
|
|
103
|
-
}),
|
|
104
|
-
ok: false
|
|
105
|
-
};
|
|
106
|
-
if (decision.delayMs > 0) {
|
|
107
|
-
const delayAbortResult = await waitForDelay(sleep, decision.delayMs, signal, attempt);
|
|
108
|
-
if (delayAbortResult !== void 0) return delayAbortResult;
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
/**
|
|
112
|
-
* Runs the non-throw retry loop, returning
|
|
113
|
-
* `RetryRunResult`.
|
|
114
|
-
*
|
|
115
|
-
* @param policy - Object providing `next` and `onExhausted` (same contract as
|
|
116
|
-
* `BaseRetryPolicy`).
|
|
117
|
-
* @param execute - Async work callback per attempt.
|
|
118
|
-
* @param sleep - Delay function between retries.
|
|
119
|
-
* @param signal - Optional cancel signal.
|
|
120
|
-
* @returns Terminal success or failure object.
|
|
121
|
-
* @throws {Error} Any error thrown by `next`, `onExhausted`, or a non-abort `sleep`
|
|
122
|
-
* failure.
|
|
123
|
-
*/
|
|
124
|
-
const runResultMode = async (policy, execute, sleep, signal) => {
|
|
125
|
-
let attempt = 1;
|
|
126
|
-
while (true) {
|
|
127
|
-
const abortResult = buildAbortResult(signal, Math.max(0, attempt - 1));
|
|
128
|
-
if (abortResult !== void 0) return abortResult;
|
|
129
|
-
const execution = await runAttempt(execute, attempt);
|
|
130
|
-
if (execution.ok) return {
|
|
131
|
-
ok: true,
|
|
132
|
-
value: execution.value
|
|
133
|
-
};
|
|
134
|
-
const failure = await handleFailure(policy, {
|
|
135
|
-
attempt,
|
|
136
|
-
error: execution.error,
|
|
137
|
-
signal,
|
|
138
|
-
sleep
|
|
139
|
-
});
|
|
140
|
-
if (failure !== void 0) return failure;
|
|
141
|
-
attempt += 1;
|
|
142
|
-
}
|
|
143
|
-
};
|
|
144
|
-
//#endregion
|
|
145
|
-
//#region src/_throw-mode.ts
|
|
146
|
-
/**
|
|
147
|
-
* Throw-mode execution path for `BaseRetryPolicy.run` (default when
|
|
148
|
-
* `throwOnExhausted` is not `false`).
|
|
149
|
-
*
|
|
150
|
-
* @module @zap-studio/retry/_throw-mode (private)
|
|
151
|
-
*/
|
|
152
|
-
/**
|
|
153
|
-
* Runs the throw-mode retry loop: throws `RetryError` on exhaustion and
|
|
154
|
-
* `AbortError` when `signal` aborts.
|
|
155
|
-
*
|
|
156
|
-
* @param policy - Object providing `next` and `onExhausted` (same contract as
|
|
157
|
-
* `BaseRetryPolicy`).
|
|
158
|
-
* @param execute - Async work callback per attempt.
|
|
159
|
-
* @param sleep - Delay function between retries.
|
|
160
|
-
* @param signal - Optional cancel signal.
|
|
161
|
-
* @returns Resolves to the first successful return value.
|
|
162
|
-
* @throws {RetryError} When retries are exhausted and `onExhausted` returns
|
|
163
|
-
* the terminal error.
|
|
164
|
-
* @throws {AbortError} When `signal` is already aborted or aborts while waiting.
|
|
165
|
-
* @throws {Error} Any error thrown by `next`, `onExhausted`, or `sleep`.
|
|
166
|
-
*/
|
|
167
|
-
const runThrowMode = async (policy, execute, sleep, signal) => {
|
|
168
|
-
let attempt = 1;
|
|
169
|
-
while (true) {
|
|
170
|
-
throwIfAborted(signal);
|
|
171
|
-
try {
|
|
172
|
-
return await execute(attempt);
|
|
173
|
-
} catch (error) {
|
|
174
|
-
throwIfAborted(signal);
|
|
175
|
-
const typedError = error;
|
|
176
|
-
const decision = policy.next({
|
|
177
|
-
attempt,
|
|
178
|
-
error: typedError
|
|
179
|
-
});
|
|
180
|
-
if (!decision.shouldRetry) throw policy.onExhausted({
|
|
181
|
-
attempts: attempt,
|
|
182
|
-
error: typedError
|
|
183
|
-
});
|
|
184
|
-
if (decision.delayMs > 0) await (signal === void 0 ? sleep(decision.delayMs) : sleepWithAbortSignal(sleep, decision.delayMs, signal));
|
|
185
|
-
attempt += 1;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
};
|
|
189
|
-
//#endregion
|
|
190
|
-
//#region src/base-policy.ts
|
|
191
|
-
/**
|
|
192
|
-
* Retry runner base class and shared orchestration implementation.
|
|
193
|
-
*
|
|
194
|
-
* @module @zap-studio/retry/base-policy
|
|
195
|
-
*/
|
|
196
|
-
/**
|
|
197
|
-
* Base class for implementing retry policies and running retry orchestration.
|
|
198
|
-
*
|
|
199
|
-
* Extend this class and implement {@link BaseRetryPolicy.next} to define retry
|
|
200
|
-
* behavior, then call {@link BaseRetryPolicy.run} to execute operations with that
|
|
201
|
-
* policy.
|
|
202
|
-
*/
|
|
203
|
-
var BaseRetryPolicy = class {
|
|
204
|
-
/**
|
|
205
|
-
* Builds the terminal error thrown or returned when retries are exhausted.
|
|
206
|
-
*
|
|
207
|
-
* Override this when you need custom terminal error types.
|
|
208
|
-
*
|
|
209
|
-
* @param input - Exhaustion context.
|
|
210
|
-
* @returns `RetryError` by default.
|
|
211
|
-
* @throws {Error} Any error thrown by an overriding policy implementation.
|
|
212
|
-
*/
|
|
213
|
-
onExhausted(input) {
|
|
214
|
-
return new RetryError("Retry policy exhausted all attempts.", {
|
|
215
|
-
attempts: input.attempts,
|
|
216
|
-
lastData: input.data,
|
|
217
|
-
lastError: input.error
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
/**
|
|
221
|
-
* Runs retry orchestration in non-throw mode.
|
|
222
|
-
*
|
|
223
|
-
* When `throwOnExhausted` is `false`, returns a discriminated result union.
|
|
224
|
-
*
|
|
225
|
-
* @param execute - Async function to execute per attempt.
|
|
226
|
-
* @param options - Runner settings.
|
|
227
|
-
* @returns Success value or terminal result object based on option mode.
|
|
228
|
-
* @throws {Error} Any error thrown by `next`, by `onExhausted`, or by a custom `sleep`
|
|
229
|
-
* function. When `throwOnExhausted` is `false`, exhaustion itself is returned
|
|
230
|
-
* as `{ ok: false }` instead of thrown.
|
|
231
|
-
* Cancellation is returned as `{ ok: false, error: AbortError }` in non-throw
|
|
232
|
-
* mode.
|
|
233
|
-
*
|
|
234
|
-
* @example
|
|
235
|
-
* const result = await policy.run(doWork, { throwOnExhausted: false });
|
|
236
|
-
* if (!result.ok) console.error(result.error);
|
|
237
|
-
*/
|
|
238
|
-
async run(execute, options = {}) {
|
|
239
|
-
const sleep = options.sleep ?? defaultSleep;
|
|
240
|
-
const { signal } = options;
|
|
241
|
-
if (options.throwOnExhausted === false) return await runResultMode(this, execute, sleep, signal);
|
|
242
|
-
return await runThrowMode(this, execute, sleep, signal);
|
|
243
|
-
}
|
|
244
|
-
};
|
|
245
|
-
//#endregion
|
|
246
|
-
export { BaseRetryPolicy as t };
|
|
247
|
-
|
|
248
|
-
//# sourceMappingURL=base-policy-Dn3TOJd3.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"base-policy-Dn3TOJd3.js","names":[],"sources":["../src/_result-mode.ts","../src/_throw-mode.ts","../src/base-policy.ts"],"sourcesContent":["/**\n * Result-mode execution path for `BaseRetryPolicy.run` when\n * `throwOnExhausted: false` is set.\n *\n * @module @zap-studio/retry/_result-mode (private)\n */\n\nimport { sleepWithAbortSignal, toAbortError } from \"./abort.js\";\nimport type { RetryPolicy, RetryRunResult } from \"./types.js\";\n\n/**\n * When `signal` is already aborted, builds the terminal `{ ok: false }` object\n * with a normalized `AbortError` on `error`.\n *\n * @param signal - Optional abort signal; only acts when `aborted` is set.\n * @param attempts - Number of finished attempts to report in the result.\n * @returns Failure result or `undefined` if not aborted.\n */\nconst buildAbortResult = (\n signal: AbortSignal | undefined,\n attempts: number\n): RetryRunResult<never> | undefined => {\n if (signal?.aborted !== true) {\n return undefined;\n }\n\n return {\n attempts,\n error: toAbortError(signal.reason),\n ok: false,\n };\n};\n\n/**\n * Runs one `execute(attempt)` call and returns either a success value or a\n * captured error without rethrowing.\n *\n * @param execute - User work callback.\n * @param attempt - One-based attempt number passed to `execute`.\n * @returns A tagged success with `value` or a tagged failure with `error`.\n */\nconst runAttempt = async <T>(\n execute: (attempt: number) => Promise<T>,\n attempt: number\n): Promise<{ ok: true; value: T } | { ok: false; error: unknown }> => {\n try {\n return {\n ok: true,\n value: await execute(attempt),\n };\n } catch (error) {\n return {\n error,\n ok: false,\n };\n }\n};\n\n/**\n * Awaits inter-attempt delay in result mode, mapping an abort during wait to\n * a terminal result instead of throwing when `throwOnExhausted` is false.\n *\n * @param sleep - Custom or default sleep implementation.\n * @param delayMs - Milliseconds to wait.\n * @param signal - If set, `sleep` is raced with the abort signal.\n * @param attempts - Attempt count to attach if the wait ends in abort.\n * @returns A terminal result when canceled during the wait, otherwise\n * `undefined`.\n * @throws {Error} The underlying `sleep` rejection when it is not an abort.\n */\nconst waitForDelay = async (\n sleep: (delayMs: number) => Promise<void>,\n delayMs: number,\n signal: AbortSignal | undefined,\n attempts: number\n): Promise<RetryRunResult<never> | undefined> => {\n if (signal === undefined) {\n await sleep(delayMs);\n return undefined;\n }\n\n try {\n await sleepWithAbortSignal(sleep, delayMs, signal);\n return undefined;\n } catch (error) {\n const aborted = buildAbortResult(signal, attempts);\n if (aborted !== undefined) {\n return aborted;\n }\n throw error;\n }\n};\n\n/**\n * After a failed attempt, applies abort rules, `next`, optional delay, and\n * either returns a terminal `RetryRunResult` or `undefined` to continue.\n *\n * @param policy - Retry policy hooks (`next`, `onExhausted`) matching\n * `BaseRetryPolicy`.\n * @param params - Failure context for the current attempt.\n * @param params.attempt - Current attempt number.\n * @param params.error - Error thrown by the attempt.\n * @param params.sleep - Delay function between retries.\n * @param params.signal - Optional abort signal.\n * @returns Terminal non-throw result if the loop should stop, otherwise\n * `undefined` to schedule another attempt.\n * @throws {Error} Any error thrown by `next`, `onExhausted`, or a custom `sleep` when\n * the error is not an abort.\n */\nconst handleFailure = async <TError, TData>(\n policy: RetryPolicy<TError, TData>,\n params: {\n attempt: number;\n error: TError;\n sleep: (delayMs: number) => Promise<void>;\n signal: AbortSignal | undefined;\n }\n): Promise<RetryRunResult<never> | undefined> => {\n const { attempt, error, sleep, signal } = params;\n const abortResult = buildAbortResult(signal, attempt);\n if (abortResult !== undefined) {\n return abortResult;\n }\n\n const decision = policy.next({\n attempt,\n error,\n });\n\n if (!decision.shouldRetry) {\n const terminalError = policy.onExhausted({\n attempts: attempt,\n error,\n });\n\n return {\n attempts: attempt,\n error: terminalError,\n ok: false,\n };\n }\n\n if (decision.delayMs > 0) {\n const delayAbortResult = await waitForDelay(\n sleep,\n decision.delayMs,\n signal,\n attempt\n );\n if (delayAbortResult !== undefined) {\n return delayAbortResult;\n }\n }\n\n return undefined;\n};\n\n/**\n * Runs the non-throw retry loop, returning\n * `RetryRunResult`.\n *\n * @param policy - Object providing `next` and `onExhausted` (same contract as\n * `BaseRetryPolicy`).\n * @param execute - Async work callback per attempt.\n * @param sleep - Delay function between retries.\n * @param signal - Optional cancel signal.\n * @returns Terminal success or failure object.\n * @throws {Error} Any error thrown by `next`, `onExhausted`, or a non-abort `sleep`\n * failure.\n */\nexport const runResultMode = async <T, TError, TData>(\n policy: RetryPolicy<TError, TData>,\n execute: (attempt: number) => Promise<T>,\n sleep: (delayMs: number) => Promise<void>,\n signal?: AbortSignal\n): Promise<RetryRunResult<T>> => {\n let attempt = 1;\n\n while (true) {\n const abortResult = buildAbortResult(signal, Math.max(0, attempt - 1));\n if (abortResult !== undefined) {\n return abortResult;\n }\n\n // oxlint-disable-next-line no-await-in-loop -- Retry attempts must run sequentially.\n const execution = await runAttempt(execute, attempt);\n if (execution.ok) {\n return { ok: true, value: execution.value };\n }\n\n // oxlint-disable-next-line no-await-in-loop -- Failure handling belongs to the current sequential attempt.\n const failure = await handleFailure(policy, {\n attempt,\n // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Policy error generic represents the caller's thrown error domain.\n error: execution.error as TError,\n signal,\n sleep,\n });\n if (failure !== undefined) {\n return failure;\n }\n\n attempt += 1;\n }\n};\n","/**\n * Throw-mode execution path for `BaseRetryPolicy.run` (default when\n * `throwOnExhausted` is not `false`).\n *\n * @module @zap-studio/retry/_throw-mode (private)\n */\n\nimport { sleepWithAbortSignal, throwIfAborted } from \"./abort.js\";\nimport type { RetryPolicy } from \"./types.js\";\n\n/**\n * Runs the throw-mode retry loop: throws `RetryError` on exhaustion and\n * `AbortError` when `signal` aborts.\n *\n * @param policy - Object providing `next` and `onExhausted` (same contract as\n * `BaseRetryPolicy`).\n * @param execute - Async work callback per attempt.\n * @param sleep - Delay function between retries.\n * @param signal - Optional cancel signal.\n * @returns Resolves to the first successful return value.\n * @throws {RetryError} When retries are exhausted and `onExhausted` returns\n * the terminal error.\n * @throws {AbortError} When `signal` is already aborted or aborts while waiting.\n * @throws {Error} Any error thrown by `next`, `onExhausted`, or `sleep`.\n */\nexport const runThrowMode = async <T, TError, TData>(\n policy: RetryPolicy<TError, TData>,\n execute: (attempt: number) => Promise<T>,\n sleep: (delayMs: number) => Promise<void>,\n signal?: AbortSignal\n): Promise<T> => {\n let attempt = 1;\n\n while (true) {\n throwIfAborted(signal);\n\n try {\n // oxlint-disable-next-line no-await-in-loop -- Retry attempts must run sequentially.\n return await execute(attempt);\n } catch (error) {\n throwIfAborted(signal);\n\n // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Policy error generic represents the caller's thrown error domain.\n const typedError = error as TError;\n const decision = policy.next({\n attempt,\n error: typedError,\n });\n\n if (!decision.shouldRetry) {\n throw policy.onExhausted({\n attempts: attempt,\n error: typedError,\n });\n }\n\n if (decision.delayMs > 0) {\n // oxlint-disable-next-line no-await-in-loop -- Delay belongs between sequential retry attempts.\n await (signal === undefined\n ? sleep(decision.delayMs)\n : sleepWithAbortSignal(sleep, decision.delayMs, signal));\n }\n\n attempt += 1;\n }\n }\n};\n","/**\n * Retry runner base class and shared orchestration implementation.\n *\n * @module @zap-studio/retry/base-policy\n */\n\nimport { runResultMode } from \"./_result-mode.js\";\nimport { runThrowMode } from \"./_throw-mode.js\";\nimport { RetryError } from \"./errors.js\";\nimport { defaultSleep } from \"./sleep.js\";\nimport type {\n RetryDecision,\n RetryDecisionInput,\n RetryExhaustedInput,\n RetryPolicy,\n RetryRunOptions,\n RetryRunResult,\n} from \"./types.js\";\n\n/**\n * Base class for implementing retry policies and running retry orchestration.\n *\n * Extend this class and implement {@link BaseRetryPolicy.next} to define retry\n * behavior, then call {@link BaseRetryPolicy.run} to execute operations with that\n * policy.\n */\nexport abstract class BaseRetryPolicy<\n TError = unknown,\n TData = unknown,\n> implements RetryPolicy<TError, TData> {\n /**\n * Returns the retry decision for a failed attempt.\n *\n * @param input - Attempt context used to compute retry behavior.\n * @throws {Error} Any error thrown by a concrete retry policy implementation.\n */\n public abstract next(input: RetryDecisionInput<TError, TData>): RetryDecision;\n\n /**\n * Builds the terminal error thrown or returned when retries are exhausted.\n *\n * Override this when you need custom terminal error types.\n *\n * @param input - Exhaustion context.\n * @returns `RetryError` by default.\n * @throws {Error} Any error thrown by an overriding policy implementation.\n */\n // oxlint-disable-next-line class-methods-use-this -- RetryPolicy requires an instance hook that subclasses may override.\n public onExhausted(input: RetryExhaustedInput<TError, TData>): RetryError {\n return new RetryError(\"Retry policy exhausted all attempts.\", {\n attempts: input.attempts,\n lastData: input.data,\n lastError: input.error,\n });\n }\n\n /**\n * Runs retry orchestration in non-throw mode.\n *\n * @param execute - Async function to execute per attempt.\n * @param options - Runner settings with `throwOnExhausted: false`.\n * @returns A discriminated result union containing success value or terminal error.\n * @throws {Error} Any error thrown by `next`, `onExhausted`, or a custom `sleep`.\n */\n public async run<T>(\n execute: (attempt: number) => Promise<T>,\n options: RetryRunOptions & { throwOnExhausted: false }\n ): Promise<RetryRunResult<T>>;\n\n /**\n * Runs retry orchestration and throws terminal error on exhaustion.\n *\n * @param execute - Async function to execute per attempt.\n * @param options - Optional runner settings.\n * @returns The successful execution value.\n * @throws {RetryError} When retries are exhausted and `onExhausted` returns the\n * terminal retry error. The default implementation returns `RetryError` with the last\n * execution failure available on `RetryError.lastError`.\n * @throws {AbortError} When `options.signal` is already aborted or aborts while retrying.\n * @throws {Error} Any error thrown by `next`, by `onExhausted`, or by a custom `sleep`\n * function.\n */\n public async run<T>(\n execute: (attempt: number) => Promise<T>,\n options?: RetryRunOptions & { throwOnExhausted?: true }\n ): Promise<T>;\n\n /**\n * Runs retry orchestration in non-throw mode.\n *\n * When `throwOnExhausted` is `false`, returns a discriminated result union.\n *\n * @param execute - Async function to execute per attempt.\n * @param options - Runner settings.\n * @returns Success value or terminal result object based on option mode.\n * @throws {Error} Any error thrown by `next`, by `onExhausted`, or by a custom `sleep`\n * function. When `throwOnExhausted` is `false`, exhaustion itself is returned\n * as `{ ok: false }` instead of thrown.\n * Cancellation is returned as `{ ok: false, error: AbortError }` in non-throw\n * mode.\n *\n * @example\n * const result = await policy.run(doWork, { throwOnExhausted: false });\n * if (!result.ok) console.error(result.error);\n */\n public async run<T>(\n execute: (attempt: number) => Promise<T>,\n options: RetryRunOptions = {}\n ): Promise<T | RetryRunResult<T>> {\n const sleep = options.sleep ?? defaultSleep;\n const { signal } = options;\n if (options.throwOnExhausted === false) {\n return await runResultMode(this, execute, sleep, signal);\n }\n\n return await runThrowMode(this, execute, sleep, signal);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAkBA,MAAM,oBACJ,QACA,aACsC;CACtC,IAAI,QAAQ,YAAY,MACtB;CAGF,OAAO;EACL;EACA,OAAO,aAAa,OAAO,MAAM;EACjC,IAAI;CACN;AACF;;;;;;;;;AAUA,MAAM,aAAa,OACjB,SACA,YACoE;CACpE,IAAI;EACF,OAAO;GACL,IAAI;GACJ,OAAO,MAAM,QAAQ,OAAO;EAC9B;CACF,SAAS,OAAO;EACd,OAAO;GACL;GACA,IAAI;EACN;CACF;AACF;;;;;;;;;;;;;AAcA,MAAM,eAAe,OACnB,OACA,SACA,QACA,aAC+C;CAC/C,IAAI,WAAW,KAAA,GAAW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|
|
1
|
-
//#region src/sleep.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* Default delay implementation used by `BaseRetryPolicy.run` when no custom
|
|
4
|
-
* `sleep` is provided.
|
|
5
|
-
*
|
|
6
|
-
* @module @zap-studio/retry/sleep
|
|
7
|
-
*/
|
|
8
|
-
/**
|
|
9
|
-
* Awaits a timer-based delay, unless `delayMs` is non-positive.
|
|
10
|
-
*
|
|
11
|
-
* @param delayMs - Milliseconds to wait before resolving.
|
|
12
|
-
* @returns Promise that resolves when the delay completes.
|
|
13
|
-
*/
|
|
14
|
-
declare const defaultSleep: (delayMs: number) => Promise<void>;
|
|
15
|
-
//#endregion
|
|
16
|
-
export { defaultSleep };
|
|
17
|
-
//# sourceMappingURL=sleep.d.ts.map
|
package/dist/sleep.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"sleep.d.ts","names":[],"sources":["../src/sleep.ts"],"mappings":";;;;;;;;;;;;;cAaa,eAAsB,oBAAkB"}
|
package/dist/sleep.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
//#region src/sleep.ts
|
|
2
|
-
/**
|
|
3
|
-
* Default delay implementation used by `BaseRetryPolicy.run` when no custom
|
|
4
|
-
* `sleep` is provided.
|
|
5
|
-
*
|
|
6
|
-
* @module @zap-studio/retry/sleep
|
|
7
|
-
*/
|
|
8
|
-
/**
|
|
9
|
-
* Awaits a timer-based delay, unless `delayMs` is non-positive.
|
|
10
|
-
*
|
|
11
|
-
* @param delayMs - Milliseconds to wait before resolving.
|
|
12
|
-
* @returns Promise that resolves when the delay completes.
|
|
13
|
-
*/
|
|
14
|
-
const defaultSleep = async (delayMs) => {
|
|
15
|
-
if (delayMs <= 0) return;
|
|
16
|
-
await new Promise((resolve) => {
|
|
17
|
-
setTimeout(resolve, delayMs);
|
|
18
|
-
});
|
|
19
|
-
};
|
|
20
|
-
//#endregion
|
|
21
|
-
export { defaultSleep };
|
|
22
|
-
|
|
23
|
-
//# sourceMappingURL=sleep.js.map
|
package/dist/sleep.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"sleep.js","names":[],"sources":["../src/sleep.ts"],"sourcesContent":["/**\n * Default delay implementation used by `BaseRetryPolicy.run` when no custom\n * `sleep` is provided.\n *\n * @module @zap-studio/retry/sleep\n */\n\n/**\n * Awaits a timer-based delay, unless `delayMs` is non-positive.\n *\n * @param delayMs - Milliseconds to wait before resolving.\n * @returns Promise that resolves when the delay completes.\n */\nexport const defaultSleep = async (delayMs: number): Promise<void> => {\n if (delayMs <= 0) {\n return;\n }\n\n // oxlint-disable-next-line promise/avoid-new -- Timer sleep requires adapting callback API to a promise.\n await new Promise<void>((resolve) => {\n setTimeout(resolve, delayMs);\n });\n};\n"],"mappings":";;;;;;;;;;;;;AAaA,MAAa,eAAe,OAAO,YAAmC;CACpE,IAAI,WAAW,GACb;CAIF,MAAM,IAAI,SAAe,YAAY;EACnC,WAAW,SAAS,OAAO;CAC7B,CAAC;AACH"}
|