@ocap/util 1.20.2 → 1.20.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  ## Usage
4
4
 
5
5
  ```shell
6
- yarn add @ocap/util
6
+ pnpm install @ocap/util
7
7
  ```
8
8
 
9
9
  Then:
package/esm/retry.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ interface RetryOptions {
2
+ retryLimit?: number;
3
+ backoff?: {
4
+ baseDelay: number;
5
+ maxDelay: number;
6
+ };
7
+ shouldRetry?: (error: unknown) => boolean;
8
+ onError?: (error: unknown, attempt: number) => Promise<void> | void;
9
+ }
10
+ export declare function withRetry<T>(handle: () => Promise<T>, { retryLimit, backoff, shouldRetry, onError, }?: RetryOptions): Promise<T>;
11
+ export {};
package/esm/retry.js ADDED
@@ -0,0 +1,30 @@
1
+ /* eslint-disable no-await-in-loop */
2
+ function wait(ms) {
3
+ return new Promise((resolve) => {
4
+ setTimeout(resolve, ms);
5
+ });
6
+ }
7
+ // eslint-disable-next-line consistent-return
8
+ export async function withRetry(handle, { retryLimit = 30, backoff = { baseDelay: 20, maxDelay: 1000 }, shouldRetry = () => true, onError = () => { }, } = {}) {
9
+ let attempt = 0;
10
+ let lastError;
11
+ while (attempt <= retryLimit) {
12
+ attempt++;
13
+ try {
14
+ const result = await handle();
15
+ return result;
16
+ }
17
+ catch (error) {
18
+ if (!shouldRetry(error) || attempt > retryLimit) {
19
+ throw error;
20
+ }
21
+ lastError = error;
22
+ if (onError) {
23
+ await onError(lastError, attempt);
24
+ }
25
+ const { baseDelay, maxDelay } = backoff;
26
+ const delay = Math.min(maxDelay, baseDelay * 2 ** attempt);
27
+ await wait(Math.random() * delay);
28
+ }
29
+ }
30
+ }
package/lib/retry.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ interface RetryOptions {
2
+ retryLimit?: number;
3
+ backoff?: {
4
+ baseDelay: number;
5
+ maxDelay: number;
6
+ };
7
+ shouldRetry?: (error: unknown) => boolean;
8
+ onError?: (error: unknown, attempt: number) => Promise<void> | void;
9
+ }
10
+ export declare function withRetry<T>(handle: () => Promise<T>, { retryLimit, backoff, shouldRetry, onError, }?: RetryOptions): Promise<T>;
11
+ export {};
package/lib/retry.js ADDED
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ /* eslint-disable no-await-in-loop */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.withRetry = withRetry;
5
+ function wait(ms) {
6
+ return new Promise((resolve) => {
7
+ setTimeout(resolve, ms);
8
+ });
9
+ }
10
+ // eslint-disable-next-line consistent-return
11
+ async function withRetry(handle, { retryLimit = 30, backoff = { baseDelay: 20, maxDelay: 1000 }, shouldRetry = () => true, onError = () => { }, } = {}) {
12
+ let attempt = 0;
13
+ let lastError;
14
+ while (attempt <= retryLimit) {
15
+ attempt++;
16
+ try {
17
+ const result = await handle();
18
+ return result;
19
+ }
20
+ catch (error) {
21
+ if (!shouldRetry(error) || attempt > retryLimit) {
22
+ throw error;
23
+ }
24
+ lastError = error;
25
+ if (onError) {
26
+ await onError(lastError, attempt);
27
+ }
28
+ const { baseDelay, maxDelay } = backoff;
29
+ const delay = Math.min(maxDelay, baseDelay * 2 ** attempt);
30
+ await wait(Math.random() * delay);
31
+ }
32
+ }
33
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ocap/util",
3
- "version": "1.20.2",
3
+ "version": "1.20.3",
4
4
  "description": "utils shared across multiple forge js libs, works in both node.js and browser",
5
5
  "keywords": [
6
6
  "arcblock",
@@ -54,6 +54,9 @@
54
54
  "type": "git",
55
55
  "url": "git+https://github.com/ArcBlock/blockchain.git"
56
56
  },
57
+ "bugs": {
58
+ "url": "https://github.com/ArcBlock/blockchain/issues"
59
+ },
57
60
  "scripts": {
58
61
  "lint": "eslint src tests",
59
62
  "lint:fix": "npm run lint -- --fix",
@@ -65,9 +68,5 @@
65
68
  "build:esm": "tsc -p tsconfig.esm.json",
66
69
  "build": "npm run build:cjs && npm run build:esm",
67
70
  "build:watch": "npm run build -- -w"
68
- },
69
- "bugs": {
70
- "url": "https://github.com/ArcBlock/blockchain/issues"
71
- },
72
- "gitHead": "70ddd9b43070605879fed7dff50a9f19f5325ea8"
73
- }
71
+ }
72
+ }