@forwardimpact/libutil 0.1.83 → 0.1.84

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/retry.js +8 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forwardimpact/libutil",
3
- "version": "0.1.83",
3
+ "version": "0.1.84",
4
4
  "description": "Cross-cutting utilities: retry, hashing, token counting, and project discovery.",
5
5
  "keywords": [
6
6
  "util",
@@ -44,7 +44,7 @@
44
44
  "@forwardimpact/libtelemetry": "^0.1.22"
45
45
  },
46
46
  "devDependencies": {
47
- "@forwardimpact/libharness": "^0.1.5"
47
+ "@forwardimpact/libmock": "^0.1.0"
48
48
  },
49
49
  "engines": {
50
50
  "bun": ">=1.2.0",
package/src/retry.js CHANGED
@@ -1,19 +1,25 @@
1
+ const defaultSleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
2
+
1
3
  /**
2
4
  * Retry class for handling transient errors with exponential backoff
3
5
  */
4
6
  export class Retry {
5
7
  #retries;
6
8
  #delay;
9
+ #sleep;
7
10
 
8
11
  /**
9
12
  * Creates a new Retry instance
10
13
  * @param {object} [config] - Optional configuration object
11
14
  * @param {number} [config.retries] - Maximum number of retry attempts (default: 10)
12
15
  * @param {number} [config.delay] - Initial delay in milliseconds (default: 1000)
16
+ * @param {(ms: number) => Promise<void>} [config.sleep] - Injectable sleep
17
+ * for deterministic tests (default: real setTimeout-based wait).
13
18
  */
14
19
  constructor(config = {}) {
15
20
  this.#retries = config.retries ?? 10;
16
21
  this.#delay = config.delay ?? 1000;
22
+ this.#sleep = config.sleep ?? defaultSleep;
17
23
  }
18
24
 
19
25
  /**
@@ -87,7 +93,7 @@ export class Retry {
87
93
  const exponentialDelay = this.#delay * Math.pow(2, attempt);
88
94
  const jitter = Math.random() * 0.3 * exponentialDelay;
89
95
  const wait = exponentialDelay + jitter;
90
- await new Promise((resolve) => setTimeout(resolve, wait));
96
+ await this.#sleep(wait);
91
97
  continue;
92
98
  }
93
99
 
@@ -100,7 +106,7 @@ export class Retry {
100
106
  const exponentialDelay = this.#delay * Math.pow(2, attempt);
101
107
  const jitter = Math.random() * 0.3 * exponentialDelay;
102
108
  const wait = exponentialDelay + jitter;
103
- await new Promise((resolve) => setTimeout(resolve, wait));
109
+ await this.#sleep(wait);
104
110
  continue;
105
111
  }
106
112