@zap-studio/retry 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/README.md +23 -12
  3. package/dist/abort.d.mts +29 -0
  4. package/dist/abort.d.mts.map +1 -0
  5. package/dist/abort.mjs +61 -0
  6. package/dist/abort.mjs.map +1 -0
  7. package/dist/errors-BVZjP1Q5.d.mts +76 -0
  8. package/dist/errors-BVZjP1Q5.d.mts.map +1 -0
  9. package/dist/errors.d.mts +2 -0
  10. package/dist/{error.mjs → errors.mjs} +21 -3
  11. package/dist/errors.mjs.map +1 -0
  12. package/dist/exponential-backoff.d.mts +33 -16
  13. package/dist/exponential-backoff.d.mts.map +1 -1
  14. package/dist/exponential-backoff.mjs +13 -4
  15. package/dist/exponential-backoff.mjs.map +1 -1
  16. package/dist/fixed-delay.d.mts +26 -15
  17. package/dist/fixed-delay.d.mts.map +1 -1
  18. package/dist/fixed-delay.mjs +10 -4
  19. package/dist/fixed-delay.mjs.map +1 -1
  20. package/dist/index.d.mts +40 -70
  21. package/dist/index.d.mts.map +1 -1
  22. package/dist/index.mjs +14 -177
  23. package/dist/index.mjs.map +1 -1
  24. package/dist/result-mode.d.mts +19 -0
  25. package/dist/result-mode.d.mts.map +1 -0
  26. package/dist/result-mode.mjs +145 -0
  27. package/dist/result-mode.mjs.map +1 -0
  28. package/dist/sleep.d.mts +17 -0
  29. package/dist/sleep.d.mts.map +1 -0
  30. package/dist/sleep.mjs +23 -0
  31. package/dist/sleep.mjs.map +1 -0
  32. package/dist/throw-mode.d.mts +21 -0
  33. package/dist/throw-mode.d.mts.map +1 -0
  34. package/dist/throw-mode.mjs +49 -0
  35. package/dist/throw-mode.mjs.map +1 -0
  36. package/dist/types.d.mts +99 -46
  37. package/dist/types.d.mts.map +1 -1
  38. package/package.json +10 -7
  39. package/dist/error-CVW4I654.d.mts +0 -44
  40. package/dist/error-CVW4I654.d.mts.map +0 -1
  41. package/dist/error.d.mts +0 -2
  42. package/dist/error.mjs.map +0 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,29 @@
1
+ ## @zap-studio/retry@0.3.1
2
+
3
+ ### Migrate to ultracite lint/format
4
+
5
+ Internal formatting and lint cleanup only. No public API or behavior change.
6
+
1
7
  # @zap-studio/retry
2
8
 
9
+ ## 0.3.0
10
+
11
+ ### Breaking
12
+
13
+ - **Subpath for error types:** use `@zap-studio/retry/errors` (plural) for `RetryError`, `AbortError`, and related types. A prior JSR `error` subpath that pointed at a non-existent `error.ts` entry is removed; update deep imports from `@zap-studio/retry/error` to `@zap-studio/retry/errors`.
14
+
15
+ ### Changed
16
+
17
+ - Add dedicated `AbortError` and normalize cancellation paths so retry internals throw/return `RetryError` or `AbortError` instead of plain `Error`.
18
+ - Expose `defaultSleep` from the `@zap-studio/retry/sleep` subpath only (the main entry does not re-export it; `run` still uses it internally when `sleep` is omitted).
19
+ - Align non-throw exhaustion metadata so `result.attempts` and `result.error.attempts` stay consistent for `RetryError` outcomes.
20
+ - In non-throw mode, return a normalized `AbortError` on `result.error` for cancellation; `result.attempts` still reports completed attempts.
21
+ - Refactor result-mode internals into smaller helpers for lower complexity and cleaner maintainability.
22
+ - Expand docs across README and package docs pages to explain `AbortError` behavior in throw and non-throw modes.
23
+ - Split the retry runner into dedicated modules: `throw-mode` (throwing execution path), `result-mode` (non-throw `RetryRunResult` path), and `sleep` (the default `defaultSleep` implementation). `BaseRetryPolicy` in `index` now delegates to these modules without changing public behavior.
24
+ - Add exhaustive TSDoc for `result-mode` and other `src` modules, including private helpers, policy option and state fields, and `RetryRunResult` union members.
25
+ - Rework test layout into `sleep`, `throw-mode`, `result-mode`, and `index` test files with a shared `sequence-policy` fixture, replacing the prior combined `index` and `abort` test files.
26
+
3
27
  ## 0.2.0
4
28
 
5
29
  ### Changed
package/README.md CHANGED
@@ -33,10 +33,10 @@ const data = await exponential.run(async () => {
33
33
 
34
34
  `run(...)` throws when retries are exhausted.
35
35
 
36
- By default, policies extending `BaseRetryPolicy` throw `RetryError`.
36
+ By default, policies extending `BaseRetryPolicy` throw `RetryError` on exhaustion and `AbortError` on cancellation.
37
37
 
38
38
  ```ts
39
- import { RetryError } from "@zap-studio/retry/error";
39
+ import { AbortError, RetryError } from "@zap-studio/retry/errors";
40
40
 
41
41
  try {
42
42
  const data = await exponential.run(async () => {
@@ -50,6 +50,8 @@ try {
50
50
  if (error instanceof RetryError) {
51
51
  console.error("Retries exhausted:", error.attempts);
52
52
  console.error("Last error:", error.lastError);
53
+ } else if (error instanceof AbortError) {
54
+ console.error("Retry aborted:", error.message);
53
55
  } else {
54
56
  throw error;
55
57
  }
@@ -66,7 +68,7 @@ const result = await exponential.run(
66
68
  });
67
69
  return await response.json();
68
70
  },
69
- { throwOnExhausted: false },
71
+ { throwOnExhausted: false }
70
72
  );
71
73
 
72
74
  if (!result.ok) {
@@ -77,6 +79,14 @@ if (!result.ok) {
77
79
  }
78
80
  ```
79
81
 
82
+ ## Default sleep
83
+
84
+ `BaseRetryPolicy.run` automatically applies a delay between retry attempts when no custom `sleep` function is provided in the options.
85
+
86
+ That default is the `defaultSleep` helper, exported from `@zap-studio/retry/sleep`.
87
+
88
+ By default, this delay mechanism relies on the native JavaScript `setTimeout`, meaning retries are scheduled using the standard event loop timing rather than any custom or blocking implementation.
89
+
80
90
  ## Cancellation With AbortSignal
81
91
 
82
92
  Use `signal` in `run(...)` options to stop retrying early.
@@ -91,7 +101,7 @@ const promise = exponential.run(
91
101
  });
92
102
  return await response.json();
93
103
  },
