fix(utils): clamp shared sleep timers

This commit is contained in:
Peter Steinberger
2026-05-30 17:38:58 -04:00
parent 1cf264c468
commit dbddf4093f
2 changed files with 19 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
import fs from "node:fs";
import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import { MAX_TIMER_TIMEOUT_MS } from "./shared/number-coercion.js";
import { withTempDir } from "./test-helpers/temp-dir.js";
import {
ensureDir,
@@ -33,6 +34,22 @@ describe("sleep", () => {
vi.useRealTimers();
}
});
it("clamps oversized sleep delays before scheduling", async () => {
vi.useFakeTimers();
const setTimeoutSpy = vi.spyOn(globalThis, "setTimeout");
try {
const promise = sleep(Number.MAX_SAFE_INTEGER);
expect(setTimeoutSpy).toHaveBeenCalledWith(expect.any(Function), MAX_TIMER_TIMEOUT_MS);
vi.advanceTimersByTime(MAX_TIMER_TIMEOUT_MS);
await expect(promise).resolves.toBeUndefined();
} finally {
setTimeoutSpy.mockRestore();
vi.useRealTimers();
}
});
});
describe("resolveConfigDir", () => {

View File

@@ -8,6 +8,7 @@ import {
resolveRequiredHomeDir,
} from "./infra/home-dir.js";
import { isPlainObject } from "./infra/plain-object.js";
import { resolveTimerTimeoutMs } from "./shared/number-coercion.js";
export { escapeRegExp } from "./shared/regexp.js";
export async function ensureDir(dir: string) {
@@ -57,7 +58,7 @@ export function normalizeE164(number: string): string {
}
export function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
return new Promise((resolve) => setTimeout(resolve, resolveTimerTimeoutMs(ms, 0, 0)));
}
function isHighSurrogate(codeUnit: number): boolean {