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
Provenance
- Commit
f446745- Tree
8a53546- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
8d8e7a4
3 files changed+94 added−7 removed
modified:source/core/Ky.ts+18 added-7 removed
78 unmodified lines79 };80 81 const invalidSchemaMessage = 'The `schema` argument must follow the Standard Schema specification';82 83+// 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.85+const validateTimeoutOption = (value: unknown, name: 'timeout' | 'totalTimeout'): void => {86+ if (value === false) {87+ return;88+ }89+90+ 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+ }93+94+ if (value > maxSafeTimeout) {95+ throw new RangeError(`The \`${name}\` option cannot be greater than ${maxSafeTimeout}`);96+ }97+};98+99 const 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 lines230 231 const ky = new Ky(input, initHookOptions);232 233 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');236 222- if (typeof ky.#options.totalTimeout === 'number' && ky.#options.totalTimeout > maxSafeTimeout) {223- throw new RangeError(`The \`totalTimeout\` option cannot be greater than ${maxSafeTimeout}`);224- }225-237 // 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 linesadded:test/validation.ts+75 added
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475import test from 'ava';import ky from '../source/index.js';
const okFetch = async () => new Response('ok');
// `timeout` and `totalTimeout` are documented as milliseconds or `false`. Anything else used to be passed// straight to `setTimeout()`, which clamps it to ~1ms, or was silently ignored, so the request never got the// timeout the caller asked for. `null` is left out: it is nullish, so it selects the default.test('rejects non-numeric and negative `timeout` values', async t => { for (const timeout of ['1000', Number.NaN, -1, -0.5, {}]) { // eslint-disable-next-line no-await-in-loop await t.throwsAsync( ky('https://example.com', {timeout: timeout as never, fetch: okFetch}).text(), { name: 'TypeError', message: 'The `timeout` option must be a non-negative number or `false`', }, `timeout: ${String(timeout)}`, ); }});
test('rejects non-numeric and negative `totalTimeout` values', async t => { for (const totalTimeout of ['1000', Number.NaN, -1, -0.5, {}]) { // eslint-disable-next-line no-await-in-loop await t.throwsAsync( ky('https://example.com', {totalTimeout: totalTimeout as never, fetch: okFetch}).text(), { name: 'TypeError', message: 'The `totalTimeout` option must be a non-negative number or `false`', }, `totalTimeout: ${String(totalTimeout)}`, ); }});
test('treats a nullish timeout as absent', async t => { t.is(await ky('https://example.com', {timeout: null as never, totalTimeout: null as never, fetch: okFetch}).text(), 'ok');});
test('rejects `timeout` and `totalTimeout` above the maximum safe value', async t => { for (const option of ['timeout', 'totalTimeout'] as const) { // eslint-disable-next-line no-await-in-loop await t.throwsAsync( ky('https://example.com', {[option]: 2_147_483_648, fetch: okFetch}).text(), { instanceOf: RangeError, message: `The \`${option}\` option cannot be greater than 2147483647`, }, option, ); }});
test('accepts `false`, `0` and finite non-negative numbers for the timeout options', async t => { t.is(await ky('https://example.com', {timeout: false, totalTimeout: false, fetch: okFetch}).text(), 'ok'); t.is(await ky('https://example.com', {timeout: 0, fetch: async () => new Response('a'), totalTimeout: 5_000}).text(), 'a');});
test('rejects invalid timeout values set from an `init` hook', async t => { await t.throwsAsync( ky('https://example.com', { fetch: okFetch, hooks: { init: [options => { options.timeout = 'soon' as never; }], }, }).text(), { name: 'TypeError', message: 'The `timeout` option must be a non-negative number or `false`', }, );});modified:.gitignore+1 added
1 node_modules/2 yarn.lock3 distribution/4 package-lock.json5+Stuff/