breaker-box 4.2.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -27,7 +27,6 @@ async function unreliableApiCall(data: string) {
27
27
  }
28
28
 
29
29
  const protectedApiCall = createCircuitBreaker(unreliableApiCall, {
30
- errorIsFailure: (error) => error.message.includes("404"), // Don't retry 404s
31
30
  errorThreshold: 0.5, // Open circuit when 50% of calls fail
32
31
  errorWindow: 10_000, // Track errors over 10 second window
33
32
  minimumCandidates: 6, // Need at least 6 calls before calculating error rate
@@ -44,37 +43,54 @@ try {
44
43
 
45
44
  ### Retry Strategies
46
45
 
46
+ The circuit breaker doesn't retry - it only tracks failures and prevents calls when too many fail. Use `withRetry` to add retry logic:
47
+
47
48
  ```typescript
48
49
  import {
49
50
  createCircuitBreaker,
51
+ withRetry,
50
52
  useExponentialBackoff,
51
53
  useFibonacciBackoff,
52
54
  } from "breaker-box"
53
55
 
54
56
  // Exponential backoff: 1s, 2s, 4s, 8s, up to 30s max
55
- const protectedWithExponential = createCircuitBreaker(unreliableApiCall, {
56
- retryDelay: useExponentialBackoff(30),
57
- })
57
+ const protectedWithExponential = createCircuitBreaker(
58
+ withRetry(unreliableApiCall, { retryDelay: useExponentialBackoff(30) }),
59
+ )
58
60
 
59
61
  // Fibonacci backoff: 1s, 2s, 3s, 5s, 8s, up to 60s max
60
- const protectedWithFibonacci = createCircuitBreaker(unreliableApiCall, {
61
- retryDelay: useFibonacciBackoff(60),
62
- })
62
+ const protectedWithFibonacci = createCircuitBreaker(
63
+ withRetry(unreliableApiCall, { retryDelay: useFibonacciBackoff(60) }),
64
+ )
63
65
  ```
64
66
 
65
67
  ### Timeout Protection
66
68
 
69
+ Compose `withTimeout`, `withRetry`, and circuit breaker for complete fault tolerance:
70
+
67
71
  ```typescript
68
- import { createCircuitBreaker, withTimeout } from "breaker-box"
72
+ import {
73
+ createCircuitBreaker,
74
+ withRetry,
75
+ withTimeout,
76
+ useExponentialBackoff,
77
+ } from "breaker-box"
69
78
 
70
- // Wrap function with 5-second timeout
71
- const timeoutProtectedCall = withTimeout(
72
- unreliableApiCall,
73
- 5_000,
74
- "Request timed out",
75
- )
79
+ // 1. Add timeout (innermost - fails fast)
80
+ const timedCall = withTimeout(unreliableApiCall, 5_000, "Request timed out")
81
+
82
+ // 2. Add retry logic (middle - retries on transient errors)
83
+ const retryCall = withRetry(timedCall, {
84
+ maxAttempts: 3,
85
+ retryDelay: useExponentialBackoff(10),
86
+ shouldRetry: (error) => !error.message.includes("404"), // Don't retry 404s
87
+ })
76
88
 
77
- const protectedApiCall = createCircuitBreaker(timeoutProtectedCall, {})
89
+ // 3. Add circuit breaker (outermost - prevents cascading failures)
90
+ const protectedApiCall = createCircuitBreaker(retryCall, {
91
+ errorThreshold: 0.5,
92
+ minimumCandidates: 5,
93
+ })
78
94
  ```
79
95
 
80
96
  ### Event Monitoring
@@ -82,10 +98,13 @@ const protectedApiCall = createCircuitBreaker(timeoutProtectedCall, {})
82
98
  ```typescript
83
99
  const protectedFunction = createCircuitBreaker(unreliableApiCall, {
84
100
  onClose: () => {
85
- console.log("Circuit closed - normal operation resumed")
101
+ console.log("🟢 Circuit closed - normal operation resumed")
102
+ },
103
+ onHalfOpen: () => {
104
+ console.log("🟡 Circuit half-opened - waiting for success")
86
105
  },
87
106
  onOpen: (cause) => {
88
- console.log("Circuit opened due to:", cause.message)
107
+ console.log("🔴 Circuit opened due to:", cause.message)
89
108
  },
90
109
  })
91
110
 
@@ -111,15 +130,15 @@ Creates a circuit breaker around the provided async function.
111
130
 
112
131
  - `fn`: The async function to protect
113
132
  - `options`: Configuration object (optional)
114
- - `errorIsFailure`: Function to determine if an error is non-retryable (default: `() => false`)
133
+ - `errorIsFailure`: Function to determine if an error should not count toward circuit breaker metrics (default: `() => false`)
115
134
  - `errorThreshold`: Percentage (0-1) of errors that triggers circuit opening (default: `0`)
116
135
  - `errorWindow`: Time window in ms for tracking errors (default: `10_000`)
117
136
  - `fallback`: Function to call when circuit is open (default: undefined)
118
137
  - `minimumCandidates`: Minimum calls before calculating error rate (default: `6`)
119
138
  - `onClose`: Function called when circuit closes (default: undefined)
139
+ - `onHalfOpen`: Function called when circuit enters half-open state (default: undefined)
120
140
  - `onOpen`: Function called when circuit opens (default: undefined)
121
141
  - `resetAfter`: Milliseconds to wait before trying half-open (default: `30_000`)
122
- - `retryDelay`: Function returning promise for retry delays (default: immediate retry)
123
142
 
124
143
  #### Returns
125
144
 
@@ -131,6 +150,26 @@ A function with the same signature as `fn` and additional methods:
131
150
 
132
151
  ### Helper Functions
133
152
 
153
+ #### `withRetry(fn, options?)`
154
+
155
+ Wraps a function with retry logic. Failures will be retried according to the provided options.
156
+
157
+ **Options:**
158
+
159
+ - `maxAttempts`: Maximum number of attempts (default: `3`)
160
+ - `retryDelay`: Function returning promise for when to retry (default: immediate)
161
+ - `shouldRetry`: Function to determine if error should be retried (default: `() => true`)
162
+
163
+ **Example:**
164
+
165
+ ```typescript
166
+ const retryCall = withRetry(apiCall, {
167
+ maxAttempts: 5,
168
+ retryDelay: useExponentialBackoff(30),
169
+ shouldRetry: (error) => error.statusCode !== 404,
170
+ })
171
+ ```
172
+
134
173
  #### `useExponentialBackoff(maxSeconds)`
135
174
 
136
175
  Returns a retry delay function that implements exponential backoff (2^n seconds, capped at maxSeconds).
package/dist/index.cjs CHANGED
@@ -10,12 +10,12 @@ const delayMs = (ms) => new Promise((next) => setTimeout(next, ms));
10
10
  const rejectOnAbort = (signal, pending) => {
11
11
  let teardown;
12
12
  return Promise.race([
13
- Promise.resolve(pending).finally(
14
- () => signal.removeEventListener("abort", teardown)
15
- ),
13
+ Promise.resolve(pending).finally(() => {
14
+ signal.removeEventListener("abort", teardown);
15
+ }),
16
16
  new Promise((_, reject) => {
17
17
  teardown = () => reject(signal.reason);
18
- signal.addEventListener("abort", teardown);
18
+ signal.addEventListener("abort", teardown, { once: true });
19
19
  })
20
20
  ]);
21
21
  };
@@ -29,8 +29,7 @@ function parseOptions(options) {
29
29
  onClose,
30
30
  onHalfOpen,
31
31
  onOpen,
32
- resetAfter = 3e4,
33
- retryDelay = () => void 0
32
+ resetAfter = 3e4
34
33
  } = options;
35
34
  assert(
36
35
  typeof errorIsFailure === "function",
@@ -68,10 +67,6 @@ function parseOptions(options) {
68
67
  resetAfter >= errorWindow,
69
68
  `"resetAfter" must be greater than or equal to "errorWindow" (received ${resetAfter}, expected >= ${errorWindow})`
70
69
  );
71
- assert(
72
- typeof retryDelay === "function",
73
- `"retryDelay" must be a function (received ${typeof retryDelay})`
74
- );
75
70
  return {
76
71
  errorIsFailure,
77
72
  errorThreshold,
@@ -80,11 +75,12 @@ function parseOptions(options) {
80
75
  onClose,
81
76
  onHalfOpen,
82
77
  onOpen,
83
- resetAfter,
84
- retryDelay
78
+ resetAfter
85
79
  };
86
80
  }
87
81
 
82
+ const disposeKey = Symbol("disposeKey");
83
+
88
84
  function useExponentialBackoff(maxSeconds) {
89
85
  return function exponentialBackoff(attempt) {
90
86
  const num = Math.max(attempt - 2, 0);
@@ -100,17 +96,70 @@ function useFibonacciBackoff(maxSeconds) {
100
96
  return delayMs(delay * 1e3);
101
97
  };
102
98
  }
99
+ function withRetry(main, options = {}) {
100
+ const {
101
+ shouldRetry = () => true,
102
+ maxAttempts = 3,
103
+ retryDelay = () => Promise.resolve()
104
+ } = options;
105
+ assert(maxAttempts >= 1, "maxAttempts must be a number greater than 0");
106
+ const controller = new AbortController();
107
+ const { signal } = controller;
108
+ async function execute(args, attempt = 1) {
109
+ try {
110
+ return await main(...args);
111
+ } catch (cause) {
112
+ if (!shouldRetry(cause, attempt)) throw cause;
113
+ if (attempt >= maxAttempts)
114
+ throw new Error(`ERR_CIRCUIT_BREAKER_MAX_ATTEMPTS (${maxAttempts})`, {
115
+ cause
116
+ });
117
+ await rejectOnAbort(signal, retryDelay(attempt + 1, signal));
118
+ return execute(args, attempt + 1);
119
+ }
120
+ }
121
+ return Object.assign(
122
+ function withRetryFunction(...args) {
123
+ return execute(args);
124
+ },
125
+ {
126
+ [disposeKey]: (disposeMessage = "ERR_CIRCUIT_BREAKER_DISPOSED") => {
127
+ const reason = new ReferenceError(disposeMessage);
128
+ main[disposeKey]?.(disposeMessage);
129
+ controller.abort(reason);
130
+ }
131
+ }
132
+ );
133
+ }
103
134
  function withTimeout(main, timeoutMs, timeoutMessage = "ERR_CIRCUIT_BREAKER_TIMEOUT") {
104
135
  const error = new Error(timeoutMessage);
105
- return function withTimeoutFunction(...args) {
136
+ const controller = new AbortController();
137
+ const { signal } = controller;
138
+ function withTimeoutFunction(...args) {
139
+ let teardown;
106
140
  let timer;
107
141
  return Promise.race([
108
- main(...args).finally(() => clearTimeout(timer)),
142
+ main(...args).finally(() => {
143
+ clearTimeout(timer);
144
+ signal.removeEventListener("abort", teardown);
145
+ }),
109
146
  new Promise((_, reject) => {
147
+ teardown = () => {
148
+ clearTimeout(timer);
149
+ reject(signal.reason);
150
+ };
110
151
  timer = setTimeout(reject, timeoutMs, error);
152
+ signal.addEventListener("abort", teardown, { once: true });
111
153
  })
112
154
  ]);
113
- };
155
+ }
156
+ return Object.assign(withTimeoutFunction, {
157
+ [disposeKey]: (disposeMessage = "ERR_CIRCUIT_BREAKER_DISPOSED") => {
158
+ const reason = new ReferenceError(disposeMessage);
159
+ main[disposeKey]?.(disposeMessage);
160
+ controller.abort(reason);
161
+ }
162
+ });
114
163
  }
115
164
 
116
165
  function createCircuitBreaker(main, options = {}) {
@@ -122,8 +171,7 @@ function createCircuitBreaker(main, options = {}) {
122
171
  onClose,
123
172
  onHalfOpen,
124
173
  onOpen,
125
- resetAfter,
126
- retryDelay
174
+ resetAfter
127
175
  } = parseOptions(options);
128
176
  const controller = new AbortController();
129
177
  const history = /* @__PURE__ */ new Map();
@@ -169,7 +217,7 @@ function createCircuitBreaker(main, options = {}) {
169
217
  history.delete(pending);
170
218
  signal.removeEventListener("abort", teardown);
171
219
  };
172
- signal.addEventListener("abort", teardown);
220
+ signal.addEventListener("abort", teardown, { once: true });
173
221
  const settle = (value) => {
174
222
  if (signal.aborted) return;
175
223
  entry.status = value;
@@ -178,7 +226,7 @@ function createCircuitBreaker(main, options = {}) {
178
226
  history.set(pending, entry);
179
227
  return { pending, settle, teardown };
180
228
  }
181
- function execute(attempt, args) {
229
+ function execute(args) {
182
230
  if (state === "closed") {
183
231
  const { pending, settle, teardown } = createHistoryItem(main(...args));
184
232
  return pending.then(
@@ -186,16 +234,14 @@ function createCircuitBreaker(main, options = {}) {
186
234
  settle("resolved");
187
235
  return result;
188
236
  },
189
- async (cause) => {
237
+ (cause) => {
190
238
  if (signal.aborted || errorIsFailure(cause)) {
191
239
  teardown();
192
240
  throw cause;
193
241
  }
194
242
  settle("rejected");
195
243
  if (failureRate() > errorThreshold) openCircuit(cause);
196
- const next = attempt + 1;
197
- await rejectOnAbort(signal, retryDelay(next, signal));
198
- return execute(next, args);
244
+ return fallback(...args);
199
245
  }
200
246
  );
201
247
  } else if (state === "open" || halfOpenPending) {
@@ -207,20 +253,19 @@ function createCircuitBreaker(main, options = {}) {
207
253
  closeCircuit();
208
254
  return result;
209
255
  },
210
- async (cause) => {
256
+ (cause) => {
211
257
  if (signal.aborted || errorIsFailure(cause)) throw cause;
212
258
  openCircuit(cause);
213
- const next = attempt + 1;
214
- await rejectOnAbort(signal, retryDelay(next, signal));
215
- return execute(next, args);
259
+ return fallback(...args);
216
260
  }
217
261
  );
218
262
  }
219
263
  return assertNever(state);
220
264
  }
221
- return Object.assign((...args) => execute(1, args), {
265
+ return Object.assign((...args) => execute(args), {
222
266
  dispose: (disposeMessage = "ERR_CIRCUIT_BREAKER_DISPOSED") => {
223
267
  const reason = new ReferenceError(disposeMessage);
268
+ main[disposeKey]?.(disposeMessage);
224
269
  clearFailure();
225
270
  clearTimeout(resetTimer);
226
271
  history.forEach((entry) => clearTimeout(entry.timer));
@@ -238,5 +283,6 @@ exports.createCircuitBreaker = createCircuitBreaker;
238
283
  exports.delayMs = delayMs;
239
284
  exports.useExponentialBackoff = useExponentialBackoff;
240
285
  exports.useFibonacciBackoff = useFibonacciBackoff;
286
+ exports.withRetry = withRetry;
241
287
  exports.withTimeout = withTimeout;
242
288
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../lib/util.ts","../lib/options.ts","../lib/helpers.ts","../lib/index.ts"],"sourcesContent":["export type AnyFn = (...args: any[]) => any\n\n/**\n * Asserts that the given value is truthy. If not, throws a `TypeError`.\n */\nexport function assert(value: unknown, message?: string): asserts value {\n\tif (!value) throw new TypeError(message)\n}\n\n/**\n * `[TypeScript]` For exhaustive checks in switch statements or if/else. Add\n * this check to `default` case or final `else` to ensure all possible values\n * have been handled. If a new value is added to the type, TypeScript will\n * throw an error and the editor will underline the `value`.\n */\n/* v8 ignore next 3 */\nexport const assertNever = (val: never, msg = \"Unexpected value\") => {\n\tthrow new TypeError(`${msg}: ${val}`)\n}\n\n/**\n * Returns a promise that resolves after the specified number of milliseconds.\n */\nexport const delayMs = (ms: number): Promise<void> =>\n\tnew Promise((next) => setTimeout(next, ms))\n\n/**\n * Rejects the given promise when the abort signal is triggered.\n */\nexport const rejectOnAbort = <T extends Promise<unknown> | undefined>(\n\tsignal: AbortSignal,\n\tpending: T,\n): Promise<Awaited<T>> => {\n\tlet teardown: () => void\n\treturn Promise.race([\n\t\tPromise.resolve(pending).finally(() =>\n\t\t\tsignal.removeEventListener(\"abort\", teardown),\n\t\t),\n\t\tnew Promise<never>((_, reject) => {\n\t\t\tteardown = () => reject(signal.reason)\n\t\t\tsignal.addEventListener(\"abort\", teardown)\n\t\t}),\n\t])\n}\n","import type { CircuitBreakerOptions } from \"./types.js\"\nimport { assert, type AnyFn } from \"./util.js\"\n\nexport function parseOptions<Fallback extends AnyFn>(\n\toptions: CircuitBreakerOptions<Fallback>,\n) {\n\tconst {\n\t\terrorIsFailure = () => false,\n\t\terrorThreshold = 0,\n\t\terrorWindow = 10_000,\n\t\tminimumCandidates = 6,\n\t\tonClose,\n\t\tonHalfOpen,\n\t\tonOpen,\n\t\tresetAfter = 30_000,\n\t\tretryDelay = () => undefined,\n\t} = options\n\n\t// errorIsFailure\n\tassert(\n\t\ttypeof errorIsFailure === \"function\",\n\t\t`\"errorIsFailure\" must be a function (received ${typeof errorIsFailure})`,\n\t)\n\n\t// errorThreshold\n\tassert(\n\t\terrorThreshold >= 0 && errorThreshold <= 1,\n\t\t`\"errorThreshold\" must be between 0 and 1 (received ${errorThreshold})`,\n\t)\n\n\t// errorWindow\n\tassert(\n\t\terrorWindow >= 1_000,\n\t\t`\"errorWindow\" must be milliseconds of at least 1 second (received ${errorWindow})`,\n\t)\n\n\t// minimumCandidates\n\tassert(\n\t\tminimumCandidates >= 1,\n\t\t`\"minimumCandidates\" must be greater than 0 (received ${minimumCandidates})`,\n\t)\n\n\t// (optional) onClose\n\tassert(\n\t\t!onClose || typeof onClose === \"function\",\n\t\t`\"onClose\" must be a function (received ${typeof onClose})`,\n\t)\n\n\t// (optional) onHalfOpen\n\tassert(\n\t\t!onHalfOpen || typeof onHalfOpen === \"function\",\n\t\t`\"onHalfOpen\" must be a function (received ${typeof onHalfOpen})`,\n\t)\n\n\t// (optional) onOpen\n\tassert(\n\t\t!onOpen || typeof onOpen === \"function\",\n\t\t`\"onOpen\" must be a function (received ${typeof onOpen})`,\n\t)\n\n\t// resetAfter\n\tassert(\n\t\tresetAfter >= 1_000,\n\t\t`\"resetAfter\" must be milliseconds of at least 1 second (received ${resetAfter})`,\n\t)\n\tassert(\n\t\tresetAfter >= errorWindow,\n\t\t`\"resetAfter\" must be greater than or equal to \"errorWindow\" (received ${resetAfter}, expected >= ${errorWindow})`,\n\t)\n\n\t// retryDelay\n\tassert(\n\t\ttypeof retryDelay === \"function\",\n\t\t`\"retryDelay\" must be a function (received ${typeof retryDelay})`,\n\t)\n\n\treturn {\n\t\terrorIsFailure,\n\t\terrorThreshold,\n\t\terrorWindow,\n\t\tminimumCandidates,\n\t\tonClose,\n\t\tonHalfOpen,\n\t\tonOpen,\n\t\tresetAfter,\n\t\tretryDelay,\n\t}\n}\n","import type { MainFn } from \"./types.js\"\nimport { delayMs } from \"./util.js\"\n\n/**\n * Returns a function which implements exponential backoff.\n *\n * @param maxSeconds - The maximum number of seconds to wait before retrying.\n * @returns A function which takes an `attempt` number and returns a promise\n * that resolves after the calculated delay.\n */\nexport function useExponentialBackoff(maxSeconds: number) {\n\treturn function exponentialBackoff(attempt: number) {\n\t\tconst num = Math.max(attempt - 2, 0)\n\t\tconst delay = Math.min(2 ** num, maxSeconds)\n\t\treturn delayMs(delay * 1_000)\n\t}\n}\n\nconst sqrt5 = /* @__PURE__ */ Math.sqrt(5)\n/**\n * @see https://en.wikipedia.org/wiki/Fibonacci_sequence#Closed-form_expression\n */\nconst binet = (n: number) =>\n\tMath.round(((1 + sqrt5) ** n - (1 - sqrt5) ** n) / (2 ** n * sqrt5))\n\n/**\n * Returns a function which implements Fibonacci backoff.\n *\n * @param maxSeconds - The maximum number of seconds to wait before retrying.\n * @returns A function which takes an `attempt` number and returns a promise\n * that resolves after the calculated delay.\n */\nexport function useFibonacciBackoff(maxSeconds: number) {\n\treturn function fibonacciBackoff(attempt: number) {\n\t\tconst delay = Math.min(binet(attempt), maxSeconds)\n\t\treturn delayMs(delay * 1_000)\n\t}\n}\n\n/**\n * Wrap a function with a timeout. If execution of `main` exceeds `timeoutMs`,\n * then the call is rejected with `new Error(timeoutMessage)`.\n */\nexport function withTimeout<Ret, Args extends unknown[]>(\n\tmain: MainFn<Ret, Args>,\n\ttimeoutMs: number,\n\ttimeoutMessage = \"ERR_CIRCUIT_BREAKER_TIMEOUT\",\n): MainFn<Ret, Args> {\n\tconst error = new Error(timeoutMessage)\n\n\treturn function withTimeoutFunction(...args) {\n\t\tlet timer: NodeJS.Timeout\n\t\treturn Promise.race([\n\t\t\tmain(...args).finally(() => clearTimeout(timer)),\n\t\t\tnew Promise<never>((_, reject) => {\n\t\t\t\ttimer = setTimeout(reject, timeoutMs, error)\n\t\t\t}),\n\t\t])\n\t}\n}\n","import { parseOptions } from \"./options.js\"\nimport type {\n\tHistoryEntry,\n\tHistoryMap,\n\tCircuitBreakerOptions,\n\tCircuitBreakerProtectedFn,\n\tCircuitState,\n\tMainFn,\n} from \"./types.js\"\nimport { assertNever, rejectOnAbort, type AnyFn } from \"./util.js\"\n\nexport * from \"./helpers.js\"\nexport { delayMs } from \"./util.js\"\n\nexport function createCircuitBreaker<\n\tRet,\n\tArgs extends unknown[],\n\tFallback extends AnyFn = MainFn<Ret, Args>,\n>(\n\tmain: MainFn<Ret, Args>,\n\toptions: CircuitBreakerOptions<Fallback> = {},\n): CircuitBreakerProtectedFn<Ret, Args> {\n\tconst {\n\t\terrorIsFailure,\n\t\terrorThreshold,\n\t\terrorWindow,\n\t\tminimumCandidates,\n\t\tonClose,\n\t\tonHalfOpen,\n\t\tonOpen,\n\t\tresetAfter,\n\t\tretryDelay,\n\t} = parseOptions(options)\n\tconst controller = new AbortController()\n\tconst history: HistoryMap = new Map()\n\tconst signal = controller.signal\n\tlet failureCause: unknown\n\tlet fallback = options.fallback || (() => Promise.reject(failureCause))\n\tlet halfOpenPending: Promise<unknown> | undefined\n\tlet resetTimer: NodeJS.Timeout\n\tlet state: CircuitState = \"closed\"\n\n\tfunction clearFailure() {\n\t\tfailureCause = undefined\n\t}\n\n\tfunction closeCircuit() {\n\t\tstate = \"closed\"\n\t\tclearFailure()\n\t\tclearTimeout(resetTimer)\n\t\tonClose?.()\n\t}\n\n\tfunction failureRate() {\n\t\tlet failures = 0\n\t\tlet total = 0\n\t\tfor (const { status } of history.values()) {\n\t\t\tif (status === \"rejected\") failures++\n\t\t\tif (status !== \"pending\") total++\n\t\t}\n\t\t// Don't calculate anything until we have enough data\n\t\tif (!total || total < minimumCandidates) return 0\n\t\treturn failures / total\n\t}\n\n\t/**\n\t * Break the circuit and wait for a reset\n\t */\n\tfunction openCircuit(cause: unknown) {\n\t\tfailureCause = cause\n\t\tstate = \"open\"\n\t\tclearTimeout(resetTimer)\n\t\tresetTimer = setTimeout(() => {\n\t\t\tstate = \"halfOpen\"\n\t\t\tonHalfOpen?.()\n\t\t}, resetAfter)\n\t\tonOpen?.(cause)\n\t}\n\n\tfunction createHistoryItem<T>(pending: Promise<T>) {\n\t\tconst entry: HistoryEntry = { status: \"pending\", timer: undefined }\n\t\tconst teardown = () => {\n\t\t\tclearTimeout(entry.timer)\n\t\t\thistory.delete(pending)\n\t\t\tsignal.removeEventListener(\"abort\", teardown)\n\t\t}\n\t\tsignal.addEventListener(\"abort\", teardown)\n\t\tconst settle = (value: \"resolved\" | \"rejected\") => {\n\t\t\tif (signal.aborted) return\n\t\t\tentry.status = value\n\t\t\t// Remove the entry from history when it falls outside of the error window\n\t\t\tentry.timer = setTimeout(teardown, errorWindow)\n\t\t}\n\t\thistory.set(pending, entry)\n\t\treturn { pending, settle, teardown }\n\t}\n\n\t/**\n\t * Wrap calls to `main` with circuit breaker logic\n\t */\n\tfunction execute(attempt: number, args: Args): Promise<Ret> {\n\t\t// Normal operation when circuit is closed. If an error occurs, keep track\n\t\t// of the failure count and open the circuit if it exceeds the threshold.\n\t\tif (state === \"closed\") {\n\t\t\tconst { pending, settle, teardown } = createHistoryItem(main(...args))\n\t\t\treturn pending.then(\n\t\t\t\t(result) => {\n\t\t\t\t\tsettle(\"resolved\")\n\t\t\t\t\treturn result\n\t\t\t\t},\n\t\t\t\tasync (cause: unknown) => {\n\t\t\t\t\t// Was the circuit disposed, or was this a non-retryable error?\n\t\t\t\t\tif (signal.aborted || errorIsFailure(cause)) {\n\t\t\t\t\t\tteardown()\n\t\t\t\t\t\tthrow cause\n\t\t\t\t\t}\n\n\t\t\t\t\t// Should this failure open the circuit?\n\t\t\t\t\tsettle(\"rejected\")\n\t\t\t\t\tif (failureRate() > errorThreshold) openCircuit(cause)\n\n\t\t\t\t\t// Retry the call after a delay.\n\t\t\t\t\tconst next = attempt + 1\n\t\t\t\t\tawait rejectOnAbort(signal, retryDelay(next, signal))\n\t\t\t\t\treturn execute(next, args)\n\t\t\t\t},\n\t\t\t)\n\t\t}\n\n\t\t// Use the fallback while the circuit is open, or if a half-open trial\n\t\t// attempt was already made.\n\t\telse if (state === \"open\" || halfOpenPending) {\n\t\t\treturn fallback(...args)\n\t\t}\n\n\t\t// If the circuit is half-open, make one attempt. If it succeeds, close\n\t\t// the circuit and resume normal operation. If it fails, re-open the\n\t\t// circuit and run the fallback instead.\n\t\telse if (state === \"halfOpen\") {\n\t\t\treturn (halfOpenPending = main(...args))\n\t\t\t\t.finally(() => (halfOpenPending = undefined))\n\t\t\t\t.then(\n\t\t\t\t\t(result) => {\n\t\t\t\t\t\tif (signal.aborted) return result // disposed\n\t\t\t\t\t\tcloseCircuit()\n\t\t\t\t\t\treturn result\n\t\t\t\t\t},\n\t\t\t\t\tasync (cause: unknown) => {\n\t\t\t\t\t\t// Was the circuit disposed, or was this a non-retryable error?\n\t\t\t\t\t\tif (signal.aborted || errorIsFailure(cause)) throw cause\n\n\t\t\t\t\t\t// Open the circuit and try again later\n\t\t\t\t\t\topenCircuit(cause)\n\n\t\t\t\t\t\t// Retry the call after a delay.\n\t\t\t\t\t\tconst next = attempt + 1\n\t\t\t\t\t\tawait rejectOnAbort(signal, retryDelay(next, signal))\n\t\t\t\t\t\treturn execute(next, args)\n\t\t\t\t\t},\n\t\t\t\t)\n\t\t\t/* v8 ignore next */\n\t\t}\n\n\t\t// exhaustive check\n\t\t/* v8 ignore next */\n\t\treturn assertNever(state)\n\t}\n\n\treturn Object.assign((...args: Args) => execute(1, args), {\n\t\tdispose: (disposeMessage = \"ERR_CIRCUIT_BREAKER_DISPOSED\") => {\n\t\t\tconst reason = new ReferenceError(disposeMessage)\n\t\t\tclearFailure()\n\t\t\tclearTimeout(resetTimer)\n\t\t\thistory.forEach((entry) => clearTimeout(entry.timer))\n\t\t\thistory.clear()\n\t\t\tfallback = () => Promise.reject(reason)\n\t\t\tstate = \"open\"\n\t\t\tcontroller.abort(reason)\n\t\t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,KAAK,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AAC3D,MAAM,OAAO,CAAC,KAAK,EAAE;AACrB,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC;AAC7C,MAAM,KAAK,GAAG,MAAM;AACpB,MAAM,UAAU,CAAC,KAAK,CAAC,MAAM,CAAC;AAC9B,IAAI,CAAC;AACL,IAAI,cAAc,EAAE,MAAM,YAAY;AACtC,IAAI,QAAQ,EAAE,MAAM;AACpB,GAAG,CAAC;AACJ;;;;;;;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../lib/util.ts","../lib/options.ts","../lib/types.ts","../lib/helpers.ts","../lib/index.ts"],"sourcesContent":["export type AnyFn = (...args: any[]) => any\n\n/**\n * Asserts that the given value is truthy. If not, throws a `TypeError`.\n */\nexport function assert(value: unknown, message?: string): asserts value {\n\tif (!value) throw new TypeError(message)\n}\n\n/**\n * `[TypeScript]` For exhaustive checks in switch statements or if/else. Add\n * this check to `default` case or final `else` to ensure all possible values\n * have been handled. If a new value is added to the type, TypeScript will\n * throw an error and the editor will underline the `value`.\n */\n/* v8 ignore next 3 */\nexport const assertNever = (val: never, msg = \"Unexpected value\") => {\n\tthrow new TypeError(`${msg}: ${val}`)\n}\n\n/**\n * Returns a promise that resolves after the specified number of milliseconds.\n */\nexport const delayMs = (ms: number): Promise<void> =>\n\tnew Promise((next) => setTimeout(next, ms))\n\n/**\n * Rejects the given promise when the abort signal is triggered.\n */\nexport const rejectOnAbort = <T extends Promise<unknown> | undefined>(\n\tsignal: AbortSignal,\n\tpending: T,\n): Promise<Awaited<T>> => {\n\tlet teardown: () => void\n\treturn Promise.race([\n\t\tPromise.resolve(pending).finally(() => {\n\t\t\tsignal.removeEventListener(\"abort\", teardown)\n\t\t}),\n\t\tnew Promise<never>((_, reject) => {\n\t\t\tteardown = () => reject(signal.reason)\n\t\t\tsignal.addEventListener(\"abort\", teardown, { once: true })\n\t\t}),\n\t])\n}\n","import type { CircuitBreakerOptions } from \"./types.js\"\nimport { assert, type AnyFn } from \"./util.js\"\n\nexport function parseOptions<Fallback extends AnyFn>(\n\toptions: CircuitBreakerOptions<Fallback>,\n) {\n\tconst {\n\t\terrorIsFailure = () => false,\n\t\terrorThreshold = 0,\n\t\terrorWindow = 10_000,\n\t\tminimumCandidates = 6,\n\t\tonClose,\n\t\tonHalfOpen,\n\t\tonOpen,\n\t\tresetAfter = 30_000,\n\t} = options\n\n\t// errorIsFailure\n\tassert(\n\t\ttypeof errorIsFailure === \"function\",\n\t\t`\"errorIsFailure\" must be a function (received ${typeof errorIsFailure})`,\n\t)\n\n\t// errorThreshold\n\tassert(\n\t\terrorThreshold >= 0 && errorThreshold <= 1,\n\t\t`\"errorThreshold\" must be between 0 and 1 (received ${errorThreshold})`,\n\t)\n\n\t// errorWindow\n\tassert(\n\t\terrorWindow >= 1_000,\n\t\t`\"errorWindow\" must be milliseconds of at least 1 second (received ${errorWindow})`,\n\t)\n\n\t// minimumCandidates\n\tassert(\n\t\tminimumCandidates >= 1,\n\t\t`\"minimumCandidates\" must be greater than 0 (received ${minimumCandidates})`,\n\t)\n\n\t// (optional) onClose\n\tassert(\n\t\t!onClose || typeof onClose === \"function\",\n\t\t`\"onClose\" must be a function (received ${typeof onClose})`,\n\t)\n\n\t// (optional) onHalfOpen\n\tassert(\n\t\t!onHalfOpen || typeof onHalfOpen === \"function\",\n\t\t`\"onHalfOpen\" must be a function (received ${typeof onHalfOpen})`,\n\t)\n\n\t// (optional) onOpen\n\tassert(\n\t\t!onOpen || typeof onOpen === \"function\",\n\t\t`\"onOpen\" must be a function (received ${typeof onOpen})`,\n\t)\n\n\t// resetAfter\n\tassert(\n\t\tresetAfter >= 1_000,\n\t\t`\"resetAfter\" must be milliseconds of at least 1 second (received ${resetAfter})`,\n\t)\n\tassert(\n\t\tresetAfter >= errorWindow,\n\t\t`\"resetAfter\" must be greater than or equal to \"errorWindow\" (received ${resetAfter}, expected >= ${errorWindow})`,\n\t)\n\n\treturn {\n\t\terrorIsFailure,\n\t\terrorThreshold,\n\t\terrorWindow,\n\t\tminimumCandidates,\n\t\tonClose,\n\t\tonHalfOpen,\n\t\tonOpen,\n\t\tresetAfter,\n\t}\n}\n","import type { AnyFn } from \"./util\"\n\nexport const disposeKey = Symbol(\"disposeKey\")\n\nexport type CircuitState = \"closed\" | \"halfOpen\" | \"open\"\n\nexport interface CircuitBreakerOptions<Fallback extends AnyFn = AnyFn> {\n\t/**\n\t * Whether an error should be treated as non-retryable failure. When used and\n\t * when an error is considered a failure, the error will be thrown to the\n\t * caller.\n\t *\n\t * @default () => false // Errors are retryable by default\n\t */\n\terrorIsFailure?: (error: unknown) => boolean\n\n\t/**\n\t * The percentage of errors (as a number between 0 and 1) which must occur\n\t * within the error window before the circuit breaker opens.\n\t *\n\t * @default 0 // Any error opens the circuit\n\t */\n\terrorThreshold?: number\n\n\t/**\n\t * The sliding window of time in milliseconds over which errors are counted.\n\t *\n\t * @default 10_000 // 10 seconds\n\t */\n\terrorWindow?: number\n\n\t/**\n\t * If provided, then all rejected calls to `main` will be forwarded to\n\t * this function instead.\n\t *\n\t * @default undefined // No fallback, errors are propagated\n\t */\n\tfallback?: Fallback\n\n\t/**\n\t * The minimum number of calls that must be made before calculating the\n\t * error rate and determining whether the circuit breaker should open based on\n\t * the `errorThreshold`.\n\t *\n\t * @default 6\n\t */\n\tminimumCandidates?: number\n\n\t/**\n\t * Provide a function to be called when the circuit breaker is closed.\n\t */\n\tonClose?: () => void\n\n\t/**\n\t * Provide a function to be called when the circuit breaker transitions to\n\t * half-open state.\n\t */\n\tonHalfOpen?: () => void\n\n\t/**\n\t * Provide a function to be called when the circuit breaker is opened. It\n\t * receives the error as its only argument.\n\t */\n\tonOpen?: (cause: unknown) => void\n\n\t/**\n\t * The amount of time in milliseconds to wait before transitioning to a\n\t * half-open state.\n\t *\n\t * @default 30_000 // 30 seconds\n\t */\n\tresetAfter?: number\n}\n\nexport interface CircuitBreakerProtectedFn<\n\tRet = unknown,\n\tArgs extends unknown[] = never[],\n> {\n\t(...args: Args): Promise<Ret>\n\n\t/**\n\t * Free memory and stop timers. All future calls will be rejected with the\n\t * provided message.\n\t *\n\t * @default \"ERR_CIRCUIT_BREAKER_DISPOSED\"\n\t */\n\tdispose(disposeMessage?: string): void\n\n\t/** Get the last error which triggered the circuit breaker */\n\tgetLatestError(): unknown | undefined\n\n\t/** Get the current state of the circuit breaker */\n\tgetState(): CircuitState\n}\n\nexport interface HistoryEntry {\n\ttimer: NodeJS.Timeout | undefined\n\tstatus: \"pending\" | \"resolved\" | \"rejected\"\n}\n\nexport type HistoryMap = Map<Promise<unknown>, HistoryEntry>\n\nexport interface MainFn<Ret = unknown, Args extends unknown[] = never[]> {\n\t(...args: Args): Promise<Ret>\n\n\t[disposeKey]?: (disposeMessage?: string) => void\n}\n","import { disposeKey, type MainFn } from \"./types.js\"\nimport { assert, delayMs, rejectOnAbort } from \"./util.js\"\n\n/**\n * Returns a function which implements exponential backoff.\n *\n * @param maxSeconds - The maximum number of seconds to wait before retrying.\n * @returns A function which takes an `attempt` number and returns a promise\n * that resolves after the calculated delay.\n */\nexport function useExponentialBackoff(maxSeconds: number) {\n\treturn function exponentialBackoff(attempt: number) {\n\t\tconst num = Math.max(attempt - 2, 0)\n\t\tconst delay = Math.min(2 ** num, maxSeconds)\n\t\treturn delayMs(delay * 1_000)\n\t}\n}\n\nconst sqrt5 = /* @__PURE__ */ Math.sqrt(5)\n/**\n * @see https://en.wikipedia.org/wiki/Fibonacci_sequence#Closed-form_expression\n */\nconst binet = (n: number) =>\n\tMath.round(((1 + sqrt5) ** n - (1 - sqrt5) ** n) / (2 ** n * sqrt5))\n\n/**\n * Returns a function which implements Fibonacci backoff.\n *\n * @param maxSeconds - The maximum number of seconds to wait before retrying.\n * @returns A function which takes an `attempt` number and returns a promise\n * that resolves after the calculated delay.\n */\nexport function useFibonacciBackoff(maxSeconds: number) {\n\treturn function fibonacciBackoff(attempt: number) {\n\t\tconst delay = Math.min(binet(attempt), maxSeconds)\n\t\treturn delayMs(delay * 1_000)\n\t}\n}\n\nexport interface RetryOptions {\n\t/**\n\t * Whether an error should be treated as non-retryable. When this returns\n\t * true, the error will be thrown immediately without retrying.\n\t *\n\t * @default () => false // All errors are retried\n\t */\n\tshouldRetry?: (error: unknown, attempt: number) => boolean\n\n\t/**\n\t * Maximum number of retries\n\t *\n\t * @default 3\n\t */\n\tmaxAttempts?: number\n\n\t/**\n\t * Function that returns a promise resolving when the next retry should occur.\n\t * Receives the attempt number (starting at 2) and an abort signal.\n\t *\n\t * @default () => Promise.resolve() // Immediate retry\n\t */\n\tretryDelay?: (attempt: number, signal: AbortSignal) => Promise<void>\n}\n\n/**\n * Wrap a function with retry logic. Errors will be retried according to the\n * provided options.\n *\n * @example\n * ```ts\n * // Compose with circuit breaker. Retry up to 3 times with no delay\n * const protectedA = createCircuitBreaker(\n * withRetry(unreliableApiCall, { maxAttempts: 3 })\n * )\n *\n * // Retry up to 5 times with exponential backoff\n * const protectedB = createCircuitBreaker(\n * withRetry(unreliableApiCall, {\n * maxAttempts: 5,\n * retryDelay: useExponentialBackoff(30),\n * })\n * )\n * ```\n */\nexport function withRetry<Ret, Args extends unknown[]>(\n\tmain: MainFn<Ret, Args>,\n\toptions: RetryOptions = {},\n): MainFn<Ret, Args> {\n\tconst {\n\t\tshouldRetry = () => true,\n\t\tmaxAttempts = 3,\n\t\tretryDelay = () => Promise.resolve(),\n\t} = options\n\n\tassert(maxAttempts >= 1, \"maxAttempts must be a number greater than 0\")\n\n\tconst controller = new AbortController()\n\tconst { signal } = controller\n\n\tasync function execute(args: Args, attempt = 1): Promise<Ret> {\n\t\ttry {\n\t\t\treturn await main(...args)\n\t\t} catch (cause) {\n\t\t\t// Check if we should retry this error\n\t\t\tif (!shouldRetry(cause, attempt)) throw cause\n\n\t\t\t// Check if we've exhausted attempts\n\t\t\tif (attempt >= maxAttempts)\n\t\t\t\tthrow new Error(`ERR_CIRCUIT_BREAKER_MAX_ATTEMPTS (${maxAttempts})`, {\n\t\t\t\t\tcause,\n\t\t\t\t})\n\n\t\t\t// Wait before retrying\n\t\t\tawait rejectOnAbort(signal, retryDelay(attempt + 1, signal))\n\n\t\t\t// Retry\n\t\t\treturn execute(args, attempt + 1)\n\t\t}\n\t}\n\n\treturn Object.assign(\n\t\tfunction withRetryFunction(...args: Args) {\n\t\t\treturn execute(args)\n\t\t},\n\t\t{\n\t\t\t[disposeKey]: (disposeMessage = \"ERR_CIRCUIT_BREAKER_DISPOSED\") => {\n\t\t\t\tconst reason = new ReferenceError(disposeMessage)\n\t\t\t\tmain[disposeKey]?.(disposeMessage)\n\t\t\t\tcontroller.abort(reason)\n\t\t\t},\n\t\t},\n\t)\n}\n\n/**\n * Wrap a function with a timeout. If execution of `main` exceeds `timeoutMs`,\n * then the call is rejected with `new Error(timeoutMessage)`.\n */\nexport function withTimeout<Ret, Args extends unknown[]>(\n\tmain: MainFn<Ret, Args>,\n\ttimeoutMs: number,\n\ttimeoutMessage = \"ERR_CIRCUIT_BREAKER_TIMEOUT\",\n): MainFn<Ret, Args> {\n\tconst error = new Error(timeoutMessage)\n\tconst controller = new AbortController()\n\tconst { signal } = controller\n\n\tfunction withTimeoutFunction(...args: Args) {\n\t\tlet teardown: () => void\n\t\tlet timer: NodeJS.Timeout\n\t\treturn Promise.race([\n\t\t\tmain(...args).finally(() => {\n\t\t\t\tclearTimeout(timer)\n\t\t\t\tsignal.removeEventListener(\"abort\", teardown)\n\t\t\t}),\n\t\t\tnew Promise<never>((_, reject) => {\n\t\t\t\tteardown = () => {\n\t\t\t\t\tclearTimeout(timer)\n\t\t\t\t\treject(signal.reason)\n\t\t\t\t}\n\t\t\t\ttimer = setTimeout(reject, timeoutMs, error)\n\t\t\t\tsignal.addEventListener(\"abort\", teardown, { once: true })\n\t\t\t}),\n\t\t])\n\t}\n\n\treturn Object.assign(withTimeoutFunction, {\n\t\t[disposeKey]: (disposeMessage = \"ERR_CIRCUIT_BREAKER_DISPOSED\") => {\n\t\t\tconst reason = new ReferenceError(disposeMessage)\n\t\t\tmain[disposeKey]?.(disposeMessage)\n\t\t\tcontroller.abort(reason)\n\t\t},\n\t})\n}\n","import { parseOptions } from \"./options.js\"\nimport {\n\tdisposeKey,\n\ttype CircuitBreakerOptions,\n\ttype CircuitBreakerProtectedFn,\n\ttype CircuitState,\n\ttype HistoryEntry,\n\ttype HistoryMap,\n\ttype MainFn,\n} from \"./types.js\"\nimport { assertNever, type AnyFn } from \"./util.js\"\n\nexport * from \"./helpers.js\"\nexport { delayMs } from \"./util.js\"\n\nexport function createCircuitBreaker<\n\tRet,\n\tArgs extends unknown[],\n\tFallback extends AnyFn = MainFn<Ret, Args>,\n>(\n\tmain: MainFn<Ret, Args>,\n\toptions: CircuitBreakerOptions<Fallback> = {},\n): CircuitBreakerProtectedFn<Ret, Args> {\n\tconst {\n\t\terrorIsFailure,\n\t\terrorThreshold,\n\t\terrorWindow,\n\t\tminimumCandidates,\n\t\tonClose,\n\t\tonHalfOpen,\n\t\tonOpen,\n\t\tresetAfter,\n\t} = parseOptions(options)\n\tconst controller = new AbortController()\n\tconst history: HistoryMap = new Map()\n\tconst signal = controller.signal\n\tlet failureCause: unknown\n\tlet fallback = options.fallback || (() => Promise.reject(failureCause))\n\tlet halfOpenPending: Promise<unknown> | undefined\n\tlet resetTimer: NodeJS.Timeout\n\tlet state: CircuitState = \"closed\"\n\n\tfunction clearFailure() {\n\t\tfailureCause = undefined\n\t}\n\n\tfunction closeCircuit() {\n\t\tstate = \"closed\"\n\t\tclearFailure()\n\t\tclearTimeout(resetTimer)\n\t\tonClose?.()\n\t}\n\n\tfunction failureRate() {\n\t\tlet failures = 0\n\t\tlet total = 0\n\t\tfor (const { status } of history.values()) {\n\t\t\tif (status === \"rejected\") failures++\n\t\t\tif (status !== \"pending\") total++\n\t\t}\n\t\t// Don't calculate anything until we have enough data\n\t\tif (!total || total < minimumCandidates) return 0\n\t\treturn failures / total\n\t}\n\n\t/**\n\t * Break the circuit and wait for a reset\n\t */\n\tfunction openCircuit(cause: unknown) {\n\t\tfailureCause = cause\n\t\tstate = \"open\"\n\t\tclearTimeout(resetTimer)\n\t\tresetTimer = setTimeout(() => {\n\t\t\tstate = \"halfOpen\"\n\t\t\tonHalfOpen?.()\n\t\t}, resetAfter)\n\t\tonOpen?.(cause)\n\t}\n\n\tfunction createHistoryItem<T>(pending: Promise<T>) {\n\t\tconst entry: HistoryEntry = { status: \"pending\", timer: undefined }\n\t\tconst teardown = () => {\n\t\t\tclearTimeout(entry.timer)\n\t\t\thistory.delete(pending)\n\t\t\tsignal.removeEventListener(\"abort\", teardown)\n\t\t}\n\t\tsignal.addEventListener(\"abort\", teardown, { once: true })\n\t\tconst settle = (value: \"resolved\" | \"rejected\") => {\n\t\t\tif (signal.aborted) return\n\t\t\tentry.status = value\n\t\t\t// Remove the entry from history when it falls outside of the error window\n\t\t\tentry.timer = setTimeout(teardown, errorWindow)\n\t\t}\n\t\thistory.set(pending, entry)\n\t\treturn { pending, settle, teardown }\n\t}\n\n\t/**\n\t * Wrap calls to `main` with circuit breaker logic\n\t */\n\tfunction execute(args: Args): Promise<Ret> {\n\t\t// Normal operation when circuit is closed. If an error occurs, keep track\n\t\t// of the failure count and open the circuit if it exceeds the threshold.\n\t\tif (state === \"closed\") {\n\t\t\tconst { pending, settle, teardown } = createHistoryItem(main(...args))\n\t\t\treturn pending.then(\n\t\t\t\t(result) => {\n\t\t\t\t\tsettle(\"resolved\")\n\t\t\t\t\treturn result\n\t\t\t\t},\n\t\t\t\t(cause: unknown) => {\n\t\t\t\t\t// Was the circuit disposed, or is this error considered a failure?\n\t\t\t\t\tif (signal.aborted || errorIsFailure(cause)) {\n\t\t\t\t\t\tteardown()\n\t\t\t\t\t\tthrow cause\n\t\t\t\t\t}\n\n\t\t\t\t\t// Should this error open the circuit?\n\t\t\t\t\tsettle(\"rejected\")\n\t\t\t\t\tif (failureRate() > errorThreshold) openCircuit(cause)\n\n\t\t\t\t\treturn fallback(...args)\n\t\t\t\t},\n\t\t\t)\n\t\t}\n\n\t\t// Use the fallback while the circuit is open, or if a half-open trial\n\t\t// attempt was already made.\n\t\telse if (state === \"open\" || halfOpenPending) {\n\t\t\treturn fallback(...args)\n\t\t}\n\n\t\t// If the circuit is half-open, make one attempt. If it succeeds, close\n\t\t// the circuit and resume normal operation. If it fails, re-open the\n\t\t// circuit and run the fallback instead.\n\t\telse if (state === \"halfOpen\") {\n\t\t\treturn (halfOpenPending = main(...args))\n\t\t\t\t.finally(() => (halfOpenPending = undefined))\n\t\t\t\t.then(\n\t\t\t\t\t(result) => {\n\t\t\t\t\t\tif (signal.aborted) return result // disposed\n\t\t\t\t\t\tcloseCircuit()\n\t\t\t\t\t\treturn result\n\t\t\t\t\t},\n\t\t\t\t\t(cause: unknown) => {\n\t\t\t\t\t\t// Was the circuit disposed, or was this a non-retryable error?\n\t\t\t\t\t\tif (signal.aborted || errorIsFailure(cause)) throw cause\n\n\t\t\t\t\t\t// Open the circuit and use fallback\n\t\t\t\t\t\topenCircuit(cause)\n\t\t\t\t\t\treturn fallback(...args)\n\t\t\t\t\t},\n\t\t\t\t)\n\t\t\t/* v8 ignore next */\n\t\t}\n\n\t\t// exhaustive check\n\t\t/* v8 ignore next */\n\t\treturn assertNever(state)\n\t}\n\n\treturn Object.assign((...args: Args) => execute(args), {\n\t\tdispose: (disposeMessage = \"ERR_CIRCUIT_BREAKER_DISPOSED\") => {\n\t\t\tconst reason = new ReferenceError(disposeMessage)\n\t\t\tmain[disposeKey]?.(disposeMessage)\n\t\t\tclearFailure()\n\t\t\tclearTimeout(resetTimer)\n\t\t\thistory.forEach((entry) => clearTimeout(entry.timer))\n\t\t\thistory.clear()\n\t\t\tfallback = () => Promise.reject(reason)\n\t\t\tstate = \"open\"\n\t\t\tcontroller.abort(reason)\n\t\t},\n\t\tgetLatestError: () => failureCause,\n\t\tgetState: () => state,\n\t})\n}\n"],"names":[],"mappings":";;AACO,SAAS,MAAM,CAAC,KAAK,EAAE,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KAAK,CAAC;AAC5B,UAAU,OAAO,QAAQ,CAAC,GAAG,IAAI,CAAC;AAClC,QAAQ;AACR,OAAO;AACP,IAAI;AACJ,IAAI,OAAO,WAAW,CAAC,KAAK,CAAC;AAC7B,EAAE;AACF,EAAE,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,IAAI,KAAK,OAAO,CAAC,IAAI,CAAC,EAAE;AACnD,IAAI,OAAO,EAAE,CAAC,cAAc,GAAG,8BAA8B,KAAK;AAClE,MAAM,MAAM,MAAM,GAAG,IAAI,cAAc,CAAC,cAAc,CAAC;AACvD,MAAM,IAAI,CAAC,UAAU,CAAC,GAAG,cAAc,CAAC;AACxC,MAAM,YAAY,EAAE;AACpB,MAAM,YAAY,CAAC,UAAU,CAAC;AAC9B,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,KAAK,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AAC3D,MAAM,OAAO,CAAC,KAAK,EAAE;AACrB,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC;AAC7C,MAAM,KAAK,GAAG,MAAM;AACpB,MAAM,UAAU,CAAC,KAAK,CAAC,MAAM,CAAC;AAC9B,IAAI,CAAC;AACL,IAAI,cAAc,EAAE,MAAM,YAAY;AACtC,IAAI,QAAQ,EAAE,MAAM;AACpB,GAAG,CAAC;AACJ;;;;;;;;;"}
package/dist/index.d.cts CHANGED
@@ -4,6 +4,7 @@ type AnyFn = (...args: any[]) => any;
4
4
  */
5
5
  declare const delayMs: (ms: number) => Promise<void>;
6
6
 
7
+ declare const disposeKey: unique symbol;
7
8
  type CircuitState = "closed" | "halfOpen" | "open";
8
9
  interface CircuitBreakerOptions<Fallback extends AnyFn = AnyFn> {
9
10
  /**
@@ -63,34 +64,6 @@ interface CircuitBreakerOptions<Fallback extends AnyFn = AnyFn> {
63
64
  * @default 30_000 // 30 seconds
64
65
  */
65
66
  resetAfter?: number;
66
- /**
67
- * Provide a function which returns a promise that resolves when the next
68
- * retry attempt should be made. Use this to implement custom retry logic,
69
- * such as exponential backoff.
70
- *
71
- * Note that `attempt` always starts at 2, since first attempts are always
72
- * made as soon as they come in.
73
- *
74
- * @default none // Retry errors immediately
75
- * @example
76
- * ```ts
77
- * // Constant delay of 1 second for all retries
78
- * const breaker = createCircuitBreaker(main, {
79
- * retryDelay: () => delayMs(1_000),
80
- * })
81
- *
82
- * // Double the previous delay each time, up to 30 seconds
83
- * const breaker = createCircuitBreaker(main, {
84
- * retryDelay: useExponentialBackoff(30),
85
- * })
86
- *
87
- * // Use Fibonacci sequence for delay, up to 90 seconds
88
- * const breaker = createCircuitBreaker(main, {
89
- * retryDelay: useFibonacciBackoff(90),
90
- * })
91
- * ```
92
- */
93
- retryDelay?: (attempt: number, signal: AbortSignal) => Promise<void>;
94
67
  }
95
68
  interface CircuitBreakerProtectedFn<Ret = unknown, Args extends unknown[] = never[]> {
96
69
  (...args: Args): Promise<Ret>;
@@ -106,7 +79,10 @@ interface CircuitBreakerProtectedFn<Ret = unknown, Args extends unknown[] = neve
106
79
  /** Get the current state of the circuit breaker */
107
80
  getState(): CircuitState;
108
81
  }
109
- type MainFn<Ret = unknown, Args extends unknown[] = never[]> = (...args: Args) => Promise<Ret>;
82
+ interface MainFn<Ret = unknown, Args extends unknown[] = never[]> {
83
+ (...args: Args): Promise<Ret>;
84
+ [disposeKey]?: (disposeMessage?: string) => void;
85
+ }
110
86
 
111
87
  /**
112
88
  * Returns a function which implements exponential backoff.
@@ -124,6 +100,49 @@ declare function useExponentialBackoff(maxSeconds: number): (attempt: number) =>
124
100
  * that resolves after the calculated delay.
125
101
  */
126
102
  declare function useFibonacciBackoff(maxSeconds: number): (attempt: number) => Promise<void>;
103
+ interface RetryOptions {
104
+ /**
105
+ * Whether an error should be treated as non-retryable. When this returns
106
+ * true, the error will be thrown immediately without retrying.
107
+ *
108
+ * @default () => false // All errors are retried
109
+ */
110
+ shouldRetry?: (error: unknown, attempt: number) => boolean;
111
+ /**
112
+ * Maximum number of retries
113
+ *
114
+ * @default 3
115
+ */
116
+ maxAttempts?: number;
117
+ /**
118
+ * Function that returns a promise resolving when the next retry should occur.
119
+ * Receives the attempt number (starting at 2) and an abort signal.
120
+ *
121
+ * @default () => Promise.resolve() // Immediate retry
122
+ */
123
+ retryDelay?: (attempt: number, signal: AbortSignal) => Promise<void>;
124
+ }
125
+ /**
126
+ * Wrap a function with retry logic. Errors will be retried according to the
127
+ * provided options.
128
+ *
129
+ * @example
130
+ * ```ts
131
+ * // Compose with circuit breaker. Retry up to 3 times with no delay
132
+ * const protectedA = createCircuitBreaker(
133
+ * withRetry(unreliableApiCall, { maxAttempts: 3 })
134
+ * )
135
+ *
136
+ * // Retry up to 5 times with exponential backoff
137
+ * const protectedB = createCircuitBreaker(
138
+ * withRetry(unreliableApiCall, {
139
+ * maxAttempts: 5,
140
+ * retryDelay: useExponentialBackoff(30),
141
+ * })
142
+ * )
143
+ * ```
144
+ */
145
+ declare function withRetry<Ret, Args extends unknown[]>(main: MainFn<Ret, Args>, options?: RetryOptions): MainFn<Ret, Args>;
127
146
  /**
128
147
  * Wrap a function with a timeout. If execution of `main` exceeds `timeoutMs`,
129
148
  * then the call is rejected with `new Error(timeoutMessage)`.
@@ -132,4 +151,5 @@ declare function withTimeout<Ret, Args extends unknown[]>(main: MainFn<Ret, Args
132
151
 
133
152
  declare function createCircuitBreaker<Ret, Args extends unknown[], Fallback extends AnyFn = MainFn<Ret, Args>>(main: MainFn<Ret, Args>, options?: CircuitBreakerOptions<Fallback>): CircuitBreakerProtectedFn<Ret, Args>;
134
153
 
135
- export { createCircuitBreaker, delayMs, useExponentialBackoff, useFibonacciBackoff, withTimeout };
154
+ export { createCircuitBreaker, delayMs, useExponentialBackoff, useFibonacciBackoff, withRetry, withTimeout };
155
+ export type { RetryOptions };
package/dist/index.d.mts CHANGED
@@ -4,6 +4,7 @@ type AnyFn = (...args: any[]) => any;
4
4
  */
5
5
  declare const delayMs: (ms: number) => Promise<void>;
6
6
 
7
+ declare const disposeKey: unique symbol;
7
8
  type CircuitState = "closed" | "halfOpen" | "open";
8
9
  interface CircuitBreakerOptions<Fallback extends AnyFn = AnyFn> {
9
10
  /**
@@ -63,34 +64,6 @@ interface CircuitBreakerOptions<Fallback extends AnyFn = AnyFn> {
63
64
  * @default 30_000 // 30 seconds
64
65
  */
65
66
  resetAfter?: number;
66
- /**
67
- * Provide a function which returns a promise that resolves when the next
68
- * retry attempt should be made. Use this to implement custom retry logic,
69
- * such as exponential backoff.
70
- *
71
- * Note that `attempt` always starts at 2, since first attempts are always
72
- * made as soon as they come in.
73
- *
74
- * @default none // Retry errors immediately
75
- * @example
76
- * ```ts
77
- * // Constant delay of 1 second for all retries
78
- * const breaker = createCircuitBreaker(main, {
79
- * retryDelay: () => delayMs(1_000),
80
- * })
81
- *
82
- * // Double the previous delay each time, up to 30 seconds
83
- * const breaker = createCircuitBreaker(main, {
84
- * retryDelay: useExponentialBackoff(30),
85
- * })
86
- *
87
- * // Use Fibonacci sequence for delay, up to 90 seconds
88
- * const breaker = createCircuitBreaker(main, {
89
- * retryDelay: useFibonacciBackoff(90),
90
- * })
91
- * ```
92
- */
93
- retryDelay?: (attempt: number, signal: AbortSignal) => Promise<void>;
94
67
  }
95
68
  interface CircuitBreakerProtectedFn<Ret = unknown, Args extends unknown[] = never[]> {
96
69
  (...args: Args): Promise<Ret>;
@@ -106,7 +79,10 @@ interface CircuitBreakerProtectedFn<Ret = unknown, Args extends unknown[] = neve
106
79
  /** Get the current state of the circuit breaker */
107
80
  getState(): CircuitState;
108
81
  }
109
- type MainFn<Ret = unknown, Args extends unknown[] = never[]> = (...args: Args) => Promise<Ret>;
82
+ interface MainFn<Ret = unknown, Args extends unknown[] = never[]> {
83
+ (...args: Args): Promise<Ret>;
84
+ [disposeKey]?: (disposeMessage?: string) => void;
85
+ }
110
86
 
111
87
  /**
112
88
  * Returns a function which implements exponential backoff.
@@ -124,6 +100,49 @@ declare function useExponentialBackoff(maxSeconds: number): (attempt: number) =>
124
100
  * that resolves after the calculated delay.
125
101
  */
126
102
  declare function useFibonacciBackoff(maxSeconds: number): (attempt: number) => Promise<void>;
103
+ interface RetryOptions {
104
+ /**
105
+ * Whether an error should be treated as non-retryable. When this returns
106
+ * true, the error will be thrown immediately without retrying.
107
+ *
108
+ * @default () => false // All errors are retried
109
+ */
110
+ shouldRetry?: (error: unknown, attempt: number) => boolean;
111
+ /**
112
+ * Maximum number of retries
113
+ *
114
+ * @default 3
115
+ */
116
+ maxAttempts?: number;
117
+ /**
118
+ * Function that returns a promise resolving when the next retry should occur.
119
+ * Receives the attempt number (starting at 2) and an abort signal.
120
+ *
121
+ * @default () => Promise.resolve() // Immediate retry
122
+ */
123
+ retryDelay?: (attempt: number, signal: AbortSignal) => Promise<void>;
124
+ }
125
+ /**
126
+ * Wrap a function with retry logic. Errors will be retried according to the
127
+ * provided options.
128
+ *
129
+ * @example
130
+ * ```ts
131
+ * // Compose with circuit breaker. Retry up to 3 times with no delay
132
+ * const protectedA = createCircuitBreaker(
133
+ * withRetry(unreliableApiCall, { maxAttempts: 3 })
134
+ * )
135
+ *
136
+ * // Retry up to 5 times with exponential backoff
137
+ * const protectedB = createCircuitBreaker(
138
+ * withRetry(unreliableApiCall, {
139
+ * maxAttempts: 5,
140
+ * retryDelay: useExponentialBackoff(30),
141
+ * })
142
+ * )
143
+ * ```
144
+ */
145
+ declare function withRetry<Ret, Args extends unknown[]>(main: MainFn<Ret, Args>, options?: RetryOptions): MainFn<Ret, Args>;
127
146
  /**
128
147
  * Wrap a function with a timeout. If execution of `main` exceeds `timeoutMs`,
129
148
  * then the call is rejected with `new Error(timeoutMessage)`.
@@ -132,4 +151,5 @@ declare function withTimeout<Ret, Args extends unknown[]>(main: MainFn<Ret, Args
132
151
 
133
152
  declare function createCircuitBreaker<Ret, Args extends unknown[], Fallback extends AnyFn = MainFn<Ret, Args>>(main: MainFn<Ret, Args>, options?: CircuitBreakerOptions<Fallback>): CircuitBreakerProtectedFn<Ret, Args>;
134
153
 
135
- export { createCircuitBreaker, delayMs, useExponentialBackoff, useFibonacciBackoff, withTimeout };
154
+ export { createCircuitBreaker, delayMs, useExponentialBackoff, useFibonacciBackoff, withRetry, withTimeout };
155
+ export type { RetryOptions };
package/dist/index.mjs CHANGED
@@ -8,12 +8,12 @@ const delayMs = (ms) => new Promise((next) => setTimeout(next, ms));
8
8
  const rejectOnAbort = (signal, pending) => {
9
9
  let teardown;
10
10
  return Promise.race([
11
- Promise.resolve(pending).finally(
12
- () => signal.removeEventListener("abort", teardown)
13
- ),
11
+ Promise.resolve(pending).finally(() => {
12
+ signal.removeEventListener("abort", teardown);
13
+ }),
14
14
  new Promise((_, reject) => {
15
15
  teardown = () => reject(signal.reason);
16
- signal.addEventListener("abort", teardown);
16
+ signal.addEventListener("abort", teardown, { once: true });
17
17
  })
18
18
  ]);
19
19
  };
@@ -27,8 +27,7 @@ function parseOptions(options) {
27
27
  onClose,
28
28
  onHalfOpen,
29
29
  onOpen,
30
- resetAfter = 3e4,
31
- retryDelay = () => void 0
30
+ resetAfter = 3e4
32
31
  } = options;
33
32
  assert(
34
33
  typeof errorIsFailure === "function",
@@ -66,10 +65,6 @@ function parseOptions(options) {
66
65
  resetAfter >= errorWindow,
67
66
  `"resetAfter" must be greater than or equal to "errorWindow" (received ${resetAfter}, expected >= ${errorWindow})`
68
67
  );
69
- assert(
70
- typeof retryDelay === "function",
71
- `"retryDelay" must be a function (received ${typeof retryDelay})`
72
- );
73
68
  return {
74
69
  errorIsFailure,
75
70
  errorThreshold,
@@ -78,11 +73,12 @@ function parseOptions(options) {
78
73
  onClose,
79
74
  onHalfOpen,
80
75
  onOpen,
81
- resetAfter,
82
- retryDelay
76
+ resetAfter
83
77
  };
84
78
  }
85
79
 
80
+ const disposeKey = Symbol("disposeKey");
81
+
86
82
  function useExponentialBackoff(maxSeconds) {
87
83
  return function exponentialBackoff(attempt) {
88
84
  const num = Math.max(attempt - 2, 0);
@@ -98,17 +94,70 @@ function useFibonacciBackoff(maxSeconds) {
98
94
  return delayMs(delay * 1e3);
99
95
  };
100
96
  }
97
+ function withRetry(main, options = {}) {
98
+ const {
99
+ shouldRetry = () => true,
100
+ maxAttempts = 3,
101
+ retryDelay = () => Promise.resolve()
102
+ } = options;
103
+ assert(maxAttempts >= 1, "maxAttempts must be a number greater than 0");
104
+ const controller = new AbortController();
105
+ const { signal } = controller;
106
+ async function execute(args, attempt = 1) {
107
+ try {
108
+ return await main(...args);
109
+ } catch (cause) {
110
+ if (!shouldRetry(cause, attempt)) throw cause;
111
+ if (attempt >= maxAttempts)
112
+ throw new Error(`ERR_CIRCUIT_BREAKER_MAX_ATTEMPTS (${maxAttempts})`, {
113
+ cause
114
+ });
115
+ await rejectOnAbort(signal, retryDelay(attempt + 1, signal));
116
+ return execute(args, attempt + 1);
117
+ }
118
+ }
119
+ return Object.assign(
120
+ function withRetryFunction(...args) {
121
+ return execute(args);
122
+ },
123
+ {
124
+ [disposeKey]: (disposeMessage = "ERR_CIRCUIT_BREAKER_DISPOSED") => {
125
+ const reason = new ReferenceError(disposeMessage);
126
+ main[disposeKey]?.(disposeMessage);
127
+ controller.abort(reason);
128
+ }
129
+ }
130
+ );
131
+ }
101
132
  function withTimeout(main, timeoutMs, timeoutMessage = "ERR_CIRCUIT_BREAKER_TIMEOUT") {
102
133
  const error = new Error(timeoutMessage);
103
- return function withTimeoutFunction(...args) {
134
+ const controller = new AbortController();
135
+ const { signal } = controller;
136
+ function withTimeoutFunction(...args) {
137
+ let teardown;
104
138
  let timer;
105
139
  return Promise.race([
106
- main(...args).finally(() => clearTimeout(timer)),
140
+ main(...args).finally(() => {
141
+ clearTimeout(timer);
142
+ signal.removeEventListener("abort", teardown);
143
+ }),
107
144
  new Promise((_, reject) => {
145
+ teardown = () => {
146
+ clearTimeout(timer);
147
+ reject(signal.reason);
148
+ };
108
149
  timer = setTimeout(reject, timeoutMs, error);
150
+ signal.addEventListener("abort", teardown, { once: true });
109
151
  })
110
152
  ]);
111
- };
153
+ }
154
+ return Object.assign(withTimeoutFunction, {
155
+ [disposeKey]: (disposeMessage = "ERR_CIRCUIT_BREAKER_DISPOSED") => {
156
+ const reason = new ReferenceError(disposeMessage);
157
+ main[disposeKey]?.(disposeMessage);
158
+ controller.abort(reason);
159
+ }
160
+ });
112
161
  }
113
162
 
114
163
  function createCircuitBreaker(main, options = {}) {
@@ -120,8 +169,7 @@ function createCircuitBreaker(main, options = {}) {
120
169
  onClose,
121
170
  onHalfOpen,
122
171
  onOpen,
123
- resetAfter,
124
- retryDelay
172
+ resetAfter
125
173
  } = parseOptions(options);
126
174
  const controller = new AbortController();
127
175
  const history = /* @__PURE__ */ new Map();
@@ -167,7 +215,7 @@ function createCircuitBreaker(main, options = {}) {
167
215
  history.delete(pending);
168
216
  signal.removeEventListener("abort", teardown);
169
217
  };
170
- signal.addEventListener("abort", teardown);
218
+ signal.addEventListener("abort", teardown, { once: true });
171
219
  const settle = (value) => {
172
220
  if (signal.aborted) return;
173
221
  entry.status = value;
@@ -176,7 +224,7 @@ function createCircuitBreaker(main, options = {}) {
176
224
  history.set(pending, entry);
177
225
  return { pending, settle, teardown };
178
226
  }
179
- function execute(attempt, args) {
227
+ function execute(args) {
180
228
  if (state === "closed") {
181
229
  const { pending, settle, teardown } = createHistoryItem(main(...args));
182
230
  return pending.then(
@@ -184,16 +232,14 @@ function createCircuitBreaker(main, options = {}) {
184
232
  settle("resolved");
185
233
  return result;
186
234
  },
187
- async (cause) => {
235
+ (cause) => {
188
236
  if (signal.aborted || errorIsFailure(cause)) {
189
237
  teardown();
190
238
  throw cause;
191
239
  }
192
240
  settle("rejected");
193
241
  if (failureRate() > errorThreshold) openCircuit(cause);
194
- const next = attempt + 1;
195
- await rejectOnAbort(signal, retryDelay(next, signal));
196
- return execute(next, args);
242
+ return fallback(...args);
197
243
  }
198
244
  );
199
245
  } else if (state === "open" || halfOpenPending) {
@@ -205,20 +251,19 @@ function createCircuitBreaker(main, options = {}) {
205
251
  closeCircuit();
206
252
  return result;
207
253
  },
208
- async (cause) => {
254
+ (cause) => {
209
255
  if (signal.aborted || errorIsFailure(cause)) throw cause;
210
256
  openCircuit(cause);
211
- const next = attempt + 1;
212
- await rejectOnAbort(signal, retryDelay(next, signal));
213
- return execute(next, args);
257
+ return fallback(...args);
214
258
  }
215
259
  );
216
260
  }
217
261
  return assertNever(state);
218
262
  }
219
- return Object.assign((...args) => execute(1, args), {
263
+ return Object.assign((...args) => execute(args), {
220
264
  dispose: (disposeMessage = "ERR_CIRCUIT_BREAKER_DISPOSED") => {
221
265
  const reason = new ReferenceError(disposeMessage);
266
+ main[disposeKey]?.(disposeMessage);
222
267
  clearFailure();
223
268
  clearTimeout(resetTimer);
224
269
  history.forEach((entry) => clearTimeout(entry.timer));
@@ -232,5 +277,5 @@ function createCircuitBreaker(main, options = {}) {
232
277
  });
233
278
  }
234
279
 
235
- export { createCircuitBreaker, delayMs, useExponentialBackoff, useFibonacciBackoff, withTimeout };
280
+ export { createCircuitBreaker, delayMs, useExponentialBackoff, useFibonacciBackoff, withRetry, withTimeout };
236
281
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../lib/util.ts","../lib/options.ts","../lib/helpers.ts","../lib/index.ts"],"sourcesContent":["export type AnyFn = (...args: any[]) => any\n\n/**\n * Asserts that the given value is truthy. If not, throws a `TypeError`.\n */\nexport function assert(value: unknown, message?: string): asserts value {\n\tif (!value) throw new TypeError(message)\n}\n\n/**\n * `[TypeScript]` For exhaustive checks in switch statements or if/else. Add\n * this check to `default` case or final `else` to ensure all possible values\n * have been handled. If a new value is added to the type, TypeScript will\n * throw an error and the editor will underline the `value`.\n */\n/* v8 ignore next 3 */\nexport const assertNever = (val: never, msg = \"Unexpected value\") => {\n\tthrow new TypeError(`${msg}: ${val}`)\n}\n\n/**\n * Returns a promise that resolves after the specified number of milliseconds.\n */\nexport const delayMs = (ms: number): Promise<void> =>\n\tnew Promise((next) => setTimeout(next, ms))\n\n/**\n * Rejects the given promise when the abort signal is triggered.\n */\nexport const rejectOnAbort = <T extends Promise<unknown> | undefined>(\n\tsignal: AbortSignal,\n\tpending: T,\n): Promise<Awaited<T>> => {\n\tlet teardown: () => void\n\treturn Promise.race([\n\t\tPromise.resolve(pending).finally(() =>\n\t\t\tsignal.removeEventListener(\"abort\", teardown),\n\t\t),\n\t\tnew Promise<never>((_, reject) => {\n\t\t\tteardown = () => reject(signal.reason)\n\t\t\tsignal.addEventListener(\"abort\", teardown)\n\t\t}),\n\t])\n}\n","import type { CircuitBreakerOptions } from \"./types.js\"\nimport { assert, type AnyFn } from \"./util.js\"\n\nexport function parseOptions<Fallback extends AnyFn>(\n\toptions: CircuitBreakerOptions<Fallback>,\n) {\n\tconst {\n\t\terrorIsFailure = () => false,\n\t\terrorThreshold = 0,\n\t\terrorWindow = 10_000,\n\t\tminimumCandidates = 6,\n\t\tonClose,\n\t\tonHalfOpen,\n\t\tonOpen,\n\t\tresetAfter = 30_000,\n\t\tretryDelay = () => undefined,\n\t} = options\n\n\t// errorIsFailure\n\tassert(\n\t\ttypeof errorIsFailure === \"function\",\n\t\t`\"errorIsFailure\" must be a function (received ${typeof errorIsFailure})`,\n\t)\n\n\t// errorThreshold\n\tassert(\n\t\terrorThreshold >= 0 && errorThreshold <= 1,\n\t\t`\"errorThreshold\" must be between 0 and 1 (received ${errorThreshold})`,\n\t)\n\n\t// errorWindow\n\tassert(\n\t\terrorWindow >= 1_000,\n\t\t`\"errorWindow\" must be milliseconds of at least 1 second (received ${errorWindow})`,\n\t)\n\n\t// minimumCandidates\n\tassert(\n\t\tminimumCandidates >= 1,\n\t\t`\"minimumCandidates\" must be greater than 0 (received ${minimumCandidates})`,\n\t)\n\n\t// (optional) onClose\n\tassert(\n\t\t!onClose || typeof onClose === \"function\",\n\t\t`\"onClose\" must be a function (received ${typeof onClose})`,\n\t)\n\n\t// (optional) onHalfOpen\n\tassert(\n\t\t!onHalfOpen || typeof onHalfOpen === \"function\",\n\t\t`\"onHalfOpen\" must be a function (received ${typeof onHalfOpen})`,\n\t)\n\n\t// (optional) onOpen\n\tassert(\n\t\t!onOpen || typeof onOpen === \"function\",\n\t\t`\"onOpen\" must be a function (received ${typeof onOpen})`,\n\t)\n\n\t// resetAfter\n\tassert(\n\t\tresetAfter >= 1_000,\n\t\t`\"resetAfter\" must be milliseconds of at least 1 second (received ${resetAfter})`,\n\t)\n\tassert(\n\t\tresetAfter >= errorWindow,\n\t\t`\"resetAfter\" must be greater than or equal to \"errorWindow\" (received ${resetAfter}, expected >= ${errorWindow})`,\n\t)\n\n\t// retryDelay\n\tassert(\n\t\ttypeof retryDelay === \"function\",\n\t\t`\"retryDelay\" must be a function (received ${typeof retryDelay})`,\n\t)\n\n\treturn {\n\t\terrorIsFailure,\n\t\terrorThreshold,\n\t\terrorWindow,\n\t\tminimumCandidates,\n\t\tonClose,\n\t\tonHalfOpen,\n\t\tonOpen,\n\t\tresetAfter,\n\t\tretryDelay,\n\t}\n}\n","import type { MainFn } from \"./types.js\"\nimport { delayMs } from \"./util.js\"\n\n/**\n * Returns a function which implements exponential backoff.\n *\n * @param maxSeconds - The maximum number of seconds to wait before retrying.\n * @returns A function which takes an `attempt` number and returns a promise\n * that resolves after the calculated delay.\n */\nexport function useExponentialBackoff(maxSeconds: number) {\n\treturn function exponentialBackoff(attempt: number) {\n\t\tconst num = Math.max(attempt - 2, 0)\n\t\tconst delay = Math.min(2 ** num, maxSeconds)\n\t\treturn delayMs(delay * 1_000)\n\t}\n}\n\nconst sqrt5 = /* @__PURE__ */ Math.sqrt(5)\n/**\n * @see https://en.wikipedia.org/wiki/Fibonacci_sequence#Closed-form_expression\n */\nconst binet = (n: number) =>\n\tMath.round(((1 + sqrt5) ** n - (1 - sqrt5) ** n) / (2 ** n * sqrt5))\n\n/**\n * Returns a function which implements Fibonacci backoff.\n *\n * @param maxSeconds - The maximum number of seconds to wait before retrying.\n * @returns A function which takes an `attempt` number and returns a promise\n * that resolves after the calculated delay.\n */\nexport function useFibonacciBackoff(maxSeconds: number) {\n\treturn function fibonacciBackoff(attempt: number) {\n\t\tconst delay = Math.min(binet(attempt), maxSeconds)\n\t\treturn delayMs(delay * 1_000)\n\t}\n}\n\n/**\n * Wrap a function with a timeout. If execution of `main` exceeds `timeoutMs`,\n * then the call is rejected with `new Error(timeoutMessage)`.\n */\nexport function withTimeout<Ret, Args extends unknown[]>(\n\tmain: MainFn<Ret, Args>,\n\ttimeoutMs: number,\n\ttimeoutMessage = \"ERR_CIRCUIT_BREAKER_TIMEOUT\",\n): MainFn<Ret, Args> {\n\tconst error = new Error(timeoutMessage)\n\n\treturn function withTimeoutFunction(...args) {\n\t\tlet timer: NodeJS.Timeout\n\t\treturn Promise.race([\n\t\t\tmain(...args).finally(() => clearTimeout(timer)),\n\t\t\tnew Promise<never>((_, reject) => {\n\t\t\t\ttimer = setTimeout(reject, timeoutMs, error)\n\t\t\t}),\n\t\t])\n\t}\n}\n","import { parseOptions } from \"./options.js\"\nimport type {\n\tHistoryEntry,\n\tHistoryMap,\n\tCircuitBreakerOptions,\n\tCircuitBreakerProtectedFn,\n\tCircuitState,\n\tMainFn,\n} from \"./types.js\"\nimport { assertNever, rejectOnAbort, type AnyFn } from \"./util.js\"\n\nexport * from \"./helpers.js\"\nexport { delayMs } from \"./util.js\"\n\nexport function createCircuitBreaker<\n\tRet,\n\tArgs extends unknown[],\n\tFallback extends AnyFn = MainFn<Ret, Args>,\n>(\n\tmain: MainFn<Ret, Args>,\n\toptions: CircuitBreakerOptions<Fallback> = {},\n): CircuitBreakerProtectedFn<Ret, Args> {\n\tconst {\n\t\terrorIsFailure,\n\t\terrorThreshold,\n\t\terrorWindow,\n\t\tminimumCandidates,\n\t\tonClose,\n\t\tonHalfOpen,\n\t\tonOpen,\n\t\tresetAfter,\n\t\tretryDelay,\n\t} = parseOptions(options)\n\tconst controller = new AbortController()\n\tconst history: HistoryMap = new Map()\n\tconst signal = controller.signal\n\tlet failureCause: unknown\n\tlet fallback = options.fallback || (() => Promise.reject(failureCause))\n\tlet halfOpenPending: Promise<unknown> | undefined\n\tlet resetTimer: NodeJS.Timeout\n\tlet state: CircuitState = \"closed\"\n\n\tfunction clearFailure() {\n\t\tfailureCause = undefined\n\t}\n\n\tfunction closeCircuit() {\n\t\tstate = \"closed\"\n\t\tclearFailure()\n\t\tclearTimeout(resetTimer)\n\t\tonClose?.()\n\t}\n\n\tfunction failureRate() {\n\t\tlet failures = 0\n\t\tlet total = 0\n\t\tfor (const { status } of history.values()) {\n\t\t\tif (status === \"rejected\") failures++\n\t\t\tif (status !== \"pending\") total++\n\t\t}\n\t\t// Don't calculate anything until we have enough data\n\t\tif (!total || total < minimumCandidates) return 0\n\t\treturn failures / total\n\t}\n\n\t/**\n\t * Break the circuit and wait for a reset\n\t */\n\tfunction openCircuit(cause: unknown) {\n\t\tfailureCause = cause\n\t\tstate = \"open\"\n\t\tclearTimeout(resetTimer)\n\t\tresetTimer = setTimeout(() => {\n\t\t\tstate = \"halfOpen\"\n\t\t\tonHalfOpen?.()\n\t\t}, resetAfter)\n\t\tonOpen?.(cause)\n\t}\n\n\tfunction createHistoryItem<T>(pending: Promise<T>) {\n\t\tconst entry: HistoryEntry = { status: \"pending\", timer: undefined }\n\t\tconst teardown = () => {\n\t\t\tclearTimeout(entry.timer)\n\t\t\thistory.delete(pending)\n\t\t\tsignal.removeEventListener(\"abort\", teardown)\n\t\t}\n\t\tsignal.addEventListener(\"abort\", teardown)\n\t\tconst settle = (value: \"resolved\" | \"rejected\") => {\n\t\t\tif (signal.aborted) return\n\t\t\tentry.status = value\n\t\t\t// Remove the entry from history when it falls outside of the error window\n\t\t\tentry.timer = setTimeout(teardown, errorWindow)\n\t\t}\n\t\thistory.set(pending, entry)\n\t\treturn { pending, settle, teardown }\n\t}\n\n\t/**\n\t * Wrap calls to `main` with circuit breaker logic\n\t */\n\tfunction execute(attempt: number, args: Args): Promise<Ret> {\n\t\t// Normal operation when circuit is closed. If an error occurs, keep track\n\t\t// of the failure count and open the circuit if it exceeds the threshold.\n\t\tif (state === \"closed\") {\n\t\t\tconst { pending, settle, teardown } = createHistoryItem(main(...args))\n\t\t\treturn pending.then(\n\t\t\t\t(result) => {\n\t\t\t\t\tsettle(\"resolved\")\n\t\t\t\t\treturn result\n\t\t\t\t},\n\t\t\t\tasync (cause: unknown) => {\n\t\t\t\t\t// Was the circuit disposed, or was this a non-retryable error?\n\t\t\t\t\tif (signal.aborted || errorIsFailure(cause)) {\n\t\t\t\t\t\tteardown()\n\t\t\t\t\t\tthrow cause\n\t\t\t\t\t}\n\n\t\t\t\t\t// Should this failure open the circuit?\n\t\t\t\t\tsettle(\"rejected\")\n\t\t\t\t\tif (failureRate() > errorThreshold) openCircuit(cause)\n\n\t\t\t\t\t// Retry the call after a delay.\n\t\t\t\t\tconst next = attempt + 1\n\t\t\t\t\tawait rejectOnAbort(signal, retryDelay(next, signal))\n\t\t\t\t\treturn execute(next, args)\n\t\t\t\t},\n\t\t\t)\n\t\t}\n\n\t\t// Use the fallback while the circuit is open, or if a half-open trial\n\t\t// attempt was already made.\n\t\telse if (state === \"open\" || halfOpenPending) {\n\t\t\treturn fallback(...args)\n\t\t}\n\n\t\t// If the circuit is half-open, make one attempt. If it succeeds, close\n\t\t// the circuit and resume normal operation. If it fails, re-open the\n\t\t// circuit and run the fallback instead.\n\t\telse if (state === \"halfOpen\") {\n\t\t\treturn (halfOpenPending = main(...args))\n\t\t\t\t.finally(() => (halfOpenPending = undefined))\n\t\t\t\t.then(\n\t\t\t\t\t(result) => {\n\t\t\t\t\t\tif (signal.aborted) return result // disposed\n\t\t\t\t\t\tcloseCircuit()\n\t\t\t\t\t\treturn result\n\t\t\t\t\t},\n\t\t\t\t\tasync (cause: unknown) => {\n\t\t\t\t\t\t// Was the circuit disposed, or was this a non-retryable error?\n\t\t\t\t\t\tif (signal.aborted || errorIsFailure(cause)) throw cause\n\n\t\t\t\t\t\t// Open the circuit and try again later\n\t\t\t\t\t\topenCircuit(cause)\n\n\t\t\t\t\t\t// Retry the call after a delay.\n\t\t\t\t\t\tconst next = attempt + 1\n\t\t\t\t\t\tawait rejectOnAbort(signal, retryDelay(next, signal))\n\t\t\t\t\t\treturn execute(next, args)\n\t\t\t\t\t},\n\t\t\t\t)\n\t\t\t/* v8 ignore next */\n\t\t}\n\n\t\t// exhaustive check\n\t\t/* v8 ignore next */\n\t\treturn assertNever(state)\n\t}\n\n\treturn Object.assign((...args: Args) => execute(1, args), {\n\t\tdispose: (disposeMessage = \"ERR_CIRCUIT_BREAKER_DISPOSED\") => {\n\t\t\tconst reason = new ReferenceError(disposeMessage)\n\t\t\tclearFailure()\n\t\t\tclearTimeout(resetTimer)\n\t\t\thistory.forEach((entry) => clearTimeout(entry.timer))\n\t\t\thistory.clear()\n\t\t\tfallback = () => Promise.reject(reason)\n\t\t\tstate = \"open\"\n\t\t\tcontroller.abort(reason)\n\t\t},\n\t\tgetLatestError: () => failureCause,\n\t\tgetState: () => state,\n\t})\n}\n"],"names":[],"mappings":"AACO,SAAS,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE;AACvC,EAAE,IAAI,CAAC,KAAK,EAAE,MAAM,IAAI,SAAS,CAAC,OAAO,CAAC;AAC1C;AACO,MAAM,WAAW,GAAG,CAAC,GAAG,EAAE,GAAG,GAAG,kBAAkB,KAAK;AAC9D,EAAE,MAAM,IAAI,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC,CAAC;AACvC,CAAC;AACW,MAAC,OAAO,GAAG,CAAC,EAAE,KAAK,IAAI,OAAO,CAAC,CAAC,IAAI,KAAK,UAAU,CAAC,IAAI,EAAE,EAAE,CAAC;AAClE,MAAM,aAAa,GAAG,CAAC,MAAM,EAAE,OAAO,KAAK;AAClD,EAAE,IAAI,QAAQ;AACd,EAAE,OAAO,OAAO,CAAC,IAAI,CAAC;AACtB,IAAI,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,OAAO;AACpC,MA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UAAU,MAAM,aAAa,CAAC,MAAM,EAAE,UAAU,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;AAC/D,UAAU,OAAO,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC;AACpC,QAAQ;AACR,OAAO;AACP,IAAI;AACJ,IAAI,OAAO,WAAW,CAAC,KAAK,CAAC;AAC7B,EAAE;AACF,EAAE,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,IAAI,KAAK,OAAO,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE;AACtD,IAAI,OAAO,EAAE,CAAC,cAAc,GAAG,8BAA8B,KAAK;AAClE,MAAM,MAAM,MAAM,GAAG,IAAI,cAAc,CAAC,cAAc,CAAC;AACvD,MAAM,YAAY,EAAE;AACpB,MAAM,YAAY,CAAC,UAAU,CAAC;AAC9B,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,KAAK,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AAC3D,MAAM,OAAO,CAAC,KAAK,EAAE;AACrB,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC;AAC7C,MAAM,KAAK,GAAG,MAAM;AACpB,MAAM,UAAU,CAAC,KAAK,CAAC,MAAM,CAAC;AAC9B,IAAI,CAAC;AACL,IAAI,cAAc,EAAE,MAAM,YAAY;AACtC,IAAI,QAAQ,EAAE,MAAM;AACpB,GAAG,CAAC;AACJ;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":["../lib/util.ts","../lib/options.ts","../lib/types.ts","../lib/helpers.ts","../lib/index.ts"],"sourcesContent":["export type AnyFn = (...args: any[]) => any\n\n/**\n * Asserts that the given value is truthy. If not, throws a `TypeError`.\n */\nexport function assert(value: unknown, message?: string): asserts value {\n\tif (!value) throw new TypeError(message)\n}\n\n/**\n * `[TypeScript]` For exhaustive checks in switch statements or if/else. Add\n * this check to `default` case or final `else` to ensure all possible values\n * have been handled. If a new value is added to the type, TypeScript will\n * throw an error and the editor will underline the `value`.\n */\n/* v8 ignore next 3 */\nexport const assertNever = (val: never, msg = \"Unexpected value\") => {\n\tthrow new TypeError(`${msg}: ${val}`)\n}\n\n/**\n * Returns a promise that resolves after the specified number of milliseconds.\n */\nexport const delayMs = (ms: number): Promise<void> =>\n\tnew Promise((next) => setTimeout(next, ms))\n\n/**\n * Rejects the given promise when the abort signal is triggered.\n */\nexport const rejectOnAbort = <T extends Promise<unknown> | undefined>(\n\tsignal: AbortSignal,\n\tpending: T,\n): Promise<Awaited<T>> => {\n\tlet teardown: () => void\n\treturn Promise.race([\n\t\tPromise.resolve(pending).finally(() => {\n\t\t\tsignal.removeEventListener(\"abort\", teardown)\n\t\t}),\n\t\tnew Promise<never>((_, reject) => {\n\t\t\tteardown = () => reject(signal.reason)\n\t\t\tsignal.addEventListener(\"abort\", teardown, { once: true })\n\t\t}),\n\t])\n}\n","import type { CircuitBreakerOptions } from \"./types.js\"\nimport { assert, type AnyFn } from \"./util.js\"\n\nexport function parseOptions<Fallback extends AnyFn>(\n\toptions: CircuitBreakerOptions<Fallback>,\n) {\n\tconst {\n\t\terrorIsFailure = () => false,\n\t\terrorThreshold = 0,\n\t\terrorWindow = 10_000,\n\t\tminimumCandidates = 6,\n\t\tonClose,\n\t\tonHalfOpen,\n\t\tonOpen,\n\t\tresetAfter = 30_000,\n\t} = options\n\n\t// errorIsFailure\n\tassert(\n\t\ttypeof errorIsFailure === \"function\",\n\t\t`\"errorIsFailure\" must be a function (received ${typeof errorIsFailure})`,\n\t)\n\n\t// errorThreshold\n\tassert(\n\t\terrorThreshold >= 0 && errorThreshold <= 1,\n\t\t`\"errorThreshold\" must be between 0 and 1 (received ${errorThreshold})`,\n\t)\n\n\t// errorWindow\n\tassert(\n\t\terrorWindow >= 1_000,\n\t\t`\"errorWindow\" must be milliseconds of at least 1 second (received ${errorWindow})`,\n\t)\n\n\t// minimumCandidates\n\tassert(\n\t\tminimumCandidates >= 1,\n\t\t`\"minimumCandidates\" must be greater than 0 (received ${minimumCandidates})`,\n\t)\n\n\t// (optional) onClose\n\tassert(\n\t\t!onClose || typeof onClose === \"function\",\n\t\t`\"onClose\" must be a function (received ${typeof onClose})`,\n\t)\n\n\t// (optional) onHalfOpen\n\tassert(\n\t\t!onHalfOpen || typeof onHalfOpen === \"function\",\n\t\t`\"onHalfOpen\" must be a function (received ${typeof onHalfOpen})`,\n\t)\n\n\t// (optional) onOpen\n\tassert(\n\t\t!onOpen || typeof onOpen === \"function\",\n\t\t`\"onOpen\" must be a function (received ${typeof onOpen})`,\n\t)\n\n\t// resetAfter\n\tassert(\n\t\tresetAfter >= 1_000,\n\t\t`\"resetAfter\" must be milliseconds of at least 1 second (received ${resetAfter})`,\n\t)\n\tassert(\n\t\tresetAfter >= errorWindow,\n\t\t`\"resetAfter\" must be greater than or equal to \"errorWindow\" (received ${resetAfter}, expected >= ${errorWindow})`,\n\t)\n\n\treturn {\n\t\terrorIsFailure,\n\t\terrorThreshold,\n\t\terrorWindow,\n\t\tminimumCandidates,\n\t\tonClose,\n\t\tonHalfOpen,\n\t\tonOpen,\n\t\tresetAfter,\n\t}\n}\n","import type { AnyFn } from \"./util\"\n\nexport const disposeKey = Symbol(\"disposeKey\")\n\nexport type CircuitState = \"closed\" | \"halfOpen\" | \"open\"\n\nexport interface CircuitBreakerOptions<Fallback extends AnyFn = AnyFn> {\n\t/**\n\t * Whether an error should be treated as non-retryable failure. When used and\n\t * when an error is considered a failure, the error will be thrown to the\n\t * caller.\n\t *\n\t * @default () => false // Errors are retryable by default\n\t */\n\terrorIsFailure?: (error: unknown) => boolean\n\n\t/**\n\t * The percentage of errors (as a number between 0 and 1) which must occur\n\t * within the error window before the circuit breaker opens.\n\t *\n\t * @default 0 // Any error opens the circuit\n\t */\n\terrorThreshold?: number\n\n\t/**\n\t * The sliding window of time in milliseconds over which errors are counted.\n\t *\n\t * @default 10_000 // 10 seconds\n\t */\n\terrorWindow?: number\n\n\t/**\n\t * If provided, then all rejected calls to `main` will be forwarded to\n\t * this function instead.\n\t *\n\t * @default undefined // No fallback, errors are propagated\n\t */\n\tfallback?: Fallback\n\n\t/**\n\t * The minimum number of calls that must be made before calculating the\n\t * error rate and determining whether the circuit breaker should open based on\n\t * the `errorThreshold`.\n\t *\n\t * @default 6\n\t */\n\tminimumCandidates?: number\n\n\t/**\n\t * Provide a function to be called when the circuit breaker is closed.\n\t */\n\tonClose?: () => void\n\n\t/**\n\t * Provide a function to be called when the circuit breaker transitions to\n\t * half-open state.\n\t */\n\tonHalfOpen?: () => void\n\n\t/**\n\t * Provide a function to be called when the circuit breaker is opened. It\n\t * receives the error as its only argument.\n\t */\n\tonOpen?: (cause: unknown) => void\n\n\t/**\n\t * The amount of time in milliseconds to wait before transitioning to a\n\t * half-open state.\n\t *\n\t * @default 30_000 // 30 seconds\n\t */\n\tresetAfter?: number\n}\n\nexport interface CircuitBreakerProtectedFn<\n\tRet = unknown,\n\tArgs extends unknown[] = never[],\n> {\n\t(...args: Args): Promise<Ret>\n\n\t/**\n\t * Free memory and stop timers. All future calls will be rejected with the\n\t * provided message.\n\t *\n\t * @default \"ERR_CIRCUIT_BREAKER_DISPOSED\"\n\t */\n\tdispose(disposeMessage?: string): void\n\n\t/** Get the last error which triggered the circuit breaker */\n\tgetLatestError(): unknown | undefined\n\n\t/** Get the current state of the circuit breaker */\n\tgetState(): CircuitState\n}\n\nexport interface HistoryEntry {\n\ttimer: NodeJS.Timeout | undefined\n\tstatus: \"pending\" | \"resolved\" | \"rejected\"\n}\n\nexport type HistoryMap = Map<Promise<unknown>, HistoryEntry>\n\nexport interface MainFn<Ret = unknown, Args extends unknown[] = never[]> {\n\t(...args: Args): Promise<Ret>\n\n\t[disposeKey]?: (disposeMessage?: string) => void\n}\n","import { disposeKey, type MainFn } from \"./types.js\"\nimport { assert, delayMs, rejectOnAbort } from \"./util.js\"\n\n/**\n * Returns a function which implements exponential backoff.\n *\n * @param maxSeconds - The maximum number of seconds to wait before retrying.\n * @returns A function which takes an `attempt` number and returns a promise\n * that resolves after the calculated delay.\n */\nexport function useExponentialBackoff(maxSeconds: number) {\n\treturn function exponentialBackoff(attempt: number) {\n\t\tconst num = Math.max(attempt - 2, 0)\n\t\tconst delay = Math.min(2 ** num, maxSeconds)\n\t\treturn delayMs(delay * 1_000)\n\t}\n}\n\nconst sqrt5 = /* @__PURE__ */ Math.sqrt(5)\n/**\n * @see https://en.wikipedia.org/wiki/Fibonacci_sequence#Closed-form_expression\n */\nconst binet = (n: number) =>\n\tMath.round(((1 + sqrt5) ** n - (1 - sqrt5) ** n) / (2 ** n * sqrt5))\n\n/**\n * Returns a function which implements Fibonacci backoff.\n *\n * @param maxSeconds - The maximum number of seconds to wait before retrying.\n * @returns A function which takes an `attempt` number and returns a promise\n * that resolves after the calculated delay.\n */\nexport function useFibonacciBackoff(maxSeconds: number) {\n\treturn function fibonacciBackoff(attempt: number) {\n\t\tconst delay = Math.min(binet(attempt), maxSeconds)\n\t\treturn delayMs(delay * 1_000)\n\t}\n}\n\nexport interface RetryOptions {\n\t/**\n\t * Whether an error should be treated as non-retryable. When this returns\n\t * true, the error will be thrown immediately without retrying.\n\t *\n\t * @default () => false // All errors are retried\n\t */\n\tshouldRetry?: (error: unknown, attempt: number) => boolean\n\n\t/**\n\t * Maximum number of retries\n\t *\n\t * @default 3\n\t */\n\tmaxAttempts?: number\n\n\t/**\n\t * Function that returns a promise resolving when the next retry should occur.\n\t * Receives the attempt number (starting at 2) and an abort signal.\n\t *\n\t * @default () => Promise.resolve() // Immediate retry\n\t */\n\tretryDelay?: (attempt: number, signal: AbortSignal) => Promise<void>\n}\n\n/**\n * Wrap a function with retry logic. Errors will be retried according to the\n * provided options.\n *\n * @example\n * ```ts\n * // Compose with circuit breaker. Retry up to 3 times with no delay\n * const protectedA = createCircuitBreaker(\n * withRetry(unreliableApiCall, { maxAttempts: 3 })\n * )\n *\n * // Retry up to 5 times with exponential backoff\n * const protectedB = createCircuitBreaker(\n * withRetry(unreliableApiCall, {\n * maxAttempts: 5,\n * retryDelay: useExponentialBackoff(30),\n * })\n * )\n * ```\n */\nexport function withRetry<Ret, Args extends unknown[]>(\n\tmain: MainFn<Ret, Args>,\n\toptions: RetryOptions = {},\n): MainFn<Ret, Args> {\n\tconst {\n\t\tshouldRetry = () => true,\n\t\tmaxAttempts = 3,\n\t\tretryDelay = () => Promise.resolve(),\n\t} = options\n\n\tassert(maxAttempts >= 1, \"maxAttempts must be a number greater than 0\")\n\n\tconst controller = new AbortController()\n\tconst { signal } = controller\n\n\tasync function execute(args: Args, attempt = 1): Promise<Ret> {\n\t\ttry {\n\t\t\treturn await main(...args)\n\t\t} catch (cause) {\n\t\t\t// Check if we should retry this error\n\t\t\tif (!shouldRetry(cause, attempt)) throw cause\n\n\t\t\t// Check if we've exhausted attempts\n\t\t\tif (attempt >= maxAttempts)\n\t\t\t\tthrow new Error(`ERR_CIRCUIT_BREAKER_MAX_ATTEMPTS (${maxAttempts})`, {\n\t\t\t\t\tcause,\n\t\t\t\t})\n\n\t\t\t// Wait before retrying\n\t\t\tawait rejectOnAbort(signal, retryDelay(attempt + 1, signal))\n\n\t\t\t// Retry\n\t\t\treturn execute(args, attempt + 1)\n\t\t}\n\t}\n\n\treturn Object.assign(\n\t\tfunction withRetryFunction(...args: Args) {\n\t\t\treturn execute(args)\n\t\t},\n\t\t{\n\t\t\t[disposeKey]: (disposeMessage = \"ERR_CIRCUIT_BREAKER_DISPOSED\") => {\n\t\t\t\tconst reason = new ReferenceError(disposeMessage)\n\t\t\t\tmain[disposeKey]?.(disposeMessage)\n\t\t\t\tcontroller.abort(reason)\n\t\t\t},\n\t\t},\n\t)\n}\n\n/**\n * Wrap a function with a timeout. If execution of `main` exceeds `timeoutMs`,\n * then the call is rejected with `new Error(timeoutMessage)`.\n */\nexport function withTimeout<Ret, Args extends unknown[]>(\n\tmain: MainFn<Ret, Args>,\n\ttimeoutMs: number,\n\ttimeoutMessage = \"ERR_CIRCUIT_BREAKER_TIMEOUT\",\n): MainFn<Ret, Args> {\n\tconst error = new Error(timeoutMessage)\n\tconst controller = new AbortController()\n\tconst { signal } = controller\n\n\tfunction withTimeoutFunction(...args: Args) {\n\t\tlet teardown: () => void\n\t\tlet timer: NodeJS.Timeout\n\t\treturn Promise.race([\n\t\t\tmain(...args).finally(() => {\n\t\t\t\tclearTimeout(timer)\n\t\t\t\tsignal.removeEventListener(\"abort\", teardown)\n\t\t\t}),\n\t\t\tnew Promise<never>((_, reject) => {\n\t\t\t\tteardown = () => {\n\t\t\t\t\tclearTimeout(timer)\n\t\t\t\t\treject(signal.reason)\n\t\t\t\t}\n\t\t\t\ttimer = setTimeout(reject, timeoutMs, error)\n\t\t\t\tsignal.addEventListener(\"abort\", teardown, { once: true })\n\t\t\t}),\n\t\t])\n\t}\n\n\treturn Object.assign(withTimeoutFunction, {\n\t\t[disposeKey]: (disposeMessage = \"ERR_CIRCUIT_BREAKER_DISPOSED\") => {\n\t\t\tconst reason = new ReferenceError(disposeMessage)\n\t\t\tmain[disposeKey]?.(disposeMessage)\n\t\t\tcontroller.abort(reason)\n\t\t},\n\t})\n}\n","import { parseOptions } from \"./options.js\"\nimport {\n\tdisposeKey,\n\ttype CircuitBreakerOptions,\n\ttype CircuitBreakerProtectedFn,\n\ttype CircuitState,\n\ttype HistoryEntry,\n\ttype HistoryMap,\n\ttype MainFn,\n} from \"./types.js\"\nimport { assertNever, type AnyFn } from \"./util.js\"\n\nexport * from \"./helpers.js\"\nexport { delayMs } from \"./util.js\"\n\nexport function createCircuitBreaker<\n\tRet,\n\tArgs extends unknown[],\n\tFallback extends AnyFn = MainFn<Ret, Args>,\n>(\n\tmain: MainFn<Ret, Args>,\n\toptions: CircuitBreakerOptions<Fallback> = {},\n): CircuitBreakerProtectedFn<Ret, Args> {\n\tconst {\n\t\terrorIsFailure,\n\t\terrorThreshold,\n\t\terrorWindow,\n\t\tminimumCandidates,\n\t\tonClose,\n\t\tonHalfOpen,\n\t\tonOpen,\n\t\tresetAfter,\n\t} = parseOptions(options)\n\tconst controller = new AbortController()\n\tconst history: HistoryMap = new Map()\n\tconst signal = controller.signal\n\tlet failureCause: unknown\n\tlet fallback = options.fallback || (() => Promise.reject(failureCause))\n\tlet halfOpenPending: Promise<unknown> | undefined\n\tlet resetTimer: NodeJS.Timeout\n\tlet state: CircuitState = \"closed\"\n\n\tfunction clearFailure() {\n\t\tfailureCause = undefined\n\t}\n\n\tfunction closeCircuit() {\n\t\tstate = \"closed\"\n\t\tclearFailure()\n\t\tclearTimeout(resetTimer)\n\t\tonClose?.()\n\t}\n\n\tfunction failureRate() {\n\t\tlet failures = 0\n\t\tlet total = 0\n\t\tfor (const { status } of history.values()) {\n\t\t\tif (status === \"rejected\") failures++\n\t\t\tif (status !== \"pending\") total++\n\t\t}\n\t\t// Don't calculate anything until we have enough data\n\t\tif (!total || total < minimumCandidates) return 0\n\t\treturn failures / total\n\t}\n\n\t/**\n\t * Break the circuit and wait for a reset\n\t */\n\tfunction openCircuit(cause: unknown) {\n\t\tfailureCause = cause\n\t\tstate = \"open\"\n\t\tclearTimeout(resetTimer)\n\t\tresetTimer = setTimeout(() => {\n\t\t\tstate = \"halfOpen\"\n\t\t\tonHalfOpen?.()\n\t\t}, resetAfter)\n\t\tonOpen?.(cause)\n\t}\n\n\tfunction createHistoryItem<T>(pending: Promise<T>) {\n\t\tconst entry: HistoryEntry = { status: \"pending\", timer: undefined }\n\t\tconst teardown = () => {\n\t\t\tclearTimeout(entry.timer)\n\t\t\thistory.delete(pending)\n\t\t\tsignal.removeEventListener(\"abort\", teardown)\n\t\t}\n\t\tsignal.addEventListener(\"abort\", teardown, { once: true })\n\t\tconst settle = (value: \"resolved\" | \"rejected\") => {\n\t\t\tif (signal.aborted) return\n\t\t\tentry.status = value\n\t\t\t// Remove the entry from history when it falls outside of the error window\n\t\t\tentry.timer = setTimeout(teardown, errorWindow)\n\t\t}\n\t\thistory.set(pending, entry)\n\t\treturn { pending, settle, teardown }\n\t}\n\n\t/**\n\t * Wrap calls to `main` with circuit breaker logic\n\t */\n\tfunction execute(args: Args): Promise<Ret> {\n\t\t// Normal operation when circuit is closed. If an error occurs, keep track\n\t\t// of the failure count and open the circuit if it exceeds the threshold.\n\t\tif (state === \"closed\") {\n\t\t\tconst { pending, settle, teardown } = createHistoryItem(main(...args))\n\t\t\treturn pending.then(\n\t\t\t\t(result) => {\n\t\t\t\t\tsettle(\"resolved\")\n\t\t\t\t\treturn result\n\t\t\t\t},\n\t\t\t\t(cause: unknown) => {\n\t\t\t\t\t// Was the circuit disposed, or is this error considered a failure?\n\t\t\t\t\tif (signal.aborted || errorIsFailure(cause)) {\n\t\t\t\t\t\tteardown()\n\t\t\t\t\t\tthrow cause\n\t\t\t\t\t}\n\n\t\t\t\t\t// Should this error open the circuit?\n\t\t\t\t\tsettle(\"rejected\")\n\t\t\t\t\tif (failureRate() > errorThreshold) openCircuit(cause)\n\n\t\t\t\t\treturn fallback(...args)\n\t\t\t\t},\n\t\t\t)\n\t\t}\n\n\t\t// Use the fallback while the circuit is open, or if a half-open trial\n\t\t// attempt was already made.\n\t\telse if (state === \"open\" || halfOpenPending) {\n\t\t\treturn fallback(...args)\n\t\t}\n\n\t\t// If the circuit is half-open, make one attempt. If it succeeds, close\n\t\t// the circuit and resume normal operation. If it fails, re-open the\n\t\t// circuit and run the fallback instead.\n\t\telse if (state === \"halfOpen\") {\n\t\t\treturn (halfOpenPending = main(...args))\n\t\t\t\t.finally(() => (halfOpenPending = undefined))\n\t\t\t\t.then(\n\t\t\t\t\t(result) => {\n\t\t\t\t\t\tif (signal.aborted) return result // disposed\n\t\t\t\t\t\tcloseCircuit()\n\t\t\t\t\t\treturn result\n\t\t\t\t\t},\n\t\t\t\t\t(cause: unknown) => {\n\t\t\t\t\t\t// Was the circuit disposed, or was this a non-retryable error?\n\t\t\t\t\t\tif (signal.aborted || errorIsFailure(cause)) throw cause\n\n\t\t\t\t\t\t// Open the circuit and use fallback\n\t\t\t\t\t\topenCircuit(cause)\n\t\t\t\t\t\treturn fallback(...args)\n\t\t\t\t\t},\n\t\t\t\t)\n\t\t\t/* v8 ignore next */\n\t\t}\n\n\t\t// exhaustive check\n\t\t/* v8 ignore next */\n\t\treturn assertNever(state)\n\t}\n\n\treturn Object.assign((...args: Args) => execute(args), {\n\t\tdispose: (disposeMessage = \"ERR_CIRCUIT_BREAKER_DISPOSED\") => {\n\t\t\tconst reason = new ReferenceError(disposeMessage)\n\t\t\tmain[disposeKey]?.(disposeMessage)\n\t\t\tclearFailure()\n\t\t\tclearTimeout(resetTimer)\n\t\t\thistory.forEach((entry) => clearTimeout(entry.timer))\n\t\t\thistory.clear()\n\t\t\tfallback = () => Promise.reject(reason)\n\t\t\tstate = \"open\"\n\t\t\tcontroller.abort(reason)\n\t\t},\n\t\tgetLatestError: () => failureCause,\n\t\tgetState: () => state,\n\t})\n}\n"],"names":[],"mappings":"AACO,SAAS,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE;AACvC,EAAE,IAAI,CAAC,KAAK,EAAE,MAAM,IAAI,SAAS,CAAC,OAAO,CAAC;AAC1C;AACO,MAAM,WAAW,GAAG,CAAC,GAAG,EAAE,GAAG,GAAG,kBAAkB,KAAK;AAC9D,EAAE,MAAM,IAAI,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC,CAAC;AACvC,CAAC;AACW,MAAC,OAAO,GAAG,CAAC,EAAE,KAAK,IAAI,OAAO,CAAC,CAAC,IAAI,KAAK,UAAU,CAAC,IAAI,EAAE,EAAE,CAAC;AAClE,MAAM,aAAa,GAAG,CAAC,MAAM,EAAE,OAAO,KAAK;AAClD,EAAE,IAAI,QAAQ;AACd,EAAE,OAAO,OAAO,CAAC,IAAI,CAAC;AACtB,IAAI,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,MAAM;AAC3C,MAAM,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,QAAQ,CAAC;AACnD,IAAI,CAAC,CAAC;AACN,IAAI,IAAI,OA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AC7C,MAAM,KAAK,GAAG,MAAM;AACpB,MAAM,UAAU,CAAC,KAAK,CAAC,MAAM,CAAC;AAC9B,IAAI,CAAC;AACL,IAAI,cAAc,EAAE,MAAM,YAAY;AACtC,IAAI,QAAQ,EAAE,MAAM;AACpB,GAAG,CAAC;AACJ;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "breaker-box",
3
- "version": "4.2.0",
3
+ "version": "5.0.0",
4
4
  "description": "A zero-dependency circuit breaker implementation for Node.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -34,20 +34,21 @@
34
34
  "test:ts": "tsc --noEmit"
35
35
  },
36
36
  "keywords": [
37
- "circuit-breaker",
37
+ "async",
38
38
  "fallback",
39
+ "fault-tolerance",
39
40
  "reliability",
40
41
  "try-again"
41
42
  ],
42
43
  "author": "Matthew Pietz <sirlancelot@gmail.com>",
43
44
  "license": "ISC",
44
45
  "devDependencies": {
45
- "@types/node": "24.7.0",
46
- "@vitest/coverage-v8": "3.2.4",
47
- "pkgroll": "2.18.0",
48
- "prettier": "3.6.2",
46
+ "@types/node": "24.10.2",
47
+ "@vitest/coverage-v8": "4.0.15",
48
+ "pkgroll": "2.21.4",
49
+ "prettier": "3.7.4",
49
50
  "typescript": "5.9.3",
50
- "vitest": "3.2.4",
51
- "vitest-when": "0.8.1"
51
+ "vitest": "4.0.15",
52
+ "vitest-when": "0.10.0"
52
53
  }
53
54
  }