@wentools/simmer 0.1.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/LICENSE +21 -0
- package/README.md +107 -0
- package/esm/debounce/debounce.d.ts +15 -0
- package/esm/debounce/debounce.d.ts.map +1 -0
- package/esm/debounce/debounce.js +61 -0
- package/esm/debounce/mod.d.ts +2 -0
- package/esm/debounce/mod.d.ts.map +1 -0
- package/esm/debounce/mod.js +1 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/err/err.d.ts +38 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/err/err.d.ts.map +1 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/err/err.js +38 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/err/mod.d.ts +2 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/err/mod.d.ts.map +1 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/err/mod.js +1 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/is_result/is_result.d.ts +11 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/is_result/is_result.d.ts.map +1 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/is_result/is_result.js +14 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/is_result/mod.d.ts +2 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/is_result/mod.d.ts.map +1 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/is_result/mod.js +1 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/mod.d.ts +156 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/mod.d.ts.map +1 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/mod.js +65 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/mod.d.ts +2 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/mod.d.ts.map +1 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/mod.js +1 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/propagate_err.d.ts +28 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/propagate_err.d.ts.map +1 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/propagate_err.js +30 -0
- package/esm/eventual/eventual.d.ts +32 -0
- package/esm/eventual/eventual.d.ts.map +1 -0
- package/esm/eventual/eventual.js +47 -0
- package/esm/eventual/mod.d.ts +3 -0
- package/esm/eventual/mod.d.ts.map +1 -0
- package/esm/eventual/mod.js +1 -0
- package/esm/mod.d.ts +7 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +4 -0
- package/esm/package.json +3 -0
- package/esm/retry/delay.d.ts +8 -0
- package/esm/retry/delay.d.ts.map +1 -0
- package/esm/retry/delay.js +28 -0
- package/esm/retry/mod.d.ts +5 -0
- package/esm/retry/mod.d.ts.map +1 -0
- package/esm/retry/mod.js +3 -0
- package/esm/retry/retry.d.ts +23 -0
- package/esm/retry/retry.d.ts.map +1 -0
- package/esm/retry/retry.js +128 -0
- package/esm/retry/types.d.ts +59 -0
- package/esm/retry/types.d.ts.map +1 -0
- package/esm/retry/types.js +8 -0
- package/esm/sleep/mod.d.ts +2 -0
- package/esm/sleep/mod.d.ts.map +1 -0
- package/esm/sleep/mod.js +1 -0
- package/esm/sleep/sleep.d.ts +6 -0
- package/esm/sleep/sleep.d.ts.map +1 -0
- package/esm/sleep/sleep.js +7 -0
- package/package.json +33 -0
- package/script/debounce/debounce.d.ts +15 -0
- package/script/debounce/debounce.d.ts.map +1 -0
- package/script/debounce/debounce.js +65 -0
- package/script/debounce/mod.d.ts +2 -0
- package/script/debounce/mod.d.ts.map +1 -0
- package/script/debounce/mod.js +6 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/err/err.d.ts +38 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/err/err.d.ts.map +1 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/err/err.js +41 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/err/mod.d.ts +2 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/err/mod.d.ts.map +1 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/err/mod.js +5 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/is_result/is_result.d.ts +11 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/is_result/is_result.d.ts.map +1 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/is_result/is_result.js +17 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/is_result/mod.d.ts +2 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/is_result/mod.d.ts.map +1 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/is_result/mod.js +5 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/mod.d.ts +156 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/mod.d.ts.map +1 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/mod.js +77 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/mod.d.ts +2 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/mod.d.ts.map +1 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/mod.js +5 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/propagate_err.d.ts +28 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/propagate_err.d.ts.map +1 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/propagate_err.js +33 -0
- package/script/eventual/eventual.d.ts +32 -0
- package/script/eventual/eventual.d.ts.map +1 -0
- package/script/eventual/eventual.js +53 -0
- package/script/eventual/mod.d.ts +3 -0
- package/script/eventual/mod.d.ts.map +1 -0
- package/script/eventual/mod.js +8 -0
- package/script/mod.d.ts +7 -0
- package/script/mod.d.ts.map +1 -0
- package/script/mod.js +20 -0
- package/script/package.json +3 -0
- package/script/retry/delay.d.ts +8 -0
- package/script/retry/delay.d.ts.map +1 -0
- package/script/retry/delay.js +32 -0
- package/script/retry/mod.d.ts +5 -0
- package/script/retry/mod.d.ts.map +1 -0
- package/script/retry/mod.js +12 -0
- package/script/retry/retry.d.ts +23 -0
- package/script/retry/retry.d.ts.map +1 -0
- package/script/retry/retry.js +134 -0
- package/script/retry/types.d.ts +59 -0
- package/script/retry/types.d.ts.map +1 -0
- package/script/retry/types.js +11 -0
- package/script/sleep/mod.d.ts +2 -0
- package/script/sleep/mod.d.ts.map +1 -0
- package/script/sleep/mod.js +5 -0
- package/script/sleep/sleep.d.ts +6 -0
- package/script/sleep/sleep.d.ts.map +1 -0
- package/script/sleep/sleep.js +10 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sleep = exports.calculateDelay = void 0;
|
|
4
|
+
const mod_js_1 = require("../sleep/mod.js");
|
|
5
|
+
Object.defineProperty(exports, "sleep", { enumerable: true, get: function () { return mod_js_1.sleep; } });
|
|
6
|
+
/**
|
|
7
|
+
* Calculate delay for next retry attempt based on strategy
|
|
8
|
+
*/
|
|
9
|
+
const calculateDelay = (attempt, options) => {
|
|
10
|
+
let delay;
|
|
11
|
+
switch (options.backoffStrategy) {
|
|
12
|
+
case 'exponential':
|
|
13
|
+
delay = options.initialDelayMs * Math.pow(2, attempt);
|
|
14
|
+
break;
|
|
15
|
+
case 'linear':
|
|
16
|
+
delay = options.initialDelayMs * (attempt + 1);
|
|
17
|
+
break;
|
|
18
|
+
case 'constant':
|
|
19
|
+
default:
|
|
20
|
+
delay = options.initialDelayMs;
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
// Cap at maxDelay
|
|
24
|
+
delay = Math.min(delay, options.maxDelayMs);
|
|
25
|
+
// Add jitter to prevent thundering herd
|
|
26
|
+
if (options.jitter) {
|
|
27
|
+
const jitterFactor = 0.8 + Math.random() * 0.4;
|
|
28
|
+
delay = Math.floor(delay * jitterFactor);
|
|
29
|
+
}
|
|
30
|
+
return delay;
|
|
31
|
+
};
|
|
32
|
+
exports.calculateDelay = calculateDelay;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { withRetry, retryPromise, makeRetryable, withRetryMetadata } from './retry.js';
|
|
2
|
+
export { calculateDelay } from './delay.js';
|
|
3
|
+
export { defaultRetryOptions } from './types.js';
|
|
4
|
+
export type { RetryOptions, RetryError, ExecutionAttempt, ExecutionMetadata } from './types.js';
|
|
5
|
+
//# sourceMappingURL=mod.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/retry/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AACtF,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAC3C,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAChD,YAAY,EAAE,YAAY,EAAE,UAAU,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultRetryOptions = exports.calculateDelay = exports.withRetryMetadata = exports.makeRetryable = exports.retryPromise = exports.withRetry = void 0;
|
|
4
|
+
var retry_js_1 = require("./retry.js");
|
|
5
|
+
Object.defineProperty(exports, "withRetry", { enumerable: true, get: function () { return retry_js_1.withRetry; } });
|
|
6
|
+
Object.defineProperty(exports, "retryPromise", { enumerable: true, get: function () { return retry_js_1.retryPromise; } });
|
|
7
|
+
Object.defineProperty(exports, "makeRetryable", { enumerable: true, get: function () { return retry_js_1.makeRetryable; } });
|
|
8
|
+
Object.defineProperty(exports, "withRetryMetadata", { enumerable: true, get: function () { return retry_js_1.withRetryMetadata; } });
|
|
9
|
+
var delay_js_1 = require("./delay.js");
|
|
10
|
+
Object.defineProperty(exports, "calculateDelay", { enumerable: true, get: function () { return delay_js_1.calculateDelay; } });
|
|
11
|
+
var types_js_1 = require("./types.js");
|
|
12
|
+
Object.defineProperty(exports, "defaultRetryOptions", { enumerable: true, get: function () { return types_js_1.defaultRetryOptions; } });
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Result } from '../deps/jsr.io/@wentools/result/0.1.3/src/mod.js';
|
|
2
|
+
import type { RetryOptions, RetryError, ExecutionMetadata } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Retry a function that returns a Result until it succeeds or max attempts reached
|
|
5
|
+
*/
|
|
6
|
+
declare const withRetry: <TSuccess, TError>(fn: () => Promise<Result<TSuccess, TError>>, options?: Partial<RetryOptions>) => Promise<Result<TSuccess, RetryError<TError>>>;
|
|
7
|
+
/**
|
|
8
|
+
* Retry a function that returns a Promise (not Result) until it succeeds
|
|
9
|
+
*/
|
|
10
|
+
declare const retryPromise: <TSuccess>(fn: () => Promise<TSuccess>, options?: Partial<RetryOptions>) => Promise<Result<TSuccess, RetryError<unknown>>>;
|
|
11
|
+
/**
|
|
12
|
+
* Create a retryable version of a function
|
|
13
|
+
*/
|
|
14
|
+
declare const makeRetryable: <TArgs extends unknown[], TSuccess, TError>(fn: (...args: TArgs) => Promise<Result<TSuccess, TError>>, defaultOptions?: Partial<RetryOptions>) => (...args: TArgs) => ((options?: Partial<RetryOptions>) => Promise<Result<TSuccess, RetryError<TError>>>);
|
|
15
|
+
/**
|
|
16
|
+
* Execute a Result-returning function with retry logic and full execution tracking
|
|
17
|
+
*
|
|
18
|
+
* Like withRetry, but returns detailed metadata about all attempts including
|
|
19
|
+
* timing, results, and delays. Useful for observability, monitoring, and audit trails.
|
|
20
|
+
*/
|
|
21
|
+
declare const withRetryMetadata: <TSuccess, TError>(fn: () => Promise<Result<TSuccess, TError>>, options?: Partial<RetryOptions>) => Promise<ExecutionMetadata<TSuccess, TError>>;
|
|
22
|
+
export { withRetry, retryPromise, makeRetryable, withRetryMetadata };
|
|
23
|
+
//# sourceMappingURL=retry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../../src/retry/retry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAW,MAAM,kDAAkD,CAAA;AACvF,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAoB,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAI/F;;GAEG;AACH,QAAA,MAAM,SAAS,GAAU,QAAQ,EAAE,MAAM,EACxC,IAAI,MAAM,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,EAC3C,UAAS,OAAO,CAAC,YAAY,CAAM,KACjC,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,UAAU,CAAC,MAAM,CAAC,CAAC,CA+C9C,CAAA;AAED;;GAEG;AACH,QAAA,MAAM,YAAY,GAAU,QAAQ,EACnC,IAAI,MAAM,OAAO,CAAC,QAAQ,CAAC,EAC3B,UAAS,OAAO,CAAC,YAAY,CAAM,KACjC,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC,CAW/C,CAAA;AAED;;GAEG;AACH,QAAA,MAAM,aAAa,GAAI,KAAK,SAAS,OAAO,EAAE,EAAE,QAAQ,EAAE,MAAM,EAC/D,IAAI,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,EACzD,iBAAgB,OAAO,CAAC,YAAY,CAAM,MAGzC,GAAG,MAAM,KAAK,KACZ,CAAC,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,YAAY,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAItF,CAAA;AAED;;;;;GAKG;AACH,QAAA,MAAM,iBAAiB,GAAU,QAAQ,EAAE,MAAM,EAChD,IAAI,MAAM,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,EAC3C,UAAS,OAAO,CAAC,YAAY,CAAM,KACjC,OAAO,CAAC,iBAAiB,CAAC,QAAQ,EAAE,MAAM,CAAC,CAoE7C,CAAA;AAED,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,iBAAiB,EAAE,CAAA"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.withRetryMetadata = exports.makeRetryable = exports.retryPromise = exports.withRetry = void 0;
|
|
4
|
+
const mod_js_1 = require("../deps/jsr.io/@wentools/result/0.1.3/src/mod.js");
|
|
5
|
+
const types_js_1 = require("./types.js");
|
|
6
|
+
const delay_js_1 = require("./delay.js");
|
|
7
|
+
/**
|
|
8
|
+
* Retry a function that returns a Result until it succeeds or max attempts reached
|
|
9
|
+
*/
|
|
10
|
+
const withRetry = async (fn, options = {}) => {
|
|
11
|
+
const opts = {
|
|
12
|
+
...types_js_1.defaultRetryOptions,
|
|
13
|
+
...options,
|
|
14
|
+
};
|
|
15
|
+
const errors = [];
|
|
16
|
+
let lastError;
|
|
17
|
+
for (let attempt = 0; attempt < opts.maxAttempts; attempt++) {
|
|
18
|
+
const result = await fn();
|
|
19
|
+
if (result.isOk()) {
|
|
20
|
+
return (0, mod_js_1.ok)(result.value);
|
|
21
|
+
}
|
|
22
|
+
lastError = result.error;
|
|
23
|
+
errors.push(result.error);
|
|
24
|
+
if (opts.shouldRetry && !opts.shouldRetry(result.error, attempt)) {
|
|
25
|
+
return (0, mod_js_1.err)('RETRY_EXHAUSTED', `Retry aborted after ${attempt + 1} attempts - error not retryable`, {
|
|
26
|
+
attempts: attempt + 1,
|
|
27
|
+
lastError,
|
|
28
|
+
errors,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
if (attempt < opts.maxAttempts - 1) {
|
|
32
|
+
const delayMs = (0, delay_js_1.calculateDelay)(attempt, opts);
|
|
33
|
+
if (opts.onRetry) {
|
|
34
|
+
opts.onRetry(attempt + 1, result.error, delayMs);
|
|
35
|
+
}
|
|
36
|
+
await (0, delay_js_1.sleep)(delayMs);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return (0, mod_js_1.err)('RETRY_EXHAUSTED', `All ${opts.maxAttempts} retry attempts failed`, {
|
|
40
|
+
attempts: opts.maxAttempts,
|
|
41
|
+
lastError: lastError,
|
|
42
|
+
errors,
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
exports.withRetry = withRetry;
|
|
46
|
+
/**
|
|
47
|
+
* Retry a function that returns a Promise (not Result) until it succeeds
|
|
48
|
+
*/
|
|
49
|
+
const retryPromise = async (fn, options = {}) => {
|
|
50
|
+
const resultFn = async () => {
|
|
51
|
+
try {
|
|
52
|
+
const value = await fn();
|
|
53
|
+
return (0, mod_js_1.ok)(value);
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
return (0, mod_js_1.err)('PROMISE_ERROR', 'Promise rejected', { cause: error });
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
return withRetry(resultFn, options);
|
|
60
|
+
};
|
|
61
|
+
exports.retryPromise = retryPromise;
|
|
62
|
+
/**
|
|
63
|
+
* Create a retryable version of a function
|
|
64
|
+
*/
|
|
65
|
+
const makeRetryable = (fn, defaultOptions = {}) => {
|
|
66
|
+
return (...args) => {
|
|
67
|
+
return (options) => withRetry(() => fn(...args), { ...defaultOptions, ...options });
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
exports.makeRetryable = makeRetryable;
|
|
71
|
+
/**
|
|
72
|
+
* Execute a Result-returning function with retry logic and full execution tracking
|
|
73
|
+
*
|
|
74
|
+
* Like withRetry, but returns detailed metadata about all attempts including
|
|
75
|
+
* timing, results, and delays. Useful for observability, monitoring, and audit trails.
|
|
76
|
+
*/
|
|
77
|
+
const withRetryMetadata = async (fn, options = {}) => {
|
|
78
|
+
const opts = {
|
|
79
|
+
...types_js_1.defaultRetryOptions,
|
|
80
|
+
...options,
|
|
81
|
+
};
|
|
82
|
+
if (opts.maxAttempts < 1) {
|
|
83
|
+
opts.maxAttempts = 1;
|
|
84
|
+
}
|
|
85
|
+
const attempts = [];
|
|
86
|
+
const overallStartTime = Date.now();
|
|
87
|
+
let previousDelayMs = 0;
|
|
88
|
+
for (let attempt = 0; attempt < opts.maxAttempts; attempt++) {
|
|
89
|
+
const attemptStartTime = Date.now();
|
|
90
|
+
const attemptTimestamp = new Date(attemptStartTime);
|
|
91
|
+
const result = await fn();
|
|
92
|
+
const attemptDuration = Date.now() - attemptStartTime;
|
|
93
|
+
attempts.push({
|
|
94
|
+
attemptNumber: attempt + 1,
|
|
95
|
+
duration: attemptDuration,
|
|
96
|
+
result,
|
|
97
|
+
timestamp: attemptTimestamp,
|
|
98
|
+
delayBeforeMs: previousDelayMs,
|
|
99
|
+
});
|
|
100
|
+
if (result.isOk()) {
|
|
101
|
+
const totalDuration = Date.now() - overallStartTime;
|
|
102
|
+
return {
|
|
103
|
+
result,
|
|
104
|
+
totalAttempts: attempt + 1,
|
|
105
|
+
totalDuration,
|
|
106
|
+
attempts,
|
|
107
|
+
succeeded: true,
|
|
108
|
+
retriesNeeded: attempt,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
if (opts.shouldRetry && !opts.shouldRetry(result.error, attempt)) {
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
if (attempt < opts.maxAttempts - 1) {
|
|
115
|
+
const delayMs = (0, delay_js_1.calculateDelay)(attempt, opts);
|
|
116
|
+
previousDelayMs = delayMs;
|
|
117
|
+
if (opts.onRetry) {
|
|
118
|
+
opts.onRetry(attempt + 1, result.error, delayMs);
|
|
119
|
+
}
|
|
120
|
+
await (0, delay_js_1.sleep)(delayMs);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const lastAttempt = attempts[attempts.length - 1];
|
|
124
|
+
const totalDuration = Date.now() - overallStartTime;
|
|
125
|
+
return {
|
|
126
|
+
result: lastAttempt.result,
|
|
127
|
+
totalAttempts: attempts.length,
|
|
128
|
+
totalDuration,
|
|
129
|
+
attempts,
|
|
130
|
+
succeeded: false,
|
|
131
|
+
retriesNeeded: attempts.length - 1,
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
exports.withRetryMetadata = withRetryMetadata;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { Result } from '../deps/jsr.io/@wentools/result/0.1.3/src/mod.js';
|
|
2
|
+
interface RetryOptions {
|
|
3
|
+
maxAttempts: number;
|
|
4
|
+
initialDelayMs: number;
|
|
5
|
+
maxDelayMs: number;
|
|
6
|
+
backoffStrategy: 'exponential' | 'linear' | 'constant';
|
|
7
|
+
jitter?: boolean;
|
|
8
|
+
shouldRetry?: <TError>(error: TError, attempt: number) => boolean;
|
|
9
|
+
onRetry?: <TError>(attempt: number, error: TError, nextDelayMs: number) => void;
|
|
10
|
+
}
|
|
11
|
+
interface RetryError<TOriginalError> {
|
|
12
|
+
type: 'RETRY_EXHAUSTED';
|
|
13
|
+
message: string;
|
|
14
|
+
attempts: number;
|
|
15
|
+
lastError: TOriginalError;
|
|
16
|
+
errors: TOriginalError[];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Single execution attempt of a retryable operation
|
|
20
|
+
*
|
|
21
|
+
* Represents one attempt to execute the operation, whether it's the
|
|
22
|
+
* first attempt or a retry. Includes timing and result information.
|
|
23
|
+
*/
|
|
24
|
+
interface ExecutionAttempt<TSuccess, TError> {
|
|
25
|
+
/** Attempt number (1 = first attempt, 2 = first retry, etc.) */
|
|
26
|
+
attemptNumber: number;
|
|
27
|
+
/** Duration of this specific attempt in milliseconds */
|
|
28
|
+
duration: number;
|
|
29
|
+
/** Result of this attempt (Ok or Err) */
|
|
30
|
+
result: Result<TSuccess, TError>;
|
|
31
|
+
/** Timestamp when this attempt started */
|
|
32
|
+
timestamp: Date;
|
|
33
|
+
/** Delay before this attempt (0 for first attempt) */
|
|
34
|
+
delayBeforeMs: number;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Complete execution metadata for a retry operation
|
|
38
|
+
*
|
|
39
|
+
* Contains the final result plus full execution history of all attempts.
|
|
40
|
+
* Provides rich context for monitoring, debugging, and audit trails.
|
|
41
|
+
*/
|
|
42
|
+
interface ExecutionMetadata<TSuccess, TError> {
|
|
43
|
+
/** The final result of the operation (after all attempts) */
|
|
44
|
+
result: Result<TSuccess, TError>;
|
|
45
|
+
/** Total number of attempts made (1 = no retries, 2 = 1 retry, etc.) */
|
|
46
|
+
totalAttempts: number;
|
|
47
|
+
/** Total wall-clock duration including all delays in milliseconds */
|
|
48
|
+
totalDuration: number;
|
|
49
|
+
/** Complete history of all execution attempts */
|
|
50
|
+
attempts: ExecutionAttempt<TSuccess, TError>[];
|
|
51
|
+
/** Convenience: whether the operation ultimately succeeded */
|
|
52
|
+
succeeded: boolean;
|
|
53
|
+
/** Convenience: number of retries needed (totalAttempts - 1) */
|
|
54
|
+
retriesNeeded: number;
|
|
55
|
+
}
|
|
56
|
+
declare const defaultRetryOptions: Partial<RetryOptions>;
|
|
57
|
+
export type { RetryOptions, RetryError, ExecutionAttempt, ExecutionMetadata };
|
|
58
|
+
export { defaultRetryOptions };
|
|
59
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/retry/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kDAAkD,CAAA;AAE9E,UAAU,YAAY;IACrB,WAAW,EAAE,MAAM,CAAA;IACnB,cAAc,EAAE,MAAM,CAAA;IACtB,UAAU,EAAE,MAAM,CAAA;IAClB,eAAe,EAAE,aAAa,GAAG,QAAQ,GAAG,UAAU,CAAA;IACtD,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,WAAW,CAAC,EAAE,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,OAAO,CAAA;IACjE,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,KAAK,IAAI,CAAA;CAC/E;AAED,UAAU,UAAU,CAAC,cAAc;IAClC,IAAI,EAAE,iBAAiB,CAAA;IACvB,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,MAAM,CAAA;IAChB,SAAS,EAAE,cAAc,CAAA;IACzB,MAAM,EAAE,cAAc,EAAE,CAAA;CACxB;AAED;;;;;GAKG;AACH,UAAU,gBAAgB,CAAC,QAAQ,EAAE,MAAM;IAC1C,gEAAgE;IAChE,aAAa,EAAE,MAAM,CAAA;IAErB,wDAAwD;IACxD,QAAQ,EAAE,MAAM,CAAA;IAEhB,yCAAyC;IACzC,MAAM,EAAE,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAA;IAEhC,0CAA0C;IAC1C,SAAS,EAAE,IAAI,CAAA;IAEf,sDAAsD;IACtD,aAAa,EAAE,MAAM,CAAA;CACrB;AAED;;;;;GAKG;AACH,UAAU,iBAAiB,CAAC,QAAQ,EAAE,MAAM;IAC3C,6DAA6D;IAC7D,MAAM,EAAE,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAA;IAEhC,wEAAwE;IACxE,aAAa,EAAE,MAAM,CAAA;IAErB,qEAAqE;IACrE,aAAa,EAAE,MAAM,CAAA;IAErB,iDAAiD;IACjD,QAAQ,EAAE,gBAAgB,CAAC,QAAQ,EAAE,MAAM,CAAC,EAAE,CAAA;IAE9C,8DAA8D;IAC9D,SAAS,EAAE,OAAO,CAAA;IAElB,gEAAgE;IAChE,aAAa,EAAE,MAAM,CAAA;CACrB;AAED,QAAA,MAAM,mBAAmB,EAAE,OAAO,CAAC,YAAY,CAM9C,CAAA;AAED,YAAY,EAAE,YAAY,EAAE,UAAU,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,CAAA;AAC7E,OAAO,EAAE,mBAAmB,EAAE,CAAA"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultRetryOptions = void 0;
|
|
4
|
+
const defaultRetryOptions = {
|
|
5
|
+
maxAttempts: 3,
|
|
6
|
+
initialDelayMs: 1000,
|
|
7
|
+
maxDelayMs: 30000,
|
|
8
|
+
backoffStrategy: 'exponential',
|
|
9
|
+
jitter: true,
|
|
10
|
+
};
|
|
11
|
+
exports.defaultRetryOptions = defaultRetryOptions;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/sleep/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sleep.d.ts","sourceRoot":"","sources":["../../src/sleep/sleep.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,QAAA,MAAM,KAAK,GAAI,IAAI,MAAM,KAAG,OAAO,CAAC,IAAI,CAEvC,CAAA;AAED,OAAO,EAAE,KAAK,EAAE,CAAA"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sleep = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Sleep for specified milliseconds
|
|
6
|
+
*/
|
|
7
|
+
const sleep = (ms) => {
|
|
8
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
9
|
+
};
|
|
10
|
+
exports.sleep = sleep;
|