@metamask-previews/controller-utils 11.4.5-preview-ee77a6d9 → 11.4.5-preview-4c4eb52a

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,12 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9
9
 
10
10
  ### Added
11
11
 
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), [#5225](https://github.com/MetaMask/core/pull/5225))
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`
17
- - Export `CockatielEvent` type which is an alias of the `Event` type from `cockatiel`
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))
18
13
 
19
14
  ## [11.4.5]
20
15
 
@@ -1,11 +1,7 @@
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 = exports.handleWhen = exports.handleAll = exports.BrokenCircuitError = exports.CircuitState = void 0;
3
+ exports.createServicePolicy = exports.DEFAULT_DEGRADED_THRESHOLD = exports.DEFAULT_CIRCUIT_BREAK_DURATION = exports.DEFAULT_MAX_CONSECUTIVE_FAILURES = exports.DEFAULT_MAX_RETRIES = 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; } });
9
5
  /**
10
6
  * The maximum number of times that a failing service should be re-run before
11
7
  * giving up.
@@ -13,9 +9,7 @@ Object.defineProperty(exports, "handleWhen", { enumerable: true, get: function (
13
9
  exports.DEFAULT_MAX_RETRIES = 3;
14
10
  /**
15
11
  * The maximum number of times that the service is allowed to fail before
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.
12
+ * pausing further retries.
19
13
  */
20
14
  exports.DEFAULT_MAX_CONSECUTIVE_FAILURES = (1 + exports.DEFAULT_MAX_RETRIES) * 3;
21
15
  /**
@@ -44,12 +38,6 @@ exports.DEFAULT_DEGRADED_THRESHOLD = 5000;
44
38
  * there for more.
45
39
  *
46
40
  * @param options - The options to this function.
47
- * @param options.maxRetries - The maximum number of times that a failing
48
- * service should be re-invoked before giving up. Defaults to 3.
49
- * @param options.retryFilterPolicy - The policy used to control when the
50
- * service should be retried based on either the result of the servce or an
51
- * error that it throws. For instance, you could use this to retry only certain
52
- * errors. See `handleWhen` and friends from Cockatiel for more.
53
41
  * @param options.maxConsecutiveFailures - The maximum number of times that the
54
42
  * service is allowed to fail before pausing further retries. Defaults to 12.
55
43
  * @param options.circuitBreakDuration - The length of time (in milliseconds) to
@@ -58,6 +46,14 @@ exports.DEFAULT_DEGRADED_THRESHOLD = 5000;
58
46
  * @param options.degradedThreshold - The length of time (in milliseconds) that
59
47
  * governs when the service is regarded as degraded (affecting when `onDegraded`
60
48
  * is called). Defaults to 5 seconds.
49
+ * @param options.onBreak - A function which is called when the service fails
50
+ * too many times in a row (specifically, more than `maxConsecutiveFailures`).
51
+ * @param options.onDegraded - A function which is called when the service
52
+ * succeeds before `maxConsecutiveFailures` is reached, but takes more time than
53
+ * the `degradedThreshold` to run.
54
+ * @param options.onRetry - A function which will be called the moment the
55
+ * policy kicks off a timer to re-run the function passed to the policy. This is
56
+ * primarily useful in tests where we are mocking timers.
61
57
  * @returns The service policy.
62
58
  * @example
63
59
  * This function is designed to be used in the context of a service class like
@@ -66,10 +62,6 @@ exports.DEFAULT_DEGRADED_THRESHOLD = 5000;
66
62
  * class Service {
67
63
  * constructor() {
68
64
  * this.#policy = createServicePolicy({
69
- * maxRetries: 3,
70
- * retryFilterPolicy: handleWhen((error) => {
71
- * return error.message.includes('oops');
72
- * }),
73
65
  * maxConsecutiveFailures: 3,
74
66
  * circuitBreakDuration: 5000,
75
67
  * degradedThreshold: 2000,
@@ -91,16 +83,21 @@ exports.DEFAULT_DEGRADED_THRESHOLD = 5000;
91
83
  * }
92
84
  * ```
93
85
  */
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, } = {}) {
95
- const retryPolicy = (0, cockatiel_1.retry)(retryFilterPolicy, {
86
+ function createServicePolicy({ maxConsecutiveFailures = exports.DEFAULT_MAX_CONSECUTIVE_FAILURES, circuitBreakDuration = exports.DEFAULT_CIRCUIT_BREAK_DURATION, degradedThreshold = exports.DEFAULT_DEGRADED_THRESHOLD, onBreak = () => {
87
+ // do nothing
88
+ }, onDegraded = () => {
89
+ // do nothing
90
+ }, onRetry = () => {
91
+ // do nothing
92
+ }, } = {}) {
93
+ const retryPolicy = (0, cockatiel_1.retry)(cockatiel_1.handleAll, {
96
94
  // Note that although the option here is called "max attempts", it's really
97
95
  // maximum number of *retries* (attempts past the initial attempt).
98
- maxAttempts: maxRetries,
96
+ maxAttempts: exports.DEFAULT_MAX_RETRIES,
99
97
  // Retries of the service will be executed following ever increasing delays,
100
98
  // determined by a backoff formula.
101
99
  backoff: new cockatiel_1.ExponentialBackoff(),
102
100
  });
103
- const onRetry = retryPolicy.onRetry.bind(retryPolicy);
104
101
  const circuitBreakerPolicy = (0, cockatiel_1.circuitBreaker)(cockatiel_1.handleAll, {
105
102
  // While the circuit is open, any additional invocations of the service
106
103
  // passed to the policy (either via automatic retries or by manually
@@ -111,31 +108,38 @@ function createServicePolicy({ maxRetries = exports.DEFAULT_MAX_RETRIES, retryFi
111
108
  halfOpenAfter: circuitBreakDuration,
112
109
  breaker: new cockatiel_1.ConsecutiveBreaker(maxConsecutiveFailures),
113
110
  });
114
- const onBreak = circuitBreakerPolicy.onBreak.bind(circuitBreakerPolicy);
115
- const onDegradedEventEmitter = new cockatiel_1.EventEmitter();
111
+ // The `onBreak` callback will be called if the service consistently throws
112
+ // for as many times as exceeds the maximum consecutive number of failures.
113
+ // Combined with the retry policy, this can happen if:
114
+ // - `maxConsecutiveFailures` < the default max retries (3) and the policy is
115
+ // executed once
116
+ // - `maxConsecutiveFailures` >= the default max retries (3) but the policy is
117
+ // executed multiple times, enough for the total number of retries to exceed
118
+ // `maxConsecutiveFailures`
119
+ circuitBreakerPolicy.onBreak(onBreak);
120
+ // The `onRetryPolicy` callback will be called each time the service is
121
+ // invoked (including retries).
122
+ retryPolicy.onRetry(onRetry);
116
123
  retryPolicy.onGiveUp(() => {
117
124
  if (circuitBreakerPolicy.state === cockatiel_1.CircuitState.Closed) {
118
- onDegradedEventEmitter.emit();
125
+ // The `onDegraded` callback will be called if the number of retries is
126
+ // exceeded and the maximum number of consecutive failures has not been
127
+ // reached yet (whether the policy is called once or multiple times).
128
+ onDegraded();
119
129
  }
120
130
  });
121
131
  retryPolicy.onSuccess(({ duration }) => {
122
132
  if (circuitBreakerPolicy.state === cockatiel_1.CircuitState.Closed &&
123
133
  duration > degradedThreshold) {
124
- onDegradedEventEmitter.emit();
134
+ // The `onDegraded` callback will also be called if the service does not
135
+ // throw, but the time it takes for the service to run exceeds the
136
+ // `degradedThreshold`.
137
+ onDegraded();
125
138
  }
126
139
  });
127
- const onDegraded = onDegradedEventEmitter.addListener;
128
- // Every time the retry policy makes an attempt, it executes the circuit
129
- // breaker policy, which executes the service.
130
- const policy = (0, cockatiel_1.wrap)(retryPolicy, circuitBreakerPolicy);
131
- return {
132
- ...policy,
133
- circuitBreakerPolicy,
134
- retryPolicy,
135
- onBreak,
136
- onDegraded,
137
- onRetry,
138
- };
140
+ // The retry policy really retries the circuit breaker policy, which invokes
141
+ // the service.
142
+ return (0, cockatiel_1.wrap)(retryPolicy, circuitBreakerPolicy);
139
143
  }
140
144
  exports.createServicePolicy = createServicePolicy;
141
145
  //# 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,yCAWmB;AASI,mGAnBrB,8BAAkB,OAmBqB;AAAhC,6FAlBP,wBAAY,OAkBO;AAAsB,0FAbzC,qBAAS,OAayC;AAAE,2FAZpD,sBAAU,OAYoD;AAuEhE;;;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;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,CAAC;IACH,MAAM,UAAU,GAAG,sBAAsB,CAAC,WAAW,CAAC;IAEtD,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;AAzDD,kDAyDC","sourcesContent":["import {\n BrokenCircuitError,\n CircuitState,\n EventEmitter as CockatielEventEmitter,\n ConsecutiveBreaker,\n ExponentialBackoff,\n circuitBreaker,\n handleAll,\n handleWhen,\n retry,\n wrap,\n} from 'cockatiel';\nimport type {\n CircuitBreakerPolicy,\n Event as CockatielEvent,\n IPolicy,\n Policy,\n RetryPolicy,\n} from 'cockatiel';\n\nexport { CircuitState, BrokenCircuitError, handleAll, handleWhen };\n\nexport type { CockatielEvent };\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: CockatielEvent<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 onDegradedEventEmitter = new CockatielEventEmitter<void>();\n retryPolicy.onGiveUp(() => {\n if (circuitBreakerPolicy.state === CircuitState.Closed) {\n onDegradedEventEmitter.emit();\n }\n });\n retryPolicy.onSuccess(({ duration }) => {\n if (\n circuitBreakerPolicy.state === CircuitState.Closed &&\n duration > degradedThreshold\n ) {\n onDegradedEventEmitter.emit();\n }\n });\n const onDegraded = onDegradedEventEmitter.addListener;\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
+ {"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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AACH,SAAgB,mBAAmB,CAAC,EAClC,sBAAsB,GAAG,wCAAgC,EACzD,oBA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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;AA9ED,kDA8EC","sourcesContent":["import {\n circuitBreaker,\n ConsecutiveBreaker,\n ExponentialBackoff,\n handleAll,\n retry,\n wrap,\n CircuitState,\n} from 'cockatiel';\nimport type { IPolicy } 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.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 * 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 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 maxConsecutiveFailures?: number;\n circuitBreakDuration?: number;\n degradedThreshold?: number;\n onBreak?: () => void;\n onDegraded?: () => void;\n onRetry?: () => void;\n} = {}): IPolicy {\n const retryPolicy = retry(handleAll, {\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: DEFAULT_MAX_RETRIES,\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,72 +1,5 @@
1
- import { BrokenCircuitError, CircuitState, handleAll, handleWhen } from "cockatiel";
2
- import type { CircuitBreakerPolicy, Event as CockatielEvent, IPolicy, Policy, RetryPolicy } from "cockatiel";
3
- export { CircuitState, BrokenCircuitError, handleAll, handleWhen };
4
- export type { CockatielEvent };
5
- /**
6
- * The options for `createServicePolicy`.
7
- */
8
- export type CreateServicePolicyOptions = {
9
- /**
10
- * The length of time (in milliseconds) to pause retries of the action after
11
- * the number of failures reaches `maxConsecutiveFailures`.
12
- */
13
- circuitBreakDuration?: number;
14
- /**
15
- * The length of time (in milliseconds) that governs when the service is
16
- * regarded as degraded (affecting when `onDegraded` is called).
17
- */
18
- degradedThreshold?: number;
19
- /**
20
- * The maximum number of times that the service is allowed to fail before
21
- * pausing further retries.
22
- */
23
- maxConsecutiveFailures?: number;
24
- /**
25
- * The maximum number of times that a failing service should be re-invoked
26
- * before giving up.
27
- */
28
- maxRetries?: number;
29
- /**
30
- * The policy used to control when the service should be retried based on
31
- * either the result of the service or an error that it throws. For instance,
32
- * you could use this to retry only certain errors. See `handleWhen` and
33
- * friends from Cockatiel for more.
34
- */
35
- retryFilterPolicy?: Policy;
36
- };
37
- /**
38
- * The service policy object.
39
- */
40
- export type ServicePolicy = IPolicy & {
41
- /**
42
- * The Cockatiel circuit breaker policy that the service policy uses
43
- * internally.
44
- */
45
- circuitBreakerPolicy: CircuitBreakerPolicy;
46
- /**
47
- * The Cockatiel retry policy that the service policy uses internally.
48
- */
49
- retryPolicy: RetryPolicy;
50
- /**
51
- * A function which is called when the number of times that the service fails
52
- * in a row meets the set maximum number of consecutive failures.
53
- */
54
- onBreak: CircuitBreakerPolicy['onBreak'];
55
- /**
56
- * A function which is called in two circumstances: 1) when the service
57
- * succeeds before the maximum number of consecutive failures is reached, but
58
- * takes more time than the `degradedThreshold` to run, or 2) if the service
59
- * never succeeds before the retry policy gives up and before the maximum
60
- * number of consecutive failures has been reached.
61
- */
62
- onDegraded: CockatielEvent<void>;
63
- /**
64
- * A function which will be called by the retry policy each time the service
65
- * fails and the policy kicks off a timer to re-run the service. This is
66
- * primarily useful in tests where we are mocking timers.
67
- */
68
- onRetry: RetryPolicy['onRetry'];
69
- };
1
+ import type { IPolicy } from "cockatiel";
2
+ export type { IPolicy as IServicePolicy };
70
3
  /**
71
4
  * The maximum number of times that a failing service should be re-run before
72
5
  * giving up.
@@ -74,9 +7,7 @@ export type ServicePolicy = IPolicy & {
74
7
  export declare const DEFAULT_MAX_RETRIES = 3;
75
8
  /**
76
9
  * The maximum number of times that the service is allowed to fail before
77
- * pausing further retries. This is set to a value such that if given a
78
- * service that continually fails, the policy needs to be executed 3 times
79
- * before further retries are paused.
10
+ * pausing further retries.
80
11
  */
81
12
  export declare const DEFAULT_MAX_CONSECUTIVE_FAILURES: number;
82
13
  /**
@@ -105,12 +36,6 @@ export declare const DEFAULT_DEGRADED_THRESHOLD = 5000;
105
36
  * there for more.
106
37
  *
107
38
  * @param options - The options to this function.
108
- * @param options.maxRetries - The maximum number of times that a failing
109
- * service should be re-invoked before giving up. Defaults to 3.
110
- * @param options.retryFilterPolicy - The policy used to control when the
111
- * service should be retried based on either the result of the servce or an
112
- * error that it throws. For instance, you could use this to retry only certain
113
- * errors. See `handleWhen` and friends from Cockatiel for more.
114
39
  * @param options.maxConsecutiveFailures - The maximum number of times that the
115
40
  * service is allowed to fail before pausing further retries. Defaults to 12.
116
41
  * @param options.circuitBreakDuration - The length of time (in milliseconds) to
@@ -119,6 +44,14 @@ export declare const DEFAULT_DEGRADED_THRESHOLD = 5000;
119
44
  * @param options.degradedThreshold - The length of time (in milliseconds) that
120
45
  * governs when the service is regarded as degraded (affecting when `onDegraded`
121
46
  * is called). Defaults to 5 seconds.
47
+ * @param options.onBreak - A function which is called when the service fails
48
+ * too many times in a row (specifically, more than `maxConsecutiveFailures`).
49
+ * @param options.onDegraded - A function which is called when the service
50
+ * succeeds before `maxConsecutiveFailures` is reached, but takes more time than
51
+ * the `degradedThreshold` to run.
52
+ * @param options.onRetry - A function which will be called the moment the
53
+ * policy kicks off a timer to re-run the function passed to the policy. This is
54
+ * primarily useful in tests where we are mocking timers.
122
55
  * @returns The service policy.
123
56
  * @example
124
57
  * This function is designed to be used in the context of a service class like
@@ -127,10 +60,6 @@ export declare const DEFAULT_DEGRADED_THRESHOLD = 5000;
127
60
  * class Service {
128
61
  * constructor() {
129
62
  * this.#policy = createServicePolicy({
130
- * maxRetries: 3,
131
- * retryFilterPolicy: handleWhen((error) => {
132
- * return error.message.includes('oops');
133
- * }),
134
63
  * maxConsecutiveFailures: 3,
135
64
  * circuitBreakDuration: 5000,
136
65
  * degradedThreshold: 2000,
@@ -152,5 +81,12 @@ export declare const DEFAULT_DEGRADED_THRESHOLD = 5000;
152
81
  * }
153
82
  * ```
154
83
  */
155
- export declare function createServicePolicy({ maxRetries, retryFilterPolicy, maxConsecutiveFailures, circuitBreakDuration, degradedThreshold, }?: CreateServicePolicyOptions): ServicePolicy;
84
+ export declare function createServicePolicy({ maxConsecutiveFailures, circuitBreakDuration, degradedThreshold, onBreak, onDegraded, onRetry, }?: {
85
+ maxConsecutiveFailures?: number;
86
+ circuitBreakDuration?: number;
87
+ degradedThreshold?: number;
88
+ onBreak?: () => void;
89
+ onDegraded?: () => void;
90
+ onRetry?: () => void;
91
+ }): IPolicy;
156
92
  //# 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":"AAAA,OAAO,EACL,kBAAkB,EAClB,YAAY,EAKZ,SAAS,EACT,UAAU,EAGX,kBAAkB;AACnB,OAAO,KAAK,EACV,oBAAoB,EACpB,KAAK,IAAI,cAAc,EACvB,OAAO,EACP,MAAM,EACN,WAAW,EACZ,kBAAkB;AAEnB,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC;AAEnE,YAAY,EAAE,cAAc,EAAE,CAAC;AAE/B;;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,UA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AGG;AACH,eAAO,MAAM,0BAA0B,OAAQ,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AACH,wBAAgB,mBAAmB,CAAC,EAClC,sBAAyD,EACzD,oBAAqD,EACrD,iBAA8C,EAC9C,OAEC,EACD,UAEC,EACD,OAEC,GACF,GAAE;IACD,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,72 +1,5 @@
1
- import { BrokenCircuitError, CircuitState, handleAll, handleWhen } from "cockatiel";
2
- import type { CircuitBreakerPolicy, Event as CockatielEvent, IPolicy, Policy, RetryPolicy } from "cockatiel";
3
- export { CircuitState, BrokenCircuitError, handleAll, handleWhen };
4
- export type { CockatielEvent };
5
- /**
6
- * The options for `createServicePolicy`.
7
- */
8
- export type CreateServicePolicyOptions = {
9
- /**
10
- * The length of time (in milliseconds) to pause retries of the action after
11
- * the number of failures reaches `maxConsecutiveFailures`.
12
- */
13
- circuitBreakDuration?: number;
14
- /**
15
- * The length of time (in milliseconds) that governs when the service is
16
- * regarded as degraded (affecting when `onDegraded` is called).
17
- */
18
- degradedThreshold?: number;
19
- /**
20
- * The maximum number of times that the service is allowed to fail before
21
- * pausing further retries.
22
- */
23
- maxConsecutiveFailures?: number;
24
- /**
25
- * The maximum number of times that a failing service should be re-invoked
26
- * before giving up.
27
- */
28
- maxRetries?: number;
29
- /**
30
- * The policy used to control when the service should be retried based on
31
- * either the result of the service or an error that it throws. For instance,
32
- * you could use this to retry only certain errors. See `handleWhen` and
33
- * friends from Cockatiel for more.
34
- */
35
- retryFilterPolicy?: Policy;
36
- };
37
- /**
38
- * The service policy object.
39
- */
40
- export type ServicePolicy = IPolicy & {
41
- /**
42
- * The Cockatiel circuit breaker policy that the service policy uses
43
- * internally.
44
- */
45
- circuitBreakerPolicy: CircuitBreakerPolicy;
46
- /**
47
- * The Cockatiel retry policy that the service policy uses internally.
48
- */
49
- retryPolicy: RetryPolicy;
50
- /**
51
- * A function which is called when the number of times that the service fails
52
- * in a row meets the set maximum number of consecutive failures.
53
- */
54
- onBreak: CircuitBreakerPolicy['onBreak'];
55
- /**
56
- * A function which is called in two circumstances: 1) when the service
57
- * succeeds before the maximum number of consecutive failures is reached, but
58
- * takes more time than the `degradedThreshold` to run, or 2) if the service
59
- * never succeeds before the retry policy gives up and before the maximum
60
- * number of consecutive failures has been reached.
61
- */
62
- onDegraded: CockatielEvent<void>;
63
- /**
64
- * A function which will be called by the retry policy each time the service
65
- * fails and the policy kicks off a timer to re-run the service. This is
66
- * primarily useful in tests where we are mocking timers.
67
- */
68
- onRetry: RetryPolicy['onRetry'];
69
- };
1
+ import type { IPolicy } from "cockatiel";
2
+ export type { IPolicy as IServicePolicy };
70
3
  /**
71
4
  * The maximum number of times that a failing service should be re-run before
72
5
  * giving up.
@@ -74,9 +7,7 @@ export type ServicePolicy = IPolicy & {
74
7
  export declare const DEFAULT_MAX_RETRIES = 3;
75
8
  /**
76
9
  * The maximum number of times that the service is allowed to fail before
77
- * pausing further retries. This is set to a value such that if given a
78
- * service that continually fails, the policy needs to be executed 3 times
79
- * before further retries are paused.
10
+ * pausing further retries.
80
11
  */
81
12
  export declare const DEFAULT_MAX_CONSECUTIVE_FAILURES: number;
82
13
  /**
@@ -105,12 +36,6 @@ export declare const DEFAULT_DEGRADED_THRESHOLD = 5000;
105
36
  * there for more.
106
37
  *
107
38
  * @param options - The options to this function.
108
- * @param options.maxRetries - The maximum number of times that a failing
109
- * service should be re-invoked before giving up. Defaults to 3.
110
- * @param options.retryFilterPolicy - The policy used to control when the
111
- * service should be retried based on either the result of the servce or an
112
- * error that it throws. For instance, you could use this to retry only certain
113
- * errors. See `handleWhen` and friends from Cockatiel for more.
114
39
  * @param options.maxConsecutiveFailures - The maximum number of times that the
115
40
  * service is allowed to fail before pausing further retries. Defaults to 12.
116
41
  * @param options.circuitBreakDuration - The length of time (in milliseconds) to
@@ -119,6 +44,14 @@ export declare const DEFAULT_DEGRADED_THRESHOLD = 5000;
119
44
  * @param options.degradedThreshold - The length of time (in milliseconds) that
120
45
  * governs when the service is regarded as degraded (affecting when `onDegraded`
121
46
  * is called). Defaults to 5 seconds.
47
+ * @param options.onBreak - A function which is called when the service fails
48
+ * too many times in a row (specifically, more than `maxConsecutiveFailures`).
49
+ * @param options.onDegraded - A function which is called when the service
50
+ * succeeds before `maxConsecutiveFailures` is reached, but takes more time than
51
+ * the `degradedThreshold` to run.
52
+ * @param options.onRetry - A function which will be called the moment the
53
+ * policy kicks off a timer to re-run the function passed to the policy. This is
54
+ * primarily useful in tests where we are mocking timers.
122
55
  * @returns The service policy.
123
56
  * @example
124
57
  * This function is designed to be used in the context of a service class like
@@ -127,10 +60,6 @@ export declare const DEFAULT_DEGRADED_THRESHOLD = 5000;
127
60
  * class Service {
128
61
  * constructor() {
129
62
  * this.#policy = createServicePolicy({
130
- * maxRetries: 3,
131
- * retryFilterPolicy: handleWhen((error) => {
132
- * return error.message.includes('oops');
133
- * }),
134
63
  * maxConsecutiveFailures: 3,
135
64
  * circuitBreakDuration: 5000,
136
65
  * degradedThreshold: 2000,
@@ -152,5 +81,12 @@ export declare const DEFAULT_DEGRADED_THRESHOLD = 5000;
152
81
  * }
153
82
  * ```
154
83
  */
155
- export declare function createServicePolicy({ maxRetries, retryFilterPolicy, maxConsecutiveFailures, circuitBreakDuration, degradedThreshold, }?: CreateServicePolicyOptions): ServicePolicy;
84
+ export declare function createServicePolicy({ maxConsecutiveFailures, circuitBreakDuration, degradedThreshold, onBreak, onDegraded, onRetry, }?: {
85
+ maxConsecutiveFailures?: number;
86
+ circuitBreakDuration?: number;
87
+ degradedThreshold?: number;
88
+ onBreak?: () => void;
89
+ onDegraded?: () => void;
90
+ onRetry?: () => void;
91
+ }): IPolicy;
156
92
  //# 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":"AAAA,OAAO,EACL,kBAAkB,EAClB,YAAY,EAKZ,SAAS,EACT,UAAU,EAGX,kBAAkB;AACnB,OAAO,KAAK,EACV,oBAAoB,EACpB,KAAK,IAAI,cAAc,EACvB,OAAO,EACP,MAAM,EACN,WAAW,EACZ,kBAAkB;AAEnB,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC;AAEnE,YAAY,EAAE,cAAc,EAAE,CAAC;AAE/B;;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,CAAC,IAAI,CAAC,CAAC;IACjC;;;;OAIG;IACH,OAAO,EAAE,WAAW,CAAC,SAAS,CAAC,CAAC;CACjC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,mBAAmB,IA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C;CACjB,GAAG,OAAO,CA0Df"}
@@ -1,5 +1,4 @@
1
- import { BrokenCircuitError, CircuitState, EventEmitter as CockatielEventEmitter, ConsecutiveBreaker, ExponentialBackoff, circuitBreaker, handleAll, handleWhen, retry, wrap } from "cockatiel";
2
- export { CircuitState, BrokenCircuitError, handleAll, handleWhen };
1
+ import { circuitBreaker, ConsecutiveBreaker, ExponentialBackoff, handleAll, retry, wrap, CircuitState } from "cockatiel";
3
2
  /**
4
3
  * The maximum number of times that a failing service should be re-run before
5
4
  * giving up.
@@ -7,9 +6,7 @@ export { CircuitState, BrokenCircuitError, handleAll, handleWhen };
7
6
  export const DEFAULT_MAX_RETRIES = 3;
8
7
  /**
9
8
  * The maximum number of times that the service is allowed to fail before
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.
9
+ * pausing further retries.
13
10
  */
14
11
  export const DEFAULT_MAX_CONSECUTIVE_FAILURES = (1 + DEFAULT_MAX_RETRIES) * 3;
15
12
  /**
@@ -38,12 +35,6 @@ export const DEFAULT_DEGRADED_THRESHOLD = 5000;
38
35
  * there for more.
39
36
  *
40
37
  * @param options - The options to this function.
41
- * @param options.maxRetries - The maximum number of times that a failing
42
- * service should be re-invoked before giving up. Defaults to 3.
43
- * @param options.retryFilterPolicy - The policy used to control when the
44
- * service should be retried based on either the result of the servce or an
45
- * error that it throws. For instance, you could use this to retry only certain
46
- * errors. See `handleWhen` and friends from Cockatiel for more.
47
38
  * @param options.maxConsecutiveFailures - The maximum number of times that the
48
39
  * service is allowed to fail before pausing further retries. Defaults to 12.
49
40
  * @param options.circuitBreakDuration - The length of time (in milliseconds) to
@@ -52,6 +43,14 @@ export const DEFAULT_DEGRADED_THRESHOLD = 5000;
52
43
  * @param options.degradedThreshold - The length of time (in milliseconds) that
53
44
  * governs when the service is regarded as degraded (affecting when `onDegraded`
54
45
  * is called). Defaults to 5 seconds.
46
+ * @param options.onBreak - A function which is called when the service fails
47
+ * too many times in a row (specifically, more than `maxConsecutiveFailures`).
48
+ * @param options.onDegraded - A function which is called when the service
49
+ * succeeds before `maxConsecutiveFailures` is reached, but takes more time than
50
+ * the `degradedThreshold` to run.
51
+ * @param options.onRetry - A function which will be called the moment the
52
+ * policy kicks off a timer to re-run the function passed to the policy. This is
53
+ * primarily useful in tests where we are mocking timers.
55
54
  * @returns The service policy.
56
55
  * @example
57
56
  * This function is designed to be used in the context of a service class like
@@ -60,10 +59,6 @@ export const DEFAULT_DEGRADED_THRESHOLD = 5000;
60
59
  * class Service {
61
60
  * constructor() {
62
61
  * this.#policy = createServicePolicy({
63
- * maxRetries: 3,
64
- * retryFilterPolicy: handleWhen((error) => {
65
- * return error.message.includes('oops');
66
- * }),
67
62
  * maxConsecutiveFailures: 3,
68
63
  * circuitBreakDuration: 5000,
69
64
  * degradedThreshold: 2000,
@@ -85,16 +80,21 @@ export const DEFAULT_DEGRADED_THRESHOLD = 5000;
85
80
  * }
86
81
  * ```
87
82
  */
88
- export function createServicePolicy({ maxRetries = DEFAULT_MAX_RETRIES, retryFilterPolicy = handleAll, maxConsecutiveFailures = DEFAULT_MAX_CONSECUTIVE_FAILURES, circuitBreakDuration = DEFAULT_CIRCUIT_BREAK_DURATION, degradedThreshold = DEFAULT_DEGRADED_THRESHOLD, } = {}) {
89
- const retryPolicy = retry(retryFilterPolicy, {
83
+ export function createServicePolicy({ maxConsecutiveFailures = DEFAULT_MAX_CONSECUTIVE_FAILURES, circuitBreakDuration = DEFAULT_CIRCUIT_BREAK_DURATION, degradedThreshold = DEFAULT_DEGRADED_THRESHOLD, onBreak = () => {
84
+ // do nothing
85
+ }, onDegraded = () => {
86
+ // do nothing
87
+ }, onRetry = () => {
88
+ // do nothing
89
+ }, } = {}) {
90
+ const retryPolicy = retry(handleAll, {
90
91
  // Note that although the option here is called "max attempts", it's really
91
92
  // maximum number of *retries* (attempts past the initial attempt).
92
- maxAttempts: maxRetries,
93
+ maxAttempts: DEFAULT_MAX_RETRIES,
93
94
  // Retries of the service will be executed following ever increasing delays,
94
95
  // determined by a backoff formula.
95
96
  backoff: new ExponentialBackoff(),
96
97
  });
97
- const onRetry = retryPolicy.onRetry.bind(retryPolicy);
98
98
  const circuitBreakerPolicy = circuitBreaker(handleAll, {
99
99
  // While the circuit is open, any additional invocations of the service
100
100
  // passed to the policy (either via automatic retries or by manually
@@ -105,30 +105,37 @@ export function createServicePolicy({ maxRetries = DEFAULT_MAX_RETRIES, retryFil
105
105
  halfOpenAfter: circuitBreakDuration,
106
106
  breaker: new ConsecutiveBreaker(maxConsecutiveFailures),
107
107
  });
108
- const onBreak = circuitBreakerPolicy.onBreak.bind(circuitBreakerPolicy);
109
- const onDegradedEventEmitter = new CockatielEventEmitter();
108
+ // The `onBreak` callback will be called if the service consistently throws
109
+ // for as many times as exceeds the maximum consecutive number of failures.
110
+ // Combined with the retry policy, this can happen if:
111
+ // - `maxConsecutiveFailures` < the default max retries (3) and the policy is
112
+ // executed once
113
+ // - `maxConsecutiveFailures` >= the default max retries (3) but the policy is
114
+ // executed multiple times, enough for the total number of retries to exceed
115
+ // `maxConsecutiveFailures`
116
+ circuitBreakerPolicy.onBreak(onBreak);
117
+ // The `onRetryPolicy` callback will be called each time the service is
118
+ // invoked (including retries).
119
+ retryPolicy.onRetry(onRetry);
110
120
  retryPolicy.onGiveUp(() => {
111
121
  if (circuitBreakerPolicy.state === CircuitState.Closed) {
112
- onDegradedEventEmitter.emit();
122
+ // The `onDegraded` callback will be called if the number of retries is
123
+ // exceeded and the maximum number of consecutive failures has not been
124
+ // reached yet (whether the policy is called once or multiple times).
125
+ onDegraded();
113
126
  }
114
127
  });
115
128
  retryPolicy.onSuccess(({ duration }) => {
116
129
  if (circuitBreakerPolicy.state === CircuitState.Closed &&
117
130
  duration > degradedThreshold) {
118
- onDegradedEventEmitter.emit();
131
+ // The `onDegraded` callback will also be called if the service does not
132
+ // throw, but the time it takes for the service to run exceeds the
133
+ // `degradedThreshold`.
134
+ onDegraded();
119
135
  }
120
136
  });
121
- const onDegraded = onDegradedEventEmitter.addListener;
122
- // Every time the retry policy makes an attempt, it executes the circuit
123
- // breaker policy, which executes the service.
124
- const policy = wrap(retryPolicy, circuitBreakerPolicy);
125
- return {
126
- ...policy,
127
- circuitBreakerPolicy,
128
- retryPolicy,
129
- onBreak,
130
- onDegraded,
131
- onRetry,
132
- };
137
+ // The retry policy really retries the circuit breaker policy, which invokes
138
+ // the service.
139
+ return wrap(retryPolicy, circuitBreakerPolicy);
133
140
  }
134
141
  //# 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,kBAAkB,EAClB,YAAY,EACZ,YAAY,IAAI,qBAAqB,EACrC,kBAAkB,EAClB,kBAAkB,EAClB,cAAc,EACd,SAAS,EACT,UAAU,EACV,KAAK,EACL,IAAI,EACL,kBAAkB;AASnB,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC;AAuEnE;;;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,EACr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D,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 EventEmitter as CockatielEventEmitter,\n ConsecutiveBreaker,\n ExponentialBackoff,\n circuitBreaker,\n handleAll,\n handleWhen,\n retry,\n wrap,\n} from 'cockatiel';\nimport type {\n CircuitBreakerPolicy,\n Event as CockatielEvent,\n IPolicy,\n Policy,\n RetryPolicy,\n} from 'cockatiel';\n\nexport { CircuitState, BrokenCircuitError, handleAll, handleWhen };\n\nexport type { CockatielEvent };\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: CockatielEvent<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 onDegradedEventEmitter = new CockatielEventEmitter<void>();\n retryPolicy.onGiveUp(() => {\n if (circuitBreakerPolicy.state === CircuitState.Closed) {\n onDegradedEventEmitter.emit();\n }\n });\n retryPolicy.onSuccess(({ duration }) => {\n if (\n circuitBreakerPolicy.state === CircuitState.Closed &&\n duration > degradedThreshold\n ) {\n onDegradedEventEmitter.emit();\n }\n });\n const onDegraded = onDegradedEventEmitter.addListener;\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
+ {"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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AACH,MAAM,UAAU,mBAAmB,CAAC,EAClC,sBAAsB,GAAG,gCAAgC,EACzD,oBAAoB,GAAG,8BAA8B,EACrD,iBAAiB,GAAG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E,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 } 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.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 * 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 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 maxConsecutiveFailures?: number;\n circuitBreakDuration?: number;\n degradedThreshold?: number;\n onBreak?: () => void;\n onDegraded?: () => void;\n onRetry?: () => void;\n} = {}): IPolicy {\n const retryPolicy = retry(handleAll, {\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: DEFAULT_MAX_RETRIES,\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"]}
package/dist/index.cjs CHANGED
@@ -14,17 +14,9 @@ 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.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;
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 = 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; } });
21
- Object.defineProperty(exports, "DEFAULT_CIRCUIT_BREAK_DURATION", { enumerable: true, get: function () { return create_service_policy_1.DEFAULT_CIRCUIT_BREAK_DURATION; } });
22
- Object.defineProperty(exports, "DEFAULT_DEGRADED_THRESHOLD", { enumerable: true, get: function () { return create_service_policy_1.DEFAULT_DEGRADED_THRESHOLD; } });
23
- Object.defineProperty(exports, "DEFAULT_MAX_CONSECUTIVE_FAILURES", { enumerable: true, get: function () { return create_service_policy_1.DEFAULT_MAX_CONSECUTIVE_FAILURES; } });
24
- Object.defineProperty(exports, "DEFAULT_MAX_RETRIES", { enumerable: true, get: function () { return create_service_policy_1.DEFAULT_MAX_RETRIES; } });
25
19
  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; } });
28
20
  __exportStar(require("./constants.cjs"), exports);
29
21
  var util_1 = require("./util.cjs");
30
22
  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,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;AAOZ,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 CockatielEvent,\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"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,qEAA8D;AAArD,4HAAA,mBAAmB,OAAA;AAC5B,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 { createServicePolicy } 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,4 @@
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 { CockatielEvent, CreateServicePolicyOptions, ServicePolicy, } from "./create-service-policy.cjs";
1
+ export { createServicePolicy } from "./create-service-policy.cjs";
3
2
  export * from "./constants.cjs";
4
3
  export type { NonEmptyArray } from "./util.cjs";
5
4
  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,kBAAkB,EAClB,YAAY,EACZ,8BAA8B,EAC9B,0BAA0B,EAC1B,gCAAgC,EAChC,mBAAmB,EACnB,mBAAmB,EACnB,SAAS,EACT,UAAU,GACX,oCAAgC;AACjC,YAAY,EACV,cAAc,EACd,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"}
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,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"}
package/dist/index.d.mts CHANGED
@@ -1,5 +1,4 @@
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 { CockatielEvent, CreateServicePolicyOptions, ServicePolicy, } from "./create-service-policy.mjs";
1
+ export { createServicePolicy } from "./create-service-policy.mjs";
3
2
  export * from "./constants.mjs";
4
3
  export type { NonEmptyArray } from "./util.mjs";
5
4
  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,kBAAkB,EAClB,YAAY,EACZ,8BAA8B,EAC9B,0BAA0B,EAC1B,gCAAgC,EAChC,mBAAmB,EACnB,mBAAmB,EACnB,SAAS,EACT,UAAU,GACX,oCAAgC;AACjC,YAAY,EACV,cAAc,EACd,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"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,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"}
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
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";
1
+ export { createServicePolicy } 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,kBAAkB,EAClB,YAAY,EACZ,8BAA8B,EAC9B,0BAA0B,EAC1B,gCAAgC,EAChC,mBAAmB,EACnB,mBAAmB,EACnB,SAAS,EACT,UAAU,EACX,oCAAgC;AAMjC,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 CockatielEvent,\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"]}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,oCAAgC;AAC9D,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 { createServicePolicy } 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-ee77a6d9",
3
+ "version": "11.4.5-preview-4c4eb52a",
4
4
  "description": "Data and convenience functions shared by multiple packages",
5
5
  "keywords": [
6
6
  "MetaMask",
@@ -50,7 +50,7 @@
50
50
  "@ethereumjs/util": "^8.1.0",
51
51
  "@metamask/eth-query": "^4.0.0",
52
52
  "@metamask/ethjs-unit": "^0.3.0",
53
- "@metamask/utils": "^11.1.0",
53
+ "@metamask/utils": "^11.0.1",
54
54
  "@spruceid/siwe-parser": "2.1.0",
55
55
  "@types/bn.js": "^5.1.5",
56
56
  "bignumber.js": "^9.1.2",