Skip to content

Commit bc44179

Browse files
committed
fix: sanitize initialDelayMs in retryOnTransientError to prevent warnings
Signed-off-by: leocavalcante <[email protected]>
1 parent 81c6d7b commit bc44179

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/paths.mjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ function delay(ms) {
177177
*/
178178
export async function retryOnTransientError(fn, options = {}) {
179179
const { retries = 3, initialDelayMs = 100 } = options
180+
// Sanitize initialDelayMs: clamp negative to 0, handle NaN by using default
181+
const sanitizedDelayMs = Number.isNaN(initialDelayMs)
182+
? 100
183+
: Math.max(0, initialDelayMs)
180184
let lastError
181185

182186
for (let attempt = 0; attempt <= retries; attempt++) {
@@ -191,8 +195,8 @@ export async function retryOnTransientError(fn, options = {}) {
191195
throw err
192196
}
193197

194-
// Calculate exponential backoff delay: initialDelayMs * 2^attempt
195-
const backoffDelay = initialDelayMs * 2 ** attempt
198+
// Calculate exponential backoff delay: sanitizedDelayMs * 2^attempt
199+
const backoffDelay = sanitizedDelayMs * 2 ** attempt
196200
await delay(backoffDelay)
197201
}
198202
}

tests/paths.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ describe("paths.mjs exports", () => {
595595
expect(elapsed).toBeLessThan(50)
596596
})
597597

598-
it("should handle negative initialDelayMs (treated as ~1ms delay by setTimeout)", async () => {
598+
it("should handle negative initialDelayMs (clamped to 0ms)", async () => {
599599
let callCount = 0
600600
const start = Date.now()
601601
await retryOnTransientError(
@@ -611,7 +611,7 @@ describe("paths.mjs exports", () => {
611611
)
612612
const elapsed = Date.now() - start
613613
expect(callCount).toBe(2)
614-
// Negative delay is clamped to ~1ms by setTimeout, should complete quickly
614+
// Negative delay is clamped to 0ms, should complete quickly
615615
expect(elapsed).toBeLessThan(50)
616616
})
617617

@@ -632,11 +632,10 @@ describe("paths.mjs exports", () => {
632632
expect(callCount).toBe(0) // Loop never runs
633633
})
634634

635-
it("should handle non-numeric initialDelayMs (NaN becomes ~1ms delay)", async () => {
635+
it("should handle non-numeric initialDelayMs (NaN falls back to default 100ms)", async () => {
636636
let callCount = 0
637637
const start = Date.now()
638-
// When initialDelayMs is NaN, delay calculation produces NaN
639-
// setTimeout with NaN delay treats it as ~1ms
638+
// When initialDelayMs is NaN, it is sanitized to the default 100ms
640639
await retryOnTransientError(
641640
() => {
642641
callCount++
@@ -650,8 +649,9 @@ describe("paths.mjs exports", () => {
650649
)
651650
const elapsed = Date.now() - start
652651
expect(callCount).toBe(2)
653-
// NaN delay becomes ~1ms
654-
expect(elapsed).toBeLessThan(50)
652+
// NaN delay falls back to default 100ms
653+
expect(elapsed).toBeGreaterThanOrEqual(90)
654+
expect(elapsed).toBeLessThan(200)
655655
})
656656

657657
it("should handle Infinity retries (up to a reasonable test limit)", async () => {

0 commit comments

Comments
 (0)