opticedge-cloud-utils 1.1.7 → 1.1.8

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/dist/retry.js CHANGED
@@ -66,7 +66,8 @@ async function retry(fn, opts = {}) {
66
66
  const start = Date.now();
67
67
  let attempt = 0;
68
68
  let lastErr;
69
- for (; attempt <= retries;) {
69
+ /* eslint-disable no-constant-condition */
70
+ while (true) {
70
71
  if (signal?.aborted)
71
72
  throw new Error('Aborted');
72
73
  try {
@@ -80,8 +81,8 @@ async function retry(fn, opts = {}) {
80
81
  if (attempt >= retries) {
81
82
  break;
82
83
  }
83
- const nextAttempt = attempt + 1;
84
- const exp = Math.min(max, base * Math.pow(2, attempt));
84
+ attempt++;
85
+ const exp = Math.min(max, base * Math.pow(2, attempt - 1));
85
86
  const delay = Math.floor(Math.random() * exp);
86
87
  if (typeof timeoutMs === 'number') {
87
88
  const elapsed = Date.now() - start;
@@ -90,14 +91,13 @@ async function retry(fn, opts = {}) {
90
91
  }
91
92
  }
92
93
  try {
93
- onRetry?.(err, nextAttempt, delay);
94
+ onRetry?.(err, attempt, delay);
94
95
  /* eslint-disable @typescript-eslint/no-unused-vars */
95
96
  }
96
97
  catch (e) {
97
98
  // ignore errors from onRetry
98
99
  }
99
100
  await sleep(delay, signal);
100
- attempt = nextAttempt;
101
101
  }
102
102
  }
103
103
  const finalErr = new Error(`Retry failed after ${attempt} retries: ${String(lastErr?.message ?? lastErr)}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opticedge-cloud-utils",
3
- "version": "1.1.7",
3
+ "version": "1.1.8",
4
4
  "description": "Common utilities for cloud functions",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/retry.ts CHANGED
@@ -82,7 +82,8 @@ export async function retry<T>(fn: () => Promise<T>, opts: RetryOptions = {}): P
82
82
  let attempt = 0
83
83
  let lastErr: any
84
84
 
85
- for (; attempt <= retries; ) {
85
+ /* eslint-disable no-constant-condition */
86
+ while (true) {
86
87
  if (signal?.aborted) throw new Error('Aborted')
87
88
 
88
89
  try {
@@ -98,9 +99,8 @@ export async function retry<T>(fn: () => Promise<T>, opts: RetryOptions = {}): P
98
99
  break
99
100
  }
100
101
 
101
- const nextAttempt = attempt + 1
102
-
103
- const exp = Math.min(max, base * Math.pow(2, attempt))
102
+ attempt++
103
+ const exp = Math.min(max, base * Math.pow(2, attempt - 1))
104
104
  const delay = Math.floor(Math.random() * exp)
105
105
 
106
106
  if (typeof timeoutMs === 'number') {
@@ -111,15 +111,13 @@ export async function retry<T>(fn: () => Promise<T>, opts: RetryOptions = {}): P
111
111
  }
112
112
 
113
113
  try {
114
- onRetry?.(err, nextAttempt, delay)
114
+ onRetry?.(err, attempt, delay)
115
115
  /* eslint-disable @typescript-eslint/no-unused-vars */
116
116
  } catch (e) {
117
117
  // ignore errors from onRetry
118
118
  }
119
119
 
120
120
  await sleep(delay, signal)
121
-
122
- attempt = nextAttempt
123
121
  }
124
122
  }
125
123