@backendkit-labs/retry 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +801 -0
- package/dist/chunk-HOW7IDKS.cjs +752 -0
- package/dist/chunk-HOW7IDKS.cjs.map +1 -0
- package/dist/chunk-ZWXN4OXT.js +734 -0
- package/dist/chunk-ZWXN4OXT.js.map +1 -0
- package/dist/index.cjs +98 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +207 -0
- package/dist/index.d.ts +207 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/nestjs/index.cjs +108 -0
- package/dist/nestjs/index.cjs.map +1 -0
- package/dist/nestjs/index.d.cts +43 -0
- package/dist/nestjs/index.d.ts +43 -0
- package/dist/nestjs/index.js +104 -0
- package/dist/nestjs/index.js.map +1 -0
- package/dist/retry.engine-DSm8xWKJ.d.cts +202 -0
- package/dist/retry.engine-DSm8xWKJ.d.ts +202 -0
- package/package.json +100 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { Result } from '@backendkit-labs/result';
|
|
2
|
+
|
|
3
|
+
declare class RetryRegistry {
|
|
4
|
+
private readonly engines;
|
|
5
|
+
getOrCreate(name: string, config?: Partial<RetryEngineConfig>): RetryEngine;
|
|
6
|
+
get(name: string): RetryEngine | undefined;
|
|
7
|
+
reset(name: string): void;
|
|
8
|
+
resetAll(): void;
|
|
9
|
+
getAllMetrics(): Record<string, RetryMetricsSnapshot>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
type JitterType = 'full' | 'equal' | 'decorrelated';
|
|
13
|
+
interface BackoffStrategy {
|
|
14
|
+
/** Compute delay in ms for attempt number (1-based). */
|
|
15
|
+
nextDelay(attempt: number): number;
|
|
16
|
+
/** Reset internal state (for stateful strategies like decorrelated jitter). */
|
|
17
|
+
reset(): void;
|
|
18
|
+
/** Deep clone with optional overrides. */
|
|
19
|
+
clone(overrides?: Partial<BackoffConfig>): BackoffStrategy;
|
|
20
|
+
}
|
|
21
|
+
type BackoffConfig = {
|
|
22
|
+
type: 'fixed';
|
|
23
|
+
baseDelay: number;
|
|
24
|
+
} | {
|
|
25
|
+
type: 'linear';
|
|
26
|
+
baseDelay: number;
|
|
27
|
+
multiplier?: number;
|
|
28
|
+
maxDelay?: number;
|
|
29
|
+
} | {
|
|
30
|
+
type: 'exponential';
|
|
31
|
+
baseDelay: number;
|
|
32
|
+
multiplier?: number;
|
|
33
|
+
maxDelay?: number;
|
|
34
|
+
jitter?: JitterType;
|
|
35
|
+
};
|
|
36
|
+
interface RetryCondition {
|
|
37
|
+
shouldRetry(error: RetryErrorPayload): boolean | Promise<boolean>;
|
|
38
|
+
}
|
|
39
|
+
interface AbortCondition {
|
|
40
|
+
shouldAbort(error: RetryErrorPayload): boolean | Promise<boolean>;
|
|
41
|
+
}
|
|
42
|
+
type RetryConditionFn = (error: RetryErrorPayload) => boolean | Promise<boolean>;
|
|
43
|
+
type AbortConditionFn = (error: RetryErrorPayload) => boolean | Promise<boolean>;
|
|
44
|
+
interface ClassifierRule {
|
|
45
|
+
name: string;
|
|
46
|
+
priority?: number;
|
|
47
|
+
match: (error: RetryErrorPayload) => boolean;
|
|
48
|
+
classification: ErrorClassification;
|
|
49
|
+
}
|
|
50
|
+
type ErrorClassification = 'transient' | 'permanent';
|
|
51
|
+
interface RetryBudgetConfig {
|
|
52
|
+
windowMs: number;
|
|
53
|
+
maxRetryRatio: number;
|
|
54
|
+
minRequestCount: number;
|
|
55
|
+
}
|
|
56
|
+
interface TimeoutConfig {
|
|
57
|
+
globalTimeoutMs: number;
|
|
58
|
+
attemptTimeoutMs: number;
|
|
59
|
+
}
|
|
60
|
+
interface IdempotencyStore {
|
|
61
|
+
get(key: string): Promise<string | null>;
|
|
62
|
+
set(key: string, value: string, ttlMs?: number): Promise<void>;
|
|
63
|
+
exists(key: string): Promise<boolean>;
|
|
64
|
+
}
|
|
65
|
+
interface IdempotencyConfig {
|
|
66
|
+
enabled: boolean;
|
|
67
|
+
store?: IdempotencyStore;
|
|
68
|
+
idempotentMethods: string[];
|
|
69
|
+
headerName: string;
|
|
70
|
+
ttlMs: number;
|
|
71
|
+
}
|
|
72
|
+
interface RetryHooks {
|
|
73
|
+
beforeRetry?: (ctx: BeforeRetryContext) => void | Promise<void>;
|
|
74
|
+
afterRetry?: (ctx: AfterRetryContext) => void | Promise<void>;
|
|
75
|
+
onRetrySuccess?: (ctx: RetrySuccessContext) => void | Promise<void>;
|
|
76
|
+
onExhausted?: (ctx: ExhaustedContext) => void | Promise<void>;
|
|
77
|
+
onBudgetExhausted?: (ctx: BudgetExhaustedContext) => void | Promise<void>;
|
|
78
|
+
}
|
|
79
|
+
interface BeforeRetryContext {
|
|
80
|
+
attempt: number;
|
|
81
|
+
error: RetryErrorPayload;
|
|
82
|
+
delayMs: number;
|
|
83
|
+
correlationId?: string;
|
|
84
|
+
}
|
|
85
|
+
interface AfterRetryContext {
|
|
86
|
+
attempt: number;
|
|
87
|
+
error: RetryErrorPayload;
|
|
88
|
+
delayMs: number;
|
|
89
|
+
correlationId?: string;
|
|
90
|
+
}
|
|
91
|
+
interface RetrySuccessContext {
|
|
92
|
+
attempt: number;
|
|
93
|
+
totalAttempts: number;
|
|
94
|
+
totalElapsedMs: number;
|
|
95
|
+
correlationId?: string;
|
|
96
|
+
}
|
|
97
|
+
interface ExhaustedContext {
|
|
98
|
+
lastError: RetryErrorPayload;
|
|
99
|
+
totalAttempts: number;
|
|
100
|
+
totalElapsedMs: number;
|
|
101
|
+
correlationId?: string;
|
|
102
|
+
}
|
|
103
|
+
interface BudgetExhaustedContext {
|
|
104
|
+
correlationId?: string;
|
|
105
|
+
}
|
|
106
|
+
interface RetryErrorPayload {
|
|
107
|
+
type: ErrorType;
|
|
108
|
+
message: string;
|
|
109
|
+
status?: number;
|
|
110
|
+
cause?: unknown;
|
|
111
|
+
attempt: number;
|
|
112
|
+
elapsedMs: number;
|
|
113
|
+
}
|
|
114
|
+
type ErrorType = 'http' | 'network' | 'timeout' | 'circuit-open' | 'bulkhead-rejected' | 'business' | 'unknown';
|
|
115
|
+
interface RetryMetadata {
|
|
116
|
+
attempts: number;
|
|
117
|
+
totalElapsedMs: number;
|
|
118
|
+
lastError?: RetryErrorPayload;
|
|
119
|
+
budgetExhausted?: boolean;
|
|
120
|
+
circuitOpen?: boolean;
|
|
121
|
+
}
|
|
122
|
+
type RetryError = RetryErrorPayload & {
|
|
123
|
+
metadata: RetryMetadata;
|
|
124
|
+
};
|
|
125
|
+
interface RetryMetricsSnapshot {
|
|
126
|
+
totalAttempts: number;
|
|
127
|
+
totalSuccesses: number;
|
|
128
|
+
totalFailures: number;
|
|
129
|
+
totalExhausted: number;
|
|
130
|
+
totalBudgetExhausted: number;
|
|
131
|
+
totalTimeouts: number;
|
|
132
|
+
}
|
|
133
|
+
interface RetryConfig {
|
|
134
|
+
maxAttempts: number;
|
|
135
|
+
backoff: BackoffConfig | BackoffStrategy;
|
|
136
|
+
retryIf?: RetryCondition | RetryConditionFn;
|
|
137
|
+
abortIf?: AbortCondition | AbortConditionFn;
|
|
138
|
+
timeout?: Partial<TimeoutConfig>;
|
|
139
|
+
budget?: Partial<RetryBudgetConfig>;
|
|
140
|
+
idempotency?: Partial<IdempotencyConfig>;
|
|
141
|
+
classifiers?: ClassifierRule[];
|
|
142
|
+
/** Dynamic delay based on error (overrides backoff). */
|
|
143
|
+
dynamicDelay?: (error: RetryErrorPayload, attempt: number) => number;
|
|
144
|
+
hooks?: RetryHooks;
|
|
145
|
+
/** Fallback value/function when retries exhausted. */
|
|
146
|
+
fallback?: (error: RetryErrorPayload) => unknown | Promise<unknown>;
|
|
147
|
+
correlationId?: string;
|
|
148
|
+
}
|
|
149
|
+
interface RetryEngineConfig {
|
|
150
|
+
name: string;
|
|
151
|
+
/** Reference to registry (optional, for shared config across services). */
|
|
152
|
+
registry?: RetryRegistry;
|
|
153
|
+
/** Default config applied to all execute() calls. */
|
|
154
|
+
defaultConfig?: Partial<RetryConfig>;
|
|
155
|
+
/** External integrations (optional -- duck-typed at runtime). */
|
|
156
|
+
integrations?: {
|
|
157
|
+
circuitBreaker?: CircuitBreakerLike;
|
|
158
|
+
bulkhead?: BulkheadLike;
|
|
159
|
+
observability?: {
|
|
160
|
+
logger?: BkLoggerLike;
|
|
161
|
+
metrics?: MetricsEmitterLike;
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
interface CircuitBreakerLike {
|
|
166
|
+
canAttempt(): boolean;
|
|
167
|
+
onSuccess(durationMs: number): void;
|
|
168
|
+
onError(error: unknown): void;
|
|
169
|
+
getState(): unknown;
|
|
170
|
+
}
|
|
171
|
+
interface BulkheadLike {
|
|
172
|
+
execute<T>(fn: () => Promise<T>): Promise<T>;
|
|
173
|
+
isFull(): boolean;
|
|
174
|
+
}
|
|
175
|
+
interface BkLoggerLike {
|
|
176
|
+
info(msg: string, meta?: unknown): void;
|
|
177
|
+
warn(msg: string, meta?: unknown): void;
|
|
178
|
+
error(msg: string, meta?: unknown): void;
|
|
179
|
+
}
|
|
180
|
+
interface MetricsEmitterLike {
|
|
181
|
+
emit(event: MetricEvent): void;
|
|
182
|
+
}
|
|
183
|
+
interface MetricEvent {
|
|
184
|
+
name: string;
|
|
185
|
+
value: number;
|
|
186
|
+
tags?: Record<string, string | number>;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
declare class RetryEngine {
|
|
190
|
+
private config;
|
|
191
|
+
private metrics;
|
|
192
|
+
constructor(config: RetryEngineConfig);
|
|
193
|
+
execute<T>(task: () => Promise<T>, options?: Partial<RetryConfig>): Promise<Result<T, RetryError>>;
|
|
194
|
+
executeWithContext<T>(task: () => Promise<T>, context: {
|
|
195
|
+
correlationId?: string;
|
|
196
|
+
}, options?: Partial<RetryConfig>): Promise<Result<T, RetryError>>;
|
|
197
|
+
updateDefaults(partial: Partial<RetryConfig>): void;
|
|
198
|
+
getMetrics(): RetryMetricsSnapshot;
|
|
199
|
+
resetMetrics(): void;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export { type AbortCondition as A, type BackoffStrategy as B, type ClassifierRule as C, type ErrorClassification as E, type IdempotencyConfig as I, type JitterType as J, type MetricEvent as M, type RetryConfig as R, type TimeoutConfig as T, type RetryError as a, type BackoffConfig as b, type RetryCondition as c, type RetryErrorPayload as d, type RetryBudgetConfig as e, type IdempotencyStore as f, type AbortConditionFn as g, type AfterRetryContext as h, type BeforeRetryContext as i, type BkLoggerLike as j, type BudgetExhaustedContext as k, type BulkheadLike as l, type CircuitBreakerLike as m, type ErrorType as n, type ExhaustedContext as o, type MetricsEmitterLike as p, type RetryConditionFn as q, RetryEngine as r, type RetryEngineConfig as s, type RetryHooks as t, type RetryMetadata as u, type RetryMetricsSnapshot as v, RetryRegistry as w, type RetrySuccessContext as x };
|
package/package.json
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@backendkit-labs/retry",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "BackendKit Labs",
|
|
7
|
+
"email": "hello@backendkitlabs.dev"
|
|
8
|
+
},
|
|
9
|
+
"description": "Enterprise-grade retry library for Node.js -- exponential backoff, sliding window budget, error classification, duck-typed integrations, and optional NestJS support",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "./dist/index.cjs",
|
|
12
|
+
"module": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"import": "./dist/index.js",
|
|
18
|
+
"require": "./dist/index.cjs"
|
|
19
|
+
},
|
|
20
|
+
"./nestjs": {
|
|
21
|
+
"types": "./dist/nestjs/index.d.ts",
|
|
22
|
+
"import": "./dist/nestjs/index.js",
|
|
23
|
+
"require": "./dist/nestjs/index.cjs"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist",
|
|
28
|
+
"README.md",
|
|
29
|
+
"LICENSE"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsup",
|
|
33
|
+
"dev": "tsup --watch",
|
|
34
|
+
"test": "vitest run",
|
|
35
|
+
"test:watch": "vitest",
|
|
36
|
+
"test:coverage": "vitest run --coverage",
|
|
37
|
+
"typecheck": "tsc --noEmit",
|
|
38
|
+
"lint": "eslint src/",
|
|
39
|
+
"format": "prettier --write src/",
|
|
40
|
+
"prepublishOnly": "npm run build && npm run test && npm run lint"
|
|
41
|
+
},
|
|
42
|
+
"keywords": [
|
|
43
|
+
"retry",
|
|
44
|
+
"again",
|
|
45
|
+
"resilience",
|
|
46
|
+
"backoff",
|
|
47
|
+
"fault-tolerance",
|
|
48
|
+
"circuit-breaker",
|
|
49
|
+
"bulkhead",
|
|
50
|
+
"nestjs",
|
|
51
|
+
"node",
|
|
52
|
+
"backend"
|
|
53
|
+
],
|
|
54
|
+
"homepage": "https://backendkitlabs.dev/docs/retry/",
|
|
55
|
+
"repository": {
|
|
56
|
+
"type": "git",
|
|
57
|
+
"url": "git+https://github.com/BackendKit-labs/backendkit-monorepo.git",
|
|
58
|
+
"directory": "packages/retry"
|
|
59
|
+
},
|
|
60
|
+
"bugs": {
|
|
61
|
+
"url": "https://github.com/BackendKit-labs/backendkit-monorepo/issues"
|
|
62
|
+
},
|
|
63
|
+
"publishConfig": {
|
|
64
|
+
"access": "public"
|
|
65
|
+
},
|
|
66
|
+
"sideEffects": false,
|
|
67
|
+
"engines": {
|
|
68
|
+
"node": ">=18"
|
|
69
|
+
},
|
|
70
|
+
"peerDependencies": {
|
|
71
|
+
"@nestjs/common": ">=10.0.0",
|
|
72
|
+
"@nestjs/core": ">=10.0.0",
|
|
73
|
+
"rxjs": ">=7.0.0"
|
|
74
|
+
},
|
|
75
|
+
"peerDependenciesMeta": {
|
|
76
|
+
"@nestjs/common": {
|
|
77
|
+
"optional": true
|
|
78
|
+
},
|
|
79
|
+
"@nestjs/core": {
|
|
80
|
+
"optional": true
|
|
81
|
+
},
|
|
82
|
+
"rxjs": {
|
|
83
|
+
"optional": true
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
"devDependencies": {
|
|
87
|
+
"@eslint/js": "^9.39.4",
|
|
88
|
+
"@nestjs/common": "^10.0.0",
|
|
89
|
+
"@nestjs/core": "^10.0.0",
|
|
90
|
+
"@types/node": "^22.0.0",
|
|
91
|
+
"eslint": "^9.0.0",
|
|
92
|
+
"prettier": "^3.0.0",
|
|
93
|
+
"reflect-metadata": "^0.2.0",
|
|
94
|
+
"rxjs": "^7.8.0",
|
|
95
|
+
"tsup": "^8.0.0",
|
|
96
|
+
"typescript": "^5.5.0",
|
|
97
|
+
"typescript-eslint": "^8.59.3",
|
|
98
|
+
"vitest": "^2.0.0"
|
|
99
|
+
}
|
|
100
|
+
}
|