@zap-studio/retry 0.3.0 → 0.3.2
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 +15 -0
- package/LICENSE +1 -1
- package/README.md +28 -17
- package/dist/abort.d.ts +29 -0
- package/dist/abort.d.ts.map +1 -0
- package/dist/{abort.mjs → abort.js} +17 -17
- package/dist/abort.js.map +1 -0
- package/dist/base-policy-Dn3TOJd3.js +248 -0
- package/dist/base-policy-Dn3TOJd3.js.map +1 -0
- package/dist/base-policy.d.ts +59 -0
- package/dist/base-policy.d.ts.map +1 -0
- package/dist/base-policy.js +2 -0
- package/dist/errors-BVZjP1Q5.d.ts +76 -0
- package/dist/errors-BVZjP1Q5.d.ts.map +1 -0
- package/dist/{errors.d.mts → errors.d.ts} +1 -1
- package/dist/{errors.mjs → errors.js} +1 -1
- package/dist/errors.js.map +1 -0
- package/dist/exponential-backoff.d.ts +55 -0
- package/dist/exponential-backoff.d.ts.map +1 -0
- package/dist/{exponential-backoff.mjs → exponential-backoff.js} +6 -6
- package/dist/exponential-backoff.js.map +1 -0
- package/dist/fixed-delay.d.ts +46 -0
- package/dist/fixed-delay.d.ts.map +1 -0
- package/dist/{fixed-delay.mjs → fixed-delay.js} +6 -6
- package/dist/fixed-delay.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +7 -0
- package/dist/sleep.d.ts +17 -0
- package/dist/sleep.d.ts.map +1 -0
- package/dist/{sleep.mjs → sleep.js} +6 -4
- package/dist/sleep.js.map +1 -0
- package/dist/types.d.ts +143 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +0 -0
- package/package.json +15 -26
- package/dist/abort.d.mts +0 -30
- package/dist/abort.d.mts.map +0 -1
- package/dist/abort.mjs.map +0 -1
- package/dist/errors-fWo_KyVO.d.mts +0 -76
- package/dist/errors-fWo_KyVO.d.mts.map +0 -1
- package/dist/errors.mjs.map +0 -1
- package/dist/exponential-backoff.d.mts +0 -56
- package/dist/exponential-backoff.d.mts.map +0 -1
- package/dist/exponential-backoff.mjs.map +0 -1
- package/dist/fixed-delay.d.mts +0 -47
- package/dist/fixed-delay.d.mts.map +0 -1
- package/dist/fixed-delay.mjs.map +0 -1
- package/dist/index.d.mts +0 -60
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -63
- package/dist/index.mjs.map +0 -1
- package/dist/result-mode.d.mts +0 -24
- package/dist/result-mode.d.mts.map +0 -1
- package/dist/result-mode.mjs +0 -145
- package/dist/result-mode.mjs.map +0 -1
- package/dist/sleep.d.mts +0 -17
- package/dist/sleep.d.mts.map +0 -1
- package/dist/sleep.mjs.map +0 -1
- package/dist/throw-mode.d.mts +0 -26
- package/dist/throw-mode.d.mts.map +0 -1
- package/dist/throw-mode.mjs +0 -50
- package/dist/throw-mode.mjs.map +0 -1
- package/dist/types.d.mts +0 -145
- package/dist/types.d.mts.map +0 -1
- package/dist/types.mjs +0 -1
package/dist/throw-mode.mjs.map
DELETED
|
@@ -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 { RetryError } from \"./errors.js\";\nimport type { RetryDecision, RetryDecisionInput, RetryExhaustedInput } 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 Any error thrown by `next`, `onExhausted`, or `sleep`.\n */\nexport async function runThrowMode<T, TError, TData>(\n policy: {\n next: (input: RetryDecisionInput<TError, TData>) => RetryDecision;\n onExhausted: (input: RetryExhaustedInput<TError, TData>) => RetryError;\n },\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 return await execute(attempt);\n } catch (error) {\n throwIfAborted(signal);\n\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 if (signal) {\n await sleepWithAbortSignal(sleep, decision.delayMs, signal);\n } else {\n await sleep(decision.delayMs);\n }\n }\n\n attempt += 1;\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA0BA,eAAsB,aACpB,QAIA,SACA,OACA,QACY;CACZ,IAAI,UAAU;AAEd,QAAO,MAAM;AACX,iBAAe,OAAO;AAEtB,MAAI;AACF,UAAO,MAAM,QAAQ,QAAQ;WACtB,OAAO;AACd,kBAAe,OAAO;GAEtB,MAAM,aAAa;GACnB,MAAM,WAAW,OAAO,KAAK;IAC3B;IACA,OAAO;IACR,CAAC;AAEF,OAAI,CAAC,SAAS,YACZ,OAAM,OAAO,YAAY;IACvB,UAAU;IACV,OAAO;IACR,CAAC;AAGJ,OAAI,SAAS,UAAU,EACrB,KAAI,OACF,OAAM,qBAAqB,OAAO,SAAS,SAAS,OAAO;OAE3D,OAAM,MAAM,SAAS,QAAQ;AAIjC,cAAW"}
|
package/dist/types.d.mts
DELETED
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
import { r as RetryError, t as AbortError } from "./errors-fWo_KyVO.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/types.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* Retry policy contract used by `BaseRetryPolicy`.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* const policy: RetryPolicy = {
|
|
9
|
-
* next: ({ attempt }) => ({ shouldRetry: attempt < 3, delayMs: 100 }),
|
|
10
|
-
* onExhausted: ({ attempts }) => new RetryError("done", { attempts }),
|
|
11
|
-
* };
|
|
12
|
-
*/
|
|
13
|
-
interface RetryPolicy<TError = unknown, TData = unknown> {
|
|
14
|
-
/**
|
|
15
|
-
* Returns the retry decision for a failed attempt.
|
|
16
|
-
*
|
|
17
|
-
* @throws Any error thrown by the policy implementation.
|
|
18
|
-
*/
|
|
19
|
-
next(input: RetryDecisionInput<TError, TData>): RetryDecision;
|
|
20
|
-
/**
|
|
21
|
-
* Builds the terminal error used when retries are exhausted.
|
|
22
|
-
*
|
|
23
|
-
* @throws Any error thrown by the policy implementation.
|
|
24
|
-
*/
|
|
25
|
-
onExhausted(input: RetryExhaustedInput<TError, TData>): RetryError;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Decision returned by a retry policy for a specific attempt.
|
|
29
|
-
*/
|
|
30
|
-
interface RetryDecision {
|
|
31
|
-
/**
|
|
32
|
-
* When `true`, the runner may schedule another attempt (subject to
|
|
33
|
-
* `delayMs` and the runner's abort rules).
|
|
34
|
-
*/
|
|
35
|
-
readonly shouldRetry: boolean;
|
|
36
|
-
/**
|
|
37
|
-
* Milliseconds to wait before the next attempt when `shouldRetry` is `true`.
|
|
38
|
-
*/
|
|
39
|
-
readonly delayMs: number;
|
|
40
|
-
/**
|
|
41
|
-
* Optional machine-readable reason for the decision.
|
|
42
|
-
*/
|
|
43
|
-
readonly reason?: "retry" | "max-attempts-reached" | "policy-declined";
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Input passed to `RetryPolicy.next(...)` for each failed attempt.
|
|
47
|
-
*/
|
|
48
|
-
interface RetryDecisionInput<TError = unknown, TData = unknown> {
|
|
49
|
-
/**
|
|
50
|
-
* One-based attempt number for the current failure.
|
|
51
|
-
*/
|
|
52
|
-
readonly attempt: number;
|
|
53
|
-
/**
|
|
54
|
-
* Optional policy-level maximum attempts, when a policy wants to pass it
|
|
55
|
-
* through to `next`.
|
|
56
|
-
*/
|
|
57
|
-
readonly maxAttempts?: number;
|
|
58
|
-
/**
|
|
59
|
-
* Error raised by the most recent `execute(attempt)` call, when a failure
|
|
60
|
-
* occurred.
|
|
61
|
-
*/
|
|
62
|
-
readonly error?: TError;
|
|
63
|
-
/**
|
|
64
|
-
* Optional data captured alongside the failure, when a policy populates
|
|
65
|
-
* it.
|
|
66
|
-
*/
|
|
67
|
-
readonly data?: TData;
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Input passed to `RetryPolicy.onExhausted(...)` when retries stop.
|
|
71
|
-
*/
|
|
72
|
-
interface RetryExhaustedInput<TError = unknown, TData = unknown> {
|
|
73
|
-
/**
|
|
74
|
-
* Count of completed attempts that led to stopping retries.
|
|
75
|
-
*/
|
|
76
|
-
readonly attempts: number;
|
|
77
|
-
/**
|
|
78
|
-
* Last execution error, when available.
|
|
79
|
-
*/
|
|
80
|
-
readonly error?: TError;
|
|
81
|
-
/**
|
|
82
|
-
* Last captured data, when a policy or runner supplies it.
|
|
83
|
-
*/
|
|
84
|
-
readonly data?: TData;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Options for `BaseRetryPolicy.run(...)`.
|
|
88
|
-
*/
|
|
89
|
-
interface RetryRunOptions {
|
|
90
|
-
/**
|
|
91
|
-
* Delay function used between retry attempts.
|
|
92
|
-
*
|
|
93
|
-
* @throws Any error thrown or rejected by the custom delay implementation.
|
|
94
|
-
*/
|
|
95
|
-
readonly sleep?: (delayMs: number) => Promise<void>;
|
|
96
|
-
/**
|
|
97
|
-
* Abort signal used to cancel retry orchestration.
|
|
98
|
-
*
|
|
99
|
-
* When aborted, the runner stops retrying and terminates immediately.
|
|
100
|
-
*/
|
|
101
|
-
readonly signal?: AbortSignal;
|
|
102
|
-
/**
|
|
103
|
-
* When `true`, the runner throws a `RetryError` when retries are exhausted.
|
|
104
|
-
*
|
|
105
|
-
* When `false`, the runner returns a `RetryRunResult` discriminated union.
|
|
106
|
-
*
|
|
107
|
-
* @default true
|
|
108
|
-
*/
|
|
109
|
-
readonly throwOnExhausted?: boolean;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Result union returned by non-throw runner mode.
|
|
113
|
-
*
|
|
114
|
-
* - Success: `ok: true` with the resolved `value`.
|
|
115
|
-
* - Failure: `ok: false` with terminal `error` and completed `attempts` count
|
|
116
|
-
* (exhaustion or abort).
|
|
117
|
-
*/
|
|
118
|
-
type RetryRunResult<T> = {
|
|
119
|
-
/**
|
|
120
|
-
* Discriminator for a successful run.
|
|
121
|
-
*/
|
|
122
|
-
ok: true;
|
|
123
|
-
/**
|
|
124
|
-
* Successful return value from the final attempt.
|
|
125
|
-
*/
|
|
126
|
-
value: T;
|
|
127
|
-
} | {
|
|
128
|
-
/**
|
|
129
|
-
* Discriminator for a failed or aborted run.
|
|
130
|
-
*/
|
|
131
|
-
ok: false;
|
|
132
|
-
/**
|
|
133
|
-
* Terminal error: `RetryError` when retries are exhausted, or
|
|
134
|
-
* `AbortError` when the run is canceled (non-throw path uses the same
|
|
135
|
-
* instances as throw mode, not wrapped).
|
|
136
|
-
*/
|
|
137
|
-
error: RetryError | AbortError;
|
|
138
|
-
/**
|
|
139
|
-
* Number of attempts that completed before the terminal outcome.
|
|
140
|
-
*/
|
|
141
|
-
attempts: number;
|
|
142
|
-
};
|
|
143
|
-
//#endregion
|
|
144
|
-
export { RetryDecision, RetryDecisionInput, RetryExhaustedInput, RetryPolicy, RetryRunOptions, RetryRunResult };
|
|
145
|
-
//# sourceMappingURL=types.d.mts.map
|
package/dist/types.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.mts","names":[],"sources":["../src/types.ts"],"mappings":";;;;;;;;;;;;UAiBiB,WAAA;;;;;;EAMf,IAAA,CAAK,KAAA,EAAO,kBAAA,CAAmB,MAAA,EAAQ,KAAA,IAAS,aAAA;;;;;;EAMhD,WAAA,CAAY,KAAA,EAAO,mBAAA,CAAoB,MAAA,EAAQ,KAAA,IAAS,UAAA;AAAA;AAM1D;;;AAAA,UAAiB,aAAA;;;;;WAKN,WAAA;EAcX;;;EAAA,SAVW,OAAA;;;;WAIA,MAAA;AAAA;;;;UAMM,kBAAA;;;;WAIN,OAAA;;;;;WAKA,WAAA;;;AAkCX;;WA7BW,KAAA,GAAQ,MAAA;EAyCC;;;;EAAA,SApCT,IAAA,GAAO,KAAA;AAAA;;;;UAMD,mBAAA;;;;WAIN,QAAA;;;;WAIA,KAAA,GAAQ,MAAA;;;;WAIR,IAAA,GAAO,KAAA;AAAA;;;;UAMD,eAAA;;;;;;WAMN,KAAA,IAAS,OAAA,aAAoB,OAAA;;;;;;WAM7B,MAAA,GAAS,WAAA;;;;;;;;WAQT,gBAAA;AAAA;;;;;;;;KAUC,cAAA;;;;EAKN,EAAA;;;;EAIA,KAAA,EAAO,CAAA;AAAA;;;;EAMP,EAAA;;;;;;EAMA,KAAA,EAAO,UAAA,GAAa,UAAA;;;;EAIpB,QAAA;AAAA"}
|
package/dist/types.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|