@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
|
@@ -1,76 +0,0 @@
|
|
|
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-fWo_KyVO.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"errors-fWo_KyVO.d.mts","names":[],"sources":["../src/errors.ts"],"mappings":";;AASA;;;;;;;UAAiB,iBAAA;EAYN;AAMX;;EANW,SARA,QAAA;EAcM;;AAgBjB;EAhBiB,SAVN,SAAA;;;;WAIA,QAAA;AAAA;;;;UAMM,iBAAA;;;;WAIN,KAAA;AAAA;;;;;;;;;;cAYE,UAAA,SAAmB,KAAA;;;;WAId,QAAA;;;;WAIA,SAAA;;;;WAIA,QAAA;;;;EAKhB,WAAA,CAAY,OAAA,UAAiB,OAAA,EAAS,iBAAA;AAAA;;;;cAY3B,UAAA,SAAmB,KAAA;;;;oBAIL,KAAA;;;;;EAMzB,WAAA,CAAY,OAAA,UAAiB,OAAA,GAAS,iBAAA;AAAA"}
|
package/dist/errors.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"errors.mjs","names":[],"sources":["../src/errors.ts"],"sourcesContent":["/**\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":";;;;;;;;;;AA2CA,IAAa,aAAb,cAAgC,MAAM;;;;CAIpC;;;;CAIA;;;;CAIA;;;;CAKA,YAAY,SAAiB,SAA4B;AACvD,QAAM,QAAQ;AACd,OAAK,OAAO;AACZ,OAAK,WAAW,QAAQ;AACxB,OAAK,YAAY,QAAQ;AACzB,OAAK,WAAW,QAAQ;;;;;;AAO5B,IAAa,aAAb,cAAgC,MAAM;;;;CAIpC;;;;;CAMA,YAAY,SAAiB,UAA6B,EAAE,EAAE;AAC5D,QAAM,QAAQ;AACd,OAAK,OAAO;AACZ,OAAK,QAAQ,QAAQ"}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { RetryDecision, RetryDecisionInput } from "./types.mjs";
|
|
2
|
-
import { BaseRetryPolicy } from "./index.mjs";
|
|
3
|
-
|
|
4
|
-
//#region src/exponential-backoff.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* Configuration for `ExponentialBackoff`.
|
|
7
|
-
*/
|
|
8
|
-
interface ExponentialBackoffOptions {
|
|
9
|
-
/**
|
|
10
|
-
* Maximum number of attempts (including the first) before giving up.
|
|
11
|
-
*/
|
|
12
|
-
maxAttempts: number;
|
|
13
|
-
/**
|
|
14
|
-
* Initial delay in milliseconds, doubled each retry until capped.
|
|
15
|
-
*/
|
|
16
|
-
baseDelayMs: number;
|
|
17
|
-
/**
|
|
18
|
-
* Hard upper bound in milliseconds for computed exponential delay.
|
|
19
|
-
*/
|
|
20
|
-
maxDelayMs: number;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Retries with exponential delay growth up to a max cap.
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* const policy = new ExponentialBackoff({
|
|
27
|
-
* maxAttempts: 5,
|
|
28
|
-
* baseDelayMs: 100,
|
|
29
|
-
* maxDelayMs: 2_000,
|
|
30
|
-
* });
|
|
31
|
-
*/
|
|
32
|
-
declare class ExponentialBackoff extends BaseRetryPolicy {
|
|
33
|
-
/**
|
|
34
|
-
* Maximum number of attempts before the policy returns `max-attempts-reached`.
|
|
35
|
-
*/
|
|
36
|
-
private readonly maxAttempts;
|
|
37
|
-
/**
|
|
38
|
-
* Base delay in milliseconds used in `baseDelayMs * 2 ** (attempt - 1)`.
|
|
39
|
-
*/
|
|
40
|
-
private readonly baseDelayMs;
|
|
41
|
-
/**
|
|
42
|
-
* Upper cap for computed delay, applied with `Math.min`.
|
|
43
|
-
*/
|
|
44
|
-
private readonly maxDelayMs;
|
|
45
|
-
/**
|
|
46
|
-
* Creates an exponential backoff retry policy.
|
|
47
|
-
*/
|
|
48
|
-
constructor(options: ExponentialBackoffOptions);
|
|
49
|
-
/**
|
|
50
|
-
* Computes retry decision for the current attempt.
|
|
51
|
-
*/
|
|
52
|
-
next(input: RetryDecisionInput): RetryDecision;
|
|
53
|
-
}
|
|
54
|
-
//#endregion
|
|
55
|
-
export { ExponentialBackoff, ExponentialBackoffOptions };
|
|
56
|
-
//# sourceMappingURL=exponential-backoff.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"exponential-backoff.d.mts","names":[],"sources":["../src/exponential-backoff.ts"],"mappings":";;;;;;;UAYiB,yBAAA;;;AAyBjB;EArBE,WAAA;;;;EAIA,WAAA;;;;EAIA,UAAA;AAAA;;;;;;;;;;;cAaW,kBAAA,SAA2B,eAAA;;;;mBAIrB,WAAA;;;;mBAIA,WAAA;;;;mBAIA,UAAA;;;;EAKjB,WAAA,CAAY,OAAA,EAAS,yBAAA;;;;EAUrB,IAAA,CAAY,KAAA,EAAO,kBAAA,GAAqB,aAAA;AAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
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 { 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":";;;;;;;;;;;;;;;;;AAqCA,IAAa,qBAAb,cAAwC,gBAAgB;;;;CAItD;;;;CAIA;;;;CAIA;;;;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"}
|
package/dist/fixed-delay.d.mts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { RetryDecision, RetryDecisionInput } from "./types.mjs";
|
|
2
|
-
import { BaseRetryPolicy } from "./index.mjs";
|
|
3
|
-
|
|
4
|
-
//#region src/fixed-delay.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* Configuration for `FixedDelay`.
|
|
7
|
-
*/
|
|
8
|
-
interface FixedDelayOptions {
|
|
9
|
-
/**
|
|
10
|
-
* Maximum number of attempts (including the first) before giving up.
|
|
11
|
-
*/
|
|
12
|
-
maxAttempts: number;
|
|
13
|
-
/**
|
|
14
|
-
* Constant delay in milliseconds before each retry after a failure.
|
|
15
|
-
*/
|
|
16
|
-
delayMs: number;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Retries with a constant delay between attempts.
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* const policy = new FixedDelay({
|
|
23
|
-
* maxAttempts: 3,
|
|
24
|
-
* delayMs: 250,
|
|
25
|
-
* });
|
|
26
|
-
*/
|
|
27
|
-
declare class FixedDelay extends BaseRetryPolicy {
|
|
28
|
-
/**
|
|
29
|
-
* Maximum number of attempts before the policy returns `max-attempts-reached`.
|
|
30
|
-
*/
|
|
31
|
-
private readonly maxAttempts;
|
|
32
|
-
/**
|
|
33
|
-
* Constant delay in milliseconds before each subsequent attempt.
|
|
34
|
-
*/
|
|
35
|
-
private readonly delayMs;
|
|
36
|
-
/**
|
|
37
|
-
* Creates a fixed-delay retry policy.
|
|
38
|
-
*/
|
|
39
|
-
constructor(options: FixedDelayOptions);
|
|
40
|
-
/**
|
|
41
|
-
* Computes retry decision for the current attempt.
|
|
42
|
-
*/
|
|
43
|
-
next(input: RetryDecisionInput): RetryDecision;
|
|
44
|
-
}
|
|
45
|
-
//#endregion
|
|
46
|
-
export { FixedDelay, FixedDelayOptions };
|
|
47
|
-
//# sourceMappingURL=fixed-delay.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"fixed-delay.d.mts","names":[],"sources":["../src/fixed-delay.ts"],"mappings":";;;;;;AAgCA;UApBiB,iBAAA;;;;EAIf,WAAA;;;;EAIA,OAAA;AAAA;;;;;;;;;;cAYW,UAAA,SAAmB,eAAA;;;;mBAIb,WAAA;;;;mBAIA,OAAA;;;;EAKjB,WAAA,CAAY,OAAA,EAAS,iBAAA;;;;EASrB,IAAA,CAAY,KAAA,EAAO,kBAAA,GAAqB,aAAA;AAAA"}
|
package/dist/fixed-delay.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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 { shouldRetry: false, delayMs: 0, reason: \"max-attempts-reached\" };\n }\n\n return { shouldRetry: true, delayMs: this.delayMs, reason: \"retry\" };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAgCA,IAAa,aAAb,cAAgC,gBAAgB;;;;CAI9C;;;;CAIA;;;;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"}
|
package/dist/index.d.mts
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { r as RetryError } from "./errors-fWo_KyVO.mjs";
|
|
2
|
-
import { RetryDecision, RetryDecisionInput, RetryExhaustedInput, RetryPolicy, RetryRunOptions, RetryRunResult } from "./types.mjs";
|
|
3
|
-
|
|
4
|
-
//#region src/index.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* Base class for implementing retry policies and running retry orchestration.
|
|
7
|
-
*
|
|
8
|
-
* Extend this class and implement {@link BaseRetryPolicy.next} to define retry
|
|
9
|
-
* behavior, then call {@link BaseRetryPolicy.run} to execute operations with that
|
|
10
|
-
* policy.
|
|
11
|
-
*/
|
|
12
|
-
declare abstract class BaseRetryPolicy<TError = unknown, TData = unknown> implements RetryPolicy<TError, TData> {
|
|
13
|
-
/**
|
|
14
|
-
* Returns the retry decision for a failed attempt.
|
|
15
|
-
*
|
|
16
|
-
* @param input - Attempt context used to compute retry behavior.
|
|
17
|
-
* @throws Any error thrown by a concrete retry policy implementation.
|
|
18
|
-
*/
|
|
19
|
-
abstract next(input: RetryDecisionInput<TError, TData>): RetryDecision;
|
|
20
|
-
/**
|
|
21
|
-
* Builds the terminal error thrown or returned when retries are exhausted.
|
|
22
|
-
*
|
|
23
|
-
* Override this when you need custom terminal error types.
|
|
24
|
-
*
|
|
25
|
-
* @param input - Exhaustion context.
|
|
26
|
-
* @returns `RetryError` by default.
|
|
27
|
-
* @throws Any error thrown by an overriding policy implementation.
|
|
28
|
-
*/
|
|
29
|
-
onExhausted(input: RetryExhaustedInput<TError, TData>): RetryError;
|
|
30
|
-
/**
|
|
31
|
-
* Runs retry orchestration in non-throw mode.
|
|
32
|
-
*
|
|
33
|
-
* @param execute - Async function to execute per attempt.
|
|
34
|
-
* @param options - Runner settings with `throwOnExhausted: false`.
|
|
35
|
-
* @returns A discriminated result union containing success value or terminal error.
|
|
36
|
-
* @throws Any error thrown by `next`, `onExhausted`, or a custom `sleep`.
|
|
37
|
-
*/
|
|
38
|
-
run<T>(execute: (attempt: number) => Promise<T>, options: RetryRunOptions & {
|
|
39
|
-
throwOnExhausted: false;
|
|
40
|
-
}): Promise<RetryRunResult<T>>;
|
|
41
|
-
/**
|
|
42
|
-
* Runs retry orchestration and throws terminal error on exhaustion.
|
|
43
|
-
*
|
|
44
|
-
* @param execute - Async function to execute per attempt.
|
|
45
|
-
* @param options - Optional runner settings.
|
|
46
|
-
* @returns The successful execution value.
|
|
47
|
-
* @throws {RetryError} When retries are exhausted and `onExhausted` returns the
|
|
48
|
-
* terminal retry error. The default implementation returns `RetryError` with the last
|
|
49
|
-
* execution failure available on `RetryError.lastError`.
|
|
50
|
-
* @throws {AbortError} When `options.signal` is already aborted or aborts while retrying.
|
|
51
|
-
* @throws Any error thrown by `next`, by `onExhausted`, or by a custom `sleep`
|
|
52
|
-
* function.
|
|
53
|
-
*/
|
|
54
|
-
run<T>(execute: (attempt: number) => Promise<T>, options?: RetryRunOptions & {
|
|
55
|
-
throwOnExhausted?: true;
|
|
56
|
-
}): Promise<T>;
|
|
57
|
-
}
|
|
58
|
-
//#endregion
|
|
59
|
-
export { BaseRetryPolicy };
|
|
60
|
-
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;;;;;;;uBA0BsB,eAAA,+CAA8D,WAAA,CAClF,MAAA,EACA,KAAA;;;;;;;WAQgB,IAAA,CAAK,KAAA,EAAO,kBAAA,CAAmB,MAAA,EAAQ,KAAA,IAAS,aAAA;;;;;;;;;;EAWhE,WAAA,CAAmB,KAAA,EAAO,mBAAA,CAAoB,MAAA,EAAQ,KAAA,IAAS,UAAA;;;;;;;;;EAgB/D,GAAA,GAAA,CACE,OAAA,GAAU,OAAA,aAAoB,OAAA,CAAQ,CAAA,GACtC,OAAA,EAAS,eAAA;IAAoB,gBAAA;EAAA,IAC5B,OAAA,CAAQ,cAAA,CAAe,CAAA;;;;;;;;;;;;;;EAe1B,GAAA,GAAA,CACE,OAAA,GAAU,OAAA,aAAoB,OAAA,CAAQ,CAAA,GACtC,OAAA,GAAU,eAAA;IAAoB,gBAAA;EAAA,IAC7B,OAAA,CAAQ,CAAA;AAAA"}
|
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 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
|
-
lastError: input.error,
|
|
32
|
-
lastData: input.data
|
|
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 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 (not wrapped in `RetryError`).
|
|
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.signal;
|
|
56
|
-
if (options.throwOnExhausted === false) return runResultMode(this, execute, sleep, signal);
|
|
57
|
-
return runThrowMode(this, execute, sleep, signal);
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
//#endregion
|
|
61
|
-
export { BaseRetryPolicy };
|
|
62
|
-
|
|
63
|
-
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
DELETED
|
@@ -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<TError = unknown, TData = unknown> implements RetryPolicy<\n TError,\n TData\n> {\n /**\n * Returns the retry decision for a failed attempt.\n *\n * @param input - Attempt context used to compute retry behavior.\n * @throws 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 Any error thrown by an overriding policy implementation.\n */\n public onExhausted(input: RetryExhaustedInput<TError, TData>): RetryError {\n return new RetryError(\"Retry policy exhausted all attempts.\", {\n attempts: input.attempts,\n lastError: input.error,\n lastData: input.data,\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 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 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 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 (not wrapped in `RetryError`).\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.signal;\n if (options.throwOnExhausted === false) {\n return runResultMode(this, execute, sleep, signal);\n }\n\n return runThrowMode(this, execute, sleep, signal);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA0BA,IAAsB,kBAAtB,MAGE;;;;;;;;;;CAkBA,YAAmB,OAAuD;AACxE,SAAO,IAAI,WAAW,wCAAwC;GAC5D,UAAU,MAAM;GAChB,WAAW,MAAM;GACjB,UAAU,MAAM;GACjB,CAAC;;;;;;;;;;;;;;;;;;;;CAoDJ,MAAa,IACX,SACA,UAA2B,EAAE,EACG;EAChC,MAAM,QAAQ,QAAQ,SAAS;EAC/B,MAAM,SAAS,QAAQ;AACvB,MAAI,QAAQ,qBAAqB,MAC/B,QAAO,cAAc,MAAM,SAAS,OAAO,OAAO;AAGpD,SAAO,aAAa,MAAM,SAAS,OAAO,OAAO"}
|
package/dist/result-mode.d.mts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { r as RetryError } from "./errors-fWo_KyVO.mjs";
|
|
2
|
-
import { RetryDecision, RetryDecisionInput, RetryExhaustedInput, RetryRunResult } from "./types.mjs";
|
|
3
|
-
|
|
4
|
-
//#region src/result-mode.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* Runs the non-throw retry loop, returning
|
|
7
|
-
* `RetryRunResult`.
|
|
8
|
-
*
|
|
9
|
-
* @param policy - Object providing `next` and `onExhausted` (same contract as
|
|
10
|
-
* `BaseRetryPolicy`).
|
|
11
|
-
* @param execute - Async work callback per attempt.
|
|
12
|
-
* @param sleep - Delay function between retries.
|
|
13
|
-
* @param signal - Optional cancel signal.
|
|
14
|
-
* @returns Terminal success or failure object.
|
|
15
|
-
* @throws Any error thrown by `next`, `onExhausted`, or a non-abort `sleep`
|
|
16
|
-
* failure.
|
|
17
|
-
*/
|
|
18
|
-
declare function runResultMode<T, TError, TData>(policy: {
|
|
19
|
-
next: (input: RetryDecisionInput<TError, TData>) => RetryDecision;
|
|
20
|
-
onExhausted: (input: RetryExhaustedInput<TError, TData>) => RetryError;
|
|
21
|
-
}, execute: (attempt: number) => Promise<T>, sleep: (delayMs: number) => Promise<void>, signal?: AbortSignal): Promise<RetryRunResult<T>>;
|
|
22
|
-
//#endregion
|
|
23
|
-
export { runResultMode };
|
|
24
|
-
//# sourceMappingURL=result-mode.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"result-mode.d.mts","names":[],"sources":["../src/result-mode.ts"],"mappings":";;;;;;AA6BA;;;;;;;;;;;iBAAsB,aAAA,kBAAA,CACpB,MAAA;EACE,IAAA,GAAO,KAAA,EAAO,kBAAA,CAAmB,MAAA,EAAQ,KAAA,MAAW,aAAA;EACpD,WAAA,GAAc,KAAA,EAAO,mBAAA,CAAoB,MAAA,EAAQ,KAAA,MAAW,UAAA;AAAA,GAE9D,OAAA,GAAU,OAAA,aAAoB,OAAA,CAAQ,CAAA,GACtC,KAAA,GAAQ,OAAA,aAAoB,OAAA,QAC5B,MAAA,GAAS,WAAA,GACR,OAAA,CAAQ,cAAA,CAAe,CAAA"}
|
package/dist/result-mode.mjs
DELETED
|
@@ -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
|
-
* Runs the non-throw retry loop, returning
|
|
11
|
-
* `RetryRunResult`.
|
|
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 Terminal success or failure object.
|
|
19
|
-
* @throws Any error thrown by `next`, `onExhausted`, or a non-abort `sleep`
|
|
20
|
-
* failure.
|
|
21
|
-
*/
|
|
22
|
-
async function runResultMode(policy, execute, sleep, signal) {
|
|
23
|
-
let attempt = 1;
|
|
24
|
-
while (true) {
|
|
25
|
-
const earlyAbortResult = abortResult(signal, Math.max(0, attempt - 1));
|
|
26
|
-
if (earlyAbortResult) return earlyAbortResult;
|
|
27
|
-
const execution = await runAttempt(execute, attempt);
|
|
28
|
-
if (execution.ok) return {
|
|
29
|
-
ok: true,
|
|
30
|
-
value: execution.value
|
|
31
|
-
};
|
|
32
|
-
const failure = await handleFailure(policy, {
|
|
33
|
-
attempt,
|
|
34
|
-
error: execution.error,
|
|
35
|
-
sleep,
|
|
36
|
-
signal
|
|
37
|
-
});
|
|
38
|
-
if (failure) return failure;
|
|
39
|
-
attempt += 1;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Runs one `execute(attempt)` call and returns either a success value or a
|
|
44
|
-
* captured error without rethrowing.
|
|
45
|
-
*
|
|
46
|
-
* @param execute - User work callback.
|
|
47
|
-
* @param attempt - One-based attempt number passed to `execute`.
|
|
48
|
-
* @returns A tagged success with `value` or a tagged failure with `error`.
|
|
49
|
-
*/
|
|
50
|
-
async function runAttempt(execute, attempt) {
|
|
51
|
-
try {
|
|
52
|
-
return {
|
|
53
|
-
ok: true,
|
|
54
|
-
value: await execute(attempt)
|
|
55
|
-
};
|
|
56
|
-
} catch (error) {
|
|
57
|
-
return {
|
|
58
|
-
ok: false,
|
|
59
|
-
error
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* After a failed attempt, applies abort rules, `next`, optional delay, and
|
|
65
|
-
* either returns a terminal `RetryRunResult` or `undefined` to continue.
|
|
66
|
-
*
|
|
67
|
-
* @param policy - Retry policy hooks (`next`, `onExhausted`) matching
|
|
68
|
-
* `BaseRetryPolicy`.
|
|
69
|
-
* @param params - Failure context for the current attempt.
|
|
70
|
-
* @param params.attempt - Current attempt number.
|
|
71
|
-
* @param params.error - Error thrown by the attempt.
|
|
72
|
-
* @param params.sleep - Delay function between retries.
|
|
73
|
-
* @param params.signal - Optional abort signal.
|
|
74
|
-
* @returns Terminal non-throw result if the loop should stop, otherwise
|
|
75
|
-
* `undefined` to schedule another attempt.
|
|
76
|
-
* @throws Any error thrown by `next`, `onExhausted`, or a custom `sleep` when
|
|
77
|
-
* the error is not an abort.
|
|
78
|
-
*/
|
|
79
|
-
async function handleFailure(policy, params) {
|
|
80
|
-
const { attempt, error, sleep, signal } = params;
|
|
81
|
-
const postExecuteAbortResult = abortResult(signal, attempt);
|
|
82
|
-
if (postExecuteAbortResult) return postExecuteAbortResult;
|
|
83
|
-
const decision = policy.next({
|
|
84
|
-
attempt,
|
|
85
|
-
error
|
|
86
|
-
});
|
|
87
|
-
if (!decision.shouldRetry) return {
|
|
88
|
-
ok: false,
|
|
89
|
-
error: policy.onExhausted({
|
|
90
|
-
attempts: attempt,
|
|
91
|
-
error
|
|
92
|
-
}),
|
|
93
|
-
attempts: attempt
|
|
94
|
-
};
|
|
95
|
-
if (decision.delayMs > 0) {
|
|
96
|
-
const sleepAbortResult = await waitForDelay(sleep, decision.delayMs, signal, attempt);
|
|
97
|
-
if (sleepAbortResult) return sleepAbortResult;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* When `signal` is already aborted, builds the terminal `{ ok: false }` object
|
|
102
|
-
* with a normalized `AbortError` on `error` (not wrapped in `RetryError`).
|
|
103
|
-
*
|
|
104
|
-
* @param signal - Optional abort signal; only acts when `aborted` is set.
|
|
105
|
-
* @param attempts - Number of finished attempts to report in the result.
|
|
106
|
-
* @returns Failure result or `undefined` if not aborted.
|
|
107
|
-
*/
|
|
108
|
-
function abortResult(signal, attempts) {
|
|
109
|
-
if (!signal?.aborted) return;
|
|
110
|
-
return {
|
|
111
|
-
ok: false,
|
|
112
|
-
error: toAbortError(signal.reason),
|
|
113
|
-
attempts
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Awaits inter-attempt delay in result mode, mapping an abort during wait to
|
|
118
|
-
* a terminal result instead of throwing when `throwOnExhausted` is false.
|
|
119
|
-
*
|
|
120
|
-
* @param sleep - Custom or default sleep implementation.
|
|
121
|
-
* @param delayMs - Milliseconds to wait.
|
|
122
|
-
* @param signal - If set, `sleep` is raced with the abort signal.
|
|
123
|
-
* @param attempts - Attempt count to attach if the wait ends in abort.
|
|
124
|
-
* @returns A terminal result when canceled during the wait, otherwise
|
|
125
|
-
* `undefined`.
|
|
126
|
-
* @throws The underlying `sleep` rejection when it is not an abort.
|
|
127
|
-
*/
|
|
128
|
-
async function waitForDelay(sleep, delayMs, signal, attempts) {
|
|
129
|
-
if (!signal) {
|
|
130
|
-
await sleep(delayMs);
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
try {
|
|
134
|
-
await sleepWithAbortSignal(sleep, delayMs, signal);
|
|
135
|
-
return;
|
|
136
|
-
} catch (error) {
|
|
137
|
-
const aborted = abortResult(signal, attempts);
|
|
138
|
-
if (aborted) return aborted;
|
|
139
|
-
throw error;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
//#endregion
|
|
143
|
-
export { runResultMode };
|
|
144
|
-
|
|
145
|
-
//# sourceMappingURL=result-mode.mjs.map
|
package/dist/result-mode.mjs.map
DELETED
|
@@ -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 { RetryError } from \"./errors.js\";\nimport type {\n RetryDecision,\n RetryDecisionInput,\n RetryExhaustedInput,\n RetryRunResult,\n} from \"./types.js\";\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 Any error thrown by `next`, `onExhausted`, or a non-abort `sleep`\n * failure.\n */\nexport async function runResultMode<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<RetryRunResult<T>> {\n let attempt = 1;\n\n while (true) {\n const earlyAbortResult = abortResult(signal, Math.max(0, attempt - 1));\n if (earlyAbortResult) return earlyAbortResult;\n\n const execution = await runAttempt(execute, attempt);\n if (execution.ok) {\n return { ok: true, value: execution.value };\n }\n\n const failure = await handleFailure(policy, {\n attempt,\n error: execution.error as TError,\n sleep,\n signal,\n });\n if (failure) return failure;\n\n attempt += 1;\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 */\nasync function runAttempt<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 ok: false,\n error,\n };\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 Any error thrown by `next`, `onExhausted`, or a custom `sleep` when\n * the error is not an abort.\n */\nasync function handleFailure<TError, TData>(\n policy: {\n next: (input: RetryDecisionInput<TError, TData>) => RetryDecision;\n onExhausted: (input: RetryExhaustedInput<TError, TData>) => RetryError;\n },\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 postExecuteAbortResult = abortResult(signal, attempt);\n if (postExecuteAbortResult) return postExecuteAbortResult;\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 ok: false,\n error: terminalError,\n attempts: attempt,\n };\n }\n\n if (decision.delayMs > 0) {\n const sleepAbortResult = await waitForDelay(sleep, decision.delayMs, signal, attempt);\n if (sleepAbortResult) return sleepAbortResult;\n }\n\n return;\n}\n\n/**\n * When `signal` is already aborted, builds the terminal `{ ok: false }` object\n * with a normalized `AbortError` on `error` (not wrapped in `RetryError`).\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 */\nfunction abortResult(\n signal: AbortSignal | undefined,\n attempts: number,\n): RetryRunResult<never> | undefined {\n if (!signal?.aborted) {\n return;\n }\n\n return {\n ok: false,\n error: toAbortError(signal.reason),\n attempts,\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 The underlying `sleep` rejection when it is not an abort.\n */\nasync function waitForDelay(\n sleep: (delayMs: number) => Promise<void>,\n delayMs: number,\n signal: AbortSignal | undefined,\n attempts: number,\n): Promise<RetryRunResult<never> | undefined> {\n if (!signal) {\n await sleep(delayMs);\n return;\n }\n\n try {\n await sleepWithAbortSignal(sleep, delayMs, signal);\n return;\n } catch (error) {\n const aborted = abortResult(signal, attempts);\n if (aborted) {\n return aborted;\n }\n throw error;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AA6BA,eAAsB,cACpB,QAIA,SACA,OACA,QAC4B;CAC5B,IAAI,UAAU;AAEd,QAAO,MAAM;EACX,MAAM,mBAAmB,YAAY,QAAQ,KAAK,IAAI,GAAG,UAAU,EAAE,CAAC;AACtE,MAAI,iBAAkB,QAAO;EAE7B,MAAM,YAAY,MAAM,WAAW,SAAS,QAAQ;AACpD,MAAI,UAAU,GACZ,QAAO;GAAE,IAAI;GAAM,OAAO,UAAU;GAAO;EAG7C,MAAM,UAAU,MAAM,cAAc,QAAQ;GAC1C;GACA,OAAO,UAAU;GACjB;GACA;GACD,CAAC;AACF,MAAI,QAAS,QAAO;AAEpB,aAAW;;;;;;;;;;;AAYf,eAAe,WACb,SACA,SACiE;AACjE,KAAI;AACF,SAAO;GACL,IAAI;GACJ,OAAO,MAAM,QAAQ,QAAQ;GAC9B;UACM,OAAO;AACd,SAAO;GACL,IAAI;GACJ;GACD;;;;;;;;;;;;;;;;;;;AAoBL,eAAe,cACb,QAIA,QAM4C;CAC5C,MAAM,EAAE,SAAS,OAAO,OAAO,WAAW;CAC1C,MAAM,yBAAyB,YAAY,QAAQ,QAAQ;AAC3D,KAAI,uBAAwB,QAAO;CAEnC,MAAM,WAAW,OAAO,KAAK;EAC3B;EACA;EACD,CAAC;AAEF,KAAI,CAAC,SAAS,YAMZ,QAAO;EACL,IAAI;EACJ,OAPoB,OAAO,YAAY;GACvC,UAAU;GACV;GACD,CAAC;EAKA,UAAU;EACX;AAGH,KAAI,SAAS,UAAU,GAAG;EACxB,MAAM,mBAAmB,MAAM,aAAa,OAAO,SAAS,SAAS,QAAQ,QAAQ;AACrF,MAAI,iBAAkB,QAAO;;;;;;;;;;;AAcjC,SAAS,YACP,QACA,UACmC;AACnC,KAAI,CAAC,QAAQ,QACX;AAGF,QAAO;EACL,IAAI;EACJ,OAAO,aAAa,OAAO,OAAO;EAClC;EACD;;;;;;;;;;;;;;AAeH,eAAe,aACb,OACA,SACA,QACA,UAC4C;AAC5C,KAAI,CAAC,QAAQ;AACX,QAAM,MAAM,QAAQ;AACpB;;AAGF,KAAI;AACF,QAAM,qBAAqB,OAAO,SAAS,OAAO;AAClD;UACO,OAAO;EACd,MAAM,UAAU,YAAY,QAAQ,SAAS;AAC7C,MAAI,QACF,QAAO;AAET,QAAM"}
|
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 function defaultSleep(delayMs: number): Promise<void>;
|
|
15
|
-
//#endregion
|
|
16
|
-
export { defaultSleep };
|
|
17
|
-
//# sourceMappingURL=sleep.d.mts.map
|
package/dist/sleep.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"sleep.d.mts","names":[],"sources":["../src/sleep.ts"],"mappings":";;AAaA;;;;;;;;;;;iBAAsB,YAAA,CAAa,OAAA,WAAkB,OAAA"}
|
package/dist/sleep.mjs.map
DELETED
|
@@ -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 async function defaultSleep(delayMs: number): Promise<void> {\n if (delayMs <= 0) {\n return;\n }\n\n await new Promise((resolve) => setTimeout(resolve, delayMs));\n}\n"],"mappings":";;;;;;;;;;;;;AAaA,eAAsB,aAAa,SAAgC;AACjE,KAAI,WAAW,EACb;AAGF,OAAM,IAAI,SAAS,YAAY,WAAW,SAAS,QAAQ,CAAC"}
|
package/dist/throw-mode.d.mts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { r as RetryError } from "./errors-fWo_KyVO.mjs";
|
|
2
|
-
import { RetryDecision, RetryDecisionInput, RetryExhaustedInput } from "./types.mjs";
|
|
3
|
-
|
|
4
|
-
//#region src/throw-mode.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* Runs the throw-mode retry loop: throws `RetryError` on exhaustion and
|
|
7
|
-
* `AbortError` when `signal` aborts.
|
|
8
|
-
*
|
|
9
|
-
* @param policy - Object providing `next` and `onExhausted` (same contract as
|
|
10
|
-
* `BaseRetryPolicy`).
|
|
11
|
-
* @param execute - Async work callback per attempt.
|
|
12
|
-
* @param sleep - Delay function between retries.
|
|
13
|
-
* @param signal - Optional cancel signal.
|
|
14
|
-
* @returns Resolves to the first successful return value.
|
|
15
|
-
* @throws {RetryError} When retries are exhausted and `onExhausted` returns
|
|
16
|
-
* the terminal error.
|
|
17
|
-
* @throws {AbortError} When `signal` is already aborted or aborts while waiting.
|
|
18
|
-
* @throws Any error thrown by `next`, `onExhausted`, or `sleep`.
|
|
19
|
-
*/
|
|
20
|
-
declare function runThrowMode<T, TError, TData>(policy: {
|
|
21
|
-
next: (input: RetryDecisionInput<TError, TData>) => RetryDecision;
|
|
22
|
-
onExhausted: (input: RetryExhaustedInput<TError, TData>) => RetryError;
|
|
23
|
-
}, execute: (attempt: number) => Promise<T>, sleep: (delayMs: number) => Promise<void>, signal?: AbortSignal): Promise<T>;
|
|
24
|
-
//#endregion
|
|
25
|
-
export { runThrowMode };
|
|
26
|
-
//# sourceMappingURL=throw-mode.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"throw-mode.d.mts","names":[],"sources":["../src/throw-mode.ts"],"mappings":";;;;;;AA0BA;;;;;;;;;;;;;iBAAsB,YAAA,kBAAA,CACpB,MAAA;EACE,IAAA,GAAO,KAAA,EAAO,kBAAA,CAAmB,MAAA,EAAQ,KAAA,MAAW,aAAA;EACpD,WAAA,GAAc,KAAA,EAAO,mBAAA,CAAoB,MAAA,EAAQ,KAAA,MAAW,UAAA;AAAA,GAE9D,OAAA,GAAU,OAAA,aAAoB,OAAA,CAAQ,CAAA,GACtC,KAAA,GAAQ,OAAA,aAAoB,OAAA,QAC5B,MAAA,GAAS,WAAA,GACR,OAAA,CAAQ,CAAA"}
|
package/dist/throw-mode.mjs
DELETED
|
@@ -1,50 +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 Any error thrown by `next`, `onExhausted`, or `sleep`.
|
|
23
|
-
*/
|
|
24
|
-
async function runThrowMode(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) if (signal) await sleepWithAbortSignal(sleep, decision.delayMs, signal);
|
|
42
|
-
else await sleep(decision.delayMs);
|
|
43
|
-
attempt += 1;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
//#endregion
|
|
48
|
-
export { runThrowMode };
|
|
49
|
-
|
|
50
|
-
//# sourceMappingURL=throw-mode.mjs.map
|