Skip to content
Myr

Reject invalid `timeout` and `totalTimeout` values

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sindre Sorhus · committed by Sindre Sorhus · parent 8d8e7a4 · Browse files · 3 files+94 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+94 added−7 removed

  • tests:+75 added−0 removed
  • other:+19 added−7 removed

3 files+94 added−7 removed

modified:source/core/Ky.ts+18 added-7 removed
78 unmodified lines
79};8081const invalidSchemaMessage = 'The `schema` argument must follow the Standard Schema specification';8283// Both timeout options are milliseconds or `false`. A non-finite or negative value used to reach `setTimeout()`,84// which silently clamps it to ~1ms, or to be ignored entirely when `totalTimeout` was not a number.85const validateTimeoutOption = (value: unknown, name: 'timeout' | 'totalTimeout'): void => {86 if (value === false) {87 return;88 }8990 if (typeof value !== 'number' || !Number.isFinite(value) || value < 0) {91 throw new TypeError(`The \`${name}\` option must be a non-negative number or \`false\``);92 }9394 if (value > maxSafeTimeout) {95 throw new RangeError(`The \`${name}\` option cannot be greater than ${maxSafeTimeout}`);96 }97};9899const cloneRetryOptions = (retry: RetryOptions | number): RetryOptions | number => {100 if (retry === null || typeof retry !== 'object' || Array.isArray(retry)) {101 return retry as RetryOptions | number;102 }
127 unmodified lines
230231 const ky = new Ky(input, initHookOptions);232233 const function_ = async (): Promise<Response | void> => {218 if (typeof ky.#options.timeout === 'number' && ky.#options.timeout > maxSafeTimeout) {219 throw new RangeError(`The \`timeout\` option cannot be greater than ${maxSafeTimeout}`);220 }234 validateTimeoutOption(ky.#options.totalTimeout, 'totalTimeout');235 validateTimeoutOption(ky.#options.timeout, 'timeout');236222 if (typeof ky.#options.totalTimeout === 'number' && ky.#options.totalTimeout > maxSafeTimeout) {223 throw new RangeError(`The \`totalTimeout\` option cannot be greater than ${maxSafeTimeout}`);224 }225237 // Delay the fetch so that body method shortcuts can set the Accept header238 await Promise.resolve();239 const beforeRequestResponse = await ky.#runBeforeRequestHooks();240 if (beforeRequestResponse !== undefined) {
1217 unmodified lines
added:test/validation.ts+75 added
modified:.gitignore+1 added
1node_modules/2yarn.lock3distribution/4package-lock.json5Stuff/