@metamask-previews/controller-utils 11.4.5-preview-b1518d1 → 11.4.5-preview-f3d5e317

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -9,8 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9
9
 
10
10
  ### Added
11
11
 
12
- - Add `createServicePolicy` function to assist with reducing boilerplate for service classes ([#5154](https://github.com/MetaMask/core/pull/5154), [#5143](https://github.com/MetaMask/core/pull/5143), [#5149](https://github.com/MetaMask/core/pull/5149))
13
- - Also add `DEFAULT_CIRCUIT_BREAK_DURATION`, `DEFAULT_DEGRADED_THRESHOLD`, `DEFAULT_MAX_CONSECUTIVE_FAILURES`, and `DEFAULT_MAX_RETRIES` constants and `IServicePolicy` type
12
+ - Add utility function for reducing boilerplate for service classes ([#5154](https://github.com/MetaMask/core/pull/5154), [#5143](https://github.com/MetaMask/core/pull/5143), [#5149](https://github.com/MetaMask/core/pull/5149), [#5188](https://github.com/MetaMask/core/pull/5188), [#5192](https://github.com/MetaMask/core/pull/5192))
13
+ - Add function `createServicePolicy`
14
+ - Add constants `DEFAULT_CIRCUIT_BREAK_DURATION`, `DEFAULT_DEGRADED_THRESHOLD`, `DEFAULT_MAX_CONSECUTIVE_FAILURES`, and `DEFAULT_MAX_RETRIES`
15
+ - Add types `ServicePolicy` and `CreateServicePolicyOptions`
16
+ - Re-export `BrokenCircuitError`, `CircuitState`, `handleAll`, and `handleWhen` from `cockatiel`
14
17
 
15
18
  ## [11.4.5]
16
19
 
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createServicePolicy = exports.DEFAULT_DEGRADED_THRESHOLD = exports.DEFAULT_CIRCUIT_BREAK_DURATION = exports.DEFAULT_MAX_CONSECUTIVE_FAILURES = exports.DEFAULT_MAX_RETRIES = void 0;
3
+ exports.createServicePolicy = exports.DEFAULT_DEGRADED_THRESHOLD = exports.DEFAULT_CIRCUIT_BREAK_DURATION = exports.DEFAULT_MAX_CONSECUTIVE_FAILURES = exports.DEFAULT_MAX_RETRIES = exports.handleWhen = exports.handleAll = exports.BrokenCircuitError = exports.CircuitState = void 0;
4
4
  const cockatiel_1 = require("cockatiel");
5
+ Object.defineProperty(exports, "BrokenCircuitError", { enumerable: true, get: function () { return cockatiel_1.BrokenCircuitError; } });
6
+ Object.defineProperty(exports, "CircuitState", { enumerable: true, get: function () { return cockatiel_1.CircuitState; } });
7
+ Object.defineProperty(exports, "handleAll", { enumerable: true, get: function () { return cockatiel_1.handleAll; } });
8
+ Object.defineProperty(exports, "handleWhen", { enumerable: true, get: function () { return cockatiel_1.handleWhen; } });
5
9
  /**
6
10
  * The maximum number of times that a failing service should be re-run before
7
11
  * giving up.
@@ -9,7 +13,9 @@ const cockatiel_1 = require("cockatiel");
9
13
  exports.DEFAULT_MAX_RETRIES = 3;
10
14
  /**
11
15
  * The maximum number of times that the service is allowed to fail before
12
- * pausing further retries.
16
+ * pausing further retries. This is set to a value such that if given a
17
+ * service that continually fails, the policy needs to be executed 3 times
18
+ * before further retries are paused.
13
19
  */
14
20
  exports.DEFAULT_MAX_CONSECUTIVE_FAILURES = (1 + exports.DEFAULT_MAX_RETRIES) * 3;
15
21
  /**
@@ -52,14 +58,6 @@ exports.DEFAULT_DEGRADED_THRESHOLD = 5000;
52
58
  * @param options.degradedThreshold - The length of time (in milliseconds) that
53
59
  * governs when the service is regarded as degraded (affecting when `onDegraded`
54
60
  * is called). Defaults to 5 seconds.
55
- * @param options.onBreak - A function which is called when the service fails
56
- * too many times in a row (specifically, more than `maxConsecutiveFailures`).
57
- * @param options.onDegraded - A function which is called when the service
58
- * succeeds before `maxConsecutiveFailures` is reached, but takes more time than
59
- * the `degradedThreshold` to run.
60
- * @param options.onRetry - A function which will be called the moment the
61
- * policy kicks off a timer to re-run the function passed to the policy. This is
62
- * primarily useful in tests where we are mocking timers.
63
61
  * @returns The service policy.
64
62
  * @example
65
63
  * This function is designed to be used in the context of a service class like
@@ -93,13 +91,7 @@ exports.DEFAULT_DEGRADED_THRESHOLD = 5000;
93
91
  * }
94
92
  * ```
95
93
  */
96
- function createServicePolicy({ maxRetries = exports.DEFAULT_MAX_RETRIES, retryFilterPolicy = cockatiel_1.handleAll, maxConsecutiveFailures = exports.DEFAULT_MAX_CONSECUTIVE_FAILURES, circuitBreakDuration = exports.DEFAULT_CIRCUIT_BREAK_DURATION, degradedThreshold = exports.DEFAULT_DEGRADED_THRESHOLD, onBreak = () => {
97
- // do nothing
98
- }, onDegraded = () => {
99
- // do nothing
100
- }, onRetry = () => {
101
- // do nothing
102
- }, } = {}) {
94
+ function createServicePolicy({ maxRetries = exports.DEFAULT_MAX_RETRIES, retryFilterPolicy = cockatiel_1.handleAll, maxConsecutiveFailures = exports.DEFAULT_MAX_CONSECUTIVE_FAILURES, circuitBreakDuration = exports.DEFAULT_CIRCUIT_BREAK_DURATION, degradedThreshold = exports.DEFAULT_DEGRADED_THRESHOLD, } = {}) {
103
95
  const retryPolicy = (0, cockatiel_1.retry)(retryFilterPolicy, {
104
96
  // Note that although the option here is called "max attempts", it's really
105
97
  // maximum number of *retries* (attempts past the initial attempt).
@@ -108,6 +100,7 @@ function createServicePolicy({ maxRetries = exports.DEFAULT_MAX_RETRIES, retryFi
108
100
  // determined by a backoff formula.
109
101
  backoff: new cockatiel_1.ExponentialBackoff(),
110
102
  });
103
+ const onRetry = retryPolicy.onRetry.bind(retryPolicy);
111
104
  const circuitBreakerPolicy = (0, cockatiel_1.circuitBreaker)(cockatiel_1.handleAll, {
112
105
  // While the circuit is open, any additional invocations of the service
113
106
  // passed to the policy (either via automatic retries or by manually
@@ -118,38 +111,37 @@ function createServicePolicy({ maxRetries = exports.DEFAULT_MAX_RETRIES, retryFi
118
111
  halfOpenAfter: circuitBreakDuration,
119
112
  breaker: new cockatiel_1.ConsecutiveBreaker(maxConsecutiveFailures),
120
113
  });
121
- // The `onBreak` callback will be called if the service consistently throws
122
- // for as many times as exceeds the maximum consecutive number of failures.
123
- // Combined with the retry policy, this can happen if:
124
- // - `maxConsecutiveFailures` < the default max retries (3) and the policy is
125
- // executed once
126
- // - `maxConsecutiveFailures` >= the default max retries (3) but the policy is
127
- // executed multiple times, enough for the total number of retries to exceed
128
- // `maxConsecutiveFailures`
129
- circuitBreakerPolicy.onBreak(onBreak);
130
- // The `onRetryPolicy` callback will be called each time the service is
131
- // invoked (including retries).
132
- retryPolicy.onRetry(onRetry);
114
+ const onBreak = circuitBreakerPolicy.onBreak.bind(circuitBreakerPolicy);
115
+ const onDegradedListeners = [];
133
116
  retryPolicy.onGiveUp(() => {
134
117
  if (circuitBreakerPolicy.state === cockatiel_1.CircuitState.Closed) {
135
- // The `onDegraded` callback will be called if the number of retries is
136
- // exceeded and the maximum number of consecutive failures has not been
137
- // reached yet (whether the policy is called once or multiple times).
138
- onDegraded();
118
+ for (const listener of onDegradedListeners) {
119
+ listener();
120
+ }
139
121
  }
140
122
  });
141
123
  retryPolicy.onSuccess(({ duration }) => {
142
124
  if (circuitBreakerPolicy.state === cockatiel_1.CircuitState.Closed &&
143
125
  duration > degradedThreshold) {
144
- // The `onDegraded` callback will also be called if the service does not
145
- // throw, but the time it takes for the service to run exceeds the
146
- // `degradedThreshold`.
147
- onDegraded();
126
+ for (const listener of onDegradedListeners) {
127
+ listener();
128
+ }
148
129
  }
149
130
  });
150
- // The retry policy really retries the circuit breaker policy, which invokes
151
- // the service.
152
- return (0, cockatiel_1.wrap)(retryPolicy, circuitBreakerPolicy);
131
+ const onDegraded = (listener) => {
132
+ onDegradedListeners.push(listener);
133
+ };
134
+ // Every time the retry policy makes an attempt, it executes the circuit
135
+ // breaker policy, which executes the service.
136
+ const policy = (0, cockatiel_1.wrap)(retryPolicy, circuitBreakerPolicy);
137
+ return {
138
+ ...policy,
139
+ circuitBreakerPolicy,
140
+ retryPolicy,
141
+ onBreak,
142
+ onDegraded,
143
+ onRetry,
144
+ };
153
145
  }
154
146
  exports.createServicePolicy = createServicePolicy;
155
147
  //# sourceMappingURL=create-service-policy.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"create-service-policy.cjs","sourceRoot":"","sources":["../src/create-service-policy.ts"],"names":[],"mappings":";;;AAAA,yCAQmB;AAKnB;;;GAGG;AACU,QAAA,mBAAmB,GAAG,CAAC,CAAC;AAErC;;;GAGG;AACU,QAAA,gCAAgC,GAAG,CAAC,CAAC,GAAG,2BAAmB,CAAC,GAAG,CAAC,CAAC;AAE9E;;;GAGG;AACU,QAAA,8BAA8B,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAE7D;;;GAGG;AACU,QAAA,0BAA0B,GAAG,IAAK,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsEG;AACH,SAAgB,mBAAmB,CAAC,EAClC,UAAU,GAAG,2BAAmB,EAChC,iBAAiB,GAAG,qBAAS,EAC7B,sBAAsB,GAAG,wCAAgC,EACzD,oBAAoB,GAAG,sCAA8B,EACrD,iBAAiB,GAAG,kCAA0B,EAC9C,OAAO,GAAG,GAAG,EAAE;IACb,aAAa;AACf,CAAC,EACD,UAAU,GAAG,GAAG,EAAE;IAChB,aAAa;AACf,CAAC,EACD,OAAO,GAAG,GAAG,EAAE;IACb,aAAa;AACf,CAAC,MAUC,EAAE;IACJ,MAAM,WAAW,GAAG,IAAA,iBAAK,EAAC,iBAAiB,EAAE;QAC3C,2EAA2E;QAC3E,mEAAmE;QACnE,WAAW,EAAE,UAAU;QACvB,4EAA4E;QAC5E,mCAAmC;QACnC,OAAO,EAAE,IAAI,8BAAkB,EAAE;KAClC,CAAC,CAAC;IAEH,MAAM,oBAAoB,GAAG,IAAA,0BAAc,EAAC,qBAAS,EAAE;QACrD,uEAAuE;QACvE,oEAAoE;QACpE,wEAAwE;QACxE,4EAA4E;QAC5E,qEAAqE;QACrE,qDAAqD;QACrD,aAAa,EAAE,oBAAoB;QACnC,OAAO,EAAE,IAAI,8BAAkB,CAAC,sBAAsB,CAAC;KACxD,CAAC,CAAC;IAEH,2EAA2E;IAC3E,2EAA2E;IAC3E,sDAAsD;IACtD,6EAA6E;IAC7E,gBAAgB;IAChB,8EAA8E;IAC9E,8EAA8E;IAC9E,6BAA6B;IAC7B,oBAAoB,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAEtC,uEAAuE;IACvE,+BAA+B;IAC/B,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAE7B,WAAW,CAAC,QAAQ,CAAC,GAAG,EAAE;QACxB,IAAI,oBAAoB,CAAC,KAAK,KAAK,wBAAY,CAAC,MAAM,EAAE;YACtD,uEAAuE;YACvE,uEAAuE;YACvE,qEAAqE;YACrE,UAAU,EAAE,CAAC;SACd;IACH,CAAC,CAAC,CAAC;IACH,WAAW,CAAC,SAAS,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE;QACrC,IACE,oBAAoB,CAAC,KAAK,KAAK,wBAAY,CAAC,MAAM;YAClD,QAAQ,GAAG,iBAAiB,EAC5B;YACA,wEAAwE;YACxE,kEAAkE;YAClE,uBAAuB;YACvB,UAAU,EAAE,CAAC;SACd;IACH,CAAC,CAAC,CAAC;IAEH,4EAA4E;IAC5E,eAAe;IACf,OAAO,IAAA,gBAAI,EAAC,WAAW,EAAE,oBAAoB,CAAC,CAAC;AACjD,CAAC;AAlFD,kDAkFC","sourcesContent":["import {\n circuitBreaker,\n ConsecutiveBreaker,\n ExponentialBackoff,\n handleAll,\n retry,\n wrap,\n CircuitState,\n} from 'cockatiel';\nimport type { IPolicy, Policy } from 'cockatiel';\n\nexport type { IPolicy as IServicePolicy };\n\n/**\n * The maximum number of times that a failing service should be re-run before\n * giving up.\n */\nexport const DEFAULT_MAX_RETRIES = 3;\n\n/**\n * The maximum number of times that the service is allowed to fail before\n * pausing further retries.\n */\nexport const DEFAULT_MAX_CONSECUTIVE_FAILURES = (1 + DEFAULT_MAX_RETRIES) * 3;\n\n/**\n * The default length of time (in milliseconds) to temporarily pause retries of\n * the service after enough consecutive failures.\n */\nexport const DEFAULT_CIRCUIT_BREAK_DURATION = 30 * 60 * 1000;\n\n/**\n * The default length of time (in milliseconds) that governs when the service is\n * regarded as degraded (affecting when `onDegraded` is called).\n */\nexport const DEFAULT_DEGRADED_THRESHOLD = 5_000;\n\n/**\n * Constructs an object exposing an `execute` method which, given a function —\n * hereafter called the \"service\" — will retry that service with ever increasing\n * delays until it succeeds. If the policy detects too many consecutive\n * failures, it will block further retries until a designated time period has\n * passed; this particular behavior is primarily designed for services that wrap\n * API calls so as not to make needless HTTP requests when the API is down and\n * to be able to recover when the API comes back up. In addition, hooks allow\n * for responding to certain events, one of which can be used to detect when an\n * HTTP request is performing slowly.\n *\n * Internally, this function makes use of the retry and circuit breaker policies\n * from the [Cockatiel](https://www.npmjs.com/package/cockatiel) library; see\n * there for more.\n *\n * @param options - The options to this function.\n * @param options.maxRetries - The maximum number of times that a failing\n * service should be re-invoked before giving up. Defaults to 3.\n * @param options.retryFilterPolicy - The policy used to control when the\n * service should be retried based on either the result of the servce or an\n * error that it throws. For instance, you could use this to retry only certain\n * errors. See `handleWhen` and friends from Cockatiel for more.\n * @param options.maxConsecutiveFailures - The maximum number of times that the\n * service is allowed to fail before pausing further retries. Defaults to 12.\n * @param options.circuitBreakDuration - The length of time (in milliseconds) to\n * pause retries of the action after the number of failures reaches\n * `maxConsecutiveFailures`.\n * @param options.degradedThreshold - The length of time (in milliseconds) that\n * governs when the service is regarded as degraded (affecting when `onDegraded`\n * is called). Defaults to 5 seconds.\n * @param options.onBreak - A function which is called when the service fails\n * too many times in a row (specifically, more than `maxConsecutiveFailures`).\n * @param options.onDegraded - A function which is called when the service\n * succeeds before `maxConsecutiveFailures` is reached, but takes more time than\n * the `degradedThreshold` to run.\n * @param options.onRetry - A function which will be called the moment the\n * policy kicks off a timer to re-run the function passed to the policy. This is\n * primarily useful in tests where we are mocking timers.\n * @returns The service policy.\n * @example\n * This function is designed to be used in the context of a service class like\n * this:\n * ``` ts\n * class Service {\n * constructor() {\n * this.#policy = createServicePolicy({\n * maxRetries: 3,\n * retryFilterPolicy: handleWhen((error) => {\n * return error.message.includes('oops');\n * }),\n * maxConsecutiveFailures: 3,\n * circuitBreakDuration: 5000,\n * degradedThreshold: 2000,\n * onBreak: () => {\n * console.log('Circuit broke');\n * },\n * onDegraded: () => {\n * console.log('Service is degraded');\n * },\n * });\n * }\n *\n * async fetch() {\n * return await this.#policy.execute(async () => {\n * const response = await fetch('https://some/url');\n * return await response.json();\n * });\n * }\n * }\n * ```\n */\nexport function createServicePolicy({\n maxRetries = DEFAULT_MAX_RETRIES,\n retryFilterPolicy = handleAll,\n maxConsecutiveFailures = DEFAULT_MAX_CONSECUTIVE_FAILURES,\n circuitBreakDuration = DEFAULT_CIRCUIT_BREAK_DURATION,\n degradedThreshold = DEFAULT_DEGRADED_THRESHOLD,\n onBreak = () => {\n // do nothing\n },\n onDegraded = () => {\n // do nothing\n },\n onRetry = () => {\n // do nothing\n },\n}: {\n maxRetries?: number;\n retryFilterPolicy?: Policy;\n maxConsecutiveFailures?: number;\n circuitBreakDuration?: number;\n degradedThreshold?: number;\n onBreak?: () => void;\n onDegraded?: () => void;\n onRetry?: () => void;\n} = {}): IPolicy {\n const retryPolicy = retry(retryFilterPolicy, {\n // Note that although the option here is called \"max attempts\", it's really\n // maximum number of *retries* (attempts past the initial attempt).\n maxAttempts: maxRetries,\n // Retries of the service will be executed following ever increasing delays,\n // determined by a backoff formula.\n backoff: new ExponentialBackoff(),\n });\n\n const circuitBreakerPolicy = circuitBreaker(handleAll, {\n // While the circuit is open, any additional invocations of the service\n // passed to the policy (either via automatic retries or by manually\n // executing the policy again) will result in a BrokenCircuitError. This\n // will remain the case until `circuitBreakDuration` passes, after which the\n // service will be allowed to run again. If the service succeeds, the\n // circuit will close, otherwise it will remain open.\n halfOpenAfter: circuitBreakDuration,\n breaker: new ConsecutiveBreaker(maxConsecutiveFailures),\n });\n\n // The `onBreak` callback will be called if the service consistently throws\n // for as many times as exceeds the maximum consecutive number of failures.\n // Combined with the retry policy, this can happen if:\n // - `maxConsecutiveFailures` < the default max retries (3) and the policy is\n // executed once\n // - `maxConsecutiveFailures` >= the default max retries (3) but the policy is\n // executed multiple times, enough for the total number of retries to exceed\n // `maxConsecutiveFailures`\n circuitBreakerPolicy.onBreak(onBreak);\n\n // The `onRetryPolicy` callback will be called each time the service is\n // invoked (including retries).\n retryPolicy.onRetry(onRetry);\n\n retryPolicy.onGiveUp(() => {\n if (circuitBreakerPolicy.state === CircuitState.Closed) {\n // The `onDegraded` callback will be called if the number of retries is\n // exceeded and the maximum number of consecutive failures has not been\n // reached yet (whether the policy is called once or multiple times).\n onDegraded();\n }\n });\n retryPolicy.onSuccess(({ duration }) => {\n if (\n circuitBreakerPolicy.state === CircuitState.Closed &&\n duration > degradedThreshold\n ) {\n // The `onDegraded` callback will also be called if the service does not\n // throw, but the time it takes for the service to run exceeds the\n // `degradedThreshold`.\n onDegraded();\n }\n });\n\n // The retry policy really retries the circuit breaker policy, which invokes\n // the service.\n return wrap(retryPolicy, circuitBreakerPolicy);\n}\n"]}
1
+ {"version":3,"file":"create-service-policy.cjs","sourceRoot":"","sources":["../src/create-service-policy.ts"],"names":[],"mappings":";;;AAAA,yCAUmB;AAQI,mGAjBrB,8BAAkB,OAiBqB;AAAhC,6FAhBP,wBAAY,OAgBO;AAAsB,0FAZzC,qBAAS,OAYyC;AAAE,2FAXpD,sBAAU,OAWoD;AAqEhE;;;GAGG;AACU,QAAA,mBAAmB,GAAG,CAAC,CAAC;AAErC;;;;;GAKG;AACU,QAAA,gCAAgC,GAAG,CAAC,CAAC,GAAG,2BAAmB,CAAC,GAAG,CAAC,CAAC;AAE9E;;;GAGG;AACU,QAAA,8BAA8B,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAE7D;;;GAGG;AACU,QAAA,0BAA0B,GAAG,IAAK,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8DG;AACH,SAAgB,mBAAmB,CAAC,EAClC,UAAU,GAAG,2BAAmB,EAChC,iBAAiB,GAAG,qBAAS,EAC7B,sBAAsB,GAAG,wCAAgC,EACzD,oBAAoB,GAAG,sCAA8B,EACrD,iBAAiB,GAAG,kCAA0B,MAChB,EAAE;IAChC,MAAM,WAAW,GAAG,IAAA,iBAAK,EAAC,iBAAiB,EAAE;QAC3C,2EAA2E;QAC3E,mEAAmE;QACnE,WAAW,EAAE,UAAU;QACvB,4EAA4E;QAC5E,mCAAmC;QACnC,OAAO,EAAE,IAAI,8BAAkB,EAAE;KAClC,CAAC,CAAC;IACH,MAAM,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IAEtD,MAAM,oBAAoB,GAAG,IAAA,0BAAc,EAAC,qBAAS,EAAE;QACrD,uEAAuE;QACvE,oEAAoE;QACpE,wEAAwE;QACxE,4EAA4E;QAC5E,qEAAqE;QACrE,qDAAqD;QACrD,aAAa,EAAE,oBAAoB;QACnC,OAAO,EAAE,IAAI,8BAAkB,CAAC,sBAAsB,CAAC;KACxD,CAAC,CAAC;IACH,MAAM,OAAO,GAAG,oBAAoB,CAAC,OAAO,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;IAExE,MAAM,mBAAmB,GAAmB,EAAE,CAAC;IAC/C,WAAW,CAAC,QAAQ,CAAC,GAAG,EAAE;QACxB,IAAI,oBAAoB,CAAC,KAAK,KAAK,wBAAY,CAAC,MAAM,EAAE;YACtD,KAAK,MAAM,QAAQ,IAAI,mBAAmB,EAAE;gBAC1C,QAAQ,EAAE,CAAC;aACZ;SACF;IACH,CAAC,CAAC,CAAC;IACH,WAAW,CAAC,SAAS,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE;QACrC,IACE,oBAAoB,CAAC,KAAK,KAAK,wBAAY,CAAC,MAAM;YAClD,QAAQ,GAAG,iBAAiB,EAC5B;YACA,KAAK,MAAM,QAAQ,IAAI,mBAAmB,EAAE;gBAC1C,QAAQ,EAAE,CAAC;aACZ;SACF;IACH,CAAC,CAAC,CAAC;IACH,MAAM,UAAU,GAAG,CAAC,QAAoB,EAAE,EAAE;QAC1C,mBAAmB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACrC,CAAC,CAAC;IAEF,wEAAwE;IACxE,8CAA8C;IAC9C,MAAM,MAAM,GAAG,IAAA,gBAAI,EAAC,WAAW,EAAE,oBAAoB,CAAC,CAAC;IAEvD,OAAO;QACL,GAAG,MAAM;QACT,oBAAoB;QACpB,WAAW;QACX,OAAO;QACP,UAAU;QACV,OAAO;KACR,CAAC;AACJ,CAAC;AA/DD,kDA+DC","sourcesContent":["import {\n BrokenCircuitError,\n CircuitState,\n ConsecutiveBreaker,\n ExponentialBackoff,\n circuitBreaker,\n handleAll,\n handleWhen,\n retry,\n wrap,\n} from 'cockatiel';\nimport type {\n CircuitBreakerPolicy,\n IPolicy,\n Policy,\n RetryPolicy,\n} from 'cockatiel';\n\nexport { CircuitState, BrokenCircuitError, handleAll, handleWhen };\n\n/**\n * The options for `createServicePolicy`.\n */\nexport type CreateServicePolicyOptions = {\n /**\n * The length of time (in milliseconds) to pause retries of the action after\n * the number of failures reaches `maxConsecutiveFailures`.\n */\n circuitBreakDuration?: number;\n /**\n * The length of time (in milliseconds) that governs when the service is\n * regarded as degraded (affecting when `onDegraded` is called).\n */\n degradedThreshold?: number;\n /**\n * The maximum number of times that the service is allowed to fail before\n * pausing further retries.\n */\n maxConsecutiveFailures?: number;\n /**\n * The maximum number of times that a failing service should be re-invoked\n * before giving up.\n */\n maxRetries?: number;\n /**\n * The policy used to control when the service should be retried based on\n * either the result of the service or an error that it throws. For instance,\n * you could use this to retry only certain errors. See `handleWhen` and\n * friends from Cockatiel for more.\n */\n retryFilterPolicy?: Policy;\n};\n\n/**\n * The service policy object.\n */\nexport type ServicePolicy = IPolicy & {\n /**\n * The Cockatiel circuit breaker policy that the service policy uses\n * internally.\n */\n circuitBreakerPolicy: CircuitBreakerPolicy;\n /**\n * The Cockatiel retry policy that the service policy uses internally.\n */\n retryPolicy: RetryPolicy;\n /**\n * A function which is called when the number of times that the service fails\n * in a row meets the set maximum number of consecutive failures.\n */\n onBreak: CircuitBreakerPolicy['onBreak'];\n /**\n * A function which is called in two circumstances: 1) when the service\n * succeeds before the maximum number of consecutive failures is reached, but\n * takes more time than the `degradedThreshold` to run, or 2) if the service\n * never succeeds before the retry policy gives up and before the maximum\n * number of consecutive failures has been reached.\n */\n onDegraded: (fn: () => void) => void;\n /**\n * A function which will be called by the retry policy each time the service\n * fails and the policy kicks off a timer to re-run the service. This is\n * primarily useful in tests where we are mocking timers.\n */\n onRetry: RetryPolicy['onRetry'];\n};\n\n/**\n * The maximum number of times that a failing service should be re-run before\n * giving up.\n */\nexport const DEFAULT_MAX_RETRIES = 3;\n\n/**\n * The maximum number of times that the service is allowed to fail before\n * pausing further retries. This is set to a value such that if given a\n * service that continually fails, the policy needs to be executed 3 times\n * before further retries are paused.\n */\nexport const DEFAULT_MAX_CONSECUTIVE_FAILURES = (1 + DEFAULT_MAX_RETRIES) * 3;\n\n/**\n * The default length of time (in milliseconds) to temporarily pause retries of\n * the service after enough consecutive failures.\n */\nexport const DEFAULT_CIRCUIT_BREAK_DURATION = 30 * 60 * 1000;\n\n/**\n * The default length of time (in milliseconds) that governs when the service is\n * regarded as degraded (affecting when `onDegraded` is called).\n */\nexport const DEFAULT_DEGRADED_THRESHOLD = 5_000;\n\n/**\n * Constructs an object exposing an `execute` method which, given a function —\n * hereafter called the \"service\" — will retry that service with ever increasing\n * delays until it succeeds. If the policy detects too many consecutive\n * failures, it will block further retries until a designated time period has\n * passed; this particular behavior is primarily designed for services that wrap\n * API calls so as not to make needless HTTP requests when the API is down and\n * to be able to recover when the API comes back up. In addition, hooks allow\n * for responding to certain events, one of which can be used to detect when an\n * HTTP request is performing slowly.\n *\n * Internally, this function makes use of the retry and circuit breaker policies\n * from the [Cockatiel](https://www.npmjs.com/package/cockatiel) library; see\n * there for more.\n *\n * @param options - The options to this function.\n * @param options.maxRetries - The maximum number of times that a failing\n * service should be re-invoked before giving up. Defaults to 3.\n * @param options.retryFilterPolicy - The policy used to control when the\n * service should be retried based on either the result of the servce or an\n * error that it throws. For instance, you could use this to retry only certain\n * errors. See `handleWhen` and friends from Cockatiel for more.\n * @param options.maxConsecutiveFailures - The maximum number of times that the\n * service is allowed to fail before pausing further retries. Defaults to 12.\n * @param options.circuitBreakDuration - The length of time (in milliseconds) to\n * pause retries of the action after the number of failures reaches\n * `maxConsecutiveFailures`.\n * @param options.degradedThreshold - The length of time (in milliseconds) that\n * governs when the service is regarded as degraded (affecting when `onDegraded`\n * is called). Defaults to 5 seconds.\n * @returns The service policy.\n * @example\n * This function is designed to be used in the context of a service class like\n * this:\n * ``` ts\n * class Service {\n * constructor() {\n * this.#policy = createServicePolicy({\n * maxRetries: 3,\n * retryFilterPolicy: handleWhen((error) => {\n * return error.message.includes('oops');\n * }),\n * maxConsecutiveFailures: 3,\n * circuitBreakDuration: 5000,\n * degradedThreshold: 2000,\n * onBreak: () => {\n * console.log('Circuit broke');\n * },\n * onDegraded: () => {\n * console.log('Service is degraded');\n * },\n * });\n * }\n *\n * async fetch() {\n * return await this.#policy.execute(async () => {\n * const response = await fetch('https://some/url');\n * return await response.json();\n * });\n * }\n * }\n * ```\n */\nexport function createServicePolicy({\n maxRetries = DEFAULT_MAX_RETRIES,\n retryFilterPolicy = handleAll,\n maxConsecutiveFailures = DEFAULT_MAX_CONSECUTIVE_FAILURES,\n circuitBreakDuration = DEFAULT_CIRCUIT_BREAK_DURATION,\n degradedThreshold = DEFAULT_DEGRADED_THRESHOLD,\n}: CreateServicePolicyOptions = {}): ServicePolicy {\n const retryPolicy = retry(retryFilterPolicy, {\n // Note that although the option here is called \"max attempts\", it's really\n // maximum number of *retries* (attempts past the initial attempt).\n maxAttempts: maxRetries,\n // Retries of the service will be executed following ever increasing delays,\n // determined by a backoff formula.\n backoff: new ExponentialBackoff(),\n });\n const onRetry = retryPolicy.onRetry.bind(retryPolicy);\n\n const circuitBreakerPolicy = circuitBreaker(handleAll, {\n // While the circuit is open, any additional invocations of the service\n // passed to the policy (either via automatic retries or by manually\n // executing the policy again) will result in a BrokenCircuitError. This\n // will remain the case until `circuitBreakDuration` passes, after which the\n // service will be allowed to run again. If the service succeeds, the\n // circuit will close, otherwise it will remain open.\n halfOpenAfter: circuitBreakDuration,\n breaker: new ConsecutiveBreaker(maxConsecutiveFailures),\n });\n const onBreak = circuitBreakerPolicy.onBreak.bind(circuitBreakerPolicy);\n\n const onDegradedListeners: (() => void)[] = [];\n retryPolicy.onGiveUp(() => {\n if (circuitBreakerPolicy.state === CircuitState.Closed) {\n for (const listener of onDegradedListeners) {\n listener();\n }\n }\n });\n retryPolicy.onSuccess(({ duration }) => {\n if (\n circuitBreakerPolicy.state === CircuitState.Closed &&\n duration > degradedThreshold\n ) {\n for (const listener of onDegradedListeners) {\n listener();\n }\n }\n });\n const onDegraded = (listener: () => void) => {\n onDegradedListeners.push(listener);\n };\n\n // Every time the retry policy makes an attempt, it executes the circuit\n // breaker policy, which executes the service.\n const policy = wrap(retryPolicy, circuitBreakerPolicy);\n\n return {\n ...policy,\n circuitBreakerPolicy,\n retryPolicy,\n onBreak,\n onDegraded,\n onRetry,\n };\n}\n"]}
@@ -1,5 +1,71 @@
1
- import type { IPolicy, Policy } from "cockatiel";
2
- export type { IPolicy as IServicePolicy };
1
+ import { BrokenCircuitError, CircuitState, handleAll, handleWhen } from "cockatiel";
2
+ import type { CircuitBreakerPolicy, IPolicy, Policy, RetryPolicy } from "cockatiel";
3
+ export { CircuitState, BrokenCircuitError, handleAll, handleWhen };
4
+ /**
5
+ * The options for `createServicePolicy`.
6
+ */
7
+ export type CreateServicePolicyOptions = {
8
+ /**
9
+ * The length of time (in milliseconds) to pause retries of the action after
10
+ * the number of failures reaches `maxConsecutiveFailures`.
11
+ */
12
+ circuitBreakDuration?: number;
13
+ /**
14
+ * The length of time (in milliseconds) that governs when the service is
15
+ * regarded as degraded (affecting when `onDegraded` is called).
16
+ */
17
+ degradedThreshold?: number;
18
+ /**
19
+ * The maximum number of times that the service is allowed to fail before
20
+ * pausing further retries.
21
+ */
22
+ maxConsecutiveFailures?: number;
23
+ /**
24
+ * The maximum number of times that a failing service should be re-invoked
25
+ * before giving up.
26
+ */
27
+ maxRetries?: number;
28
+ /**
29
+ * The policy used to control when the service should be retried based on
30
+ * either the result of the service or an error that it throws. For instance,
31
+ * you could use this to retry only certain errors. See `handleWhen` and
32
+ * friends from Cockatiel for more.
33
+ */
34
+ retryFilterPolicy?: Policy;
35
+ };
36
+ /**
37
+ * The service policy object.
38
+ */
39
+ export type ServicePolicy = IPolicy & {
40
+ /**
41
+ * The Cockatiel circuit breaker policy that the service policy uses
42
+ * internally.
43
+ */
44
+ circuitBreakerPolicy: CircuitBreakerPolicy;
45
+ /**
46
+ * The Cockatiel retry policy that the service policy uses internally.
47
+ */
48
+ retryPolicy: RetryPolicy;
49
+ /**
50
+ * A function which is called when the number of times that the service fails
51
+ * in a row meets the set maximum number of consecutive failures.
52
+ */
53
+ onBreak: CircuitBreakerPolicy['onBreak'];
54
+ /**
55
+ * A function which is called in two circumstances: 1) when the service
56
+ * succeeds before the maximum number of consecutive failures is reached, but
57
+ * takes more time than the `degradedThreshold` to run, or 2) if the service
58
+ * never succeeds before the retry policy gives up and before the maximum
59
+ * number of consecutive failures has been reached.
60
+ */
61
+ onDegraded: (fn: () => void) => void;
62
+ /**
63
+ * A function which will be called by the retry policy each time the service
64
+ * fails and the policy kicks off a timer to re-run the service. This is
65
+ * primarily useful in tests where we are mocking timers.
66
+ */
67
+ onRetry: RetryPolicy['onRetry'];
68
+ };
3
69
  /**
4
70
  * The maximum number of times that a failing service should be re-run before
5
71
  * giving up.
@@ -7,7 +73,9 @@ export type { IPolicy as IServicePolicy };
7
73
  export declare const DEFAULT_MAX_RETRIES = 3;
8
74
  /**
9
75
  * The maximum number of times that the service is allowed to fail before
10
- * pausing further retries.
76
+ * pausing further retries. This is set to a value such that if given a
77
+ * service that continually fails, the policy needs to be executed 3 times
78
+ * before further retries are paused.
11
79
  */
12
80
  export declare const DEFAULT_MAX_CONSECUTIVE_FAILURES: number;
13
81
  /**
@@ -50,14 +118,6 @@ export declare const DEFAULT_DEGRADED_THRESHOLD = 5000;
50
118
  * @param options.degradedThreshold - The length of time (in milliseconds) that
51
119
  * governs when the service is regarded as degraded (affecting when `onDegraded`
52
120
  * is called). Defaults to 5 seconds.
53
- * @param options.onBreak - A function which is called when the service fails
54
- * too many times in a row (specifically, more than `maxConsecutiveFailures`).
55
- * @param options.onDegraded - A function which is called when the service
56
- * succeeds before `maxConsecutiveFailures` is reached, but takes more time than
57
- * the `degradedThreshold` to run.
58
- * @param options.onRetry - A function which will be called the moment the
59
- * policy kicks off a timer to re-run the function passed to the policy. This is
60
- * primarily useful in tests where we are mocking timers.
61
121
  * @returns The service policy.
62
122
  * @example
63
123
  * This function is designed to be used in the context of a service class like
@@ -91,14 +151,5 @@ export declare const DEFAULT_DEGRADED_THRESHOLD = 5000;
91
151
  * }
92
152
  * ```
93
153
  */
94
- export declare function createServicePolicy({ maxRetries, retryFilterPolicy, maxConsecutiveFailures, circuitBreakDuration, degradedThreshold, onBreak, onDegraded, onRetry, }?: {
95
- maxRetries?: number;
96
- retryFilterPolicy?: Policy;
97
- maxConsecutiveFailures?: number;
98
- circuitBreakDuration?: number;
99
- degradedThreshold?: number;
100
- onBreak?: () => void;
101
- onDegraded?: () => void;
102
- onRetry?: () => void;
103
- }): IPolicy;
154
+ export declare function createServicePolicy({ maxRetries, retryFilterPolicy, maxConsecutiveFailures, circuitBreakDuration, degradedThreshold, }?: CreateServicePolicyOptions): ServicePolicy;
104
155
  //# sourceMappingURL=create-service-policy.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"create-service-policy.d.cts","sourceRoot":"","sources":["../src/create-service-policy.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,kBAAkB;AAEjD,YAAY,EAAE,OAAO,IAAI,cAAc,EAAE,CAAC;AAE1C;;;GAGG;AACH,eAAO,MAAM,mBAAmB,IAAI,CAAC;AAErC;;;GAGG;AACH,eAAO,MAAM,gCAAgC,QAAgC,CAAC;AAE9E;;;GAGG;AACH,eAAO,MAAM,8BAA8B,QAAiB,CAAC;AAE7D;;;GAGG;AACH,eAAO,MAAM,0BAA0B,OAAQ,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsEG;AACH,wBAAgB,mBAAmB,CAAC,EAClC,UAAgC,EAChC,iBAA6B,EAC7B,sBAAyD,EACzD,oBAAqD,EACrD,iBAA8C,EAC9C,OAEC,EACD,UAEC,EACD,OAEC,GACF,GAAE;IACD,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,IAAI,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;CACjB,GAAG,OAAO,CA0Df"}
1
+ {"version":3,"file":"create-service-policy.d.cts","sourceRoot":"","sources":["../src/create-service-policy.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,YAAY,EAIZ,SAAS,EACT,UAAU,EAGX,kBAAkB;AACnB,OAAO,KAAK,EACV,oBAAoB,EACpB,OAAO,EACP,MAAM,EACN,WAAW,EACZ,kBAAkB;AAEnB,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC;AAEnE;;GAEG;AACH,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;OAGG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B;;;OAGG;IACH,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;;;OAKG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG;IACpC;;;OAGG;IACH,oBAAoB,EAAE,oBAAoB,CAAC;IAC3C;;OAEG;IACH,WAAW,EAAE,WAAW,CAAC;IACzB;;;OAGG;IACH,OAAO,EAAE,oBAAoB,CAAC,SAAS,CAAC,CAAC;IACzC;;;;;;OAMG;IACH,UAAU,EAAE,CAAC,EAAE,EAAE,MAAM,IAAI,KAAK,IAAI,CAAC;IACrC;;;;OAIG;IACH,OAAO,EAAE,WAAW,CAAC,SAAS,CAAC,CAAC;CACjC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,mBAAmB,IAAI,CAAC;AAErC;;;;;GAKG;AACH,eAAO,MAAM,gCAAgC,QAAgC,CAAC;AAE9E;;;GAGG;AACH,eAAO,MAAM,8BAA8B,QAAiB,CAAC;AAE7D;;;GAGG;AACH,eAAO,MAAM,0BAA0B,OAAQ,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8DG;AACH,wBAAgB,mBAAmB,CAAC,EAClC,UAAgC,EAChC,iBAA6B,EAC7B,sBAAyD,EACzD,oBAAqD,EACrD,iBAA8C,GAC/C,GAAE,0BAA+B,GAAG,aAAa,CAyDjD"}
@@ -1,5 +1,71 @@
1
- import type { IPolicy, Policy } from "cockatiel";
2
- export type { IPolicy as IServicePolicy };
1
+ import { BrokenCircuitError, CircuitState, handleAll, handleWhen } from "cockatiel";
2
+ import type { CircuitBreakerPolicy, IPolicy, Policy, RetryPolicy } from "cockatiel";
3
+ export { CircuitState, BrokenCircuitError, handleAll, handleWhen };
4
+ /**
5
+ * The options for `createServicePolicy`.
6
+ */
7
+ export type CreateServicePolicyOptions = {
8
+ /**
9
+ * The length of time (in milliseconds) to pause retries of the action after
10
+ * the number of failures reaches `maxConsecutiveFailures`.
11
+ */
12
+ circuitBreakDuration?: number;
13
+ /**
14
+ * The length of time (in milliseconds) that governs when the service is
15
+ * regarded as degraded (affecting when `onDegraded` is called).
16
+ */
17
+ degradedThreshold?: number;
18
+ /**
19
+ * The maximum number of times that the service is allowed to fail before
20
+ * pausing further retries.
21
+ */
22
+ maxConsecutiveFailures?: number;
23
+ /**
24
+ * The maximum number of times that a failing service should be re-invoked
25
+ * before giving up.
26
+ */
27
+ maxRetries?: number;
28
+ /**
29
+ * The policy used to control when the service should be retried based on
30
+ * either the result of the service or an error that it throws. For instance,
31
+ * you could use this to retry only certain errors. See `handleWhen` and
32
+ * friends from Cockatiel for more.
33
+ */
34
+ retryFilterPolicy?: Policy;
35
+ };
36
+ /**
37
+ * The service policy object.
38
+ */
39
+ export type ServicePolicy = IPolicy & {
40
+ /**
41
+ * The Cockatiel circuit breaker policy that the service policy uses
42
+ * internally.
43
+ */
44
+ circuitBreakerPolicy: CircuitBreakerPolicy;
45
+ /**
46
+ * The Cockatiel retry policy that the service policy uses internally.
47
+ */
48
+ retryPolicy: RetryPolicy;
49
+ /**
50
+ * A function which is called when the number of times that the service fails
51
+ * in a row meets the set maximum number of consecutive failures.
52
+ */
53
+ onBreak: CircuitBreakerPolicy['onBreak'];
54
+ /**
55
+ * A function which is called in two circumstances: 1) when the service
56
+ * succeeds before the maximum number of consecutive failures is reached, but
57
+ * takes more time than the `degradedThreshold` to run, or 2) if the service
58
+ * never succeeds before the retry policy gives up and before the maximum
59
+ * number of consecutive failures has been reached.
60
+ */
61
+ onDegraded: (fn: () => void) => void;
62
+ /**
63
+ * A function which will be called by the retry policy each time the service
64
+ * fails and the policy kicks off a timer to re-run the service. This is
65
+ * primarily useful in tests where we are mocking timers.
66
+ */
67
+ onRetry: RetryPolicy['onRetry'];
68
+ };
3
69
  /**
4
70
  * The maximum number of times that a failing service should be re-run before
5
71
  * giving up.
@@ -7,7 +73,9 @@ export type { IPolicy as IServicePolicy };
7
73
  export declare const DEFAULT_MAX_RETRIES = 3;
8
74
  /**
9
75
  * The maximum number of times that the service is allowed to fail before
10
- * pausing further retries.
76
+ * pausing further retries. This is set to a value such that if given a
77
+ * service that continually fails, the policy needs to be executed 3 times
78
+ * before further retries are paused.
11
79
  */
12
80
  export declare const DEFAULT_MAX_CONSECUTIVE_FAILURES: number;
13
81
  /**
@@ -50,14 +118,6 @@ export declare const DEFAULT_DEGRADED_THRESHOLD = 5000;
50
118
  * @param options.degradedThreshold - The length of time (in milliseconds) that
51
119
  * governs when the service is regarded as degraded (affecting when `onDegraded`
52
120
  * is called). Defaults to 5 seconds.
53
- * @param options.onBreak - A function which is called when the service fails
54
- * too many times in a row (specifically, more than `maxConsecutiveFailures`).
55
- * @param options.onDegraded - A function which is called when the service
56
- * succeeds before `maxConsecutiveFailures` is reached, but takes more time than
57
- * the `degradedThreshold` to run.
58
- * @param options.onRetry - A function which will be called the moment the
59
- * policy kicks off a timer to re-run the function passed to the policy. This is
60
- * primarily useful in tests where we are mocking timers.
61
121
  * @returns The service policy.
62
122
  * @example
63
123
  * This function is designed to be used in the context of a service class like
@@ -91,14 +151,5 @@ export declare const DEFAULT_DEGRADED_THRESHOLD = 5000;
91
151
  * }
92
152
  * ```
93
153
  */
94
- export declare function createServicePolicy({ maxRetries, retryFilterPolicy, maxConsecutiveFailures, circuitBreakDuration, degradedThreshold, onBreak, onDegraded, onRetry, }?: {
95
- maxRetries?: number;
96
- retryFilterPolicy?: Policy;
97
- maxConsecutiveFailures?: number;
98
- circuitBreakDuration?: number;
99
- degradedThreshold?: number;
100
- onBreak?: () => void;
101
- onDegraded?: () => void;
102
- onRetry?: () => void;
103
- }): IPolicy;
154
+ export declare function createServicePolicy({ maxRetries, retryFilterPolicy, maxConsecutiveFailures, circuitBreakDuration, degradedThreshold, }?: CreateServicePolicyOptions): ServicePolicy;
104
155
  //# sourceMappingURL=create-service-policy.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"create-service-policy.d.mts","sourceRoot":"","sources":["../src/create-service-policy.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,kBAAkB;AAEjD,YAAY,EAAE,OAAO,IAAI,cAAc,EAAE,CAAC;AAE1C;;;GAGG;AACH,eAAO,MAAM,mBAAmB,IAAI,CAAC;AAErC;;;GAGG;AACH,eAAO,MAAM,gCAAgC,QAAgC,CAAC;AAE9E;;;GAGG;AACH,eAAO,MAAM,8BAA8B,QAAiB,CAAC;AAE7D;;;GAGG;AACH,eAAO,MAAM,0BAA0B,OAAQ,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsEG;AACH,wBAAgB,mBAAmB,CAAC,EAClC,UAAgC,EAChC,iBAA6B,EAC7B,sBAAyD,EACzD,oBAAqD,EACrD,iBAA8C,EAC9C,OAEC,EACD,UAEC,EACD,OAEC,GACF,GAAE;IACD,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,IAAI,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;CACjB,GAAG,OAAO,CA0Df"}
1
+ {"version":3,"file":"create-service-policy.d.mts","sourceRoot":"","sources":["../src/create-service-policy.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,YAAY,EAIZ,SAAS,EACT,UAAU,EAGX,kBAAkB;AACnB,OAAO,KAAK,EACV,oBAAoB,EACpB,OAAO,EACP,MAAM,EACN,WAAW,EACZ,kBAAkB;AAEnB,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC;AAEnE;;GAEG;AACH,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;OAGG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B;;;OAGG;IACH,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;;;OAKG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG;IACpC;;;OAGG;IACH,oBAAoB,EAAE,oBAAoB,CAAC;IAC3C;;OAEG;IACH,WAAW,EAAE,WAAW,CAAC;IACzB;;;OAGG;IACH,OAAO,EAAE,oBAAoB,CAAC,SAAS,CAAC,CAAC;IACzC;;;;;;OAMG;IACH,UAAU,EAAE,CAAC,EAAE,EAAE,MAAM,IAAI,KAAK,IAAI,CAAC;IACrC;;;;OAIG;IACH,OAAO,EAAE,WAAW,CAAC,SAAS,CAAC,CAAC;CACjC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,mBAAmB,IAAI,CAAC;AAErC;;;;;GAKG;AACH,eAAO,MAAM,gCAAgC,QAAgC,CAAC;AAE9E;;;GAGG;AACH,eAAO,MAAM,8BAA8B,QAAiB,CAAC;AAE7D;;;GAGG;AACH,eAAO,MAAM,0BAA0B,OAAQ,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8DG;AACH,wBAAgB,mBAAmB,CAAC,EAClC,UAAgC,EAChC,iBAA6B,EAC7B,sBAAyD,EACzD,oBAAqD,EACrD,iBAA8C,GAC/C,GAAE,0BAA+B,GAAG,aAAa,CAyDjD"}
@@ -1,4 +1,5 @@
1
- import { circuitBreaker, ConsecutiveBreaker, ExponentialBackoff, handleAll, retry, wrap, CircuitState } from "cockatiel";
1
+ import { BrokenCircuitError, CircuitState, ConsecutiveBreaker, ExponentialBackoff, circuitBreaker, handleAll, handleWhen, retry, wrap } from "cockatiel";
2
+ export { CircuitState, BrokenCircuitError, handleAll, handleWhen };
2
3
  /**
3
4
  * The maximum number of times that a failing service should be re-run before
4
5
  * giving up.
@@ -6,7 +7,9 @@ import { circuitBreaker, ConsecutiveBreaker, ExponentialBackoff, handleAll, retr
6
7
  export const DEFAULT_MAX_RETRIES = 3;
7
8
  /**
8
9
  * The maximum number of times that the service is allowed to fail before
9
- * pausing further retries.
10
+ * pausing further retries. This is set to a value such that if given a
11
+ * service that continually fails, the policy needs to be executed 3 times
12
+ * before further retries are paused.
10
13
  */
11
14
  export const DEFAULT_MAX_CONSECUTIVE_FAILURES = (1 + DEFAULT_MAX_RETRIES) * 3;
12
15
  /**
@@ -49,14 +52,6 @@ export const DEFAULT_DEGRADED_THRESHOLD = 5000;
49
52
  * @param options.degradedThreshold - The length of time (in milliseconds) that
50
53
  * governs when the service is regarded as degraded (affecting when `onDegraded`
51
54
  * is called). Defaults to 5 seconds.
52
- * @param options.onBreak - A function which is called when the service fails
53
- * too many times in a row (specifically, more than `maxConsecutiveFailures`).
54
- * @param options.onDegraded - A function which is called when the service
55
- * succeeds before `maxConsecutiveFailures` is reached, but takes more time than
56
- * the `degradedThreshold` to run.
57
- * @param options.onRetry - A function which will be called the moment the
58
- * policy kicks off a timer to re-run the function passed to the policy. This is
59
- * primarily useful in tests where we are mocking timers.
60
55
  * @returns The service policy.
61
56
  * @example
62
57
  * This function is designed to be used in the context of a service class like
@@ -90,13 +85,7 @@ export const DEFAULT_DEGRADED_THRESHOLD = 5000;
90
85
  * }
91
86
  * ```
92
87
  */
93
- export function createServicePolicy({ maxRetries = DEFAULT_MAX_RETRIES, retryFilterPolicy = handleAll, maxConsecutiveFailures = DEFAULT_MAX_CONSECUTIVE_FAILURES, circuitBreakDuration = DEFAULT_CIRCUIT_BREAK_DURATION, degradedThreshold = DEFAULT_DEGRADED_THRESHOLD, onBreak = () => {
94
- // do nothing
95
- }, onDegraded = () => {
96
- // do nothing
97
- }, onRetry = () => {
98
- // do nothing
99
- }, } = {}) {
88
+ export function createServicePolicy({ maxRetries = DEFAULT_MAX_RETRIES, retryFilterPolicy = handleAll, maxConsecutiveFailures = DEFAULT_MAX_CONSECUTIVE_FAILURES, circuitBreakDuration = DEFAULT_CIRCUIT_BREAK_DURATION, degradedThreshold = DEFAULT_DEGRADED_THRESHOLD, } = {}) {
100
89
  const retryPolicy = retry(retryFilterPolicy, {
101
90
  // Note that although the option here is called "max attempts", it's really
102
91
  // maximum number of *retries* (attempts past the initial attempt).
@@ -105,6 +94,7 @@ export function createServicePolicy({ maxRetries = DEFAULT_MAX_RETRIES, retryFil
105
94
  // determined by a backoff formula.
106
95
  backoff: new ExponentialBackoff(),
107
96
  });
97
+ const onRetry = retryPolicy.onRetry.bind(retryPolicy);
108
98
  const circuitBreakerPolicy = circuitBreaker(handleAll, {
109
99
  // While the circuit is open, any additional invocations of the service
110
100
  // passed to the policy (either via automatic retries or by manually
@@ -115,37 +105,36 @@ export function createServicePolicy({ maxRetries = DEFAULT_MAX_RETRIES, retryFil
115
105
  halfOpenAfter: circuitBreakDuration,
116
106
  breaker: new ConsecutiveBreaker(maxConsecutiveFailures),
117
107
  });
118
- // The `onBreak` callback will be called if the service consistently throws
119
- // for as many times as exceeds the maximum consecutive number of failures.
120
- // Combined with the retry policy, this can happen if:
121
- // - `maxConsecutiveFailures` < the default max retries (3) and the policy is
122
- // executed once
123
- // - `maxConsecutiveFailures` >= the default max retries (3) but the policy is
124
- // executed multiple times, enough for the total number of retries to exceed
125
- // `maxConsecutiveFailures`
126
- circuitBreakerPolicy.onBreak(onBreak);
127
- // The `onRetryPolicy` callback will be called each time the service is
128
- // invoked (including retries).
129
- retryPolicy.onRetry(onRetry);
108
+ const onBreak = circuitBreakerPolicy.onBreak.bind(circuitBreakerPolicy);
109
+ const onDegradedListeners = [];
130
110
  retryPolicy.onGiveUp(() => {
131
111
  if (circuitBreakerPolicy.state === CircuitState.Closed) {
132
- // The `onDegraded` callback will be called if the number of retries is
133
- // exceeded and the maximum number of consecutive failures has not been
134
- // reached yet (whether the policy is called once or multiple times).
135
- onDegraded();
112
+ for (const listener of onDegradedListeners) {
113
+ listener();
114
+ }
136
115
  }
137
116
  });
138
117
  retryPolicy.onSuccess(({ duration }) => {
139
118
  if (circuitBreakerPolicy.state === CircuitState.Closed &&
140
119
  duration > degradedThreshold) {
141
- // The `onDegraded` callback will also be called if the service does not
142
- // throw, but the time it takes for the service to run exceeds the
143
- // `degradedThreshold`.
144
- onDegraded();
120
+ for (const listener of onDegradedListeners) {
121
+ listener();
122
+ }
145
123
  }
146
124
  });
147
- // The retry policy really retries the circuit breaker policy, which invokes
148
- // the service.
149
- return wrap(retryPolicy, circuitBreakerPolicy);
125
+ const onDegraded = (listener) => {
126
+ onDegradedListeners.push(listener);
127
+ };
128
+ // Every time the retry policy makes an attempt, it executes the circuit
129
+ // breaker policy, which executes the service.
130
+ const policy = wrap(retryPolicy, circuitBreakerPolicy);
131
+ return {
132
+ ...policy,
133
+ circuitBreakerPolicy,
134
+ retryPolicy,
135
+ onBreak,
136
+ onDegraded,
137
+ onRetry,
138
+ };
150
139
  }
151
140
  //# sourceMappingURL=create-service-policy.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"create-service-policy.mjs","sourceRoot":"","sources":["../src/create-service-policy.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,kBAAkB,EAClB,kBAAkB,EAClB,SAAS,EACT,KAAK,EACL,IAAI,EACJ,YAAY,EACb,kBAAkB;AAKnB;;;GAGG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,CAAC;AAErC;;;GAGG;AACH,MAAM,CAAC,MAAM,gCAAgC,GAAG,CAAC,CAAC,GAAG,mBAAmB,CAAC,GAAG,CAAC,CAAC;AAE9E;;;GAGG;AACH,MAAM,CAAC,MAAM,8BAA8B,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAE7D;;;GAGG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,IAAK,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsEG;AACH,MAAM,UAAU,mBAAmB,CAAC,EAClC,UAAU,GAAG,mBAAmB,EAChC,iBAAiB,GAAG,SAAS,EAC7B,sBAAsB,GAAG,gCAAgC,EACzD,oBAAoB,GAAG,8BAA8B,EACrD,iBAAiB,GAAG,0BAA0B,EAC9C,OAAO,GAAG,GAAG,EAAE;IACb,aAAa;AACf,CAAC,EACD,UAAU,GAAG,GAAG,EAAE;IAChB,aAAa;AACf,CAAC,EACD,OAAO,GAAG,GAAG,EAAE;IACb,aAAa;AACf,CAAC,MAUC,EAAE;IACJ,MAAM,WAAW,GAAG,KAAK,CAAC,iBAAiB,EAAE;QAC3C,2EAA2E;QAC3E,mEAAmE;QACnE,WAAW,EAAE,UAAU;QACvB,4EAA4E;QAC5E,mCAAmC;QACnC,OAAO,EAAE,IAAI,kBAAkB,EAAE;KAClC,CAAC,CAAC;IAEH,MAAM,oBAAoB,GAAG,cAAc,CAAC,SAAS,EAAE;QACrD,uEAAuE;QACvE,oEAAoE;QACpE,wEAAwE;QACxE,4EAA4E;QAC5E,qEAAqE;QACrE,qDAAqD;QACrD,aAAa,EAAE,oBAAoB;QACnC,OAAO,EAAE,IAAI,kBAAkB,CAAC,sBAAsB,CAAC;KACxD,CAAC,CAAC;IAEH,2EAA2E;IAC3E,2EAA2E;IAC3E,sDAAsD;IACtD,6EAA6E;IAC7E,gBAAgB;IAChB,8EAA8E;IAC9E,8EAA8E;IAC9E,6BAA6B;IAC7B,oBAAoB,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAEtC,uEAAuE;IACvE,+BAA+B;IAC/B,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAE7B,WAAW,CAAC,QAAQ,CAAC,GAAG,EAAE;QACxB,IAAI,oBAAoB,CAAC,KAAK,KAAK,YAAY,CAAC,MAAM,EAAE;YACtD,uEAAuE;YACvE,uEAAuE;YACvE,qEAAqE;YACrE,UAAU,EAAE,CAAC;SACd;IACH,CAAC,CAAC,CAAC;IACH,WAAW,CAAC,SAAS,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE;QACrC,IACE,oBAAoB,CAAC,KAAK,KAAK,YAAY,CAAC,MAAM;YAClD,QAAQ,GAAG,iBAAiB,EAC5B;YACA,wEAAwE;YACxE,kEAAkE;YAClE,uBAAuB;YACvB,UAAU,EAAE,CAAC;SACd;IACH,CAAC,CAAC,CAAC;IAEH,4EAA4E;IAC5E,eAAe;IACf,OAAO,IAAI,CAAC,WAAW,EAAE,oBAAoB,CAAC,CAAC;AACjD,CAAC","sourcesContent":["import {\n circuitBreaker,\n ConsecutiveBreaker,\n ExponentialBackoff,\n handleAll,\n retry,\n wrap,\n CircuitState,\n} from 'cockatiel';\nimport type { IPolicy, Policy } from 'cockatiel';\n\nexport type { IPolicy as IServicePolicy };\n\n/**\n * The maximum number of times that a failing service should be re-run before\n * giving up.\n */\nexport const DEFAULT_MAX_RETRIES = 3;\n\n/**\n * The maximum number of times that the service is allowed to fail before\n * pausing further retries.\n */\nexport const DEFAULT_MAX_CONSECUTIVE_FAILURES = (1 + DEFAULT_MAX_RETRIES) * 3;\n\n/**\n * The default length of time (in milliseconds) to temporarily pause retries of\n * the service after enough consecutive failures.\n */\nexport const DEFAULT_CIRCUIT_BREAK_DURATION = 30 * 60 * 1000;\n\n/**\n * The default length of time (in milliseconds) that governs when the service is\n * regarded as degraded (affecting when `onDegraded` is called).\n */\nexport const DEFAULT_DEGRADED_THRESHOLD = 5_000;\n\n/**\n * Constructs an object exposing an `execute` method which, given a function —\n * hereafter called the \"service\" — will retry that service with ever increasing\n * delays until it succeeds. If the policy detects too many consecutive\n * failures, it will block further retries until a designated time period has\n * passed; this particular behavior is primarily designed for services that wrap\n * API calls so as not to make needless HTTP requests when the API is down and\n * to be able to recover when the API comes back up. In addition, hooks allow\n * for responding to certain events, one of which can be used to detect when an\n * HTTP request is performing slowly.\n *\n * Internally, this function makes use of the retry and circuit breaker policies\n * from the [Cockatiel](https://www.npmjs.com/package/cockatiel) library; see\n * there for more.\n *\n * @param options - The options to this function.\n * @param options.maxRetries - The maximum number of times that a failing\n * service should be re-invoked before giving up. Defaults to 3.\n * @param options.retryFilterPolicy - The policy used to control when the\n * service should be retried based on either the result of the servce or an\n * error that it throws. For instance, you could use this to retry only certain\n * errors. See `handleWhen` and friends from Cockatiel for more.\n * @param options.maxConsecutiveFailures - The maximum number of times that the\n * service is allowed to fail before pausing further retries. Defaults to 12.\n * @param options.circuitBreakDuration - The length of time (in milliseconds) to\n * pause retries of the action after the number of failures reaches\n * `maxConsecutiveFailures`.\n * @param options.degradedThreshold - The length of time (in milliseconds) that\n * governs when the service is regarded as degraded (affecting when `onDegraded`\n * is called). Defaults to 5 seconds.\n * @param options.onBreak - A function which is called when the service fails\n * too many times in a row (specifically, more than `maxConsecutiveFailures`).\n * @param options.onDegraded - A function which is called when the service\n * succeeds before `maxConsecutiveFailures` is reached, but takes more time than\n * the `degradedThreshold` to run.\n * @param options.onRetry - A function which will be called the moment the\n * policy kicks off a timer to re-run the function passed to the policy. This is\n * primarily useful in tests where we are mocking timers.\n * @returns The service policy.\n * @example\n * This function is designed to be used in the context of a service class like\n * this:\n * ``` ts\n * class Service {\n * constructor() {\n * this.#policy = createServicePolicy({\n * maxRetries: 3,\n * retryFilterPolicy: handleWhen((error) => {\n * return error.message.includes('oops');\n * }),\n * maxConsecutiveFailures: 3,\n * circuitBreakDuration: 5000,\n * degradedThreshold: 2000,\n * onBreak: () => {\n * console.log('Circuit broke');\n * },\n * onDegraded: () => {\n * console.log('Service is degraded');\n * },\n * });\n * }\n *\n * async fetch() {\n * return await this.#policy.execute(async () => {\n * const response = await fetch('https://some/url');\n * return await response.json();\n * });\n * }\n * }\n * ```\n */\nexport function createServicePolicy({\n maxRetries = DEFAULT_MAX_RETRIES,\n retryFilterPolicy = handleAll,\n maxConsecutiveFailures = DEFAULT_MAX_CONSECUTIVE_FAILURES,\n circuitBreakDuration = DEFAULT_CIRCUIT_BREAK_DURATION,\n degradedThreshold = DEFAULT_DEGRADED_THRESHOLD,\n onBreak = () => {\n // do nothing\n },\n onDegraded = () => {\n // do nothing\n },\n onRetry = () => {\n // do nothing\n },\n}: {\n maxRetries?: number;\n retryFilterPolicy?: Policy;\n maxConsecutiveFailures?: number;\n circuitBreakDuration?: number;\n degradedThreshold?: number;\n onBreak?: () => void;\n onDegraded?: () => void;\n onRetry?: () => void;\n} = {}): IPolicy {\n const retryPolicy = retry(retryFilterPolicy, {\n // Note that although the option here is called \"max attempts\", it's really\n // maximum number of *retries* (attempts past the initial attempt).\n maxAttempts: maxRetries,\n // Retries of the service will be executed following ever increasing delays,\n // determined by a backoff formula.\n backoff: new ExponentialBackoff(),\n });\n\n const circuitBreakerPolicy = circuitBreaker(handleAll, {\n // While the circuit is open, any additional invocations of the service\n // passed to the policy (either via automatic retries or by manually\n // executing the policy again) will result in a BrokenCircuitError. This\n // will remain the case until `circuitBreakDuration` passes, after which the\n // service will be allowed to run again. If the service succeeds, the\n // circuit will close, otherwise it will remain open.\n halfOpenAfter: circuitBreakDuration,\n breaker: new ConsecutiveBreaker(maxConsecutiveFailures),\n });\n\n // The `onBreak` callback will be called if the service consistently throws\n // for as many times as exceeds the maximum consecutive number of failures.\n // Combined with the retry policy, this can happen if:\n // - `maxConsecutiveFailures` < the default max retries (3) and the policy is\n // executed once\n // - `maxConsecutiveFailures` >= the default max retries (3) but the policy is\n // executed multiple times, enough for the total number of retries to exceed\n // `maxConsecutiveFailures`\n circuitBreakerPolicy.onBreak(onBreak);\n\n // The `onRetryPolicy` callback will be called each time the service is\n // invoked (including retries).\n retryPolicy.onRetry(onRetry);\n\n retryPolicy.onGiveUp(() => {\n if (circuitBreakerPolicy.state === CircuitState.Closed) {\n // The `onDegraded` callback will be called if the number of retries is\n // exceeded and the maximum number of consecutive failures has not been\n // reached yet (whether the policy is called once or multiple times).\n onDegraded();\n }\n });\n retryPolicy.onSuccess(({ duration }) => {\n if (\n circuitBreakerPolicy.state === CircuitState.Closed &&\n duration > degradedThreshold\n ) {\n // The `onDegraded` callback will also be called if the service does not\n // throw, but the time it takes for the service to run exceeds the\n // `degradedThreshold`.\n onDegraded();\n }\n });\n\n // The retry policy really retries the circuit breaker policy, which invokes\n // the service.\n return wrap(retryPolicy, circuitBreakerPolicy);\n}\n"]}
1
+ {"version":3,"file":"create-service-policy.mjs","sourceRoot":"","sources":["../src/create-service-policy.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,YAAY,EACZ,kBAAkB,EAClB,kBAAkB,EAClB,cAAc,EACd,SAAS,EACT,UAAU,EACV,KAAK,EACL,IAAI,EACL,kBAAkB;AAQnB,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC;AAqEnE;;;GAGG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,CAAC;AAErC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,gCAAgC,GAAG,CAAC,CAAC,GAAG,mBAAmB,CAAC,GAAG,CAAC,CAAC;AAE9E;;;GAGG;AACH,MAAM,CAAC,MAAM,8BAA8B,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAE7D;;;GAGG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,IAAK,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8DG;AACH,MAAM,UAAU,mBAAmB,CAAC,EAClC,UAAU,GAAG,mBAAmB,EAChC,iBAAiB,GAAG,SAAS,EAC7B,sBAAsB,GAAG,gCAAgC,EACzD,oBAAoB,GAAG,8BAA8B,EACrD,iBAAiB,GAAG,0BAA0B,MAChB,EAAE;IAChC,MAAM,WAAW,GAAG,KAAK,CAAC,iBAAiB,EAAE;QAC3C,2EAA2E;QAC3E,mEAAmE;QACnE,WAAW,EAAE,UAAU;QACvB,4EAA4E;QAC5E,mCAAmC;QACnC,OAAO,EAAE,IAAI,kBAAkB,EAAE;KAClC,CAAC,CAAC;IACH,MAAM,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IAEtD,MAAM,oBAAoB,GAAG,cAAc,CAAC,SAAS,EAAE;QACrD,uEAAuE;QACvE,oEAAoE;QACpE,wEAAwE;QACxE,4EAA4E;QAC5E,qEAAqE;QACrE,qDAAqD;QACrD,aAAa,EAAE,oBAAoB;QACnC,OAAO,EAAE,IAAI,kBAAkB,CAAC,sBAAsB,CAAC;KACxD,CAAC,CAAC;IACH,MAAM,OAAO,GAAG,oBAAoB,CAAC,OAAO,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;IAExE,MAAM,mBAAmB,GAAmB,EAAE,CAAC;IAC/C,WAAW,CAAC,QAAQ,CAAC,GAAG,EAAE;QACxB,IAAI,oBAAoB,CAAC,KAAK,KAAK,YAAY,CAAC,MAAM,EAAE;YACtD,KAAK,MAAM,QAAQ,IAAI,mBAAmB,EAAE;gBAC1C,QAAQ,EAAE,CAAC;aACZ;SACF;IACH,CAAC,CAAC,CAAC;IACH,WAAW,CAAC,SAAS,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE;QACrC,IACE,oBAAoB,CAAC,KAAK,KAAK,YAAY,CAAC,MAAM;YAClD,QAAQ,GAAG,iBAAiB,EAC5B;YACA,KAAK,MAAM,QAAQ,IAAI,mBAAmB,EAAE;gBAC1C,QAAQ,EAAE,CAAC;aACZ;SACF;IACH,CAAC,CAAC,CAAC;IACH,MAAM,UAAU,GAAG,CAAC,QAAoB,EAAE,EAAE;QAC1C,mBAAmB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACrC,CAAC,CAAC;IAEF,wEAAwE;IACxE,8CAA8C;IAC9C,MAAM,MAAM,GAAG,IAAI,CAAC,WAAW,EAAE,oBAAoB,CAAC,CAAC;IAEvD,OAAO;QACL,GAAG,MAAM;QACT,oBAAoB;QACpB,WAAW;QACX,OAAO;QACP,UAAU;QACV,OAAO;KACR,CAAC;AACJ,CAAC","sourcesContent":["import {\n BrokenCircuitError,\n CircuitState,\n ConsecutiveBreaker,\n ExponentialBackoff,\n circuitBreaker,\n handleAll,\n handleWhen,\n retry,\n wrap,\n} from 'cockatiel';\nimport type {\n CircuitBreakerPolicy,\n IPolicy,\n Policy,\n RetryPolicy,\n} from 'cockatiel';\n\nexport { CircuitState, BrokenCircuitError, handleAll, handleWhen };\n\n/**\n * The options for `createServicePolicy`.\n */\nexport type CreateServicePolicyOptions = {\n /**\n * The length of time (in milliseconds) to pause retries of the action after\n * the number of failures reaches `maxConsecutiveFailures`.\n */\n circuitBreakDuration?: number;\n /**\n * The length of time (in milliseconds) that governs when the service is\n * regarded as degraded (affecting when `onDegraded` is called).\n */\n degradedThreshold?: number;\n /**\n * The maximum number of times that the service is allowed to fail before\n * pausing further retries.\n */\n maxConsecutiveFailures?: number;\n /**\n * The maximum number of times that a failing service should be re-invoked\n * before giving up.\n */\n maxRetries?: number;\n /**\n * The policy used to control when the service should be retried based on\n * either the result of the service or an error that it throws. For instance,\n * you could use this to retry only certain errors. See `handleWhen` and\n * friends from Cockatiel for more.\n */\n retryFilterPolicy?: Policy;\n};\n\n/**\n * The service policy object.\n */\nexport type ServicePolicy = IPolicy & {\n /**\n * The Cockatiel circuit breaker policy that the service policy uses\n * internally.\n */\n circuitBreakerPolicy: CircuitBreakerPolicy;\n /**\n * The Cockatiel retry policy that the service policy uses internally.\n */\n retryPolicy: RetryPolicy;\n /**\n * A function which is called when the number of times that the service fails\n * in a row meets the set maximum number of consecutive failures.\n */\n onBreak: CircuitBreakerPolicy['onBreak'];\n /**\n * A function which is called in two circumstances: 1) when the service\n * succeeds before the maximum number of consecutive failures is reached, but\n * takes more time than the `degradedThreshold` to run, or 2) if the service\n * never succeeds before the retry policy gives up and before the maximum\n * number of consecutive failures has been reached.\n */\n onDegraded: (fn: () => void) => void;\n /**\n * A function which will be called by the retry policy each time the service\n * fails and the policy kicks off a timer to re-run the service. This is\n * primarily useful in tests where we are mocking timers.\n */\n onRetry: RetryPolicy['onRetry'];\n};\n\n/**\n * The maximum number of times that a failing service should be re-run before\n * giving up.\n */\nexport const DEFAULT_MAX_RETRIES = 3;\n\n/**\n * The maximum number of times that the service is allowed to fail before\n * pausing further retries. This is set to a value such that if given a\n * service that continually fails, the policy needs to be executed 3 times\n * before further retries are paused.\n */\nexport const DEFAULT_MAX_CONSECUTIVE_FAILURES = (1 + DEFAULT_MAX_RETRIES) * 3;\n\n/**\n * The default length of time (in milliseconds) to temporarily pause retries of\n * the service after enough consecutive failures.\n */\nexport const DEFAULT_CIRCUIT_BREAK_DURATION = 30 * 60 * 1000;\n\n/**\n * The default length of time (in milliseconds) that governs when the service is\n * regarded as degraded (affecting when `onDegraded` is called).\n */\nexport const DEFAULT_DEGRADED_THRESHOLD = 5_000;\n\n/**\n * Constructs an object exposing an `execute` method which, given a function —\n * hereafter called the \"service\" — will retry that service with ever increasing\n * delays until it succeeds. If the policy detects too many consecutive\n * failures, it will block further retries until a designated time period has\n * passed; this particular behavior is primarily designed for services that wrap\n * API calls so as not to make needless HTTP requests when the API is down and\n * to be able to recover when the API comes back up. In addition, hooks allow\n * for responding to certain events, one of which can be used to detect when an\n * HTTP request is performing slowly.\n *\n * Internally, this function makes use of the retry and circuit breaker policies\n * from the [Cockatiel](https://www.npmjs.com/package/cockatiel) library; see\n * there for more.\n *\n * @param options - The options to this function.\n * @param options.maxRetries - The maximum number of times that a failing\n * service should be re-invoked before giving up. Defaults to 3.\n * @param options.retryFilterPolicy - The policy used to control when the\n * service should be retried based on either the result of the servce or an\n * error that it throws. For instance, you could use this to retry only certain\n * errors. See `handleWhen` and friends from Cockatiel for more.\n * @param options.maxConsecutiveFailures - The maximum number of times that the\n * service is allowed to fail before pausing further retries. Defaults to 12.\n * @param options.circuitBreakDuration - The length of time (in milliseconds) to\n * pause retries of the action after the number of failures reaches\n * `maxConsecutiveFailures`.\n * @param options.degradedThreshold - The length of time (in milliseconds) that\n * governs when the service is regarded as degraded (affecting when `onDegraded`\n * is called). Defaults to 5 seconds.\n * @returns The service policy.\n * @example\n * This function is designed to be used in the context of a service class like\n * this:\n * ``` ts\n * class Service {\n * constructor() {\n * this.#policy = createServicePolicy({\n * maxRetries: 3,\n * retryFilterPolicy: handleWhen((error) => {\n * return error.message.includes('oops');\n * }),\n * maxConsecutiveFailures: 3,\n * circuitBreakDuration: 5000,\n * degradedThreshold: 2000,\n * onBreak: () => {\n * console.log('Circuit broke');\n * },\n * onDegraded: () => {\n * console.log('Service is degraded');\n * },\n * });\n * }\n *\n * async fetch() {\n * return await this.#policy.execute(async () => {\n * const response = await fetch('https://some/url');\n * return await response.json();\n * });\n * }\n * }\n * ```\n */\nexport function createServicePolicy({\n maxRetries = DEFAULT_MAX_RETRIES,\n retryFilterPolicy = handleAll,\n maxConsecutiveFailures = DEFAULT_MAX_CONSECUTIVE_FAILURES,\n circuitBreakDuration = DEFAULT_CIRCUIT_BREAK_DURATION,\n degradedThreshold = DEFAULT_DEGRADED_THRESHOLD,\n}: CreateServicePolicyOptions = {}): ServicePolicy {\n const retryPolicy = retry(retryFilterPolicy, {\n // Note that although the option here is called \"max attempts\", it's really\n // maximum number of *retries* (attempts past the initial attempt).\n maxAttempts: maxRetries,\n // Retries of the service will be executed following ever increasing delays,\n // determined by a backoff formula.\n backoff: new ExponentialBackoff(),\n });\n const onRetry = retryPolicy.onRetry.bind(retryPolicy);\n\n const circuitBreakerPolicy = circuitBreaker(handleAll, {\n // While the circuit is open, any additional invocations of the service\n // passed to the policy (either via automatic retries or by manually\n // executing the policy again) will result in a BrokenCircuitError. This\n // will remain the case until `circuitBreakDuration` passes, after which the\n // service will be allowed to run again. If the service succeeds, the\n // circuit will close, otherwise it will remain open.\n halfOpenAfter: circuitBreakDuration,\n breaker: new ConsecutiveBreaker(maxConsecutiveFailures),\n });\n const onBreak = circuitBreakerPolicy.onBreak.bind(circuitBreakerPolicy);\n\n const onDegradedListeners: (() => void)[] = [];\n retryPolicy.onGiveUp(() => {\n if (circuitBreakerPolicy.state === CircuitState.Closed) {\n for (const listener of onDegradedListeners) {\n listener();\n }\n }\n });\n retryPolicy.onSuccess(({ duration }) => {\n if (\n circuitBreakerPolicy.state === CircuitState.Closed &&\n duration > degradedThreshold\n ) {\n for (const listener of onDegradedListeners) {\n listener();\n }\n }\n });\n const onDegraded = (listener: () => void) => {\n onDegradedListeners.push(listener);\n };\n\n // Every time the retry policy makes an attempt, it executes the circuit\n // breaker policy, which executes the service.\n const policy = wrap(retryPolicy, circuitBreakerPolicy);\n\n return {\n ...policy,\n circuitBreakerPolicy,\n retryPolicy,\n onBreak,\n onDegraded,\n onRetry,\n };\n}\n"]}
package/dist/index.cjs CHANGED
@@ -14,13 +14,17 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.isEqualCaseInsensitive = exports.weiHexToGweiDec = exports.toHex = exports.toChecksumHexAddress = exports.timeoutFetch = exports.successfulFetch = exports.safelyExecuteWithTimeout = exports.safelyExecute = exports.query = exports.normalizeEnsName = exports.isValidHexAddress = exports.isValidJson = exports.isSmartContractCode = exports.isSafeDynamicKey = exports.isSafeChainId = exports.isPlainObject = exports.isNonEmptyArray = exports.hexToText = exports.hexToBN = exports.handleFetch = exports.gweiDecToWEIBN = exports.getBuyURL = exports.fromHex = exports.fractionBN = exports.fetchWithErrorHandling = exports.convertHexToDecimal = exports.BNToHex = exports.createServicePolicy = exports.DEFAULT_MAX_RETRIES = exports.DEFAULT_MAX_CONSECUTIVE_FAILURES = exports.DEFAULT_DEGRADED_THRESHOLD = exports.DEFAULT_CIRCUIT_BREAK_DURATION = void 0;
17
+ exports.isEqualCaseInsensitive = exports.weiHexToGweiDec = exports.toHex = exports.toChecksumHexAddress = exports.timeoutFetch = exports.successfulFetch = exports.safelyExecuteWithTimeout = exports.safelyExecute = exports.query = exports.normalizeEnsName = exports.isValidHexAddress = exports.isValidJson = exports.isSmartContractCode = exports.isSafeDynamicKey = exports.isSafeChainId = exports.isPlainObject = exports.isNonEmptyArray = exports.hexToText = exports.hexToBN = exports.handleFetch = exports.gweiDecToWEIBN = exports.getBuyURL = exports.fromHex = exports.fractionBN = exports.fetchWithErrorHandling = exports.convertHexToDecimal = exports.BNToHex = exports.handleWhen = exports.handleAll = exports.createServicePolicy = exports.DEFAULT_MAX_RETRIES = exports.DEFAULT_MAX_CONSECUTIVE_FAILURES = exports.DEFAULT_DEGRADED_THRESHOLD = exports.DEFAULT_CIRCUIT_BREAK_DURATION = exports.CircuitState = exports.BrokenCircuitError = void 0;
18
18
  var create_service_policy_1 = require("./create-service-policy.cjs");
19
+ Object.defineProperty(exports, "BrokenCircuitError", { enumerable: true, get: function () { return create_service_policy_1.BrokenCircuitError; } });
20
+ Object.defineProperty(exports, "CircuitState", { enumerable: true, get: function () { return create_service_policy_1.CircuitState; } });
19
21
  Object.defineProperty(exports, "DEFAULT_CIRCUIT_BREAK_DURATION", { enumerable: true, get: function () { return create_service_policy_1.DEFAULT_CIRCUIT_BREAK_DURATION; } });
20
22
  Object.defineProperty(exports, "DEFAULT_DEGRADED_THRESHOLD", { enumerable: true, get: function () { return create_service_policy_1.DEFAULT_DEGRADED_THRESHOLD; } });
21
23
  Object.defineProperty(exports, "DEFAULT_MAX_CONSECUTIVE_FAILURES", { enumerable: true, get: function () { return create_service_policy_1.DEFAULT_MAX_CONSECUTIVE_FAILURES; } });
22
24
  Object.defineProperty(exports, "DEFAULT_MAX_RETRIES", { enumerable: true, get: function () { return create_service_policy_1.DEFAULT_MAX_RETRIES; } });
23
25
  Object.defineProperty(exports, "createServicePolicy", { enumerable: true, get: function () { return create_service_policy_1.createServicePolicy; } });
26
+ Object.defineProperty(exports, "handleAll", { enumerable: true, get: function () { return create_service_policy_1.handleAll; } });
27
+ Object.defineProperty(exports, "handleWhen", { enumerable: true, get: function () { return create_service_policy_1.handleWhen; } });
24
28
  __exportStar(require("./constants.cjs"), exports);
25
29
  var util_1 = require("./util.cjs");
26
30
  Object.defineProperty(exports, "BNToHex", { enumerable: true, get: function () { return util_1.BNToHex; } });
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,qEAMiC;AAL/B,uIAAA,8BAA8B,OAAA;AAC9B,mIAAA,0BAA0B,OAAA;AAC1B,yIAAA,gCAAgC,OAAA;AAChC,4HAAA,mBAAmB,OAAA;AACnB,4HAAA,mBAAmB,OAAA;AAGrB,kDAA4B;AAE5B,mCA4BgB;AA3Bd,+FAAA,OAAO,OAAA;AACP,2GAAA,mBAAmB,OAAA;AACnB,8GAAA,sBAAsB,OAAA;AACtB,kGAAA,UAAU,OAAA;AACV,+FAAA,OAAO,OAAA;AACP,iGAAA,SAAS,OAAA;AACT,sGAAA,cAAc,OAAA;AACd,mGAAA,WAAW,OAAA;AACX,+FAAA,OAAO,OAAA;AACP,iGAAA,SAAS,OAAA;AACT,uGAAA,eAAe,OAAA;AACf,qGAAA,aAAa,OAAA;AACb,qGAAA,aAAa,OAAA;AACb,wGAAA,gBAAgB,OAAA;AAChB,2GAAA,mBAAmB,OAAA;AACnB,mGAAA,WAAW,OAAA;AACX,yGAAA,iBAAiB,OAAA;AACjB,wGAAA,gBAAgB,OAAA;AAChB,6FAAA,KAAK,OAAA;AACL,qGAAA,aAAa,OAAA;AACb,gHAAA,wBAAwB,OAAA;AACxB,uGAAA,eAAe,OAAA;AACf,oGAAA,YAAY,OAAA;AACZ,4GAAA,oBAAoB,OAAA;AACpB,6FAAA,KAAK,OAAA;AACL,uGAAA,eAAe,OAAA;AACf,8GAAA,sBAAsB,OAAA;AAExB,8CAAwB;AACxB,6CAAuB","sourcesContent":["export {\n DEFAULT_CIRCUIT_BREAK_DURATION,\n DEFAULT_DEGRADED_THRESHOLD,\n DEFAULT_MAX_CONSECUTIVE_FAILURES,\n DEFAULT_MAX_RETRIES,\n createServicePolicy,\n} from './create-service-policy';\nexport type { IServicePolicy } from './create-service-policy';\nexport * from './constants';\nexport type { NonEmptyArray } from './util';\nexport {\n BNToHex,\n convertHexToDecimal,\n fetchWithErrorHandling,\n fractionBN,\n fromHex,\n getBuyURL,\n gweiDecToWEIBN,\n handleFetch,\n hexToBN,\n hexToText,\n isNonEmptyArray,\n isPlainObject,\n isSafeChainId,\n isSafeDynamicKey,\n isSmartContractCode,\n isValidJson,\n isValidHexAddress,\n normalizeEnsName,\n query,\n safelyExecute,\n safelyExecuteWithTimeout,\n successfulFetch,\n timeoutFetch,\n toChecksumHexAddress,\n toHex,\n weiHexToGweiDec,\n isEqualCaseInsensitive,\n} from './util';\nexport * from './types';\nexport * from './siwe';\n"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,qEAUiC;AAT/B,2HAAA,kBAAkB,OAAA;AAClB,qHAAA,YAAY,OAAA;AACZ,uIAAA,8BAA8B,OAAA;AAC9B,mIAAA,0BAA0B,OAAA;AAC1B,yIAAA,gCAAgC,OAAA;AAChC,4HAAA,mBAAmB,OAAA;AACnB,4HAAA,mBAAmB,OAAA;AACnB,kHAAA,SAAS,OAAA;AACT,mHAAA,UAAU,OAAA;AAMZ,kDAA4B;AAE5B,mCA4BgB;AA3Bd,+FAAA,OAAO,OAAA;AACP,2GAAA,mBAAmB,OAAA;AACnB,8GAAA,sBAAsB,OAAA;AACtB,kGAAA,UAAU,OAAA;AACV,+FAAA,OAAO,OAAA;AACP,iGAAA,SAAS,OAAA;AACT,sGAAA,cAAc,OAAA;AACd,mGAAA,WAAW,OAAA;AACX,+FAAA,OAAO,OAAA;AACP,iGAAA,SAAS,OAAA;AACT,uGAAA,eAAe,OAAA;AACf,qGAAA,aAAa,OAAA;AACb,qGAAA,aAAa,OAAA;AACb,wGAAA,gBAAgB,OAAA;AAChB,2GAAA,mBAAmB,OAAA;AACnB,mGAAA,WAAW,OAAA;AACX,yGAAA,iBAAiB,OAAA;AACjB,wGAAA,gBAAgB,OAAA;AAChB,6FAAA,KAAK,OAAA;AACL,qGAAA,aAAa,OAAA;AACb,gHAAA,wBAAwB,OAAA;AACxB,uGAAA,eAAe,OAAA;AACf,oGAAA,YAAY,OAAA;AACZ,4GAAA,oBAAoB,OAAA;AACpB,6FAAA,KAAK,OAAA;AACL,uGAAA,eAAe,OAAA;AACf,8GAAA,sBAAsB,OAAA;AAExB,8CAAwB;AACxB,6CAAuB","sourcesContent":["export {\n BrokenCircuitError,\n CircuitState,\n DEFAULT_CIRCUIT_BREAK_DURATION,\n DEFAULT_DEGRADED_THRESHOLD,\n DEFAULT_MAX_CONSECUTIVE_FAILURES,\n DEFAULT_MAX_RETRIES,\n createServicePolicy,\n handleAll,\n handleWhen,\n} from './create-service-policy';\nexport type {\n CreateServicePolicyOptions,\n ServicePolicy,\n} from './create-service-policy';\nexport * from './constants';\nexport type { NonEmptyArray } from './util';\nexport {\n BNToHex,\n convertHexToDecimal,\n fetchWithErrorHandling,\n fractionBN,\n fromHex,\n getBuyURL,\n gweiDecToWEIBN,\n handleFetch,\n hexToBN,\n hexToText,\n isNonEmptyArray,\n isPlainObject,\n isSafeChainId,\n isSafeDynamicKey,\n isSmartContractCode,\n isValidJson,\n isValidHexAddress,\n normalizeEnsName,\n query,\n safelyExecute,\n safelyExecuteWithTimeout,\n successfulFetch,\n timeoutFetch,\n toChecksumHexAddress,\n toHex,\n weiHexToGweiDec,\n isEqualCaseInsensitive,\n} from './util';\nexport * from './types';\nexport * from './siwe';\n"]}
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- export { DEFAULT_CIRCUIT_BREAK_DURATION, DEFAULT_DEGRADED_THRESHOLD, DEFAULT_MAX_CONSECUTIVE_FAILURES, DEFAULT_MAX_RETRIES, createServicePolicy, } from "./create-service-policy.cjs";
2
- export type { IServicePolicy } from "./create-service-policy.cjs";
1
+ export { BrokenCircuitError, CircuitState, DEFAULT_CIRCUIT_BREAK_DURATION, DEFAULT_DEGRADED_THRESHOLD, DEFAULT_MAX_CONSECUTIVE_FAILURES, DEFAULT_MAX_RETRIES, createServicePolicy, handleAll, handleWhen, } from "./create-service-policy.cjs";
2
+ export type { CreateServicePolicyOptions, ServicePolicy, } from "./create-service-policy.cjs";
3
3
  export * from "./constants.cjs";
4
4
  export type { NonEmptyArray } from "./util.cjs";
5
5
  export { BNToHex, convertHexToDecimal, fetchWithErrorHandling, fractionBN, fromHex, getBuyURL, gweiDecToWEIBN, handleFetch, hexToBN, hexToText, isNonEmptyArray, isPlainObject, isSafeChainId, isSafeDynamicKey, isSmartContractCode, isValidJson, isValidHexAddress, normalizeEnsName, query, safelyExecute, safelyExecuteWithTimeout, successfulFetch, timeoutFetch, toChecksumHexAddress, toHex, weiHexToGweiDec, isEqualCaseInsensitive, } from "./util.cjs";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,8BAA8B,EAC9B,0BAA0B,EAC1B,gCAAgC,EAChC,mBAAmB,EACnB,mBAAmB,GACpB,oCAAgC;AACjC,YAAY,EAAE,cAAc,EAAE,oCAAgC;AAC9D,gCAA4B;AAC5B,YAAY,EAAE,aAAa,EAAE,mBAAe;AAC5C,OAAO,EACL,OAAO,EACP,mBAAmB,EACnB,sBAAsB,EACtB,UAAU,EACV,OAAO,EACP,SAAS,EACT,cAAc,EACd,WAAW,EACX,OAAO,EACP,SAAS,EACT,eAAe,EACf,aAAa,EACb,aAAa,EACb,gBAAgB,EAChB,mBAAmB,EACnB,WAAW,EACX,iBAAiB,EACjB,gBAAgB,EAChB,KAAK,EACL,aAAa,EACb,wBAAwB,EACxB,eAAe,EACf,YAAY,EACZ,oBAAoB,EACpB,KAAK,EACL,eAAe,EACf,sBAAsB,GACvB,mBAAe;AAChB,4BAAwB;AACxB,2BAAuB"}
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,YAAY,EACZ,8BAA8B,EAC9B,0BAA0B,EAC1B,gCAAgC,EAChC,mBAAmB,EACnB,mBAAmB,EACnB,SAAS,EACT,UAAU,GACX,oCAAgC;AACjC,YAAY,EACV,0BAA0B,EAC1B,aAAa,GACd,oCAAgC;AACjC,gCAA4B;AAC5B,YAAY,EAAE,aAAa,EAAE,mBAAe;AAC5C,OAAO,EACL,OAAO,EACP,mBAAmB,EACnB,sBAAsB,EACtB,UAAU,EACV,OAAO,EACP,SAAS,EACT,cAAc,EACd,WAAW,EACX,OAAO,EACP,SAAS,EACT,eAAe,EACf,aAAa,EACb,aAAa,EACb,gBAAgB,EAChB,mBAAmB,EACnB,WAAW,EACX,iBAAiB,EACjB,gBAAgB,EAChB,KAAK,EACL,aAAa,EACb,wBAAwB,EACxB,eAAe,EACf,YAAY,EACZ,oBAAoB,EACpB,KAAK,EACL,eAAe,EACf,sBAAsB,GACvB,mBAAe;AAChB,4BAAwB;AACxB,2BAAuB"}
package/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- export { DEFAULT_CIRCUIT_BREAK_DURATION, DEFAULT_DEGRADED_THRESHOLD, DEFAULT_MAX_CONSECUTIVE_FAILURES, DEFAULT_MAX_RETRIES, createServicePolicy, } from "./create-service-policy.mjs";
2
- export type { IServicePolicy } from "./create-service-policy.mjs";
1
+ export { BrokenCircuitError, CircuitState, DEFAULT_CIRCUIT_BREAK_DURATION, DEFAULT_DEGRADED_THRESHOLD, DEFAULT_MAX_CONSECUTIVE_FAILURES, DEFAULT_MAX_RETRIES, createServicePolicy, handleAll, handleWhen, } from "./create-service-policy.mjs";
2
+ export type { CreateServicePolicyOptions, ServicePolicy, } from "./create-service-policy.mjs";
3
3
  export * from "./constants.mjs";
4
4
  export type { NonEmptyArray } from "./util.mjs";
5
5
  export { BNToHex, convertHexToDecimal, fetchWithErrorHandling, fractionBN, fromHex, getBuyURL, gweiDecToWEIBN, handleFetch, hexToBN, hexToText, isNonEmptyArray, isPlainObject, isSafeChainId, isSafeDynamicKey, isSmartContractCode, isValidJson, isValidHexAddress, normalizeEnsName, query, safelyExecute, safelyExecuteWithTimeout, successfulFetch, timeoutFetch, toChecksumHexAddress, toHex, weiHexToGweiDec, isEqualCaseInsensitive, } from "./util.mjs";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,8BAA8B,EAC9B,0BAA0B,EAC1B,gCAAgC,EAChC,mBAAmB,EACnB,mBAAmB,GACpB,oCAAgC;AACjC,YAAY,EAAE,cAAc,EAAE,oCAAgC;AAC9D,gCAA4B;AAC5B,YAAY,EAAE,aAAa,EAAE,mBAAe;AAC5C,OAAO,EACL,OAAO,EACP,mBAAmB,EACnB,sBAAsB,EACtB,UAAU,EACV,OAAO,EACP,SAAS,EACT,cAAc,EACd,WAAW,EACX,OAAO,EACP,SAAS,EACT,eAAe,EACf,aAAa,EACb,aAAa,EACb,gBAAgB,EAChB,mBAAmB,EACnB,WAAW,EACX,iBAAiB,EACjB,gBAAgB,EAChB,KAAK,EACL,aAAa,EACb,wBAAwB,EACxB,eAAe,EACf,YAAY,EACZ,oBAAoB,EACpB,KAAK,EACL,eAAe,EACf,sBAAsB,GACvB,mBAAe;AAChB,4BAAwB;AACxB,2BAAuB"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,YAAY,EACZ,8BAA8B,EAC9B,0BAA0B,EAC1B,gCAAgC,EAChC,mBAAmB,EACnB,mBAAmB,EACnB,SAAS,EACT,UAAU,GACX,oCAAgC;AACjC,YAAY,EACV,0BAA0B,EAC1B,aAAa,GACd,oCAAgC;AACjC,gCAA4B;AAC5B,YAAY,EAAE,aAAa,EAAE,mBAAe;AAC5C,OAAO,EACL,OAAO,EACP,mBAAmB,EACnB,sBAAsB,EACtB,UAAU,EACV,OAAO,EACP,SAAS,EACT,cAAc,EACd,WAAW,EACX,OAAO,EACP,SAAS,EACT,eAAe,EACf,aAAa,EACb,aAAa,EACb,gBAAgB,EAChB,mBAAmB,EACnB,WAAW,EACX,iBAAiB,EACjB,gBAAgB,EAChB,KAAK,EACL,aAAa,EACb,wBAAwB,EACxB,eAAe,EACf,YAAY,EACZ,oBAAoB,EACpB,KAAK,EACL,eAAe,EACf,sBAAsB,GACvB,mBAAe;AAChB,4BAAwB;AACxB,2BAAuB"}
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- export { DEFAULT_CIRCUIT_BREAK_DURATION, DEFAULT_DEGRADED_THRESHOLD, DEFAULT_MAX_CONSECUTIVE_FAILURES, DEFAULT_MAX_RETRIES, createServicePolicy } from "./create-service-policy.mjs";
1
+ export { BrokenCircuitError, CircuitState, DEFAULT_CIRCUIT_BREAK_DURATION, DEFAULT_DEGRADED_THRESHOLD, DEFAULT_MAX_CONSECUTIVE_FAILURES, DEFAULT_MAX_RETRIES, createServicePolicy, handleAll, handleWhen } from "./create-service-policy.mjs";
2
2
  export * from "./constants.mjs";
3
3
  export { BNToHex, convertHexToDecimal, fetchWithErrorHandling, fractionBN, fromHex, getBuyURL, gweiDecToWEIBN, handleFetch, hexToBN, hexToText, isNonEmptyArray, isPlainObject, isSafeChainId, isSafeDynamicKey, isSmartContractCode, isValidJson, isValidHexAddress, normalizeEnsName, query, safelyExecute, safelyExecuteWithTimeout, successfulFetch, timeoutFetch, toChecksumHexAddress, toHex, weiHexToGweiDec, isEqualCaseInsensitive } from "./util.mjs";
4
4
  export * from "./types.mjs";
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,8BAA8B,EAC9B,0BAA0B,EAC1B,gCAAgC,EAChC,mBAAmB,EACnB,mBAAmB,EACpB,oCAAgC;AAEjC,gCAA4B;AAE5B,OAAO,EACL,OAAO,EACP,mBAAmB,EACnB,sBAAsB,EACtB,UAAU,EACV,OAAO,EACP,SAAS,EACT,cAAc,EACd,WAAW,EACX,OAAO,EACP,SAAS,EACT,eAAe,EACf,aAAa,EACb,aAAa,EACb,gBAAgB,EAChB,mBAAmB,EACnB,WAAW,EACX,iBAAiB,EACjB,gBAAgB,EAChB,KAAK,EACL,aAAa,EACb,wBAAwB,EACxB,eAAe,EACf,YAAY,EACZ,oBAAoB,EACpB,KAAK,EACL,eAAe,EACf,sBAAsB,EACvB,mBAAe;AAChB,4BAAwB;AACxB,2BAAuB","sourcesContent":["export {\n DEFAULT_CIRCUIT_BREAK_DURATION,\n DEFAULT_DEGRADED_THRESHOLD,\n DEFAULT_MAX_CONSECUTIVE_FAILURES,\n DEFAULT_MAX_RETRIES,\n createServicePolicy,\n} from './create-service-policy';\nexport type { IServicePolicy } from './create-service-policy';\nexport * from './constants';\nexport type { NonEmptyArray } from './util';\nexport {\n BNToHex,\n convertHexToDecimal,\n fetchWithErrorHandling,\n fractionBN,\n fromHex,\n getBuyURL,\n gweiDecToWEIBN,\n handleFetch,\n hexToBN,\n hexToText,\n isNonEmptyArray,\n isPlainObject,\n isSafeChainId,\n isSafeDynamicKey,\n isSmartContractCode,\n isValidJson,\n isValidHexAddress,\n normalizeEnsName,\n query,\n safelyExecute,\n safelyExecuteWithTimeout,\n successfulFetch,\n timeoutFetch,\n toChecksumHexAddress,\n toHex,\n weiHexToGweiDec,\n isEqualCaseInsensitive,\n} from './util';\nexport * from './types';\nexport * from './siwe';\n"]}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,YAAY,EACZ,8BAA8B,EAC9B,0BAA0B,EAC1B,gCAAgC,EAChC,mBAAmB,EACnB,mBAAmB,EACnB,SAAS,EACT,UAAU,EACX,oCAAgC;AAKjC,gCAA4B;AAE5B,OAAO,EACL,OAAO,EACP,mBAAmB,EACnB,sBAAsB,EACtB,UAAU,EACV,OAAO,EACP,SAAS,EACT,cAAc,EACd,WAAW,EACX,OAAO,EACP,SAAS,EACT,eAAe,EACf,aAAa,EACb,aAAa,EACb,gBAAgB,EAChB,mBAAmB,EACnB,WAAW,EACX,iBAAiB,EACjB,gBAAgB,EAChB,KAAK,EACL,aAAa,EACb,wBAAwB,EACxB,eAAe,EACf,YAAY,EACZ,oBAAoB,EACpB,KAAK,EACL,eAAe,EACf,sBAAsB,EACvB,mBAAe;AAChB,4BAAwB;AACxB,2BAAuB","sourcesContent":["export {\n BrokenCircuitError,\n CircuitState,\n DEFAULT_CIRCUIT_BREAK_DURATION,\n DEFAULT_DEGRADED_THRESHOLD,\n DEFAULT_MAX_CONSECUTIVE_FAILURES,\n DEFAULT_MAX_RETRIES,\n createServicePolicy,\n handleAll,\n handleWhen,\n} from './create-service-policy';\nexport type {\n CreateServicePolicyOptions,\n ServicePolicy,\n} from './create-service-policy';\nexport * from './constants';\nexport type { NonEmptyArray } from './util';\nexport {\n BNToHex,\n convertHexToDecimal,\n fetchWithErrorHandling,\n fractionBN,\n fromHex,\n getBuyURL,\n gweiDecToWEIBN,\n handleFetch,\n hexToBN,\n hexToText,\n isNonEmptyArray,\n isPlainObject,\n isSafeChainId,\n isSafeDynamicKey,\n isSmartContractCode,\n isValidJson,\n isValidHexAddress,\n normalizeEnsName,\n query,\n safelyExecute,\n safelyExecuteWithTimeout,\n successfulFetch,\n timeoutFetch,\n toChecksumHexAddress,\n toHex,\n weiHexToGweiDec,\n isEqualCaseInsensitive,\n} from './util';\nexport * from './types';\nexport * from './siwe';\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/controller-utils",
3
- "version": "11.4.5-preview-b1518d1",
3
+ "version": "11.4.5-preview-f3d5e317",
4
4
  "description": "Data and convenience functions shared by multiple packages",
5
5
  "keywords": [
6
6
  "MetaMask",