diff --git a/src/validators.ts b/src/validators.ts index f03f541..6806899 100644 --- a/src/validators.ts +++ b/src/validators.ts @@ -19,8 +19,11 @@ export function validateApiKey( return key; } +const MAX_TIMEOUT = 2147483647; // i chose this value because it is 2^32 - 1 ,largest number a signed 32-bit integer can hold + export function validateTimeout(value: number | undefined): number { const timeout = value ?? config.timeout; - if (timeout <= 0) throw new InvalidTimeoutError(); - return timeout; + + if (!(timeout > 0)) throw new InvalidTimeoutError(); + return Math.min(timeout, MAX_TIMEOUT); } diff --git a/tests/validators_test.ts b/tests/validators_test.ts index 770ebfa..a7f6abc 100644 --- a/tests/validators_test.ts +++ b/tests/validators_test.ts @@ -88,4 +88,14 @@ describe("validateTimeout", () => { assertEquals(validateTimeout(1), 1); assertEquals(validateTimeout(10000), 10000); }); + + it("with NaN", () => { + assertThrows(() => validateTimeout(NaN), InvalidTimeoutError); + }); + + it("with a value too big for setTimeout", () => { + assertEquals(validateTimeout(2147483647), 2147483647); + assertEquals(validateTimeout(2147483648), 2147483647); + assertEquals(validateTimeout(Infinity), 2147483647); + }); });