94
- { signal: controller.signal },
104
+ { signal: controller.signal }
95
105
  );
96
106
 
97
107
  controller.abort(new Error("Request canceled"));
@@ -99,7 +109,7 @@ controller.abort(new Error("Request canceled"));
99
109
  await promise;
100
110
  ```
101
111
 
102
- In non-throw mode, abort is returned as `{ ok: false }`:
112
+ In non-throw mode, abort is returned as `{ ok: false }` with `AbortError` on `result.error`:
103
113
 
104
114
  ```ts
105
115
  const controller = new AbortController();
@@ -114,7 +124,7 @@ const result = await exponential.run(
114
124
  {
115
125
  signal: controller.signal,
116
126
  throwOnExhausted: false,
117
- },
127
+ }
118
128
  );
119
129
 
120
130
  if (!result.ok) {
@@ -147,18 +157,19 @@ const predictableIntervalPolicy = new FixedDelay({
147
157
 
148
158
  Extend `BaseRetryPolicy` when the built-in policies do not match your retry rules.
149
159
 
150
- You implement `next(...)` only; the base class supplies `onExhausted` with a default
151
- `RetryError` and keeps the shared `run(...)` orchestration (override `onExhausted` when
152
- you need a different terminal error).
160
+ You implement `next(...)` only; the base class supplies `onExhausted` with a default `RetryError` and keeps the shared `run(...)` orchestration (override `onExhausted` when you need a different terminal error).
153
161
 
154
162
  ```ts
155
163
  import { BaseRetryPolicy } from "@zap-studio/retry";
156
- import type { RetryDecision, RetryDecisionInput } from "@zap-studio/retry/types";
164
+ import type {
165
+ RetryDecision,
166
+ RetryDecisionInput,
167
+ } from "@zap-studio/retry/types";
157
168
 
158
169
  class LinearBackoff extends BaseRetryPolicy {
159
170
  constructor(
160
171
  private readonly maxAttempts: number,
161
- private readonly stepMs: number,
172
+ private readonly stepMs: number
162
173
  ) {
163
174
  super();
164
175
  }
@@ -189,7 +200,7 @@ const value = await policy.run(doWork);
189
200
  Use `RetryError` when an orchestrator exhausts retries and needs to surface final context.
190
201
 
191
202
  ```ts
192
- import { RetryError } from "@zap-studio/retry/error";
203
+ import { RetryError } from "@zap-studio/retry/errors";
193
204
 
194
205
  throw new RetryError("Retry policy exhausted all attempts.", {
195
206
  attempts: attempt,
@@ -0,0 +1,29 @@
1
+ import { t as AbortError } from "./errors-BVZjP1Q5.mjs";
2
+ //#region src/abort.d.ts
3
+ /**
4
+ * Throws when the provided abort signal is already aborted.
5
+ *
6
+ * @param signal - Optional abort signal to inspect.
7
+ * @throws {AbortError} When the signal is aborted.
8
+ */
9
+ declare const toAbortError: (reason: unknown) => AbortError;
10
+ /**
11
+ * Throws when the provided abort signal is already aborted.
12
+ *
13
+ * @param signal - Optional abort signal to inspect.
14
+ * @throws {AbortError} When the signal is aborted.
15
+ */
16
+ declare const throwIfAborted: (signal?: AbortSignal) => void;
17
+ /**
18
+ * Waits for delay sleep while observing cancellation through an abort signal.
19
+ *
20
+ * @param sleep - Sleep function used to await `delayMs`.
21
+ * @param delayMs - Delay duration in milliseconds.
22
+ * @param signal - Abort signal to observe while waiting.
23
+ * @returns Promise that resolves when delay finishes.
24
+ * @throws {AbortError} When the signal aborts before or during wait.
25
+ */
26
+ declare const sleepWithAbortSignal: (sleep: (delayMs: number) => Promise<void>, delayMs: number, signal: AbortSignal) => Promise<void>;
27
+ //#endregion
28
+ export { sleepWithAbortSignal, throwIfAborted, toAbortError };
29
+ //# sourceMappingURL=abort.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"abort.d.mts","names":[],"sources":["../src/abort.ts"],"mappings":";;;;;;;;cAca,eAAgB,oBAAkB;;;;;;;cA8BlC,iBAAkB,SAAS;;;;;;;;;;cAiB3B,uBACX,QAAQ,oBAAoB,eAC5B,iBACA,QAAQ,gBACP"}
package/dist/abort.mjs ADDED
@@ -0,0 +1,61 @@
1
+ import { AbortError } from "./errors.mjs";
2
+ //#region src/abort.ts
3
+ /**
4
+ * Abort-signal helpers for retry orchestration internals.
5
+ *
6
+ * @module @zap-studio/retry/abort
7
+ */
8
+ /**
9
+ * Throws when the provided abort signal is already aborted.
10
+ *
11
+ * @param signal - Optional abort signal to inspect.
12
+ * @throws {AbortError} When the signal is aborted.
13
+ */
14
+ const toAbortError = (reason) => {
15
+ if (reason instanceof AbortError) return reason;
16
+ if (reason instanceof Error) return new AbortError(reason.message, { cause: reason });
17
+ if (typeof reason === "string" && reason.length > 0) return new AbortError(reason);
18
+ if (reason === void 0) return new AbortError("Retry aborted.");
19
+ try {
20
+ return new AbortError(`Retry aborted: ${JSON.stringify(reason)}`);
21
+ } catch {
22
+ return new AbortError("Retry aborted.");
23
+ }
24
+ };
25
+ /**
26
+ * Throws when the provided abort signal is already aborted.
27
+ *
28
+ * @param signal - Optional abort signal to inspect.
29
+ * @throws {AbortError} When the signal is aborted.
30
+ */
31
+ const throwIfAborted = (signal) => {
32
+ if (signal?.aborted !== true) return;
33
+ throw toAbortError(signal.reason);
34
+ };
35
+ /**
36
+ * Waits for delay sleep while observing cancellation through an abort signal.
37
+ *
38
+ * @param sleep - Sleep function used to await `delayMs`.
39
+ * @param delayMs - Delay duration in milliseconds.
40
+ * @param signal - Abort signal to observe while waiting.
41
+ * @returns Promise that resolves when delay finishes.
42
+ * @throws {AbortError} When the signal aborts before or during wait.
43
+ */
44
+ const sleepWithAbortSignal = async (sleep, delayMs, signal) => {
45
+ if (signal.aborted) throw toAbortError(signal.reason);
46
+ let onAbort;
47
+ try {
48
+ await Promise.race([sleep(delayMs), new Promise((_resolve, reject) => {
49
+ onAbort = () => {
50
+ reject(toAbortError(signal.reason));
51
+ };
52
+ signal.addEventListener("abort", onAbort, { once: true });
53
+ })]);
54
+ } finally {
55
+ if (onAbort) signal.removeEventListener("abort", onAbort);
56
+ }
57
+ };
58
+ //#endregion
59
+ export { sleepWithAbortSignal, throwIfAborted, toAbortError };
60
+
61
+ //# sourceMappingURL=abort.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"abort.mjs","names":[],"sources":["../src/abort.ts"],"sourcesContent":["/**\n * Abort-signal helpers for retry orchestration internals.\n *\n * @module @zap-studio/retry/abort\n */\n\nimport { AbortError } from \"./errors.js\";\n\n/**\n * Throws when the provided abort signal is already aborted.\n *\n * @param signal - Optional abort signal to inspect.\n * @throws {AbortError} When the signal is aborted.\n */\nexport const toAbortError = (reason: unknown): AbortError => {\n if (reason instanceof AbortError) {\n return reason;\n }\n\n if (reason instanceof Error) {\n return new AbortError(reason.message, { cause: reason });\n }\n\n if (typeof reason === \"string\" && reason.length > 0) {\n return new AbortError(reason);\n }\n\n if (reason === undefined) {\n return new AbortError(\"Retry aborted.\");\n }\n\n try {\n return new AbortError(`Retry aborted: ${JSON.stringify(reason)}`);\n } catch {\n return new AbortError(\"Retry aborted.\");\n }\n};\n\n/**\n * Throws when the provided abort signal is already aborted.\n *\n * @param signal - Optional abort signal to inspect.\n * @throws {AbortError} When the signal is aborted.\n */\nexport const throwIfAborted = (signal?: AbortSignal): void => {\n if (signal?.aborted !== true) {\n return;\n }\n\n throw toAbortError(signal.reason);\n};\n\n/**\n * Waits for delay sleep while observing cancellation through an abort signal.\n *\n * @param sleep - Sleep function used to await `delayMs`.\n * @param delayMs - Delay duration in milliseconds.\n * @param signal - Abort signal to observe while waiting.\n * @returns Promise that resolves when delay finishes.\n * @throws {AbortError} When the signal aborts before or during wait.\n */\nexport const sleepWithAbortSignal = async (\n sleep: (delayMs: number) => Promise<void>,\n delayMs: number,\n signal: AbortSignal\n): Promise<void> => {\n if (signal.aborted) {\n throw toAbortError(signal.reason);\n }\n\n let onAbort: (() => void) | undefined;\n\n try {\n await Promise.race([\n sleep(delayMs),\n // oxlint-disable-next-line promise/avoid-new -- AbortSignal callback is adapted into the race promise.\n new Promise<never>((_resolve, reject) => {\n onAbort = (): void => {\n reject(toAbortError(signal.reason));\n };\n\n signal.addEventListener(\"abort\", onAbort, { once: true });\n }),\n ]);\n } finally {\n if (onAbort) {\n signal.removeEventListener(\"abort\", onAbort);\n }\n }\n};\n"],"mappings":";;;;;;;;;;;;;AAcA,MAAa,gBAAgB,WAAgC;CAC3D,IAAI,kBAAkB,YACpB,OAAO;CAGT,IAAI,kBAAkB,OACpB,OAAO,IAAI,WAAW,OAAO,SAAS,EAAE,OAAO,OAAO,CAAC;CAGzD,IAAI,OAAO,WAAW,YAAY,OAAO,SAAS,GAChD,OAAO,IAAI,WAAW,MAAM;CAG9B,IAAI,WAAW,KAAA,GACb,OAAO,IAAI,WAAW,gBAAgB;CAGxC,IAAI;EACF,OAAO,IAAI,WAAW,kBAAkB,KAAK,UAAU,MAAM,GAAG;CAClE,QAAQ;EACN,OAAO,IAAI,WAAW,gBAAgB;CACxC;AACF;;;;;;;AAQA,MAAa,kBAAkB,WAA+B;CAC5D,IAAI,QAAQ,YAAY,MACtB;CAGF,MAAM,aAAa,OAAO,MAAM;AAClC;;;;;;;;;;AAWA,MAAa,uBAAuB,OAClC,OACA,SACA,WACkB;CAClB,IAAI,OAAO,SACT,MAAM,aAAa,OAAO,MAAM;CAGlC,IAAI;CAEJ,IAAI;EACF,MAAM,QAAQ,KAAK,CACjB,MAAM,OAAO,GAEb,IAAI,SAAgB,UAAU,WAAW;GACvC,gBAAsB;IACpB,OAAO,aAAa,OAAO,MAAM,CAAC;GACpC;GAEA,OAAO,iBAAiB,SAAS,SAAS,EAAE,MAAM,KAAK,CAAC;EAC1D,CAAC,CACH,CAAC;CACH,UAAU;EACR,IAAI,SACF,OAAO,oBAAoB,SAAS,OAAO;CAE/C;AACF"}
@@ -0,0 +1,76 @@
1
+ //#region src/errors.d.ts
2
+ /**
3
+ * Terminal error types used by retry policies and runners.
4
+ *
5
+ * @module @zap-studio/retry/errors
6
+ */
7
+ /**
8
+ * Context payload attached to `RetryError`.
9
+ */
10
+ interface RetryErrorContext {
11
+ /**
12
+ * Count of completed attempts at exhaustion.
13
+ */
14
+ readonly attempts: number;
15
+ /**
16
+ * The last error object raised by a failed `execute` attempt.
17
+ */
18
+ readonly lastError?: unknown;
19
+ /**
20
+ * Optional data captured from the last attempt when provided by a policy.
21
+ */
22
+ readonly lastData?: unknown;
23
+ }
24
+ /**
25
+ * Context payload attached to `AbortError`.
26
+ */
27
+ interface AbortErrorContext {
28
+ /**
29
+ * When the abort `reason` was an `Error`, the optional wrapped cause.
30
+ */
31
+ readonly cause?: unknown;
32
+ }
33
+ /**
34
+ * Error thrown when retries are exhausted.
35
+ *
36
+ * @example
37
+ * throw new RetryError("Retry exhausted", {
38
+ * attempts: 3,
39
+ * lastError: new Error("network"),
40
+ * });
41
+ */
42
+ declare class RetryError extends Error {
43
+ /**
44
+ * Total attempts performed before exhaustion.
45
+ */
46
+ readonly attempts: number;
47
+ /**
48
+ * Last captured error from execution.
49
+ */
50
+ readonly lastError?: unknown;
51
+ /**
52
+ * Last captured data value, when available.
53
+ */
54
+ readonly lastData?: unknown;
55
+ /**
56
+ * Creates a RetryError with structured terminal context.
57
+ */
58
+ constructor(message: string, context: RetryErrorContext);
59
+ }
60
+ /**
61
+ * Error thrown when retry orchestration is canceled through `AbortSignal`.
62
+ */
63
+ declare class AbortError extends Error {
64
+ /**
65
+ * Optional wrapped cause when the native abort `reason` was an `Error`.
66
+ */
67
+ override readonly cause?: unknown;
68
+ /**
69
+ * @param message - Human-readable abort description.
70
+ * @param context - Optional `cause` link for diagnostic chaining.
71
+ */
72
+ constructor(message: string, context?: AbortErrorContext);
73
+ }
74
+ //#endregion
75
+ export { RetryErrorContext as i, AbortErrorContext as n, RetryError as r, AbortError as t };
76
+ //# sourceMappingURL=errors-BVZjP1Q5.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors-BVZjP1Q5.d.mts","names":[],"sources":["../src/errors.ts"],"mappings":";;;;;;;;;UAWiB;;;;WAIN;;;;WAIA;;;;WAIA;;;;;UAMM;;;;WAIN;;;;;;;;;;;cAYE,mBAAmB;;;;WAId;;;;WAIA;;;;WAIA;;;;EAKhB,YAAY,iBAAiB,SAAS;;;;;cAY3B,mBAAmB;;;;oBAIL;;;;;EAMzB,YAAY,iBAAiB,UAAS"}
@@ -0,0 +1,2 @@
1
+ import { i as RetryErrorContext, n as AbortErrorContext, r as RetryError, t as AbortError } from "./errors-BVZjP1Q5.mjs";
2
+ export { AbortError, AbortErrorContext, RetryError, RetryErrorContext };
@@ -1,4 +1,4 @@
1
- //#region src/error.ts
1
+ //#region src/errors.ts
2
2
  /**
3
3
  * Error thrown when retries are exhausted.
4
4
  *
@@ -32,7 +32,25 @@ var RetryError = class extends Error {
32
32
  this.lastData = context.lastData;
33
33
  }
34
34
  };
35
+ /**
36
+ * Error thrown when retry orchestration is canceled through `AbortSignal`.
37
+ */
38
+ var AbortError = class extends Error {
39
+ /**
40
+ * Optional wrapped cause when the native abort `reason` was an `Error`.
41
+ */
42
+ cause;
43
+ /**
44
+ * @param message - Human-readable abort description.
45
+ * @param context - Optional `cause` link for diagnostic chaining.
46
+ */
47
+ constructor(message, context = {}) {
48
+ super(message);
49
+ this.name = "AbortError";
50
+ this.cause = context.cause;
51
+ }
52
+ };
35
53
  //#endregion
36
- export { RetryError };
54
+ export { AbortError, RetryError };
37
55
 
38
- //# sourceMappingURL=error.mjs.map
56
+ //# sourceMappingURL=errors.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.mjs","names":[],"sources":["../src/errors.ts"],"sourcesContent":["// oxlint-disable max-classes-per-file -- Public retry error types are intentionally colocated.\n\n/**\n * Terminal error types used by retry policies and runners.\n *\n * @module @zap-studio/retry/errors\n */\n\n/**\n * Context payload attached to `RetryError`.\n */\nexport interface RetryErrorContext {\n /**\n * Count of completed attempts at exhaustion.\n */\n readonly attempts: number;\n /**\n * The last error object raised by a failed `execute` attempt.\n */\n readonly lastError?: unknown;\n /**\n * Optional data captured from the last attempt when provided by a policy.\n */\n readonly lastData?: unknown;\n}\n\n/**\n * Context payload attached to `AbortError`.\n */\nexport interface AbortErrorContext {\n /**\n * When the abort `reason` was an `Error`, the optional wrapped cause.\n */\n readonly cause?: unknown;\n}\n\n/**\n * Error thrown when retries are exhausted.\n *\n * @example\n * throw new RetryError(\"Retry exhausted\", {\n * attempts: 3,\n * lastError: new Error(\"network\"),\n * });\n */\nexport class RetryError extends Error {\n /**\n * Total attempts performed before exhaustion.\n */\n public readonly attempts: number;\n /**\n * Last captured error from execution.\n */\n public readonly lastError?: unknown;\n /**\n * Last captured data value, when available.\n */\n public readonly lastData?: unknown;\n\n /**\n * Creates a RetryError with structured terminal context.\n */\n constructor(message: string, context: RetryErrorContext) {\n super(message);\n this.name = \"RetryError\";\n this.attempts = context.attempts;\n this.lastError = context.lastError;\n this.lastData = context.lastData;\n }\n}\n\n/**\n * Error thrown when retry orchestration is canceled through `AbortSignal`.\n */\nexport class AbortError extends Error {\n /**\n * Optional wrapped cause when the native abort `reason` was an `Error`.\n */\n public override readonly cause?: unknown;\n\n /**\n * @param message - Human-readable abort description.\n * @param context - Optional `cause` link for diagnostic chaining.\n */\n constructor(message: string, context: AbortErrorContext = {}) {\n super(message);\n this.name = \"AbortError\";\n this.cause = context.cause;\n }\n}\n"],"mappings":";;;;;;;;;;AA6CA,IAAa,aAAb,cAAgC,MAAM;;;;CAIpC;;;;CAIA;;;;CAIA;;;;CAKA,YAAY,SAAiB,SAA4B;EACvD,MAAM,OAAO;EACb,KAAK,OAAO;EACZ,KAAK,WAAW,QAAQ;EACxB,KAAK,YAAY,QAAQ;EACzB,KAAK,WAAW,QAAQ;CAC1B;AACF;;;;AAKA,IAAa,aAAb,cAAgC,MAAM;;;;CAIpC;;;;;CAMA,YAAY,SAAiB,UAA6B,CAAC,GAAG;EAC5D,MAAM,OAAO;EACb,KAAK,OAAO;EACZ,KAAK,QAAQ,QAAQ;CACvB;AACF"}
@@ -1,36 +1,53 @@
1
1
  import { RetryDecision, RetryDecisionInput } from "./types.mjs";
2
2
  import { BaseRetryPolicy } from "./index.mjs";
3
-
4
3
  //#region src/exponential-backoff.d.ts
5
4
  /**
6
- * Configuration for `ExponentialBackoff`.
7
- */
5
+ * Configuration for `ExponentialBackoff`.
6
+ */
8
7
  interface ExponentialBackoffOptions {
8
+ /**
9
+ * Maximum number of attempts (including the first) before giving up.
10
+ */
9
11
  maxAttempts: number;
12
+ /**
13
+ * Initial delay in milliseconds, doubled each retry until capped.
14
+ */
10
15
  baseDelayMs: number;
16
+ /**
17
+ * Hard upper bound in milliseconds for computed exponential delay.
18
+ */
11
19
  maxDelayMs: number;
12
20
  }
13
21
  /**
14
- * Retries with exponential delay growth up to a max cap.
15
- *
16
- * @example
17
- * const policy = new ExponentialBackoff({
18
- * maxAttempts: 5,
19
- * baseDelayMs: 100,
20
- * maxDelayMs: 2_000,
21
- * });
22
- */
22
+ * Retries with exponential delay growth up to a max cap.
23
+ *
24
+ * @example
25
+ * const policy = new ExponentialBackoff({
26
+ * maxAttempts: 5,
27
+ * baseDelayMs: 100,
28
+ * maxDelayMs: 2_000,
29
+ * });
30
+ */
23
31
  declare class ExponentialBackoff extends BaseRetryPolicy {
32
+ /**
33
+ * Maximum number of attempts before the policy returns `max-attempts-reached`.
34
+ */
24
35
  private readonly maxAttempts;
36
+ /**
37
+ * Base delay in milliseconds used in `baseDelayMs * 2 ** (attempt - 1)`.
38
+ */
25
39
  private readonly baseDelayMs;
40
+ /**
41
+ * Upper cap for computed delay, applied with `Math.min`.
42
+ */
26
43
  private readonly maxDelayMs;
27
44
  /**
28
- * Creates an exponential backoff retry policy.
29
- */
45
+ * Creates an exponential backoff retry policy.
46
+ */
30
47
  constructor(options: ExponentialBackoffOptions);
31
48
  /**
32
- * Computes retry decision for the current attempt.
33
- */
49
+ * Computes retry decision for the current attempt.
50
+ */
34
51
  next(input: RetryDecisionInput): RetryDecision;
35
52
  }
36
53
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"exponential-backoff.d.mts","names":[],"sources":["../src/exponential-backoff.ts"],"mappings":";;;;;;;UAYiB,yBAAA;EACf,WAAA;EACA,WAAA;EACA,UAAA;AAAA;;;;;;;;;;;cAaW,kBAAA,SAA2B,eAAA;EAAA,iBACrB,WAAA;EAAA,iBACA,WAAA;EAAA,iBACA,UAAA;;;;EAKjB,WAAA,CAAY,OAAA,EAAS,yBAAA;;;;EAUrB,IAAA,CAAY,KAAA,EAAO,kBAAA,GAAqB,aAAA;AAAA"}
1
+ {"version":3,"file":"exponential-backoff.d.mts","names":[],"sources":["../src/exponential-backoff.ts"],"mappings":";;;;;;UAYiB;;;;EAIf;;;;EAIA;;;;EAIA;;;;;;;;;;;;cAaW,2BAA2B;;;;mBAIrB;;;;mBAIA;;;;mBAIA;;;;EAKjB,YAAY,SAAS;;;;EAUrB,KAAY,OAAO,qBAAqB"}
@@ -16,8 +16,17 @@ import { BaseRetryPolicy } from "./index.mjs";
16
16
  * });
17
17
  */
18
18
  var ExponentialBackoff = class extends BaseRetryPolicy {
19
+ /**
20
+ * Maximum number of attempts before the policy returns `max-attempts-reached`.
21
+ */
19
22
  maxAttempts;
23
+ /**
24
+ * Base delay in milliseconds used in `baseDelayMs * 2 ** (attempt - 1)`.
25
+ */
20
26
  baseDelayMs;
27
+ /**
28
+ * Upper cap for computed delay, applied with `Math.min`.
29
+ */
21
30
  maxDelayMs;
22
31
  /**
23
32
  * Creates an exponential backoff retry policy.
@@ -33,15 +42,15 @@ var ExponentialBackoff = class extends BaseRetryPolicy {
33
42
  */
34
43
  next(input) {
35
44
  if (input.attempt >= this.maxAttempts) return {
36
- shouldRetry: false,
37
45
  delayMs: 0,
38
- reason: "max-attempts-reached"
46
+ reason: "max-attempts-reached",
47
+ shouldRetry: false
39
48
  };
40
49
  const exponent = Math.max(0, input.attempt - 1);
41
50
  return {
42
- shouldRetry: true,
43
51
  delayMs: Math.min(this.maxDelayMs, this.baseDelayMs * 2 ** exponent),
44
- reason: "retry"
52
+ reason: "retry",
53
+ shouldRetry: true
45
54
  };
46
55
  }
47
56
  };
@@ -1 +1 @@
1
- {"version":3,"file":"exponential-backoff.mjs","names":[],"sources":["../src/exponential-backoff.ts"],"sourcesContent":["/**\n * Exponential backoff retry strategy.\n *\n * @module @zap-studio/retry/exponential-backoff\n */\n\nimport { BaseRetryPolicy } from \"./index.js\";\nimport type { RetryDecision, RetryDecisionInput } from \"./types.js\";\n\n/**\n * Configuration for `ExponentialBackoff`.\n */\nexport interface ExponentialBackoffOptions {\n maxAttempts: number;\n baseDelayMs: number;\n maxDelayMs: number;\n}\n\n/**\n * Retries with exponential delay growth up to a max cap.\n *\n * @example\n * const policy = new ExponentialBackoff({\n * maxAttempts: 5,\n * baseDelayMs: 100,\n * maxDelayMs: 2_000,\n * });\n */\nexport class ExponentialBackoff extends BaseRetryPolicy {\n private readonly maxAttempts: number;\n private readonly baseDelayMs: number;\n private readonly maxDelayMs: number;\n\n /**\n * Creates an exponential backoff retry policy.\n */\n constructor(options: ExponentialBackoffOptions) {\n super();\n this.maxAttempts = options.maxAttempts;\n this.baseDelayMs = options.baseDelayMs;\n this.maxDelayMs = options.maxDelayMs;\n }\n\n /**\n * Computes retry decision for the current attempt.\n */\n public next(input: RetryDecisionInput): RetryDecision {\n if (input.attempt >= this.maxAttempts) {\n return { shouldRetry: false, delayMs: 0, reason: \"max-attempts-reached\" };\n }\n\n const exponent = Math.max(0, input.attempt - 1);\n const delayMs = Math.min(this.maxDelayMs, this.baseDelayMs * 2 ** exponent);\n\n return { shouldRetry: true, delayMs, reason: \"retry\" };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA4BA,IAAa,qBAAb,cAAwC,gBAAgB;CACtD;CACA;CACA;;;;CAKA,YAAY,SAAoC;AAC9C,SAAO;AACP,OAAK,cAAc,QAAQ;AAC3B,OAAK,cAAc,QAAQ;AAC3B,OAAK,aAAa,QAAQ;;;;;CAM5B,KAAY,OAA0C;AACpD,MAAI,MAAM,WAAW,KAAK,YACxB,QAAO;GAAE,aAAa;GAAO,SAAS;GAAG,QAAQ;GAAwB;EAG3E,MAAM,WAAW,KAAK,IAAI,GAAG,MAAM,UAAU,EAAE;AAG/C,SAAO;GAAE,aAAa;GAAM,SAFZ,KAAK,IAAI,KAAK,YAAY,KAAK,cAAc,KAAK,SAAS;GAEtC,QAAQ;GAAS"}
1
+ {"version":3,"file":"exponential-backoff.mjs","names":[],"sources":["../src/exponential-backoff.ts"],"sourcesContent":["/**\n * Exponential backoff retry strategy.\n *\n * @module @zap-studio/retry/exponential-backoff\n */\n\nimport { BaseRetryPolicy } from \"./index.js\";\nimport type { RetryDecision, RetryDecisionInput } from \"./types.js\";\n\n/**\n * Configuration for `ExponentialBackoff`.\n */\nexport interface ExponentialBackoffOptions {\n /**\n * Maximum number of attempts (including the first) before giving up.\n */\n maxAttempts: number;\n /**\n * Initial delay in milliseconds, doubled each retry until capped.\n */\n baseDelayMs: number;\n /**\n * Hard upper bound in milliseconds for computed exponential delay.\n */\n maxDelayMs: number;\n}\n\n/**\n * Retries with exponential delay growth up to a max cap.\n *\n * @example\n * const policy = new ExponentialBackoff({\n * maxAttempts: 5,\n * baseDelayMs: 100,\n * maxDelayMs: 2_000,\n * });\n */\nexport class ExponentialBackoff extends BaseRetryPolicy {\n /**\n * Maximum number of attempts before the policy returns `max-attempts-reached`.\n */\n private readonly maxAttempts: number;\n /**\n * Base delay in milliseconds used in `baseDelayMs * 2 ** (attempt - 1)`.\n */\n private readonly baseDelayMs: number;\n /**\n * Upper cap for computed delay, applied with `Math.min`.\n */\n private readonly maxDelayMs: number;\n\n /**\n * Creates an exponential backoff retry policy.\n */\n constructor(options: ExponentialBackoffOptions) {\n super();\n this.maxAttempts = options.maxAttempts;\n this.baseDelayMs = options.baseDelayMs;\n this.maxDelayMs = options.maxDelayMs;\n }\n\n /**\n * Computes retry decision for the current attempt.\n */\n public next(input: RetryDecisionInput): RetryDecision {\n if (input.attempt >= this.maxAttempts) {\n return { delayMs: 0, reason: \"max-attempts-reached\", shouldRetry: false };\n }\n\n const exponent = Math.max(0, input.attempt - 1);\n const delayMs = Math.min(this.maxDelayMs, this.baseDelayMs * 2 ** exponent);\n\n return { delayMs, reason: \"retry\", shouldRetry: true };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAqCA,IAAa,qBAAb,cAAwC,gBAAgB;;;;CAItD;;;;CAIA;;;;CAIA;;;;CAKA,YAAY,SAAoC;EAC9C,MAAM;EACN,KAAK,cAAc,QAAQ;EAC3B,KAAK,cAAc,QAAQ;EAC3B,KAAK,aAAa,QAAQ;CAC5B;;;;CAKA,KAAY,OAA0C;EACpD,IAAI,MAAM,WAAW,KAAK,aACxB,OAAO;GAAE,SAAS;GAAG,QAAQ;GAAwB,aAAa;EAAM;EAG1E,MAAM,WAAW,KAAK,IAAI,GAAG,MAAM,UAAU,CAAC;EAG9C,OAAO;GAAE,SAFO,KAAK,IAAI,KAAK,YAAY,KAAK,cAAc,KAAK,QAEnD;GAAG,QAAQ;GAAS,aAAa;EAAK;CACvD;AACF"}
@@ -1,33 +1,44 @@
1
1
  import { RetryDecision, RetryDecisionInput } from "./types.mjs";
2
2
  import { BaseRetryPolicy } from "./index.mjs";
3
-
4
3
  //#region src/fixed-delay.d.ts
5
4
  /**
6
- * Configuration for `FixedDelay`.
7
- */
5
+ * Configuration for `FixedDelay`.
6
+ */
8
7
  interface FixedDelayOptions {
8
+ /**
9
+ * Maximum number of attempts (including the first) before giving up.
10
+ */
9
11
  maxAttempts: number;
12
+ /**
13
+ * Constant delay in milliseconds before each retry after a failure.
14
+ */
10
15
  delayMs: number;
11
16
  }
12
17
  /**
13
- * Retries with a constant delay between attempts.
14
- *
15
- * @example
16
- * const policy = new FixedDelay({
17
- * maxAttempts: 3,
18
- * delayMs: 250,
19
- * });
20
- */
18
+ * Retries with a constant delay between attempts.
19
+ *
20
+ * @example
21
+ * const policy = new FixedDelay({
22
+ * maxAttempts: 3,
23
+ * delayMs: 250,
24
+ * });
25
+ */
21
26
  declare class FixedDelay extends BaseRetryPolicy {
27
+ /**
28
+ * Maximum number of attempts before the policy returns `max-attempts-reached`.
29
+ */
22
30
  private readonly maxAttempts;
31
+ /**
32
+ * Constant delay in milliseconds before each subsequent attempt.
33
+ */
23
34
  private readonly delayMs;
24
35
  /**
25
- * Creates a fixed-delay retry policy.
26
- */
36
+ * Creates a fixed-delay retry policy.
37
+ */
27
38
  constructor(options: FixedDelayOptions);
28
39
  /**
29
- * Computes retry decision for the current attempt.
30
- */
40
+ * Computes retry decision for the current attempt.
41
+ */
31
42
  next(input: RetryDecisionInput): RetryDecision;
32
43
  }
33
44
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"fixed-delay.d.mts","names":[],"sources":["../src/fixed-delay.ts"],"mappings":";;;;;;AA0BA;UAdiB,iBAAA;EACf,WAAA;EACA,OAAA;AAAA;;;;;;;;;;cAYW,UAAA,SAAmB,eAAA;EAAA,iBACb,WAAA;EAAA,iBACA,OAAA;;;;EAKjB,WAAA,CAAY,OAAA,EAAS,iBAAA;;;;EASrB,IAAA,CAAY,KAAA,EAAO,kBAAA,GAAqB,aAAA;AAAA"}
1
+ {"version":3,"file":"fixed-delay.d.mts","names":[],"sources":["../src/fixed-delay.ts"],"mappings":";;;;;;UAYiB;;;;EAIf;;;;EAIA;;;;;;;;;;;cAYW,mBAAmB;;;;mBAIb;;;;mBAIA;;;;EAKjB,YAAY,SAAS;;;;EASrB,KAAY,OAAO,qBAAqB"}
@@ -15,7 +15,13 @@ import { BaseRetryPolicy } from "./index.mjs";
15
15
  * });
16
16
  */
17
17
  var FixedDelay = class extends BaseRetryPolicy {
18
+ /**
19
+ * Maximum number of attempts before the policy returns `max-attempts-reached`.
20
+ */
18
21
  maxAttempts;
22
+ /**
23
+ * Constant delay in milliseconds before each subsequent attempt.
24
+ */
19
25
  delayMs;
20
26
  /**
21
27
  * Creates a fixed-delay retry policy.
@@ -30,14 +36,14 @@ var FixedDelay = class extends BaseRetryPolicy {
30
36
  */
31
37
  next(input) {
32
38
  if (input.attempt >= this.maxAttempts) return {
33
- shouldRetry: false,
34
39
  delayMs: 0,
35
- reason: "max-attempts-reached"
40
+ reason: "max-attempts-reached",
41
+ shouldRetry: false
36
42
  };
37
43
  return {
38
- shouldRetry: true,
39
44
  delayMs: this.delayMs,
40
- reason: "retry"
45
+ reason: "retry",
46
+ shouldRetry: true
41
47
  };
42
48
  }
43
49
  };
@@ -1 +1 @@
1
- {"version":3,"file":"fixed-delay.mjs","names":[],"sources":["../src/fixed-delay.ts"],"sourcesContent":["/**\n * Fixed-delay retry strategy.\n *\n * @module @zap-studio/retry/fixed-delay\n */\n\nimport { BaseRetryPolicy } from \"./index.js\";\nimport type { RetryDecision, RetryDecisionInput } from \"./types.js\";\n\n/**\n * Configuration for `FixedDelay`.\n */\nexport interface FixedDelayOptions {\n maxAttempts: number;\n delayMs: number;\n}\n\n/**\n * Retries with a constant delay between attempts.\n *\n * @example\n * const policy = new FixedDelay({\n * maxAttempts: 3,\n * delayMs: 250,\n * });\n */\nexport class FixedDelay extends BaseRetryPolicy {\n private readonly maxAttempts: number;\n private readonly delayMs: number;\n\n /**\n * Creates a fixed-delay retry policy.\n */\n constructor(options: FixedDelayOptions) {\n super();\n this.maxAttempts = options.maxAttempts;\n this.delayMs = options.delayMs;\n }\n\n /**\n * Computes retry decision for the current attempt.\n */\n public next(input: RetryDecisionInput): RetryDecision {\n if (input.attempt >= this.maxAttempts) {\n return { shouldRetry: false, delayMs: 0, reason: \"max-attempts-reached\" };\n }\n\n return { shouldRetry: true, delayMs: this.delayMs, reason: \"retry\" };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AA0BA,IAAa,aAAb,cAAgC,gBAAgB;CAC9C;CACA;;;;CAKA,YAAY,SAA4B;AACtC,SAAO;AACP,OAAK,cAAc,QAAQ;AAC3B,OAAK,UAAU,QAAQ;;;;;CAMzB,KAAY,OAA0C;AACpD,MAAI,MAAM,WAAW,KAAK,YACxB,QAAO;GAAE,aAAa;GAAO,SAAS;GAAG,QAAQ;GAAwB;AAG3E,SAAO;GAAE,aAAa;GAAM,SAAS,KAAK;GAAS,QAAQ;GAAS"}
1
+ {"version":3,"file":"fixed-delay.mjs","names":[],"sources":["../src/fixed-delay.ts"],"sourcesContent":["/**\n * Fixed-delay retry strategy.\n *\n * @module @zap-studio/retry/fixed-delay\n */\n\nimport { BaseRetryPolicy } from \"./index.js\";\nimport type { RetryDecision, RetryDecisionInput } from \"./types.js\";\n\n/**\n * Configuration for `FixedDelay`.\n */\nexport interface FixedDelayOptions {\n /**\n * Maximum number of attempts (including the first) before giving up.\n */\n maxAttempts: number;\n /**\n * Constant delay in milliseconds before each retry after a failure.\n */\n delayMs: number;\n}\n\n/**\n * Retries with a constant delay between attempts.\n *\n * @example\n * const policy = new FixedDelay({\n * maxAttempts: 3,\n * delayMs: 250,\n * });\n */\nexport class FixedDelay extends BaseRetryPolicy {\n /**\n * Maximum number of attempts before the policy returns `max-attempts-reached`.\n */\n private readonly maxAttempts: number;\n /**\n * Constant delay in milliseconds before each subsequent attempt.\n */\n private readonly delayMs: number;\n\n /**\n * Creates a fixed-delay retry policy.\n */\n constructor(options: FixedDelayOptions) {\n super();\n this.maxAttempts = options.maxAttempts;\n this.delayMs = options.delayMs;\n }\n\n /**\n * Computes retry decision for the current attempt.\n */\n public next(input: RetryDecisionInput): RetryDecision {\n if (input.attempt >= this.maxAttempts) {\n return { delayMs: 0, reason: \"max-attempts-reached\", shouldRetry: false };\n }\n\n return { delayMs: this.delayMs, reason: \"retry\", shouldRetry: true };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAgCA,IAAa,aAAb,cAAgC,gBAAgB;;;;CAI9C;;;;CAIA;;;;CAKA,YAAY,SAA4B;EACtC,MAAM;EACN,KAAK,cAAc,QAAQ;EAC3B,KAAK,UAAU,QAAQ;CACzB;;;;CAKA,KAAY,OAA0C;EACpD,IAAI,MAAM,WAAW,KAAK,aACxB,OAAO;GAAE,SAAS;GAAG,QAAQ;GAAwB,aAAa;EAAM;EAG1E,OAAO;GAAE,SAAS,KAAK;GAAS,QAAQ;GAAS,aAAa;EAAK;CACrE;AACF"}