@zap-studio/retry 0.3.1 → 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.
Files changed (61) hide show
  1. package/CHANGELOG.md +55 -14
  2. package/README.md +87 -141
  3. package/dist/base-policy.d.ts +67 -0
  4. package/dist/base-policy.d.ts.map +1 -0
  5. package/dist/base-policy.js +306 -0
  6. package/dist/base-policy.js.map +1 -0
  7. package/dist/{errors-BVZjP1Q5.d.mts → errors-CS5UPJWs.d.ts} +25 -1
  8. package/dist/errors-CS5UPJWs.d.ts.map +1 -0
  9. package/dist/{errors.d.mts → errors.d.ts} +1 -1
  10. package/dist/{errors.mjs → errors.js} +19 -1
  11. package/dist/errors.js.map +1 -0
  12. package/dist/exponential-backoff.d.ts +40 -0
  13. package/dist/exponential-backoff.d.ts.map +1 -0
  14. package/dist/exponential-backoff.js +35 -0
  15. package/dist/exponential-backoff.js.map +1 -0
  16. package/dist/fixed-delay.d.ts +31 -0
  17. package/dist/fixed-delay.d.ts.map +1 -0
  18. package/dist/fixed-delay.js +33 -0
  19. package/dist/fixed-delay.js.map +1 -0
  20. package/dist/index.d.ts +7 -0
  21. package/dist/index.js +6 -0
  22. package/dist/linear-backoff.d.ts +46 -0
  23. package/dist/linear-backoff.d.ts.map +1 -0
  24. package/dist/linear-backoff.js +35 -0
  25. package/dist/linear-backoff.js.map +1 -0
  26. package/dist/{types.d.mts → types.d.ts} +52 -8
  27. package/dist/types.d.ts.map +1 -0
  28. package/dist/types.js +0 -0
  29. package/package.json +12 -22
  30. package/dist/abort.d.mts +0 -29
  31. package/dist/abort.d.mts.map +0 -1
  32. package/dist/abort.mjs +0 -61
  33. package/dist/abort.mjs.map +0 -1
  34. package/dist/errors-BVZjP1Q5.d.mts.map +0 -1
  35. package/dist/errors.mjs.map +0 -1
  36. package/dist/exponential-backoff.d.mts +0 -55
  37. package/dist/exponential-backoff.d.mts.map +0 -1
  38. package/dist/exponential-backoff.mjs +0 -60
  39. package/dist/exponential-backoff.mjs.map +0 -1
  40. package/dist/fixed-delay.d.mts +0 -46
  41. package/dist/fixed-delay.d.mts.map +0 -1
  42. package/dist/fixed-delay.mjs +0 -53
  43. package/dist/fixed-delay.mjs.map +0 -1
  44. package/dist/index.d.mts +0 -59
  45. package/dist/index.d.mts.map +0 -1
  46. package/dist/index.mjs +0 -63
  47. package/dist/index.mjs.map +0 -1
  48. package/dist/result-mode.d.mts +0 -19
  49. package/dist/result-mode.d.mts.map +0 -1
  50. package/dist/result-mode.mjs +0 -145
  51. package/dist/result-mode.mjs.map +0 -1
  52. package/dist/sleep.d.mts +0 -17
  53. package/dist/sleep.d.mts.map +0 -1
  54. package/dist/sleep.mjs +0 -23
  55. package/dist/sleep.mjs.map +0 -1
  56. package/dist/throw-mode.d.mts +0 -21
  57. package/dist/throw-mode.d.mts.map +0 -1
  58. package/dist/throw-mode.mjs +0 -49
  59. package/dist/throw-mode.mjs.map +0 -1
  60. package/dist/types.d.mts.map +0 -1
  61. package/dist/types.mjs +0 -1
