@zap-studio/retry 0.3.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +50 -18
- package/LICENSE +1 -1
- package/README.md +88 -154
- package/dist/base-policy.d.ts +61 -53
- package/dist/base-policy.d.ts.map +1 -1
- package/dist/base-policy.js +306 -2
- package/dist/base-policy.js.map +1 -0
- package/dist/{errors-BVZjP1Q5.d.ts → errors-CS5UPJWs.d.ts} +25 -1
- package/dist/errors-CS5UPJWs.d.ts.map +1 -0
- package/dist/errors.d.ts +1 -1
- package/dist/errors.js +18 -0
- package/dist/errors.js.map +1 -1
- package/dist/exponential-backoff.d.ts +13 -28
- package/dist/exponential-backoff.d.ts.map +1 -1
- package/dist/exponential-backoff.js +10 -35
- package/dist/exponential-backoff.js.map +1 -1
- package/dist/fixed-delay.d.ts +9 -24
- package/dist/fixed-delay.d.ts.map +1 -1
- package/dist/fixed-delay.js +10 -30
- package/dist/fixed-delay.js.map +1 -1
- package/dist/index.d.ts +6 -7
- package/dist/index.js +5 -6
- package/dist/linear-backoff.d.ts +46 -0
- package/dist/linear-backoff.d.ts.map +1 -0
- package/dist/linear-backoff.js +35 -0
- package/dist/linear-backoff.js.map +1 -0
- package/dist/types.d.ts +51 -7
- package/dist/types.d.ts.map +1 -1
- package/package.json +9 -9
- package/dist/abort.d.ts +0 -29
- package/dist/abort.d.ts.map +0 -1
- package/dist/abort.js +0 -61
- package/dist/abort.js.map +0 -1
- package/dist/base-policy-Dn3TOJd3.js +0 -248
- package/dist/base-policy-Dn3TOJd3.js.map +0 -1
- package/dist/errors-BVZjP1Q5.d.ts.map +0 -1
- package/dist/sleep.d.ts +0 -17
- package/dist/sleep.d.ts.map +0 -1
- package/dist/sleep.js +0 -23
- package/dist/sleep.js.map +0 -1
package/dist/fixed-delay.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { BaseRetryPolicy } from "./base-policy.js";
|
|
1
|
+
import { RetryPolicy } from "./types.js";
|
|
3
2
|
//#region src/fixed-delay.d.ts
|
|
4
3
|
/**
|
|
5
|
-
* Configuration for `
|
|
4
|
+
* Configuration for `fixedDelay(...)`.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const options: FixedDelayOptions = { maxAttempts: 3, delayMs: 250 };
|
|
6
8
|
*/
|
|
7
9
|
interface FixedDelayOptions {
|
|
8
10
|
/**
|
|
@@ -15,32 +17,15 @@ interface FixedDelayOptions {
|
|
|
15
17
|
delayMs: number;
|
|
16
18
|
}
|
|
17
19
|
/**
|
|
18
|
-
*
|
|
20
|
+
* Creates a retry policy with a constant delay between attempts.
|
|
19
21
|
*
|
|
20
22
|
* @example
|
|
21
|
-
* const policy =
|
|
23
|
+
* const policy = fixedDelay({
|
|
22
24
|
* maxAttempts: 3,
|
|
23
25
|
* delayMs: 250,
|
|
24
26
|
* });
|
|
25
27
|
*/
|
|
26
|
-
declare
|
|
27
|
-
/**
|
|
28
|
-
* Maximum number of attempts before the policy returns `max-attempts-reached`.
|
|
29
|
-
*/
|
|
30
|
-
private readonly maxAttempts;
|
|
31
|
-
/**
|
|
32
|
-
* Constant delay in milliseconds before each subsequent attempt.
|
|
33
|
-
*/
|
|
34
|
-
private readonly delayMs;
|
|
35
|
-
/**
|
|
36
|
-
* Creates a fixed-delay retry policy.
|
|
37
|
-
*/
|
|
38
|
-
constructor(options: FixedDelayOptions);
|
|
39
|
-
/**
|
|
40
|
-
* Computes retry decision for the current attempt.
|
|
41
|
-
*/
|
|
42
|
-
next(input: RetryDecisionInput): RetryDecision;
|
|
43
|
-
}
|
|
28
|
+
declare const fixedDelay: (options: FixedDelayOptions) => RetryPolicy;
|
|
44
29
|
//#endregion
|
|
45
|
-
export {
|
|
30
|
+
export { FixedDelayOptions, fixedDelay };
|
|
46
31
|
//# sourceMappingURL=fixed-delay.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fixed-delay.d.ts","names":[],"sources":["../src/fixed-delay.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"fixed-delay.d.ts","names":[],"sources":["../src/fixed-delay.ts"],"mappings":";;;;;;;;UAkBiB;;;;EAIf;;;;EAIA;;;;;;;;;;;cAYW,aAAc,SAAS,sBAAoB"}
|
package/dist/fixed-delay.js
CHANGED
|
@@ -1,53 +1,33 @@
|
|
|
1
|
-
import { t as BaseRetryPolicy } from "./base-policy-Dn3TOJd3.js";
|
|
2
1
|
//#region src/fixed-delay.ts
|
|
3
2
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* @module @zap-studio/retry/fixed-delay
|
|
7
|
-
*/
|
|
8
|
-
/**
|
|
9
|
-
* Retries with a constant delay between attempts.
|
|
3
|
+
* Creates a retry policy with a constant delay between attempts.
|
|
10
4
|
*
|
|
11
5
|
* @example
|
|
12
|
-
* const policy =
|
|
6
|
+
* const policy = fixedDelay({
|
|
13
7
|
* maxAttempts: 3,
|
|
14
8
|
* delayMs: 250,
|
|
15
9
|
* });
|
|
16
10
|
*/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
*/
|
|
21
|
-
maxAttempts;
|
|
22
|
-
/**
|
|
23
|
-
* Constant delay in milliseconds before each subsequent attempt.
|
|
24
|
-
*/
|
|
25
|
-
delayMs;
|
|
26
|
-
/**
|
|
27
|
-
* Creates a fixed-delay retry policy.
|
|
28
|
-
*/
|
|
29
|
-
constructor(options) {
|
|
30
|
-
super();
|
|
31
|
-
this.maxAttempts = options.maxAttempts;
|
|
32
|
-
this.delayMs = options.delayMs;
|
|
33
|
-
}
|
|
11
|
+
const fixedDelay = (options) => {
|
|
12
|
+
const { maxAttempts, delayMs } = options;
|
|
13
|
+
return {
|
|
34
14
|
/**
|
|
35
15
|
* Computes retry decision for the current attempt.
|
|
36
16
|
*/
|
|
37
|
-
|
|
38
|
-
if (input.attempt >=
|
|
17
|
+
next(input) {
|
|
18
|
+
if (input.attempt >= maxAttempts) return {
|
|
39
19
|
delayMs: 0,
|
|
40
20
|
reason: "max-attempts-reached",
|
|
41
21
|
shouldRetry: false
|
|
42
22
|
};
|
|
43
23
|
return {
|
|
44
|
-
delayMs
|
|
24
|
+
delayMs,
|
|
45
25
|
reason: "retry",
|
|
46
26
|
shouldRetry: true
|
|
47
27
|
};
|
|
48
|
-
}
|
|
28
|
+
} };
|
|
49
29
|
};
|
|
50
30
|
//#endregion
|
|
51
|
-
export {
|
|
31
|
+
export { fixedDelay };
|
|
52
32
|
|
|
53
33
|
//# sourceMappingURL=fixed-delay.js.map
|
package/dist/fixed-delay.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fixed-delay.js","names":[],"sources":["../src/fixed-delay.ts"],"sourcesContent":["/**\n * Fixed-delay retry strategy.\n *\n * @module @zap-studio/retry/fixed-delay\n */\n\nimport
|
|
1
|
+
{"version":3,"file":"fixed-delay.js","names":[],"sources":["../src/fixed-delay.ts"],"sourcesContent":["/**\n * Fixed-delay retry strategy.\n *\n * @module @zap-studio/retry/fixed-delay\n */\n\nimport type {\n RetryDecision,\n RetryDecisionInput,\n RetryPolicy,\n} from \"./types.js\";\n\n/**\n * Configuration for `fixedDelay(...)`.\n *\n * @example\n * const options: FixedDelayOptions = { maxAttempts: 3, delayMs: 250 };\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 * Creates a retry policy with a constant delay between attempts.\n *\n * @example\n * const policy = fixedDelay({\n * maxAttempts: 3,\n * delayMs: 250,\n * });\n */\nexport const fixedDelay = (options: FixedDelayOptions): RetryPolicy => {\n const { maxAttempts, delayMs } = options;\n\n return {\n /**\n * Computes retry decision for the current attempt.\n */\n next(input: RetryDecisionInput): RetryDecision {\n if (input.attempt >= maxAttempts) {\n return {\n delayMs: 0,\n reason: \"max-attempts-reached\",\n shouldRetry: false,\n };\n }\n\n return { delayMs, reason: \"retry\", shouldRetry: true };\n },\n };\n};\n"],"mappings":";;;;;;;;;;AAsCA,MAAa,cAAc,YAA4C;CACrE,MAAM,EAAE,aAAa,YAAY;CAEjC,OAAO;;;;AAIL,KAAK,OAA0C;EAC7C,IAAI,MAAM,WAAW,aACnB,OAAO;GACL,SAAS;GACT,QAAQ;GACR,aAAa;EACf;EAGF,OAAO;GAAE;GAAS,QAAQ;GAAS,aAAa;EAAK;CACvD,EACF;AACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { i as RetryErrorContext, n as AbortErrorContext, r as RetryError, t as AbortError } from "./errors-
|
|
2
|
-
import { sleepWithAbortSignal, throwIfAborted, toAbortError } from "./abort.js";
|
|
1
|
+
import { i as RetryErrorContext, n as AbortErrorContext, r as RetryError, t as AbortError } from "./errors-CS5UPJWs.js";
|
|
3
2
|
import { RetryDecision, RetryDecisionInput, RetryExhaustedInput, RetryPolicy, RetryRunOptions, RetryRunResult } from "./types.js";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
export { AbortError, type AbortErrorContext,
|
|
3
|
+
import { defaultSleep, runRetryPolicy } from "./base-policy.js";
|
|
4
|
+
import { ExponentialBackoffOptions, exponentialBackoff } from "./exponential-backoff.js";
|
|
5
|
+
import { FixedDelayOptions, fixedDelay } from "./fixed-delay.js";
|
|
6
|
+
import { LinearBackoffOptions, linearBackoff } from "./linear-backoff.js";
|
|
7
|
+
export { AbortError, type AbortErrorContext, type ExponentialBackoffOptions, type FixedDelayOptions, type LinearBackoffOptions, type RetryDecision, type RetryDecisionInput, RetryError, type RetryErrorContext, type RetryExhaustedInput, type RetryPolicy, type RetryRunOptions, type RetryRunResult, defaultSleep, exponentialBackoff, fixedDelay, linearBackoff, runRetryPolicy };
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { AbortError, RetryError } from "./errors.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
export { AbortError, BaseRetryPolicy, ExponentialBackoff, FixedDelay, RetryError, defaultSleep, sleepWithAbortSignal, throwIfAborted, toAbortError };
|
|
2
|
+
import { defaultSleep, runRetryPolicy } from "./base-policy.js";
|
|
3
|
+
import { exponentialBackoff } from "./exponential-backoff.js";
|
|
4
|
+
import { fixedDelay } from "./fixed-delay.js";
|
|
5
|
+
import { linearBackoff } from "./linear-backoff.js";
|
|
6
|
+
export { AbortError, RetryError, defaultSleep, exponentialBackoff, fixedDelay, linearBackoff, runRetryPolicy };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { RetryPolicy } from "./types.js";
|
|
2
|
+
//#region src/linear-backoff.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Configuration for `linearBackoff(...)`.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const options: LinearBackoffOptions = {
|
|
8
|
+
* maxAttempts: 5,
|
|
9
|
+
* baseDelayMs: 100,
|
|
10
|
+
* incrementMs: 100,
|
|
11
|
+
* maxDelayMs: 2_000,
|
|
12
|
+
* };
|
|
13
|
+
*/
|
|
14
|
+
interface LinearBackoffOptions {
|
|
15
|
+
/**
|
|
16
|
+
* Maximum number of attempts (including the first) before giving up.
|
|
17
|
+
*/
|
|
18
|
+
maxAttempts: number;
|
|
19
|
+
/**
|
|
20
|
+
* Delay in milliseconds after the first failed attempt.
|
|
21
|
+
*/
|
|
22
|
+
baseDelayMs: number;
|
|
23
|
+
/**
|
|
24
|
+
* Amount added to the delay for each subsequent retry.
|
|
25
|
+
*/
|
|
26
|
+
incrementMs: number;
|
|
27
|
+
/**
|
|
28
|
+
* Hard upper bound in milliseconds for computed linear delay.
|
|
29
|
+
*/
|
|
30
|
+
maxDelayMs: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Creates a retry policy with linear delay growth up to a max cap.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* const policy = linearBackoff({
|
|
37
|
+
* maxAttempts: 5,
|
|
38
|
+
* baseDelayMs: 100,
|
|
39
|
+
* incrementMs: 100,
|
|
40
|
+
* maxDelayMs: 2_000,
|
|
41
|
+
* });
|
|
42
|
+
*/
|
|
43
|
+
declare const linearBackoff: (options: LinearBackoffOptions) => RetryPolicy;
|
|
44
|
+
//#endregion
|
|
45
|
+
export { LinearBackoffOptions, linearBackoff };
|
|
46
|
+
//# sourceMappingURL=linear-backoff.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"linear-backoff.d.ts","names":[],"sources":["../src/linear-backoff.ts"],"mappings":";;;;;;;;;;;;;UAuBiB;;;;EAIf;;;;EAIA;;;;EAIA;;;;EAIA;;;;;;;;;;;;;cAcW,gBAAiB,SAAS,yBAAuB"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
//#region src/linear-backoff.ts
|
|
2
|
+
/**
|
|
3
|
+
* Creates a retry policy with linear delay growth up to a max cap.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* const policy = linearBackoff({
|
|
7
|
+
* maxAttempts: 5,
|
|
8
|
+
* baseDelayMs: 100,
|
|
9
|
+
* incrementMs: 100,
|
|
10
|
+
* maxDelayMs: 2_000,
|
|
11
|
+
* });
|
|
12
|
+
*/
|
|
13
|
+
const linearBackoff = (options) => {
|
|
14
|
+
const { maxAttempts, baseDelayMs, incrementMs, maxDelayMs } = options;
|
|
15
|
+
return {
|
|
16
|
+
/**
|
|
17
|
+
* Computes retry decision for the current attempt.
|
|
18
|
+
*/
|
|
19
|
+
next(input) {
|
|
20
|
+
if (input.attempt >= maxAttempts) return {
|
|
21
|
+
delayMs: 0,
|
|
22
|
+
reason: "max-attempts-reached",
|
|
23
|
+
shouldRetry: false
|
|
24
|
+
};
|
|
25
|
+
return {
|
|
26
|
+
delayMs: Math.min(maxDelayMs, baseDelayMs + incrementMs * (input.attempt - 1)),
|
|
27
|
+
reason: "retry",
|
|
28
|
+
shouldRetry: true
|
|
29
|
+
};
|
|
30
|
+
} };
|
|
31
|
+
};
|
|
32
|
+
//#endregion
|
|
33
|
+
export { linearBackoff };
|
|
34
|
+
|
|
35
|
+
//# sourceMappingURL=linear-backoff.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"linear-backoff.js","names":[],"sources":["../src/linear-backoff.ts"],"sourcesContent":["/**\n * Linear backoff retry strategy.\n *\n * @module @zap-studio/retry/linear-backoff\n */\n\nimport type {\n RetryDecision,\n RetryDecisionInput,\n RetryPolicy,\n} from \"./types.js\";\n\n/**\n * Configuration for `linearBackoff(...)`.\n *\n * @example\n * const options: LinearBackoffOptions = {\n * maxAttempts: 5,\n * baseDelayMs: 100,\n * incrementMs: 100,\n * maxDelayMs: 2_000,\n * };\n */\nexport interface LinearBackoffOptions {\n /**\n * Maximum number of attempts (including the first) before giving up.\n */\n maxAttempts: number;\n /**\n * Delay in milliseconds after the first failed attempt.\n */\n baseDelayMs: number;\n /**\n * Amount added to the delay for each subsequent retry.\n */\n incrementMs: number;\n /**\n * Hard upper bound in milliseconds for computed linear delay.\n */\n maxDelayMs: number;\n}\n\n/**\n * Creates a retry policy with linear delay growth up to a max cap.\n *\n * @example\n * const policy = linearBackoff({\n * maxAttempts: 5,\n * baseDelayMs: 100,\n * incrementMs: 100,\n * maxDelayMs: 2_000,\n * });\n */\nexport const linearBackoff = (options: LinearBackoffOptions): RetryPolicy => {\n const { maxAttempts, baseDelayMs, incrementMs, maxDelayMs } = options;\n\n return {\n /**\n * Computes retry decision for the current attempt.\n */\n next(input: RetryDecisionInput): RetryDecision {\n if (input.attempt >= maxAttempts) {\n return {\n delayMs: 0,\n reason: \"max-attempts-reached\",\n shouldRetry: false,\n };\n }\n\n const delayMs = Math.min(\n maxDelayMs,\n baseDelayMs + incrementMs * (input.attempt - 1)\n );\n\n return { delayMs, reason: \"retry\", shouldRetry: true };\n },\n };\n};\n"],"mappings":";;;;;;;;;;;;AAqDA,MAAa,iBAAiB,YAA+C;CAC3E,MAAM,EAAE,aAAa,aAAa,aAAa,eAAe;CAE9D,OAAO;;;;AAIL,KAAK,OAA0C;EAC7C,IAAI,MAAM,WAAW,aACnB,OAAO;GACL,SAAS;GACT,QAAQ;GACR,aAAa;EACf;EAQF,OAAO;GAAE,SALO,KAAK,IACnB,YACA,cAAc,eAAe,MAAM,UAAU,EAGhC;GAAG,QAAQ;GAAS,aAAa;EAAK;CACvD,EACF;AACF"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import { r as RetryError, t as AbortError } from "./errors-
|
|
1
|
+
import { r as RetryError, t as AbortError } from "./errors-CS5UPJWs.js";
|
|
2
2
|
//#region src/types.d.ts
|
|
3
3
|
/**
|
|
4
|
-
* Retry policy contract
|
|
4
|
+
* Retry policy contract consumed by `runRetryPolicy(...)`.
|
|
5
|
+
*
|
|
6
|
+
* Only `next` is required. `onExhausted` and `isKnownError` fall back to
|
|
7
|
+
* `runRetryPolicy`'s defaults when omitted, so a policy can be a plain
|
|
8
|
+
* object literal.
|
|
5
9
|
*
|
|
6
10
|
* @example
|
|
7
11
|
* const policy: RetryPolicy = {
|
|
@@ -9,7 +13,7 @@ import { r as RetryError, t as AbortError } from "./errors-BVZjP1Q5.js";
|
|
|
9
13
|
* onExhausted: ({ attempts }) => new RetryError("done", { attempts }),
|
|
10
14
|
* };
|
|
11
15
|
*/
|
|
12
|
-
interface RetryPolicy<TError =
|
|
16
|
+
interface RetryPolicy<TError extends Error = Error, TData = unknown> {
|
|
13
17
|
/**
|
|
14
18
|
* Returns the retry decision for a failed attempt.
|
|
15
19
|
*
|
|
@@ -19,12 +23,39 @@ interface RetryPolicy<TError = unknown, TData = unknown> {
|
|
|
19
23
|
/**
|
|
20
24
|
* Builds the terminal error used when retries are exhausted.
|
|
21
25
|
*
|
|
26
|
+
* Defaults to a `RetryError` built by `runRetryPolicy` when omitted.
|
|
27
|
+
*
|
|
22
28
|
* @throws {Error} Any error thrown by the policy implementation.
|
|
23
29
|
*/
|
|
30
|
+
onExhausted?: (input: RetryExhaustedInput<TError, TData>) => RetryError;
|
|
31
|
+
/**
|
|
32
|
+
* Narrows a caught `unknown` value into `TError`.
|
|
33
|
+
*
|
|
34
|
+
* The runner calls this before handing an error to `next`/`onExhausted`.
|
|
35
|
+
* When it returns `false`, the value is treated as outside this policy's
|
|
36
|
+
* error domain instead of a retryable failure — `runRetryPolicy(...)`
|
|
37
|
+
* rethrows it immediately in throw mode, or wraps it in a `RetryError` on
|
|
38
|
+
* `result.error` in non-throw mode. The default (used when omitted) checks
|
|
39
|
+
* `error instanceof Error`; supply your own when `TError` is a narrower
|
|
40
|
+
* subclass (e.g. a specific HTTP or domain error) to get real narrowing
|
|
41
|
+
* instead of an assumption.
|
|
42
|
+
*/
|
|
43
|
+
isKnownError?: (error: unknown) => error is TError;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* `RetryPolicy` with `onExhausted` and `isKnownError` resolved to concrete
|
|
47
|
+
* functions, used internally once `runRetryPolicy` has applied defaults.
|
|
48
|
+
*/
|
|
49
|
+
interface ResolvedRetryPolicy<TError extends Error, TData> {
|
|
50
|
+
next: RetryPolicy<TError, TData>["next"];
|
|
24
51
|
onExhausted: (input: RetryExhaustedInput<TError, TData>) => RetryError;
|
|
52
|
+
isKnownError: (error: unknown) => error is TError;
|
|
25
53
|
}
|
|
26
54
|
/**
|
|
27
55
|
* Decision returned by a retry policy for a specific attempt.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* const decision: RetryDecision = { shouldRetry: true, delayMs: 200, reason: "retry" };
|
|
28
59
|
*/
|
|
29
60
|
interface RetryDecision {
|
|
30
61
|
/**
|
|
@@ -43,8 +74,11 @@ interface RetryDecision {
|
|
|
43
74
|
}
|
|
44
75
|
/**
|
|
45
76
|
* Input passed to `RetryPolicy.next(...)` for each failed attempt.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* const input: RetryDecisionInput = { attempt: 2, error: new Error("timeout") };
|
|
46
80
|
*/
|
|
47
|
-
interface RetryDecisionInput<TError =
|
|
81
|
+
interface RetryDecisionInput<TError extends Error = Error, TData = unknown> {
|
|
48
82
|
/**
|
|
49
83
|
* One-based attempt number for the current failure.
|
|
50
84
|
*/
|
|
@@ -67,8 +101,11 @@ interface RetryDecisionInput<TError = unknown, TData = unknown> {
|
|
|
67
101
|
}
|
|
68
102
|
/**
|
|
69
103
|
* Input passed to `RetryPolicy.onExhausted(...)` when retries stop.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* const input: RetryExhaustedInput = { attempts: 5, error: new Error("timeout") };
|
|
70
107
|
*/
|
|
71
|
-
interface RetryExhaustedInput<TError =
|
|
108
|
+
interface RetryExhaustedInput<TError extends Error = Error, TData = unknown> {
|
|
72
109
|
/**
|
|
73
110
|
* Count of completed attempts that led to stopping retries.
|
|
74
111
|
*/
|
|
@@ -83,7 +120,10 @@ interface RetryExhaustedInput<TError = unknown, TData = unknown> {
|
|
|
83
120
|
readonly data?: TData;
|
|
84
121
|
}
|
|
85
122
|
/**
|
|
86
|
-
* Options for `
|
|
123
|
+
* Options for `runRetryPolicy(...)`.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* const options: RetryRunOptions = { throwOnExhausted: false, signal: controller.signal };
|
|
87
127
|
*/
|
|
88
128
|
interface RetryRunOptions {
|
|
89
129
|
/**
|
|
@@ -113,6 +153,10 @@ interface RetryRunOptions {
|
|
|
113
153
|
* - Success: `ok: true` with the resolved `value`.
|
|
114
154
|
* - Failure: `ok: false` with terminal `error` and completed `attempts` count
|
|
115
155
|
* (exhaustion or abort).
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* const result: RetryRunResult<string> = await runRetryPolicy(policy, doWork, { throwOnExhausted: false });
|
|
159
|
+
* if (!result.ok) console.error(result.error);
|
|
116
160
|
*/
|
|
117
161
|
type RetryRunResult<T> = {
|
|
118
162
|
/**
|
|
@@ -139,5 +183,5 @@ type RetryRunResult<T> = {
|
|
|
139
183
|
attempts: number;
|
|
140
184
|
};
|
|
141
185
|
//#endregion
|
|
142
|
-
export { RetryDecision, RetryDecisionInput, RetryExhaustedInput, RetryPolicy, RetryRunOptions, RetryRunResult };
|
|
186
|
+
export { ResolvedRetryPolicy, RetryDecision, RetryDecisionInput, RetryExhaustedInput, RetryPolicy, RetryRunOptions, RetryRunResult };
|
|
143
187
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","names":[],"sources":["../src/types.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"types.d.ts","names":[],"sources":["../src/types.ts"],"mappings":";;;;;;;;;;;;;;;UAqBiB,YAAY,eAAe,QAAQ,OAAO;;;;;;EAMzD,OAAO,OAAO,mBAAmB,QAAQ,WAAW;;;;;;;;EAQpD,eAAe,OAAO,oBAAoB,QAAQ,WAAW;;;;;;;;;;;;;EAa7D,gBAAgB,mBAAmB,SAAS;;;;;;UAO7B,oBAAoB,eAAe,OAAO;EACzD,MAAM,YAAY,QAAQ;EAC1B,cAAc,OAAO,oBAAoB,QAAQ,WAAW;EAC5D,eAAe,mBAAmB,SAAS;;;;;;;;UAS5B;;;;;WAKN;;;;WAIA;;;;WAIA;;;;;;;;UASM,mBACf,eAAe,QAAQ,OACvB;;;;WAKS;;;;;WAKA;;;;;WAKA,QAAQ;;;;;WAKR,OAAO;;;;;;;;UASD,oBACf,eAAe,QAAQ,OACvB;;;;WAKS;;;;WAIA,QAAQ;;;;WAIR,OAAO;;;;;;;;UASD;;;;;;WAMN,SAAS,oBAAoB;;;;;;WAM7B,SAAS;;;;;;;;WAQT;;;;;;;;;;;;;KAcC,eAAe;;;;EAKrB;;;;EAIA,OAAO;;;;;EAMP;;;;;EAKA,OAAO,aAAa;;;;EAIpB"}
|
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zap-studio/retry",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"private": false,
|
|
5
|
-
"description": "Composable retry policies for resilient async operations.",
|
|
5
|
+
"description": "Composable, tree-shakeable retry policies for resilient async operations.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"backoff",
|
|
8
8
|
"exponential backoff",
|
|
9
9
|
"fetch",
|
|
10
10
|
"http",
|
|
11
|
+
"linear backoff",
|
|
11
12
|
"resilience",
|
|
12
13
|
"retry",
|
|
13
14
|
"typescript"
|
|
@@ -30,12 +31,11 @@
|
|
|
30
31
|
"types": "./dist/index.d.ts",
|
|
31
32
|
"exports": {
|
|
32
33
|
".": "./dist/index.js",
|
|
33
|
-
"./abort": "./dist/abort.js",
|
|
34
34
|
"./base-policy": "./dist/base-policy.js",
|
|
35
35
|
"./errors": "./dist/errors.js",
|
|
36
36
|
"./exponential-backoff": "./dist/exponential-backoff.js",
|
|
37
37
|
"./fixed-delay": "./dist/fixed-delay.js",
|
|
38
|
-
"./
|
|
38
|
+
"./linear-backoff": "./dist/linear-backoff.js",
|
|
39
39
|
"./types": "./dist/types.js",
|
|
40
40
|
"./package.json": "./package.json"
|
|
41
41
|
},
|
|
@@ -43,12 +43,12 @@
|
|
|
43
43
|
"access": "public"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
46
|
+
"tsdown": "^0.22.14",
|
|
47
|
+
"typescript": "^7.0.2",
|
|
48
|
+
"vitest": "^4.1.10",
|
|
49
|
+
"@zap-studio/typescript": "0.0.0"
|
|
50
50
|
},
|
|
51
51
|
"engines": {
|
|
52
52
|
"node": ">=18.0.0"
|
|
53
53
|
}
|
|
54
|
-
}
|
|
54
|
+
}
|
package/dist/abort.d.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { t as AbortError } from "./errors-BVZjP1Q5.js";
|
|
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.ts.map
|
package/dist/abort.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"abort.d.ts","names":[],"sources":["../src/abort.ts"],"mappings":";;;;;;;;cAca,eAAgB,oBAAkB;;;;;;;cA8BlC,iBAAkB,SAAS;;;;;;;;;;cAiB3B,uBACX,QAAQ,oBAAoB,eAC5B,iBACA,QAAQ,gBACP"}
|
package/dist/abort.js
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { AbortError } from "./errors.js";
|
|
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.js.map
|
package/dist/abort.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"abort.js","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"}
|