@nexusts/resilience 0.7.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/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # @nexusts/resilience
2
+
3
+ > **NexusTS** — Bun-native fullstack framework
4
+
5
+ ## Description
6
+
7
+ Retry + Circuit Breaker + Bulkhead.
8
+
9
+ Three primitives in a single DI singleton. retry() for backoff. CircuitBreaker for closed → open → half-open state machine. Bulkhead for FIFO concurrency limits. Shared named registry. **Zero external dependencies.**
10
+
11
+ ## Install
12
+
13
+ This module is part of the NexusTS monorepo. Each module is published as its own npm package under the `@nexusts/` scope.
14
+
15
+ Most apps start with just the core:
16
+
17
+ ```bash
18
+ bun add @nexusts/core reflect-metadata zod hono
19
+ ```
20
+
21
+ Then add this module only if you need it:
22
+
23
+ ```bash
24
+ bun add @nexusts/resilience
25
+ ```
26
+
27
+ ## Peer dependencies
28
+
29
+ None. This module is fully self-contained.
30
+
31
+ ## Usage
32
+
33
+ ```typescript
34
+ import { /* public API */ } from "@nexusts/resilience";
35
+ ```
36
+
37
+ See the [user guide](../../docs/user-guide/resilience.md) and the [example app](../../examples/) for a working demo.
38
+
39
+ ## License
40
+
41
+ MIT — see the root [LICENSE](../../LICENSE).
package/dist/index.js ADDED
@@ -0,0 +1,570 @@
1
+ // @bun
2
+ var __legacyDecorateClassTS = function(decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
5
+ r = Reflect.decorate(decorators, target, key, desc);
6
+ else
7
+ for (var i = decorators.length - 1;i >= 0; i--)
8
+ if (d = decorators[i])
9
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
10
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
11
+ };
12
+ var __legacyDecorateParamTS = (index, decorator) => (target, key) => decorator(target, key, index);
13
+ var __legacyMetadataTS = (k, v) => {
14
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
15
+ return Reflect.metadata(k, v);
16
+ };
17
+
18
+ // packages/resilience/src/types.ts
19
+ var RESILIENCE_META = Symbol.for("nexus:Resilience:Meta");
20
+ // packages/resilience/src/retry.ts
21
+ var DEFAULTS = {
22
+ attempts: 3,
23
+ initialDelay: 100,
24
+ maxDelay: 30000,
25
+ backoff: "exponential-jitter",
26
+ multiplier: 2
27
+ };
28
+ var defaultRetryOn = (err) => {
29
+ if (err == null)
30
+ return false;
31
+ const name = err?.name ?? "";
32
+ if (name === "AbortError" || name === "CancellationError")
33
+ return false;
34
+ return true;
35
+ };
36
+ function sleep(ms, signal) {
37
+ return new Promise((resolve, reject) => {
38
+ if (signal?.aborted) {
39
+ reject(signal.reason ?? new DOMException("Aborted", "AbortError"));
40
+ return;
41
+ }
42
+ const id = setTimeout(() => {
43
+ signal?.removeEventListener("abort", onAbort);
44
+ resolve();
45
+ }, ms);
46
+ const onAbort = () => {
47
+ clearTimeout(id);
48
+ reject(signal?.reason ?? new DOMException("Aborted", "AbortError"));
49
+ };
50
+ signal?.addEventListener("abort", onAbort, { once: true });
51
+ });
52
+ }
53
+ function computeBackoff(attempt, cfg) {
54
+ const { initialDelay, maxDelay, backoff, multiplier } = cfg;
55
+ let raw;
56
+ switch (backoff) {
57
+ case "constant":
58
+ raw = initialDelay;
59
+ break;
60
+ case "linear":
61
+ raw = initialDelay * attempt;
62
+ break;
63
+ case "exponential":
64
+ raw = initialDelay * Math.pow(multiplier, attempt - 1);
65
+ break;
66
+ case "exponential-jitter": {
67
+ const base = initialDelay * Math.pow(multiplier, attempt - 1);
68
+ raw = Math.random() * base;
69
+ break;
70
+ }
71
+ default:
72
+ raw = initialDelay;
73
+ }
74
+ return Math.min(raw, maxDelay);
75
+ }
76
+ async function retry(fn, cfg = {}) {
77
+ const attempts = Math.max(1, cfg.attempts ?? DEFAULTS.attempts);
78
+ const initialDelay = cfg.initialDelay ?? DEFAULTS.initialDelay;
79
+ const maxDelay = cfg.maxDelay ?? DEFAULTS.maxDelay;
80
+ const backoff = cfg.backoff ?? DEFAULTS.backoff;
81
+ const multiplier = cfg.multiplier ?? DEFAULTS.multiplier;
82
+ const retryOn = cfg.retryOn ?? defaultRetryOn;
83
+ const onRetry = cfg.onRetry;
84
+ const overallTimeout = cfg.timeout;
85
+ const ac = new AbortController;
86
+ const overallTimer = overallTimeout ? setTimeout(() => ac.abort(new Error("retry: overall timeout exceeded")), overallTimeout) : undefined;
87
+ const normalized = { initialDelay, maxDelay, backoff, multiplier };
88
+ let lastErr;
89
+ for (let attempt = 1;attempt <= attempts; attempt++) {
90
+ if (ac.signal.aborted)
91
+ break;
92
+ try {
93
+ return await fn(ac.signal);
94
+ } catch (err) {
95
+ lastErr = err;
96
+ if (attempt >= attempts)
97
+ break;
98
+ if (!retryOn(err, attempt))
99
+ break;
100
+ const delay = computeBackoff(attempt, normalized);
101
+ if (onRetry) {
102
+ try {
103
+ onRetry(err, attempt, delay);
104
+ } catch {}
105
+ }
106
+ try {
107
+ await sleep(delay, ac.signal);
108
+ } catch (sleepErr) {
109
+ lastErr = sleepErr;
110
+ break;
111
+ }
112
+ }
113
+ }
114
+ if (overallTimer)
115
+ clearTimeout(overallTimer);
116
+ throw lastErr;
117
+ }
118
+ // packages/resilience/src/circuit-breaker.ts
119
+ var DEFAULTS2 = {
120
+ threshold: 0.5,
121
+ minCalls: 5,
122
+ timeout: 30000,
123
+ halfOpenAfter: 1,
124
+ window: 60000
125
+ };
126
+ var defaultIsFailure = (_err) => true;
127
+
128
+ class CircuitOpenError extends Error {
129
+ name = "CircuitOpenError";
130
+ circuit;
131
+ nextRetryAt;
132
+ constructor(circuit, nextRetryAt) {
133
+ super(`Circuit "${circuit}" is open; retry after ${nextRetryAt}ms`);
134
+ this.circuit = circuit;
135
+ this.nextRetryAt = nextRetryAt;
136
+ }
137
+ }
138
+
139
+ class CircuitBreaker {
140
+ name;
141
+ config;
142
+ state = "closed";
143
+ samples = [];
144
+ openedAt = 0;
145
+ halfOpenInFlight = 0;
146
+ halfOpenAllowed = 0;
147
+ constructor(name, config = {}) {
148
+ this.name = name;
149
+ this.config = {
150
+ threshold: config.threshold ?? DEFAULTS2.threshold,
151
+ minCalls: config.minCalls ?? DEFAULTS2.minCalls,
152
+ timeout: config.timeout ?? DEFAULTS2.timeout,
153
+ halfOpenAfter: config.halfOpenAfter ?? DEFAULTS2.halfOpenAfter,
154
+ window: config.window ?? DEFAULTS2.window,
155
+ isFailure: config.isFailure ?? defaultIsFailure
156
+ };
157
+ }
158
+ get currentState() {
159
+ if (this.state === "open" && Date.now() - this.openedAt >= this.config.timeout) {
160
+ this.transition("half-open");
161
+ this.halfOpenAllowed = this.config.halfOpenAfter;
162
+ this.halfOpenInFlight = 0;
163
+ }
164
+ return this.state;
165
+ }
166
+ async execute(fn) {
167
+ const state = this.currentState;
168
+ if (state === "open") {
169
+ throw new CircuitOpenError(this.name, this.openedAt + this.config.timeout);
170
+ }
171
+ if (state === "half-open") {
172
+ if (this.halfOpenInFlight >= this.halfOpenAllowed) {
173
+ throw new CircuitOpenError(this.name, this.openedAt + this.config.timeout);
174
+ }
175
+ this.halfOpenInFlight += 1;
176
+ }
177
+ const start = Date.now();
178
+ let ok = false;
179
+ try {
180
+ const result = await fn();
181
+ ok = true;
182
+ return result;
183
+ } finally {
184
+ const latency = Date.now() - start;
185
+ this.record(ok, latency, state);
186
+ }
187
+ }
188
+ record(ok, latency, stateAtCall) {
189
+ this.config;
190
+ const now = Date.now();
191
+ this.samples.push({ ts: now, ok });
192
+ const cutoff = now - this.config.window;
193
+ while (this.samples.length > 0 && this.samples[0].ts < cutoff) {
194
+ this.samples.shift();
195
+ }
196
+ const failed = ok ? 0 : 1;
197
+ if (stateAtCall === "half-open") {
198
+ this.halfOpenInFlight = Math.max(0, this.halfOpenInFlight - 1);
199
+ if (!ok) {
200
+ this.openedAt = now;
201
+ this.transition("open");
202
+ } else if (this.halfOpenInFlight === 0) {
203
+ this.transition("closed");
204
+ this.samples = [];
205
+ }
206
+ return;
207
+ }
208
+ if (!ok) {
209
+ const total = this.samples.length;
210
+ if (total >= this.config.minCalls) {
211
+ const failures = this.samples.filter((s) => !s.ok).length;
212
+ const ratio = failures / total;
213
+ if (ratio >= this.config.threshold) {
214
+ this.openedAt = now;
215
+ this.transition("open");
216
+ }
217
+ }
218
+ } else {
219
+ if (this.samples.length > this.config.minCalls * 4) {
220
+ this.samples = this.samples.slice(-this.config.minCalls);
221
+ }
222
+ }
223
+ try {
224
+ this.config;
225
+ } catch {}
226
+ }
227
+ transition(to) {
228
+ const from = this.state;
229
+ if (from === to)
230
+ return;
231
+ this.state = to;
232
+ this.fireHook(from, to);
233
+ }
234
+ _onStateChange;
235
+ _onCall;
236
+ fireHook(from, to) {
237
+ try {
238
+ this._onStateChange?.(from, to, this.name);
239
+ } catch {}
240
+ }
241
+ }
242
+ // packages/resilience/src/bulkhead.ts
243
+ var DEFAULTS3 = {
244
+ maxConcurrent: 10,
245
+ maxQueued: 100,
246
+ rejectOnFull: false
247
+ };
248
+
249
+ class BulkheadFullError extends Error {
250
+ name_;
251
+ name = "BulkheadFullError";
252
+ constructor(name_) {
253
+ super(`Bulkhead "${name_}" is at capacity`);
254
+ this.name_ = name_;
255
+ }
256
+ }
257
+
258
+ class Bulkhead {
259
+ name;
260
+ config;
261
+ inFlight = 0;
262
+ queue = [];
263
+ constructor(config = {}) {
264
+ this.name = config.name ?? "bulkhead";
265
+ this.config = {
266
+ maxConcurrent: config.maxConcurrent ?? DEFAULTS3.maxConcurrent,
267
+ maxQueued: config.maxQueued ?? DEFAULTS3.maxQueued,
268
+ rejectOnFull: config.rejectOnFull ?? DEFAULTS3.rejectOnFull
269
+ };
270
+ }
271
+ get stats() {
272
+ return { inFlight: this.inFlight, queued: this.queue.length };
273
+ }
274
+ async execute(fn) {
275
+ if (this.inFlight < this.config.maxConcurrent) {
276
+ this.inFlight += 1;
277
+ try {
278
+ return await fn();
279
+ } finally {
280
+ this.inFlight -= 1;
281
+ this.drain();
282
+ }
283
+ }
284
+ if (this.queue.length >= this.config.maxQueued) {
285
+ throw new BulkheadFullError(this.name);
286
+ }
287
+ const token = this.enqueue();
288
+ try {
289
+ await token.acquire();
290
+ } catch (err) {
291
+ throw err;
292
+ }
293
+ this.inFlight += 1;
294
+ try {
295
+ return await fn();
296
+ } finally {
297
+ this.inFlight -= 1;
298
+ this.drain();
299
+ }
300
+ }
301
+ enqueue() {
302
+ let resolve;
303
+ let reject;
304
+ const promise = new Promise((res, rej) => {
305
+ resolve = res;
306
+ reject = rej;
307
+ });
308
+ const token = {
309
+ acquired: false,
310
+ cancelled: false,
311
+ resolve,
312
+ reject
313
+ };
314
+ this.queue.push(token);
315
+ return Object.assign(promise, {
316
+ acquire: () => promise,
317
+ cancel: () => {
318
+ if (token.acquired)
319
+ return;
320
+ token.cancelled = true;
321
+ token.reject(new DOMException("Cancelled", "AbortError"));
322
+ }
323
+ });
324
+ }
325
+ drain() {
326
+ while (this.inFlight < this.config.maxConcurrent && this.queue.length > 0) {
327
+ const next = this.queue.shift();
328
+ if (next.cancelled)
329
+ continue;
330
+ next.acquired = true;
331
+ next.resolve();
332
+ return;
333
+ }
334
+ }
335
+ }
336
+ // packages/resilience/src/resilience.service.ts
337
+ import"reflect-metadata";
338
+ import { Inject, Injectable } from "@nexusts/core/decorators/index.js";
339
+ class ResilienceService {
340
+ static TOKEN = Symbol.for("nexus:ResilienceService");
341
+ defaults;
342
+ circuits = new Map;
343
+ bulkheads = new Map;
344
+ constructor(config = {}) {
345
+ this.defaults = {
346
+ retry: {
347
+ attempts: 3,
348
+ initialDelay: 100,
349
+ maxDelay: 30000,
350
+ backoff: "exponential-jitter",
351
+ multiplier: 2,
352
+ ...config.retry
353
+ },
354
+ circuit: {
355
+ threshold: 0.5,
356
+ minCalls: 5,
357
+ timeout: 30000,
358
+ halfOpenAfter: 1,
359
+ window: 60000,
360
+ ...config.circuit
361
+ },
362
+ bulkhead: {
363
+ maxConcurrent: 10,
364
+ maxQueued: 100,
365
+ rejectOnFull: false,
366
+ ...config.bulkhead
367
+ }
368
+ };
369
+ }
370
+ getOrCreateCircuit(name, config = {}) {
371
+ let cb = this.circuits.get(name);
372
+ if (!cb) {
373
+ cb = new CircuitBreaker(name, { ...this.defaults.circuit, ...config });
374
+ this.circuits.set(name, cb);
375
+ }
376
+ return cb;
377
+ }
378
+ getOrCreateBulkhead(name, config = {}) {
379
+ let bh = this.bulkheads.get(name);
380
+ if (!bh) {
381
+ const { name: _ignored, ...defaultRest } = this.defaults.bulkhead;
382
+ bh = new Bulkhead({ ...defaultRest, ...config, name });
383
+ this.bulkheads.set(name, bh);
384
+ }
385
+ return bh;
386
+ }
387
+ getCircuit(name) {
388
+ return this.circuits.get(name);
389
+ }
390
+ getBulkhead(name) {
391
+ return this.bulkheads.get(name);
392
+ }
393
+ retry(fn, cfg) {
394
+ return retry(fn, { ...this.defaults.retry, ...cfg });
395
+ }
396
+ computeBackoff(attempt, overrides) {
397
+ const cfg = { ...this.defaults.retry, ...overrides };
398
+ const { initialDelay, maxDelay, multiplier } = cfg;
399
+ const backoff = cfg.backoff;
400
+ let raw;
401
+ switch (backoff) {
402
+ case "constant":
403
+ raw = initialDelay;
404
+ break;
405
+ case "linear":
406
+ raw = initialDelay * attempt;
407
+ break;
408
+ case "exponential":
409
+ raw = initialDelay * Math.pow(multiplier, attempt - 1);
410
+ break;
411
+ case "exponential-jitter":
412
+ raw = Math.random() * initialDelay * Math.pow(multiplier, attempt - 1);
413
+ break;
414
+ }
415
+ return Math.min(raw, maxDelay);
416
+ }
417
+ }
418
+ ResilienceService = __legacyDecorateClassTS([
419
+ Injectable(),
420
+ __legacyDecorateParamTS(0, Inject("RESILIENCE_CONFIG")),
421
+ __legacyMetadataTS("design:paramtypes", [
422
+ typeof ResilienceConfig === "undefined" ? Object : ResilienceConfig
423
+ ])
424
+ ], ResilienceService);
425
+ // packages/resilience/src/resilience.module.ts
426
+ import { Module } from "@nexusts/core/decorators/module.js";
427
+
428
+ // packages/resilience/src/decorators/index.ts
429
+ import"reflect-metadata";
430
+ var KEY_RETRY = Symbol.for("nexus:Resilience:Retry");
431
+ var KEY_CIRCUIT = Symbol.for("nexus:Resilience:Circuit");
432
+ var KEY_BULKHEAD = Symbol.for("nexus:Resilience:Bulkhead");
433
+ var KEY_RESILIENT = Symbol.for("nexus:Resilience:Resilient");
434
+ function getMethodRetry(target, propertyKey) {
435
+ return Reflect.getMetadata(KEY_RETRY, target, propertyKey);
436
+ }
437
+ function getMethodCircuit(target, propertyKey) {
438
+ return Reflect.getMetadata(KEY_CIRCUIT, target, propertyKey);
439
+ }
440
+ function getMethodBulkhead(target, propertyKey) {
441
+ return Reflect.getMetadata(KEY_BULKHEAD, target, propertyKey);
442
+ }
443
+ function getMethodResilient(target, propertyKey) {
444
+ return Reflect.getMetadata(KEY_RESILIENT, target, propertyKey);
445
+ }
446
+ function makeMethodDecorator(key, extract) {
447
+ return (config) => {
448
+ return (_target, propertyKey, _descriptor) => {
449
+ Reflect.defineMetadata(key, extract(config), _target, propertyKey);
450
+ };
451
+ };
452
+ }
453
+ var _resilienceService = null;
454
+ function setResilienceService(svc) {
455
+ _resilienceService = svc;
456
+ }
457
+ function getResilienceService() {
458
+ return _resilienceService;
459
+ }
460
+ var Retry = makeMethodDecorator(KEY_RETRY, (c) => c);
461
+ var CircuitBreaker2 = makeMethodDecorator(KEY_CIRCUIT, (c) => c);
462
+ var Bulkhead2 = makeMethodDecorator(KEY_BULKHEAD, (c) => c);
463
+ var Resilient = makeMethodDecorator(KEY_RESILIENT, (c) => c);
464
+ function applyResilience(target, propertyKey, descriptor, svc) {
465
+ if (!descriptor.value || typeof descriptor.value !== "function") {
466
+ return descriptor;
467
+ }
468
+ const original = descriptor.value;
469
+ const resilient = getMethodResilient(target, propertyKey);
470
+ const retry2 = resilient?.retry ?? getMethodRetry(target, propertyKey);
471
+ const circuit = resilient?.circuit ?? getMethodCircuit(target, propertyKey);
472
+ const bulkhead = resilient?.bulkhead ?? getMethodBulkhead(target, propertyKey);
473
+ if (!retry2 && !circuit && !bulkhead) {
474
+ return descriptor;
475
+ }
476
+ const wrapped = async function(...args) {
477
+ const name = String(propertyKey);
478
+ const fn = () => original.apply(this, args);
479
+ const runOnce = async () => {
480
+ if (bulkhead) {
481
+ const bh = svc.getOrCreateBulkhead(name, bulkhead);
482
+ return bh.execute(fn);
483
+ }
484
+ return fn();
485
+ };
486
+ const runWithCircuit = async () => {
487
+ if (circuit) {
488
+ const cb = svc.getOrCreateCircuit(name, circuit);
489
+ return cb.execute(runOnce);
490
+ }
491
+ return runOnce();
492
+ };
493
+ const runWithRetry = async () => {
494
+ if (retry2) {
495
+ return svc.retry(runWithCircuit, retry2);
496
+ }
497
+ return runWithCircuit();
498
+ };
499
+ return runWithRetry();
500
+ };
501
+ return {
502
+ ...descriptor,
503
+ value: wrapped
504
+ };
505
+ }
506
+
507
+ // packages/resilience/src/resilience.module.ts
508
+ class ResilienceModule {
509
+ static forRoot(config = {}) {
510
+ class ConfiguredResilienceModule {
511
+ }
512
+ ConfiguredResilienceModule = __legacyDecorateClassTS([
513
+ Module({
514
+ providers: [
515
+ {
516
+ provide: ResilienceService.TOKEN,
517
+ useFactory: () => {
518
+ const svc = new ResilienceService(config);
519
+ setResilienceService(svc);
520
+ return svc;
521
+ }
522
+ },
523
+ {
524
+ provide: "RESILIENCE_CONFIG",
525
+ useValue: config
526
+ }
527
+ ],
528
+ exports: [ResilienceService.TOKEN, "RESILIENCE_CONFIG"]
529
+ })
530
+ ], ConfiguredResilienceModule);
531
+ Object.defineProperty(ConfiguredResilienceModule, "name", {
532
+ value: "ConfiguredResilienceModule"
533
+ });
534
+ return ConfiguredResilienceModule;
535
+ }
536
+ }
537
+ ResilienceModule = __legacyDecorateClassTS([
538
+ Module({
539
+ providers: [
540
+ ResilienceService,
541
+ { provide: ResilienceService.TOKEN, useExisting: ResilienceService }
542
+ ],
543
+ exports: [ResilienceService, ResilienceService.TOKEN]
544
+ })
545
+ ], ResilienceModule);
546
+ export {
547
+ setResilienceService,
548
+ retry,
549
+ getResilienceService,
550
+ getMethodRetry,
551
+ getMethodResilient,
552
+ getMethodCircuit,
553
+ getMethodBulkhead,
554
+ computeBackoff,
555
+ applyResilience,
556
+ Retry,
557
+ Resilient,
558
+ ResilienceService,
559
+ ResilienceModule,
560
+ RESILIENCE_META,
561
+ CircuitOpenError,
562
+ CircuitBreaker2 as CircuitBreakerDecorator,
563
+ CircuitBreaker,
564
+ BulkheadFullError,
565
+ Bulkhead2 as BulkheadDecorator,
566
+ Bulkhead
567
+ };
568
+
569
+ //# debugId=2E949C7E6C6632FA64756E2164756E21
570
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,16 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/types.ts", "../src/retry.ts", "../src/circuit-breaker.ts", "../src/bulkhead.ts", "../src/resilience.service.ts", "../src/resilience.module.ts", "../src/decorators/index.ts"],
4
+ "sourcesContent": [
5
+ "/**\n * `nexusjs/resilience` — public types and config.\n *\n * The resilience module groups three classic distributed-systems\n * primitives — retry with backoff, circuit breaker, and bulkhead\n * (concurrency limiter) — under a single, decorator-friendly API.\n * Each primitive can be used as a class, a method decorator, or a\n * standalone function.\n */\n\n// ============================================================================\n// Retry\n// ============================================================================\n\n/** Backoff strategy between retry attempts. */\nexport type BackoffStrategy =\n\t| \"constant\"\n\t| \"linear\"\n\t| \"exponential\"\n\t| \"exponential-jitter\";\n\n/**\n * Predicate: should we retry this error? Default: retry on any\n * non-abort error. Set to a function to filter (e.g. only retry\n * on network errors, not on 4xx HTTP responses).\n */\nexport type RetryOnPredicate = (err: unknown, attempt: number) => boolean;\n\n/** Configuration for `@Retry()` and `retry()`. */\nexport interface RetryConfig {\n\t/** Maximum number of attempts (including the first call). Default: 3. */\n\tattempts?: number;\n\t/** Initial backoff in ms. Default: 100. */\n\tinitialDelay?: number;\n\t/** Max backoff in ms (caps the exponential growth). Default: 30_000. */\n\tmaxDelay?: number;\n\t/** Backoff strategy. Default: \"exponential-jitter\". */\n\tbackoff?: BackoffStrategy;\n\t/** Multiplier for \"linear\" and \"exponential*\". Default: 2. */\n\tmultiplier?: number;\n\t/** Optional: only retry if this predicate returns true. */\n\tretryOn?: RetryOnPredicate;\n\t/**\n\t * Optional: hook called before each retry. Useful for logging\n\t * and metrics.\n\t */\n\tonRetry?: (err: unknown, attempt: number, delayMs: number) => void;\n\t/**\n\t * Optional: hard timeout for the entire retried operation, in ms.\n\t * If the operation takes longer, it is aborted.\n\t */\n\ttimeout?: number;\n}\n\n// ============================================================================\n// Circuit Breaker\n// ============================================================================\n\n/** A circuit breaker's lifecycle state. */\nexport type CircuitState = \"closed\" | \"open\" | \"half-open\";\n\n/** Configuration for `@CircuitBreaker()` and `CircuitBreaker`. */\nexport interface CircuitBreakerConfig {\n\t/**\n\t * Failure ratio (0..1) that opens the circuit. Default: 0.5.\n\t * Open when `failures / (failures + successes) >= threshold` over\n\t * the rolling window.\n\t */\n\tthreshold?: number;\n\t/** Minimum number of calls before the threshold matters. Default: 5. */\n\tminCalls?: number;\n\t/** How long the circuit stays open before going half-open. Default: 30s. */\n\ttimeout?: number;\n\t/** Calls allowed in half-open. Default: 1. */\n\thalfOpenAfter?: number;\n\t/** Predicate: should this error count as a failure? */\n\tisFailure?: (err: unknown) => boolean;\n\t/** Hook on state change. */\n\tonStateChange?: (from: CircuitState, to: CircuitState, name: string) => void;\n\t/** Hook on every call (good for metrics). */\n\tonCall?: (name: string, success: boolean, latencyMs: number) => void;\n\t/**\n\t * Rolling window length for failure ratio, in ms. Default: 60_000.\n\t */\n\twindow?: number;\n}\n\n// ============================================================================\n// Bulkhead\n// ============================================================================\n\n/** Configuration for `Bulkhead`. */\nexport interface BulkheadConfig {\n\t/** Max concurrent executions. Default: 10. */\n\tmaxConcurrent?: number;\n\t/** Max queued callers waiting for a slot. Default: 100. */\n\tmaxQueued?: number;\n\t/** Reject immediately if queue is full (vs. wait for a slot). */\n\trejectOnFull?: boolean;\n\t/** Optional name for logging / metrics. */\n\tname?: string;\n}\n\n/** Result of a bulkhead call. */\nexport type BulkheadOutcome<T> = {\n\tok: boolean;\n\tvalue?: T;\n\terror?: unknown;\n\tqueueMs?: number;\n\texecutionMs?: number;\n};\n\n// ============================================================================\n// Combined decorator: `@Resilient`\n// ============================================================================\n\n/**\n * All three primitives in one decorator. Each section is optional;\n * pass only what you need.\n *\n * @Resilient({\n * retry: { attempts: 3, backoff: \"exponential\" },\n * circuit: { threshold: 0.5, timeout: 30_000 },\n * bulkhead: { maxConcurrent: 5 },\n * })\n * async callExternal() { ... }\n */\nexport interface ResilientConfig {\n\tretry?: RetryConfig;\n\tcircuit?: CircuitBreakerConfig;\n\tbulkhead?: BulkheadConfig;\n}\n\n// ============================================================================\n// Module\n// ============================================================================\n\n/** Top-level config for `ResilienceModule.forRoot()`. */\nexport interface ResilienceConfig {\n\t/**\n\t * Default retry config — used when `@Retry()` is applied without\n\t * explicit options.\n\t */\n\tretry?: RetryConfig;\n\t/**\n\t * Default circuit breaker config — used when `@CircuitBreaker()`\n\t * is applied without explicit options, and as the template for\n\t * circuit breakers created via `getOrCreate()`.\n\t */\n\tcircuit?: CircuitBreakerConfig;\n\t/**\n\t * Default bulkhead config — same as above.\n\t */\n\tbulkhead?: BulkheadConfig;\n}\n\n// ============================================================================\n// Helpers\n// ============================================================================\n\n/** Internal — used by decorators to attach metadata. */\nexport const RESILIENCE_META = Symbol.for(\"nexus:Resilience:Meta\");\n",
6
+ "/**\n * `retry()` — pure function. The same code that powers `@Retry()`\n * — useful when you can't (or don't want to) decorate a method.\n *\n * import { retry } from \"@nexusts/resilience\";\n *\n * const user = await retry(\n * () => fetch(\"https://api.example.com/users/42\").then(r => r.json()),\n * { attempts: 3, backoff: \"exponential-jitter\" },\n * );\n *\n * Error handling:\n * - If the function throws and `retryOn(err)` returns true, the\n * call is retried up to `attempts` times.\n * - The final error is re-thrown to the caller.\n * - AbortError / CancellationError short-circuit immediately.\n */\nimport type { RetryConfig, RetryOnPredicate } from \"./types.js\";\n\nconst DEFAULTS = {\n\tattempts: 3,\n\tinitialDelay: 100,\n\tmaxDelay: 30_000,\n\tbackoff: \"exponential-jitter\" as const,\n\tmultiplier: 2,\n};\n\nconst defaultRetryOn: RetryOnPredicate = (err: unknown) => {\n\tif (err == null) return false;\n\t// Honour explicit aborts.\n\tconst name = (err as { name?: string })?.name ?? \"\";\n\tif (name === \"AbortError\" || name === \"CancellationError\") return false;\n\treturn true;\n};\n\n/** Sleep helper that respects AbortSignal. */\nfunction sleep(ms: number, signal?: AbortSignal): Promise<void> {\n\treturn new Promise((resolve, reject) => {\n\t\tif (signal?.aborted) {\n\t\t\treject(signal.reason ?? new DOMException(\"Aborted\", \"AbortError\"));\n\t\t\treturn;\n\t\t}\n\t\tconst id = setTimeout(() => {\n\t\t\tsignal?.removeEventListener(\"abort\", onAbort);\n\t\t\tresolve();\n\t\t}, ms);\n\t\tconst onAbort = () => {\n\t\t\tclearTimeout(id);\n\t\t\treject(signal?.reason ?? new DOMException(\"Aborted\", \"AbortError\"));\n\t\t};\n\t\tsignal?.addEventListener(\"abort\", onAbort, { once: true });\n\t});\n}\n\n/**\n * Compute the next backoff delay. Returns ms.\n *\n * constant: initialDelay\n * linear: initialDelay * attempt * multiplier\n * exponential: initialDelay * multiplier^(attempt-1)\n * exponential-jitter: above * (0.5..1.5) random factor\n */\nexport function computeBackoff(\n\tattempt: number,\n\tcfg: Required<Pick<RetryConfig, \"initialDelay\" | \"maxDelay\" | \"backoff\" | \"multiplier\">>,\n): number {\n\tconst { initialDelay, maxDelay, backoff, multiplier } = cfg;\n\tlet raw: number;\n\tswitch (backoff) {\n\t\tcase \"constant\":\n\t\t\traw = initialDelay;\n\t\t\tbreak;\n\t\tcase \"linear\":\n\t\t\traw = initialDelay * attempt;\n\t\t\tbreak;\n\t\tcase \"exponential\":\n\t\t\traw = initialDelay * Math.pow(multiplier, attempt - 1);\n\t\t\tbreak;\n\t\tcase \"exponential-jitter\": {\n\t\t\tconst base = initialDelay * Math.pow(multiplier, attempt - 1);\n\t\t\t// ±50% jitter — full jitter (AWS-style).\n\t\t\traw = Math.random() * base;\n\t\t\tbreak;\n\t\t}\n\t\tdefault:\n\t\t\traw = initialDelay;\n\t}\n\treturn Math.min(raw, maxDelay);\n}\n\n/**\n * Retry `fn` according to `cfg`. Returns the eventual value or\n * re-throws the last error.\n */\nexport async function retry<T>(\n\tfn: (signal: AbortSignal) => Promise<T> | T,\n\tcfg: RetryConfig = {},\n): Promise<T> {\n\tconst attempts = Math.max(1, cfg.attempts ?? DEFAULTS.attempts);\n\tconst initialDelay = cfg.initialDelay ?? DEFAULTS.initialDelay;\n\tconst maxDelay = cfg.maxDelay ?? DEFAULTS.maxDelay;\n\tconst backoff = cfg.backoff ?? DEFAULTS.backoff;\n\tconst multiplier = cfg.multiplier ?? DEFAULTS.multiplier;\n\tconst retryOn = cfg.retryOn ?? defaultRetryOn;\n\tconst onRetry = cfg.onRetry;\n\tconst overallTimeout = cfg.timeout;\n\n\tconst ac = new AbortController();\n\tconst overallTimer = overallTimeout\n\t\t? setTimeout(() => ac.abort(new Error(\"retry: overall timeout exceeded\")), overallTimeout)\n\t\t: undefined;\n\n\tconst normalized = { initialDelay, maxDelay, backoff, multiplier };\n\tlet lastErr: unknown;\n\tfor (let attempt = 1; attempt <= attempts; attempt++) {\n\t\tif (ac.signal.aborted) break;\n\t\ttry {\n\t\t\treturn await fn(ac.signal);\n\t\t} catch (err) {\n\t\t\tlastErr = err;\n\t\t\tif (attempt >= attempts) break;\n\t\t\tif (!retryOn(err, attempt)) break;\n\t\t\tconst delay = computeBackoff(attempt, normalized);\n\t\t\tif (onRetry) {\n\t\t\t\ttry {\n\t\t\t\t\tonRetry(err, attempt, delay);\n\t\t\t\t} catch {\n\t\t\t\t\t/* hook errors must not break the retry */\n\t\t\t\t}\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tawait sleep(delay, ac.signal);\n\t\t\t} catch (sleepErr) {\n\t\t\t\tlastErr = sleepErr;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tif (overallTimer) clearTimeout(overallTimer);\n\tthrow lastErr;\n}\n",
7
+ "/**\n * `CircuitBreaker` — a single named circuit. Constructed on demand\n * via `ResilienceService.getOrCreate()` (so multiple parts of your\n * app share one circuit per external dependency), or directly:\n *\n * import { CircuitBreaker } from \"@nexusts/resilience\";\n *\n * const stripe = new CircuitBreaker(\"stripe\", {\n * threshold: 0.5,\n * timeout: 30_000,\n * });\n *\n * const result = await stripe.execute(() => stripeApi.charge(...));\n *\n * State machine:\n *\n * closed ──failures ≥ threshold──▶ open\n * ▲ │\n * │ │ after `timeout` ms\n * │ ▼\n * closed ◀──success── half-open\n * │\n * └─ failure ──▶ open (reset)\n *\n * - `closed`: all calls go through. Track success/failure in a\n * rolling window.\n * - `open`: calls fail-fast with `CircuitOpenError`. After\n * `timeout` ms, transition to `half-open`.\n * - `half-open`: at most `halfOpenAfter` calls are allowed. If any\n * of them fails → back to `open`. If all succeed → back to\n * `closed`.\n */\nimport type { CircuitBreakerConfig, CircuitState } from \"./types.js\";\n\nconst DEFAULTS = {\n\tthreshold: 0.5,\n\tminCalls: 5,\n\ttimeout: 30_000,\n\thalfOpenAfter: 1,\n\twindow: 60_000,\n};\n\nconst defaultIsFailure = (_err: unknown): boolean => true;\n\n/** Thrown when the circuit is open. Callers should treat as \"fail-fast\". */\nexport class CircuitOpenError extends Error {\n\treadonly name = \"CircuitOpenError\";\n\treadonly circuit: string;\n\treadonly nextRetryAt: number;\n\tconstructor(circuit: string, nextRetryAt: number) {\n\t\tsuper(`Circuit \"${circuit}\" is open; retry after ${nextRetryAt}ms`);\n\t\tthis.circuit = circuit;\n\t\tthis.nextRetryAt = nextRetryAt;\n\t}\n}\n\ninterface Sample {\n\tts: number;\n\tok: boolean;\n}\n\nexport class CircuitBreaker {\n\treadonly name: string;\n\treadonly config: Required<Omit<CircuitBreakerConfig, \"onStateChange\" | \"onCall\" | \"isFailure\">> & {\n\t\tisFailure: NonNullable<CircuitBreakerConfig[\"isFailure\"]>;\n\t};\n\tprivate state: CircuitState = \"closed\";\n\tprivate samples: Sample[] = [];\n\tprivate openedAt = 0;\n\tprivate halfOpenInFlight = 0;\n\tprivate halfOpenAllowed = 0;\n\n\tconstructor(name: string, config: CircuitBreakerConfig = {}) {\n\t\tthis.name = name;\n\t\tthis.config = {\n\t\t\tthreshold: config.threshold ?? DEFAULTS.threshold,\n\t\t\tminCalls: config.minCalls ?? DEFAULTS.minCalls,\n\t\t\ttimeout: config.timeout ?? DEFAULTS.timeout,\n\t\t\thalfOpenAfter: config.halfOpenAfter ?? DEFAULTS.halfOpenAfter,\n\t\t\twindow: config.window ?? DEFAULTS.window,\n\t\t\tisFailure: config.isFailure ?? defaultIsFailure,\n\t\t};\n\t}\n\n\tget currentState(): CircuitState {\n\t\t// Lazy transition: if the open timer has elapsed, flip to half-open\n\t\t// before the next call, so the consumer sees the right state.\n\t\tif (\n\t\t\tthis.state === \"open\" &&\n\t\t\tDate.now() - this.openedAt >= this.config.timeout\n\t\t) {\n\t\t\tthis.transition(\"half-open\");\n\t\t\tthis.halfOpenAllowed = this.config.halfOpenAfter;\n\t\t\tthis.halfOpenInFlight = 0;\n\t\t}\n\t\treturn this.state;\n\t}\n\n\t/** Run `fn` through the circuit. Throws `CircuitOpenError` when open. */\n\tasync execute<T>(fn: () => Promise<T> | T): Promise<T> {\n\t\tconst state = this.currentState;\n\n\t\tif (state === \"open\") {\n\t\t\tthrow new CircuitOpenError(this.name, this.openedAt + this.config.timeout);\n\t\t}\n\n\t\t// Concurrency control for half-open.\n\t\tif (state === \"half-open\") {\n\t\t\tif (this.halfOpenInFlight >= this.halfOpenAllowed) {\n\t\t\t\tthrow new CircuitOpenError(this.name, this.openedAt + this.config.timeout);\n\t\t\t}\n\t\t\tthis.halfOpenInFlight += 1;\n\t\t}\n\n\t\tconst start = Date.now();\n\t\tlet ok = false;\n\t\ttry {\n\t\t\tconst result = await fn();\n\t\t\tok = true;\n\t\t\treturn result;\n\t\t} finally {\n\t\t\tconst latency = Date.now() - start;\n\t\t\tthis.record(ok, latency, state);\n\t\t}\n\t}\n\n\tprivate record(ok: boolean, latency: number, stateAtCall: CircuitState): void {\n\t\tthis.config; // touch — keep the type narrow\n\t\t// Track in the rolling window.\n\t\tconst now = Date.now();\n\t\tthis.samples.push({ ts: now, ok });\n\t\tconst cutoff = now - this.config.window;\n\t\twhile (this.samples.length > 0 && this.samples[0].ts < cutoff) {\n\t\t\tthis.samples.shift();\n\t\t}\n\n\t\tconst failed = ok ? 0 : 1;\n\t\tvoid failed;\n\n\t\t// State transitions.\n\t\tif (stateAtCall === \"half-open\") {\n\t\t\tthis.halfOpenInFlight = Math.max(0, this.halfOpenInFlight - 1);\n\t\t\tif (!ok) {\n\t\t\t\tthis.openedAt = now;\n\t\t\t\tthis.transition(\"open\");\n\t\t\t} else if (this.halfOpenInFlight === 0) {\n\t\t\t\tthis.transition(\"closed\");\n\t\t\t\tthis.samples = [];\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\n\t\t// closed → possibly open\n\t\tif (!ok) {\n\t\t\tconst total = this.samples.length;\n\t\t\tif (total >= this.config.minCalls) {\n\t\t\t\tconst failures = this.samples.filter((s) => !s.ok).length;\n\t\t\t\tconst ratio = failures / total;\n\t\t\t\tif (ratio >= this.config.threshold) {\n\t\t\t\t\tthis.openedAt = now;\n\t\t\t\t\tthis.transition(\"open\");\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\t// Reset the window on full-success periods so a long-stable\n\t\t\t// upstream doesn't carry stale failures forever.\n\t\t\tif (this.samples.length > this.config.minCalls * 4) {\n\t\t\t\tthis.samples = this.samples.slice(-this.config.minCalls);\n\t\t\t}\n\t\t}\n\n\t\t// Hooks — best-effort, swallow errors.\n\t\ttry {\n\t\t\tthis.config; // (placeholder for future hooks read)\n\t\t} catch {\n\t\t\t/* ignore */\n\t\t}\n\t}\n\n\tprivate transition(to: CircuitState): void {\n\t\tconst from = this.state;\n\t\tif (from === to) return;\n\t\tthis.state = to;\n\t\t// Hooks are passed in `config`; we only know about them via\n\t\t// the outer constructor so we keep them on a parallel field.\n\t\t// (The hook API is set in the ResilienceService which owns\n\t\t// the onStateChange list.)\n\t\tthis.fireHook(from, to);\n\t}\n\n\t/** Set by ResilienceService — fires on each transition. */\n\t_onStateChange?: (from: CircuitState, to: CircuitState, name: string) => void;\n\t_onCall?: (name: string, success: boolean, latencyMs: number) => void;\n\tprivate fireHook(from: CircuitState, to: CircuitState): void {\n\t\ttry {\n\t\t\tthis._onStateChange?.(from, to, this.name);\n\t\t} catch {\n\t\t\t/* ignore */\n\t\t}\n\t}\n}\n",
8
+ "/**\n * `Bulkhead` — concurrency limiter with optional queue.\n *\n * import { Bulkhead } from \"@nexusts/resilience\";\n *\n * const api = new Bulkhead({ maxConcurrent: 5, maxQueued: 100 });\n *\n * const result = await api.execute(() => fetch(\"...\"));\n *\n * Behavior:\n * - `maxConcurrent`: how many `execute()` calls may be in-flight at once.\n * - `maxQueued`: how many additional callers may wait in the queue\n * for a free slot.\n * - `rejectOnFull` (default false): when the queue is full, reject\n * immediately with `BulkheadFullError` instead of waiting.\n *\n * The bulkhead is fair: callers are released in FIFO order. Each\n * waiter carries its own resolver; when a slot frees up we wake the\n * next waiter in the queue, who then re-enters and runs its function.\n */\nimport type { BulkheadConfig } from \"./types.js\";\n\nconst DEFAULTS = {\n\tmaxConcurrent: 10,\n\tmaxQueued: 100,\n\trejectOnFull: false,\n};\n\n/** Thrown when `rejectOnFull` is set and the queue is at capacity. */\nexport class BulkheadFullError extends Error {\n\treadonly name = \"BulkheadFullError\";\n\tconstructor(public readonly name_: string) {\n\t\tsuper(`Bulkhead \"${name_}\" is at capacity`);\n\t}\n}\n\n/** A queued caller. Resolves when this caller gets a slot. */\ninterface SlotToken {\n\t/** Acquired by the drain loop when a slot opens. */\n\tacquired: boolean;\n\t/** Cancelled before we got a slot. */\n\tcancelled: boolean;\n\tresolve: () => void;\n\treject: (e: unknown) => void;\n}\n\nexport class Bulkhead {\n\treadonly name: string;\n\treadonly config: Required<Omit<BulkheadConfig, \"name\">>;\n\n\tprivate inFlight = 0;\n\tprivate queue: SlotToken[] = [];\n\n\tconstructor(config: BulkheadConfig = {}) {\n\t\tthis.name = config.name ?? \"bulkhead\";\n\t\tthis.config = {\n\t\t\tmaxConcurrent: config.maxConcurrent ?? DEFAULTS.maxConcurrent,\n\t\t\tmaxQueued: config.maxQueued ?? DEFAULTS.maxQueued,\n\t\t\trejectOnFull: config.rejectOnFull ?? DEFAULTS.rejectOnFull,\n\t\t};\n\t}\n\n\tget stats(): { inFlight: number; queued: number } {\n\t\treturn { inFlight: this.inFlight, queued: this.queue.length };\n\t}\n\n\t/** Run `fn` through the bulkhead. */\n\tasync execute<T>(fn: () => Promise<T> | T): Promise<T> {\n\t\t// Fast path: a slot is free.\n\t\tif (this.inFlight < this.config.maxConcurrent) {\n\t\t\tthis.inFlight += 1;\n\t\t\ttry {\n\t\t\t\treturn await fn();\n\t\t\t} finally {\n\t\t\t\tthis.inFlight -= 1;\n\t\t\t\tthis.drain();\n\t\t\t}\n\t\t}\n\n\t\t// No slot — wait for one.\n\t\tif (this.queue.length >= this.config.maxQueued) {\n\t\t\tthrow new BulkheadFullError(this.name);\n\t\t}\n\n\t\tconst token = this.enqueue();\n\t\ttry {\n\t\t\tawait token.acquire();\n\t\t} catch (err) {\n\t\t\t// Cancelled or aborted — drop the token and re-throw.\n\t\t\tthrow err;\n\t\t}\n\t\t// Slot acquired. Run and release on completion.\n\t\tthis.inFlight += 1;\n\t\ttry {\n\t\t\treturn await fn();\n\t\t} finally {\n\t\t\tthis.inFlight -= 1;\n\t\t\tthis.drain();\n\t\t}\n\t}\n\n\t/** Allocate a slot token, joining the FIFO queue. */\n\tprivate enqueue(): SlotToken & { acquire(): Promise<void> } {\n\t\tlet resolve!: () => void;\n\t\tlet reject!: (e: unknown) => void;\n\t\tconst promise = new Promise<void>((res, rej) => {\n\t\t\tresolve = res;\n\t\t\treject = rej;\n\t\t});\n\t\tconst token: SlotToken = {\n\t\t\tacquired: false,\n\t\t\tcancelled: false,\n\t\t\tresolve,\n\t\t\treject,\n\t\t};\n\t\tthis.queue.push(token);\n\t\treturn Object.assign(promise as never, {\n\t\t\tacquire: () => promise,\n\t\t\tcancel: () => {\n\t\t\t\tif (token.acquired) return;\n\t\t\t\ttoken.cancelled = true;\n\t\t\t\ttoken.reject(new DOMException(\"Cancelled\", \"AbortError\"));\n\t\t\t},\n\t\t});\n\t}\n\n\t/** Release a slot by waking the next waiter. Idempotent. */\n\tprivate drain(): void {\n\t\twhile (\n\t\t\tthis.inFlight < this.config.maxConcurrent &&\n\t\t\tthis.queue.length > 0\n\t\t) {\n\t\t\tconst next = this.queue.shift()!;\n\t\t\tif (next.cancelled) continue;\n\t\t\tnext.acquired = true;\n\t\t\tnext.resolve();\n\t\t\treturn; // Only one slot opens per drain call.\n\t\t}\n\t}\n}\n",
9
+ "/**\n * `ResilienceService` — owns the named circuit-breaker and bulkhead\n * registry. Exposed via DI so multiple parts of an app can share\n * the same circuit for an external dependency.\n *\n * constructor(@Inject(ResilienceService.TOKEN) private r: ResilienceService) {}\n *\n * const cb = this.r.getOrCreateCircuit(\"stripe\", { threshold: 0.5 });\n * const result = await cb.execute(() => stripeApi.charge(...));\n *\n * The service also holds the *default* config (RetryConfig,\n * CircuitBreakerConfig, BulkheadConfig) used by the decorators\n * when no per-call options are given.\n */\nimport \"reflect-metadata\";\nimport { Inject, Injectable } from \"@nexusts/core/decorators/index.js\";\nimport { CircuitBreaker } from \"./circuit-breaker.js\";\nimport { Bulkhead } from \"./bulkhead.js\";\nimport { retry } from \"./retry.js\";\nimport type {\n\tBackoffStrategy,\n\tBulkheadConfig,\n\tCircuitBreakerConfig,\n\tResilienceConfig,\n\tRetryConfig,\n} from \"./types.js\";\n\n@Injectable()\nexport class ResilienceService {\n\t/** DI token — `@Inject(ResilienceService.TOKEN)`. */\n\tstatic readonly TOKEN = Symbol.for(\"nexus:ResilienceService\");\n\n\treadonly defaults: {\n\t\tretry: Required<RetryConfig>;\n\t\tcircuit: Required<CircuitBreakerConfig>;\n\t\tbulkhead: Required<BulkheadConfig>;\n\t};\n\n\tprivate circuits = new Map<string, CircuitBreaker>();\n\tprivate bulkheads = new Map<string, Bulkhead>();\n\n\tconstructor(@Inject(\"RESILIENCE_CONFIG\") config: ResilienceConfig = {}) {\n\t\tthis.defaults = {\n\t\t\tretry: {\n\t\t\t\tattempts: 3,\n\t\t\t\tinitialDelay: 100,\n\t\t\t\tmaxDelay: 30_000,\n\t\t\t\tbackoff: \"exponential-jitter\",\n\t\t\t\tmultiplier: 2,\n\t\t\t\t...config.retry,\n\t\t\t} as Required<RetryConfig>,\n\t\t\tcircuit: {\n\t\t\t\tthreshold: 0.5,\n\t\t\t\tminCalls: 5,\n\t\t\t\ttimeout: 30_000,\n\t\t\t\thalfOpenAfter: 1,\n\t\t\t\twindow: 60_000,\n\t\t\t\t...config.circuit,\n\t\t\t} as Required<CircuitBreakerConfig>,\n\t\t\tbulkhead: {\n\t\t\t\tmaxConcurrent: 10,\n\t\t\t\tmaxQueued: 100,\n\t\t\t\trejectOnFull: false,\n\t\t\t\t...config.bulkhead,\n\t\t\t} as Required<BulkheadConfig>,\n\t\t};\n\t}\n\n\t/** Get or create a named circuit breaker. Shared across the app. */\n\tgetOrCreateCircuit(name: string, config: CircuitBreakerConfig = {}): CircuitBreaker {\n\t\tlet cb = this.circuits.get(name);\n\t\tif (!cb) {\n\t\t\tcb = new CircuitBreaker(name, { ...this.defaults.circuit, ...config });\n\t\t\tthis.circuits.set(name, cb);\n\t\t}\n\t\treturn cb;\n\t}\n\n\t/** Get or create a named bulkhead. */\n\tgetOrCreateBulkhead(name: string, config: BulkheadConfig = {}): Bulkhead {\n\t\tlet bh = this.bulkheads.get(name);\n\t\tif (!bh) {\n\t\t\t// Merge: explicit config overrides defaults, except for\n\t\t\t// `name` (always use the caller's name). Strip `name` from\n\t\t\t// the defaults before spreading to avoid a duplicate.\n\t\t\tconst { name: _ignored, ...defaultRest } = this.defaults.bulkhead as BulkheadConfig;\n\t\t\tvoid _ignored;\n\t\t\tbh = new Bulkhead({ ...defaultRest, ...config, name });\n\t\t\tthis.bulkheads.set(name, bh);\n\t\t}\n\t\treturn bh;\n\t}\n\n\t/** Look up an existing circuit (no creation). */\n\tgetCircuit(name: string): CircuitBreaker | undefined {\n\t\treturn this.circuits.get(name);\n\t}\n\n\t/** Look up an existing bulkhead. */\n\tgetBulkhead(name: string): Bulkhead | undefined {\n\t\treturn this.bulkheads.get(name);\n\t}\n\n\t/** Retry with default config. */\n\tretry<T>(fn: (signal: AbortSignal) => Promise<T> | T, cfg?: RetryConfig): Promise<T> {\n\t\treturn retry(fn, { ...this.defaults.retry, ...cfg });\n\t}\n\n\t/** Compute the backoff for a given attempt. Exposed for tests. */\n\tcomputeBackoff(attempt: number, overrides?: Partial<RetryConfig>): number {\n\t\tconst cfg = { ...this.defaults.retry, ...overrides };\n\t\t// Local copy of the same algorithm — kept here to avoid\n\t\t// importing from retry.ts just for the math.\n\t\tconst { initialDelay, maxDelay, multiplier } = cfg;\n\t\tconst backoff: BackoffStrategy = cfg.backoff;\n\t\tlet raw: number;\n\t\tswitch (backoff) {\n\t\t\tcase \"constant\":\n\t\t\t\traw = initialDelay;\n\t\t\t\tbreak;\n\t\t\tcase \"linear\":\n\t\t\t\traw = initialDelay * attempt;\n\t\t\t\tbreak;\n\t\t\tcase \"exponential\":\n\t\t\t\traw = initialDelay * Math.pow(multiplier, attempt - 1);\n\t\t\t\tbreak;\n\t\t\tcase \"exponential-jitter\":\n\t\t\t\traw = Math.random() * initialDelay * Math.pow(multiplier, attempt - 1);\n\t\t\t\tbreak;\n\t\t}\n\t\treturn Math.min(raw, maxDelay);\n\t}\n}\n",
10
+ "/**\n * `ResilienceModule` — drop-in DI for the resilience primitives.\n *\n * @Module({\n * imports: [\n * ResilienceModule.forRoot({\n * retry: { attempts: 3, backoff: \"exponential-jitter\" },\n * circuit: { threshold: 0.5, timeout: 30_000 },\n * }),\n * ],\n * })\n * class AppModule {}\n *\n * After boot, the service is available as `@Inject(ResilienceService.TOKEN)`\n * — controllers can call `svc.getOrCreateCircuit(\"stripe\")` to share\n * a circuit breaker with the rest of the app.\n *\n * The decorators (`@Retry`, `@CircuitBreaker`, `@Bulkhead`,\n * `@Resilient`) work without the module — they pick up the\n * service from the DI container at controller-mount time.\n */\nimport { Module } from \"@nexusts/core/decorators/module.js\";\nimport { ResilienceService } from \"./resilience.service.js\";\nimport { setResilienceService } from \"./decorators/index.js\";\nimport type { ResilienceConfig } from \"./types.js\";\n\n@Module({\n\tproviders: [\n\t\tResilienceService,\n\t\t{ provide: ResilienceService.TOKEN, useExisting: ResilienceService },\n\t],\n\texports: [ResilienceService, ResilienceService.TOKEN],\n})\nexport class ResilienceModule {\n\tstatic forRoot(config: ResilienceConfig = {}) {\n\t\t@Module({\n\t\t\tproviders: [\n\t\t\t\t{\n\t\t\t\t\tprovide: ResilienceService.TOKEN,\n\t\t\t\t\tuseFactory: () => {\n\t\t\t\t\t\tconst svc = new ResilienceService(config);\n\t\t\t\t\t\t// Register globally so the eager-decorator path\n\t\t\t\t\t\t// (in `decorators/index.ts`) can find us without\n\t\t\t\t\t\t// needing each controller method to carry an\n\t\t\t\t\t\t// `@Inject(ResilienceService.TOKEN)` argument.\n\t\t\t\t\t\tsetResilienceService(svc);\n\t\t\t\t\t\treturn svc;\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tprovide: \"RESILIENCE_CONFIG\",\n\t\t\t\t\tuseValue: config,\n\t\t\t\t},\n\t\t\t],\n\t\t\texports: [ResilienceService.TOKEN, \"RESILIENCE_CONFIG\"],\n\t\t})\n\t\tclass ConfiguredResilienceModule {}\n\t\tObject.defineProperty(ConfiguredResilienceModule, \"name\", {\n\t\t\tvalue: \"ConfiguredResilienceModule\",\n\t\t});\n\t\treturn ConfiguredResilienceModule;\n\t}\n}\n",
11
+ "/**\n * Decorator barrel for `nexusjs/resilience`.\n *\n * The three method decorators (`@Retry`, `@CircuitBreaker`,\n * `@Bulkhead`) plus the combined `@Resilient` decorator are\n * implemented as **metadata-only** decorators — they write options\n * to the method's `reflect-metadata` store, and an\n * `ResilienceInterceptor` (or a runtime hook) reads and applies\n * them at call time.\n *\n * @Retry({ attempts: 3, backoff: \"exponential\" })\n * async fetchUser(id: string) { ... }\n *\n * @CircuitBreaker({ threshold: 0.5, timeout: 30_000 })\n * async callStripe() { ... }\n *\n * @Bulkhead({ maxConcurrent: 5 })\n * async callExpensive() { ... }\n *\n * @Resilient({ retry: {...}, circuit: {...}, bulkhead: {...} })\n * async criticalCall() { ... }\n */\nimport \"reflect-metadata\";\nimport type {\n\tBulkheadConfig,\n\tCircuitBreakerConfig,\n\tResilientConfig,\n\tRetryConfig,\n} from \"../types.js\";\nimport { RESILIENCE_META } from \"../types.js\";\n\n// Use `import type` for the service to avoid a circular import:\n// `resilience.service.ts` → `decorators/index.ts` → `resilience.service.ts`.\n// At runtime, the dependency is set via `setResilienceService()` so\n// only the *type* needs to be visible at type-check time.\nimport type { ResilienceService } from \"../resilience.service.js\";\n\n// Per-method metadata kinds. Each decorator stores its own\n// payload under a separate key so a method can have e.g. `@Retry`\n// and `@CircuitBreaker` simultaneously.\nconst KEY_RETRY = Symbol.for(\"nexus:Resilience:Retry\");\nconst KEY_CIRCUIT = Symbol.for(\"nexus:Resilience:Circuit\");\nconst KEY_BULKHEAD = Symbol.for(\"nexus:Resilience:Bulkhead\");\nconst KEY_RESILIENT = Symbol.for(\"nexus:Resilience:Resilient\");\n\nexport interface RetryMeta {\n\tconfig: RetryConfig;\n\t/** Captured bound `this` at call time. */\n}\nexport interface CircuitMeta {\n\tconfig: CircuitBreakerConfig;\n}\nexport interface BulkheadMeta {\n\tconfig: BulkheadConfig;\n}\nexport interface ResilientMeta {\n\tconfig: ResilientConfig;\n}\n\n/** Read the metadata for a given method. */\nexport function getMethodRetry(\n\ttarget: object,\n\tpropertyKey: string | symbol,\n): RetryConfig | undefined {\n\treturn Reflect.getMetadata(KEY_RETRY, target, propertyKey) as RetryConfig | undefined;\n}\n\nexport function getMethodCircuit(\n\ttarget: object,\n\tpropertyKey: string | symbol,\n): CircuitBreakerConfig | undefined {\n\treturn Reflect.getMetadata(KEY_CIRCUIT, target, propertyKey) as\n\t\t| CircuitBreakerConfig\n\t\t| undefined;\n}\n\nexport function getMethodBulkhead(\n\ttarget: object,\n\tpropertyKey: string | symbol,\n): BulkheadConfig | undefined {\n\treturn Reflect.getMetadata(KEY_BULKHEAD, target, propertyKey) as\n\t\t| BulkheadConfig\n\t\t| undefined;\n}\n\nexport function getMethodResilient(\n\ttarget: object,\n\tpropertyKey: string | symbol,\n): ResilientConfig | undefined {\n\treturn Reflect.getMetadata(KEY_RESILIENT, target, propertyKey) as\n\t\t| ResilientConfig\n\t\t| undefined;\n}\n\n// ============================================================================\n// Decorator factories\n// ============================================================================\n\nfunction makeMethodDecorator<TConfig>(\n\tkey: symbol,\n\textract: (config: TConfig) => unknown,\n): (config: TConfig) => MethodDecorator {\n\treturn (config: TConfig): MethodDecorator => {\n\t\treturn (\n\t\t\t_target: object,\n\t\t\tpropertyKey: string | symbol,\n\t\t\t_descriptor: TypedPropertyDescriptor<any>,\n\t\t): void => {\n\t\t\t// Metadata-only — we don't touch `descriptor.value`\n\t\t\t// here because Bun 1.3's stage-3 decorator mode (the\n\t\t\t// default) doesn't pass it. Instead, the framework\n\t\t\t// reads the metadata at controller-mount time and\n\t\t\t// calls `applyResilience()` to wrap the method.\n\t\t\tReflect.defineMetadata(key, extract(config), _target, propertyKey);\n\t\t};\n\t};\n}\n\n// Used by `applyResilience()` below (exported for users who want\n// to wire it into their own framework hook). Not invoked from the\n// decorator factories themselves — that would require reading\n// `descriptor.value` in the decorator body, which Bun's stage-3\n// decorator mode (the default in Bun 1.3+) doesn't supply.\nexport function getResilientMetadata(target: object, propertyKey: string | symbol) {\n\tconst resilient = getMethodResilient(target, propertyKey);\n\tconst retry = resilient?.retry ?? getMethodRetry(target, propertyKey);\n\tconst circuit = resilient?.circuit ?? getMethodCircuit(target, propertyKey);\n\tconst bulkhead = resilient?.bulkhead ?? getMethodBulkhead(target, propertyKey);\n\treturn { retry, circuit, bulkhead };\n}\n\n// Used by `applyResilience()` below; same rationale as above.\nexport function makeResilientWrapper(\n\toriginal: Function,\n\tresolveMeta: () => {\n\t\tretry?: RetryConfig;\n\t\tcircuit?: CircuitBreakerConfig;\n\t\tbulkhead?: BulkheadConfig;\n\t},\n): Function {\n\treturn function (this: unknown, ...args: any[]) {\n\t\tconst meta = resolveMeta();\n\t\tif (!meta.retry && !meta.circuit && !meta.bulkhead) {\n\t\t\treturn original.apply(this, args);\n\t\t}\n\t\tconst svc = getResilienceService();\n\t\tif (!svc) {\n\t\t\treturn original.apply(this, args);\n\t\t}\n\t\tconst name = original.name || \"anonymous\";\n\n\t\tconst fn = () => original.apply(this, args);\n\t\tconst runOnce = () =>\n\t\t\tmeta.bulkhead\n\t\t\t\t? svc.getOrCreateBulkhead(name, meta.bulkhead).execute(fn)\n\t\t\t\t: fn();\n\t\tconst runWithCircuit = () =>\n\t\t\tmeta.circuit\n\t\t\t\t? svc.getOrCreateCircuit(name, meta.circuit).execute(runOnce)\n\t\t\t\t: runOnce();\n\t\tconst runWithRetry = () =>\n\t\t\tmeta.retry ? svc.retry(runWithCircuit, meta.retry) : runWithCircuit();\n\n\t\treturn runWithRetry();\n\t};\n}\n\n/**\n * Module-level handle to the `ResilienceService`. Set by\n * `ResilienceModule.forRoot()` so the eager-decorator path can\n * access the registry without each method carrying the DI token.\n */\nlet _resilienceService: ResilienceService | null = null;\n\n/** Public — used by `ResilienceModule` to register the service. */\nexport function setResilienceService(svc: ResilienceService | null): void {\n\t_resilienceService = svc;\n}\n\n/** Public — used by the eager decorator path. */\nexport function getResilienceService(): ResilienceService | null {\n\treturn _resilienceService;\n}\n\n/** `@Retry(config)` — retry the method with backoff. */\nexport const Retry = makeMethodDecorator<RetryConfig>(KEY_RETRY, (c) => c);\n\n/** `@CircuitBreaker(config)` — wrap the method in a named circuit. */\nexport const CircuitBreaker = makeMethodDecorator<CircuitBreakerConfig>(\n\tKEY_CIRCUIT,\n\t(c) => c,\n);\n\n/** `@Bulkhead(config)` — limit concurrency for the method. */\nexport const Bulkhead = makeMethodDecorator<BulkheadConfig>(KEY_BULKHEAD, (c) => c);\n\n/** `@Resilient(config)` — combine retry + circuit + bulkhead. */\nexport const Resilient = makeMethodDecorator<ResilientConfig>(\n\tKEY_RESILIENT,\n\t(c) => c,\n);\n\n// ============================================================================\n// `applyResilience()` — runtime hook\n// ============================================================================\n\n/**\n * Read all `@Resilient` / `@Retry` / `@CircuitBreaker` / `@Bulkhead`\n * metadata on `target` and apply the corresponding wrappers around\n * `descriptor.value`. The framework wires this up via\n * `ResilienceService` when controllers are mounted.\n */\nexport function applyResilience(\n\ttarget: object,\n\tpropertyKey: string | symbol,\n\tdescriptor: TypedPropertyDescriptor<any>,\n\tsvc: import(\"../resilience.service.js\").ResilienceService,\n): TypedPropertyDescriptor<any> {\n\tif (!descriptor.value || typeof descriptor.value !== \"function\") {\n\t\treturn descriptor;\n\t}\n\tconst original = descriptor.value;\n\n\tconst resilient = getMethodResilient(target, propertyKey);\n\tconst retry = resilient?.retry ?? getMethodRetry(target, propertyKey);\n\tconst circuit = resilient?.circuit ?? getMethodCircuit(target, propertyKey);\n\tconst bulkhead = resilient?.bulkhead ?? getMethodBulkhead(target, propertyKey);\n\n\tif (!retry && !circuit && !bulkhead) {\n\t\treturn descriptor; // No resilience — leave the method alone.\n\t}\n\n\tconst wrapped = async function (this: unknown, ...args: any[]) {\n\t\tconst name = String(propertyKey);\n\t\tconst fn = () => original.apply(this, args);\n\n\t\tconst runOnce = async () => {\n\t\t\tif (bulkhead) {\n\t\t\t\tconst bh = svc.getOrCreateBulkhead(name, bulkhead);\n\t\t\t\treturn bh.execute(fn);\n\t\t\t}\n\t\t\treturn fn();\n\t\t};\n\n\t\tconst runWithCircuit = async () => {\n\t\t\tif (circuit) {\n\t\t\t\tconst cb = svc.getOrCreateCircuit(name, circuit);\n\t\t\t\treturn cb.execute(runOnce);\n\t\t\t}\n\t\t\treturn runOnce();\n\t\t};\n\n\t\tconst runWithRetry = async () => {\n\t\t\tif (retry) {\n\t\t\t\treturn svc.retry(runWithCircuit, retry);\n\t\t\t}\n\t\t\treturn runWithCircuit();\n\t\t};\n\n\t\treturn runWithRetry();\n\t};\n\n\treturn {\n\t\t...descriptor,\n\t\tvalue: wrapped,\n\t};\n}\n\n// Re-export the meta key for tests.\nexport { RESILIENCE_META };\n"
12
+ ],
13
+ "mappings": ";;;;;;;;;;;;;;;;;;AAiKO,IAAM,kBAAkB,OAAO,IAAI,uBAAuB;;AC9IjE,IAAM,WAAW;AAAA,EAChB,UAAU;AAAA,EACV,cAAc;AAAA,EACd,UAAU;AAAA,EACV,SAAS;AAAA,EACT,YAAY;AACb;AAEA,IAAM,iBAAmC,CAAC,QAAiB;AAAA,EAC1D,IAAI,OAAO;AAAA,IAAM,OAAO;AAAA,EAExB,MAAM,OAAQ,KAA2B,QAAQ;AAAA,EACjD,IAAI,SAAS,gBAAgB,SAAS;AAAA,IAAqB,OAAO;AAAA,EAClE,OAAO;AAAA;AAIR,SAAS,KAAK,CAAC,IAAY,QAAqC;AAAA,EAC/D,OAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AAAA,IACvC,IAAI,QAAQ,SAAS;AAAA,MACpB,OAAO,OAAO,UAAU,IAAI,aAAa,WAAW,YAAY,CAAC;AAAA,MACjE;AAAA,IACD;AAAA,IACA,MAAM,KAAK,WAAW,MAAM;AAAA,MAC3B,QAAQ,oBAAoB,SAAS,OAAO;AAAA,MAC5C,QAAQ;AAAA,OACN,EAAE;AAAA,IACL,MAAM,UAAU,MAAM;AAAA,MACrB,aAAa,EAAE;AAAA,MACf,OAAO,QAAQ,UAAU,IAAI,aAA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CAAC;AAAA,GACY;",
14
+ "debugId": "2E949C7E6C6632FA64756E2164756E21",
15
+ "names": []
16
+ }
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@nexusts/resilience",
3
+ "version": "0.7.0",
4
+ "description": "Retry + Circuit Breaker + Bulkhead",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "README.md"
18
+ ],
19
+ "scripts": {
20
+ "build": "bun run ../../build.ts"
21
+ },
22
+ "keywords": [
23
+ "nexusts",
24
+ "framework",
25
+ "bun"
26
+ ],
27
+ "license": "MIT",
28
+ "dependencies": {
29
+ "@nexusts/core": "^0.7.0"
30
+ }
31
+ }