package/dist/index.d.mts DELETED
@@ -1,59 +0,0 @@
1
- import { r as RetryError } from "./errors-BVZjP1Q5.mjs";
2
- import { RetryDecision, RetryDecisionInput, RetryExhaustedInput, RetryPolicy, RetryRunOptions, RetryRunResult } from "./types.mjs";
3
- //#region src/index.d.ts
4
- /**
5
- * Base class for implementing retry policies and running retry orchestration.
6
- *
7
- * Extend this class and implement {@link BaseRetryPolicy.next} to define retry
8
- * behavior, then call {@link BaseRetryPolicy.run} to execute operations with that
9
- * policy.
10
- */
11
- declare abstract class BaseRetryPolicy<TError = unknown, TData = unknown> implements RetryPolicy<TError, TData> {
12
- /**
13
- * Returns the retry decision for a failed attempt.
14
- *
15
- * @param input - Attempt context used to compute retry behavior.
16
- * @throws {Error} Any error thrown by a concrete retry policy implementation.
17
- */
18
- abstract next(input: RetryDecisionInput<TError, TData>): RetryDecision;
19
- /**
20
- * Builds the terminal error thrown or returned when retries are exhausted.
21
- *
22
- * Override this when you need custom terminal error types.
23
- *
24
- * @param input - Exhaustion context.
25
- * @returns `RetryError` by default.
26
- * @throws {Error} Any error thrown by an overriding policy implementation.
27
- */
28
- onExhausted(input: RetryExhaustedInput<TError, TData>): RetryError;
29
- /**
30
- * Runs retry orchestration in non-throw mode.
31
- *
32
- * @param execute - Async function to execute per attempt.
33
- * @param options - Runner settings with `throwOnExhausted: false`.
34
- * @returns A discriminated result union containing success value or terminal error.
35
- * @throws {Error} Any error thrown by `next`, `onExhausted`, or a custom `sleep`.
36
- */
37
- run<T>(execute: (attempt: number) => Promise<T>, options: RetryRunOptions & {
38
- throwOnExhausted: false;
39
- }): Promise<RetryRunResult<T>>;
40
- /**
41
- * Runs retry orchestration and throws terminal error on exhaustion.
42
- *
43
- * @param execute - Async function to execute per attempt.
44
- * @param options - Optional runner settings.
45
- * @returns The successful execution value.
46
- * @throws {RetryError} When retries are exhausted and `onExhausted` returns the
47
- * terminal retry error. The default implementation returns `RetryError` with the last
48
- * execution failure available on `RetryError.lastError`.
49
- * @throws {AbortError} When `options.signal` is already aborted or aborts while retrying.
50
- * @throws {Error} Any error thrown by `next`, by `onExhausted`, or by a custom `sleep`
51
- * function.
52
- */
53
- run<T>(execute: (attempt: number) => Promise<T>, options?: RetryRunOptions & {
54
- throwOnExhausted?: true;
55
- }): Promise<T>;
56
- }
57
- //#endregion
58
- export { BaseRetryPolicy };
59
- //# sourceMappingURL=index.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;;;;;;uBA0BsB,gBACpB,kBACA,4BACW,YAAY,QAAQ;;;;;;;WAOf,KAAK,OAAO,mBAAmB,QAAQ,SAAS;;;;;;;;;;EAYhE,YAAmB,OAAO,oBAAoB,QAAQ,SAAS;;;;;;;;;EAgB/D,IAAiB,GACf,UAAU,oBAAoB,QAAQ,IACtC,SAAS;IAAoB;MAC5B,QAAQ,eAAe;;;;;;;;;;;;;;EAe1B,IAAiB,GACf,UAAU,oBAAoB,QAAQ,IACtC,UAAU;IAAoB;MAC7B,QAAQ"}
package/dist/index.mjs DELETED
@@ -1,63 +0,0 @@
1
- import { RetryError } from "./errors.mjs";
2
- import { runResultMode } from "./result-mode.mjs";
3
- import { defaultSleep } from "./sleep.mjs";
4
- import { runThrowMode } from "./throw-mode.mjs";
5
- //#region src/index.ts
6
- /**
7
- * Retry runner base class and shared orchestration implementation.
8
- *
9
- * @module @zap-studio/retry
10
- */
11
- /**
12
- * Base class for implementing retry policies and running retry orchestration.
13
- *
14
- * Extend this class and implement {@link BaseRetryPolicy.next} to define retry
15
- * behavior, then call {@link BaseRetryPolicy.run} to execute operations with that
16
- * policy.
17
- */
18
- var BaseRetryPolicy = class {
19
- /**
20
- * Builds the terminal error thrown or returned when retries are exhausted.
21
- *
22
- * Override this when you need custom terminal error types.
23
- *
24
- * @param input - Exhaustion context.
25
- * @returns `RetryError` by default.
26
- * @throws {Error} Any error thrown by an overriding policy implementation.
27
- */
28
- onExhausted(input) {
29
- return new RetryError("Retry policy exhausted all attempts.", {
30
- attempts: input.attempts,
31
- lastData: input.data,
32
- lastError: input.error
33
- });
34
- }
35
- /**
36
- * Runs retry orchestration in non-throw mode.
37
- *
38
- * When `throwOnExhausted` is `false`, returns a discriminated result union.
39
- *
40
- * @param execute - Async function to execute per attempt.
41
- * @param options - Runner settings.
42
- * @returns Success value or terminal result object based on option mode.
43
- * @throws {Error} Any error thrown by `next`, by `onExhausted`, or by a custom `sleep`
44
- * function. When `throwOnExhausted` is `false`, exhaustion itself is returned
45
- * as `{ ok: false }` instead of thrown.
46
- * Cancellation is returned as `{ ok: false, error: AbortError }` in non-throw
47
- * mode.
48
- *
49
- * @example
50
- * const result = await policy.run(doWork, { throwOnExhausted: false });
51
- * if (!result.ok) console.error(result.error);
52
- */
53
- async run(execute, options = {}) {
54
- const sleep = options.sleep ?? defaultSleep;
55
- const { signal } = options;
56
- if (options.throwOnExhausted === false) return await runResultMode(this, execute, sleep, signal);
57
- return await runThrowMode(this, execute, sleep, signal);
58
- }
59
- };
60
- //#endregion
61
- export { BaseRetryPolicy };
62
-
63
- //# sourceMappingURL=index.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["/**\n * Retry runner base class and shared orchestration implementation.\n *\n * @module @zap-studio/retry\n */\n\nimport { RetryError } from \"./errors.js\";\nimport { runResultMode } from \"./result-mode.js\";\nimport { defaultSleep } from \"./sleep.js\";\nimport { runThrowMode } from \"./throw-mode.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":";;;;;;;;;;;;;;;;;AA0BA,IAAsB,kBAAtB,MAGwC;;;;;;;;;;CAmBtC,YAAmB,OAAuD;EACxE,OAAO,IAAI,WAAW,wCAAwC;GAC5D,UAAU,MAAM;GAChB,UAAU,MAAM;GAChB,WAAW,MAAM;EACnB,CAAC;CACH;;;;;;;;;;;;;;;;;;;CAmDA,MAAa,IACX,SACA,UAA2B,CAAC,GACI;EAChC,MAAM,QAAQ,QAAQ,SAAS;EAC/B,MAAM,EAAE,WAAW;EACnB,IAAI,QAAQ,qBAAqB,OAC/B,OAAO,MAAM,cAAc,MAAM,SAAS,OAAO,MAAM;EAGzD,OAAO,MAAM,aAAa,MAAM,SAAS,OAAO,MAAM;CACxD;AACF"}
@@ -1,19 +0,0 @@
1
- import { RetryPolicy, RetryRunResult } from "./types.mjs";
2
- //#region src/result-mode.d.ts
3
- /**
4
- * Runs the non-throw retry loop, returning
5
- * `RetryRunResult`.
6
- *
7
- * @param policy - Object providing `next` and `onExhausted` (same contract as
8
- * `BaseRetryPolicy`).
9
- * @param execute - Async work callback per attempt.
10
- * @param sleep - Delay function between retries.
11
- * @param signal - Optional cancel signal.
12
- * @returns Terminal success or failure object.
13
- * @throws {Error} Any error thrown by `next`, `onExhausted`, or a non-abort `sleep`
14
- * failure.
15
- */
16
- declare const runResultMode: <T, TError, TData>(policy: RetryPolicy<TError, TData>, execute: (attempt: number) => Promise<T>, sleep: (delayMs: number) => Promise<void>, signal?: AbortSignal) => Promise<RetryRunResult<T>>;
17
- //#endregion
18
- export { runResultMode };
19
- //# sourceMappingURL=result-mode.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"result-mode.d.mts","names":[],"sources":["../src/result-mode.ts"],"mappings":";;;;;;;;;;;;;;;cA0Ka,gBAAuB,GAAG,QAAQ,OAC7C,QAAQ,YAAY,QAAQ,QAC5B,UAAU,oBAAoB,QAAQ,IACtC,QAAQ,oBAAoB,eAC5B,SAAS,gBACR,QAAQ,eAAe"}
@@ -1,145 +0,0 @@
1
- import { sleepWithAbortSignal, toAbortError } from "./abort.mjs";
2
- //#region src/result-mode.ts
3
- /**
4
- * Result-mode execution path for `BaseRetryPolicy.run` when
5
- * `throwOnExhausted: false` is set.
6
- *
7
- * @module @zap-studio/retry/result-mode
8
- */
9
- /**
10
- * When `signal` is already aborted, builds the terminal `{ ok: false }` object
11
- * with a normalized `AbortError` on `error`.
12
- *
13
- * @param signal - Optional abort signal; only acts when `aborted` is set.
14
- * @param attempts - Number of finished attempts to report in the result.
15
- * @returns Failure result or `undefined` if not aborted.
16
- */
17
- const buildAbortResult = (signal, attempts) => {
18
- if (signal?.aborted !== true) return;
19
- return {
20
- attempts,
21
- error: toAbortError(signal.reason),
22
- ok: false
23
- };
24
- };
25
- /**
26
- * Runs one `execute(attempt)` call and returns either a success value or a
27
- * captured error without rethrowing.
28
- *
29
- * @param execute - User work callback.
30
- * @param attempt - One-based attempt number passed to `execute`.
31
- * @returns A tagged success with `value` or a tagged failure with `error`.
32
- */
33
- const runAttempt = async (execute, attempt) => {
34
- try {
35
- return {
36
- ok: true,
37
- value: await execute(attempt)
38
- };
39
- } catch (error) {
40
- return {
41
- error,
42
- ok: false
43
- };
44
- }
45
- };
46
- /**
47
- * Awaits inter-attempt delay in result mode, mapping an abort during wait to
48
- * a terminal result instead of throwing when `throwOnExhausted` is false.
49
- *
50
- * @param sleep - Custom or default sleep implementation.
51
- * @param delayMs - Milliseconds to wait.
52
- * @param signal - If set, `sleep` is raced with the abort signal.
53
- * @param attempts - Attempt count to attach if the wait ends in abort.
54
- * @returns A terminal result when canceled during the wait, otherwise
55
- * `undefined`.
56
- * @throws {Error} The underlying `sleep` rejection when it is not an abort.
57
- */
58
- const waitForDelay = async (sleep, delayMs, signal, attempts) => {
59
- if (signal === void 0) {
60
- await sleep(delayMs);
61
- return;
62
- }
63
- try {
64
- await sleepWithAbortSignal(sleep, delayMs, signal);
65
- return;
66
- } catch (error) {
67
- const aborted = buildAbortResult(signal, attempts);
68
- if (aborted !== void 0) return aborted;
69
- throw error;
70
- }
71
- };
72
- /**
73
- * After a failed attempt, applies abort rules, `next`, optional delay, and
74
- * either returns a terminal `RetryRunResult` or `undefined` to continue.
75
- *
76
- * @param policy - Retry policy hooks (`next`, `onExhausted`) matching
77
- * `BaseRetryPolicy`.
78
- * @param params - Failure context for the current attempt.
79
- * @param params.attempt - Current attempt number.
80
- * @param params.error - Error thrown by the attempt.
81
- * @param params.sleep - Delay function between retries.
82
- * @param params.signal - Optional abort signal.
83
- * @returns Terminal non-throw result if the loop should stop, otherwise
84
- * `undefined` to schedule another attempt.
85
- * @throws {Error} Any error thrown by `next`, `onExhausted`, or a custom `sleep` when
86
- * the error is not an abort.
87
- */
88
- const handleFailure = async (policy, params) => {
89
- const { attempt, error, sleep, signal } = params;
90
- const abortResult = buildAbortResult(signal, attempt);
91
- if (abortResult !== void 0) return abortResult;
92
- const decision = policy.next({
93
- attempt,
94
- error
95
- });
96
- if (!decision.shouldRetry) return {
97
- attempts: attempt,
98
- error: policy.onExhausted({
99
- attempts: attempt,
100
- error
101
- }),
102
- ok: false
103
- };
104
- if (decision.delayMs > 0) {
105
- const delayAbortResult = await waitForDelay(sleep, decision.delayMs, signal, attempt);
106
- if (delayAbortResult !== void 0) return delayAbortResult;
107
- }
108
- };
109
- /**
110
- * Runs the non-throw retry loop, returning
111
- * `RetryRunResult`.
112
- *
113
- * @param policy - Object providing `next` and `onExhausted` (same contract as
114
- * `BaseRetryPolicy`).
115
- * @param execute - Async work callback per attempt.
116
- * @param sleep - Delay function between retries.
117
- * @param signal - Optional cancel signal.
118
- * @returns Terminal success or failure object.
119
- * @throws {Error} Any error thrown by `next`, `onExhausted`, or a non-abort `sleep`
120
- * failure.
121
- */
122
- const runResultMode = async (policy, execute, sleep, signal) => {
123
- let attempt = 1;
124
- while (true) {
125
- const abortResult = buildAbortResult(signal, Math.max(0, attempt - 1));
126
- if (abortResult !== void 0) return abortResult;
127
- const execution = await runAttempt(execute, attempt);
128
- if (execution.ok) return {
129
- ok: true,
130
- value: execution.value
131
- };
132
- const failure = await handleFailure(policy, {
133
- attempt,
134
- error: execution.error,
135
- signal,
136
- sleep
137
- });
138
- if (failure !== void 0) return failure;
139
- attempt += 1;
140
- }
141
- };
142
- //#endregion
143
- export { runResultMode };
144
-
145
- //# sourceMappingURL=result-mode.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"result-mode.mjs","names":[],"sources":["../src/result-mode.ts"],"sourcesContent":["/**\n * Result-mode execution path for `BaseRetryPolicy.run` when\n * `throwOnExhausted: false` is set.\n *\n * @module @zap-studio/retry/result-mode\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"],"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;EACxB,MAAM,MAAM,OAAO;EACnB;CACF;CAEA,IAAI;EACF,MAAM,qBAAqB,OAAO,SAAS,MAAM;EACjD;CACF,SAAS,OAAO;EACd,MAAM,UAAU,iBAAiB,QAAQ,QAAQ;EACjD,IAAI,YAAY,KAAA,GACd,OAAO;EAET,MAAM;CACR;AACF;;;;;;;;;;;;;;;;;AAkBA,MAAM,gBAAgB,OACpB,QACA,WAM+C;CAC/C,MAAM,EAAE,SAAS,OAAO,OAAO,WAAW;CAC1C,MAAM,cAAc,iBAAiB,QAAQ,OAAO;CACpD,IAAI,gBAAgB,KAAA,GAClB,OAAO;CAGT,MAAM,WAAW,OAAO,KAAK;EAC3B;EACA;CACF,CAAC;CAED,IAAI,CAAC,SAAS,aAMZ,OAAO;EACL,UAAU;EACV,OAPoB,OAAO,YAAY;GACvC,UAAU;GACV;EACF,CAIqB;EACnB,IAAI;CACN;CAGF,IAAI,SAAS,UAAU,GAAG;EACxB,MAAM,mBAAmB,MAAM,aAC7B,OACA,SAAS,SACT,QACA,OACF;EACA,IAAI,qBAAqB,KAAA,GACvB,OAAO;CAEX;AAGF;;;;;;;;;;;;;;AAeA,MAAa,gBAAgB,OAC3B,QACA,SACA,OACA,WAC+B;CAC/B,IAAI,UAAU;CAEd,OAAO,MAAM;EACX,MAAM,cAAc,iBAAiB,QAAQ,KAAK,IAAI,GAAG,UAAU,CAAC,CAAC;EACrE,IAAI,gBAAgB,KAAA,GAClB,OAAO;EAIT,MAAM,YAAY,MAAM,WAAW,SAAS,OAAO;EACnD,IAAI,UAAU,IACZ,OAAO;GAAE,IAAI;GAAM,OAAO,UAAU;EAAM;EAI5C,MAAM,UAAU,MAAM,cAAc,QAAQ;GAC1C;GAEA,OAAO,UAAU;GACjB;GACA;EACF,CAAC;EACD,IAAI,YAAY,KAAA,GACd,OAAO;EAGT,WAAW;CACb;AACF"}
package/dist/sleep.d.mts DELETED
@@ -1,17 +0,0 @@
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.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sleep.d.mts","names":[],"sources":["../src/sleep.ts"],"mappings":";;;;;;;;;;;;;cAaa,eAAsB,oBAAkB"}
package/dist/sleep.mjs 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.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sleep.mjs","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"}
@@ -1,21 +0,0 @@
1
- import { RetryPolicy } from "./types.mjs";
2
- //#region src/throw-mode.d.ts
3
- /**
4
- * Runs the throw-mode retry loop: throws `RetryError` on exhaustion and
5
- * `AbortError` when `signal` aborts.
6
- *
7
- * @param policy - Object providing `next` and `onExhausted` (same contract as
8
- * `BaseRetryPolicy`).
9
- * @param execute - Async work callback per attempt.
10
- * @param sleep - Delay function between retries.
11
- * @param signal - Optional cancel signal.
12
- * @returns Resolves to the first successful return value.
13
- * @throws {RetryError} When retries are exhausted and `onExhausted` returns
14
- * the terminal error.
15
- * @throws {AbortError} When `signal` is already aborted or aborts while waiting.
16
- * @throws {Error} Any error thrown by `next`, `onExhausted`, or `sleep`.
17
- */
18
- declare const runThrowMode: <T, TError, TData>(policy: RetryPolicy<TError, TData>, execute: (attempt: number) => Promise<T>, sleep: (delayMs: number) => Promise<void>, signal?: AbortSignal) => Promise<T>;
19
- //#endregion
20
- export { runThrowMode };
21
- //# sourceMappingURL=throw-mode.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"throw-mode.d.mts","names":[],"sources":["../src/throw-mode.ts"],"mappings":";;;;;;;;;;;;;;;;;cAyBa,eAAsB,GAAG,QAAQ,OAC5C,QAAQ,YAAY,QAAQ,QAC5B,UAAU,oBAAoB,QAAQ,IACtC,QAAQ,oBAAoB,eAC5B,SAAS,gBACR,QAAQ"}
@@ -1,49 +0,0 @@
1
- import { sleepWithAbortSignal, throwIfAborted } from "./abort.mjs";
2
- //#region src/throw-mode.ts
3
- /**
4
- * Throw-mode execution path for `BaseRetryPolicy.run` (default when
5
- * `throwOnExhausted` is not `false`).
6
- *
7
- * @module @zap-studio/retry/throw-mode
8
- */
9
- /**
10
- * Runs the throw-mode retry loop: throws `RetryError` on exhaustion and
11
- * `AbortError` when `signal` aborts.
12
- *
13
- * @param policy - Object providing `next` and `onExhausted` (same contract as
14
- * `BaseRetryPolicy`).
15
- * @param execute - Async work callback per attempt.
16
- * @param sleep - Delay function between retries.
17
- * @param signal - Optional cancel signal.
18
- * @returns Resolves to the first successful return value.
19
- * @throws {RetryError} When retries are exhausted and `onExhausted` returns
20
- * the terminal error.
21
- * @throws {AbortError} When `signal` is already aborted or aborts while waiting.
22
- * @throws {Error} Any error thrown by `next`, `onExhausted`, or `sleep`.
23
- */
24
- const runThrowMode = async (policy, execute, sleep, signal) => {
25
- let attempt = 1;
26
- while (true) {
27
- throwIfAborted(signal);
28
- try {
29
- return await execute(attempt);
30
- } catch (error) {
31
- throwIfAborted(signal);
32
- const typedError = error;
33
- const decision = policy.next({
34
- attempt,
35
- error: typedError
36
- });
37
- if (!decision.shouldRetry) throw policy.onExhausted({
38
- attempts: attempt,
39
- error: typedError
40
- });
41
- if (decision.delayMs > 0) await (signal === void 0 ? sleep(decision.delayMs) : sleepWithAbortSignal(sleep, decision.delayMs, signal));
42
- attempt += 1;
43
- }
44
- }
45
- };
46
- //#endregion
47
- export { runThrowMode };
48
-
49
- //# sourceMappingURL=throw-mode.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"throw-mode.mjs","names":[],"sources":["../src/throw-mode.ts"],"sourcesContent":["/**\n * Throw-mode execution path for `BaseRetryPolicy.run` (default when\n * `throwOnExhausted` is not `false`).\n *\n * @module @zap-studio/retry/throw-mode\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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAyBA,MAAa,eAAe,OAC1B,QACA,SACA,OACA,WACe;CACf,IAAI,UAAU;CAEd,OAAO,MAAM;EACX,eAAe,MAAM;EAErB,IAAI;GAEF,OAAO,MAAM,QAAQ,OAAO;EAC9B,SAAS,OAAO;GACd,eAAe,MAAM;GAGrB,MAAM,aAAa;GACnB,MAAM,WAAW,OAAO,KAAK;IAC3B;IACA,OAAO;GACT,CAAC;GAED,IAAI,CAAC,SAAS,aACZ,MAAM,OAAO,YAAY;IACvB,UAAU;IACV,OAAO;GACT,CAAC;GAGH,IAAI,SAAS,UAAU,GAErB,OAAO,WAAW,KAAA,IACd,MAAM,SAAS,OAAO,IACtB,qBAAqB,OAAO,SAAS,SAAS,MAAM;GAG1D,WAAW;EACb;CACF;AACF"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.mts","names":[],"sources":["../src/types.ts"],"mappings":";;;;;;;;;;;UAiBiB,YAAY,kBAAkB;;;;;;EAM7C,OAAO,OAAO,mBAAmB,QAAQ,WAAW;;;;;;EAMpD,cAAc,OAAO,oBAAoB,QAAQ,WAAW;;;;;UAM7C;;;;;WAKN;;;;WAIA;;;;WAIA;;;;;UAMM,mBAAmB,kBAAkB;;;;WAI3C;;;;;WAKA;;;;;WAKA,QAAQ;;;;;WAKR,OAAO;;;;;UAMD,oBAAoB,kBAAkB;;;;WAI5C;;;;WAIA,QAAQ;;;;WAIR,OAAO;;;;;UAMD;;;;;;WAMN,SAAS,oBAAoB;;;;;;WAM7B,SAAS;;;;;;;;WAQT;;;;;;;;;KAUC,eAAe;;;;EAKrB;;;;EAIA,OAAO;;;;;EAMP;;;;;EAKA,OAAO,aAAa;;;;EAIpB"}
package/dist/types.mjs DELETED
@@ -1 +0,0 @@
1
- export {};