Skip to content
Myr

timeout.js

This file

Path

lib/terminate/timeout.js

33 lines · 1.2 KiB · JavaScript

Last changed by

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 1–2why, one step back
1import {setTimeout} from 'node:timers/promises';
2import {DiscardedError} from '../return/final-error.js';

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

line 3why, one step back
3import {getTypeName} from '../utils/type-name.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 4–6why, one step back
4
5// Validate `timeout` option6export const validateTimeout = ({timeout}) => {

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

lines 7–8why, one step back
7 if (timeout !== undefined) {
8 validateDelay(timeout, 'timeout');

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 9–10why, one step back
9 }
10};

Fix invalid `timeout` and `forceKillAfterDelay` valuesSindre Sorhusbefore adoptiond9a5689

line 11why, one step back
11

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

lines 12–18why, one step back
12// Used by the `timeout` and `forceKillAfterDelay` options.
13// Fractional numbers are excluded since `spawnSync()` requires an integer `timeout`.14export const validateDelay = (delay, optionName) => {15 if (!Number.isInteger(delay) || delay < 0 || delay > MAX_DELAY) {16 throw new TypeError(`Expected the \`${optionName}\` option to be a non-negative integer up to ${MAX_DELAY}, got \`${delay}\` (${getTypeName(delay)})`);17 }18};

Fix invalid `timeout` and `forceKillAfterDelay` valuesSindre Sorhusbefore adoptiond9a5689

lines 19–20why, one step back
19
20// Timers fire right away when their delay is larger than this

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

line 21why, one step back
21const MAX_DELAY = (2 ** 31) - 1;

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 22–23why, one step back
22
23// Fails when the `timeout` option is exceeded

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 24why, one step back
24export const throwOnTimeout = (kill, timeout, context, controller) => timeout === 0 || timeout === undefined

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 25why, one step back
25 ? []

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 26why, one step back
26 : [killAfterTimeout(kill, timeout, context, controller)];

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 27why, one step back
27

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 28why, one step back
28const killAfterTimeout = async (kill, timeout, context, {signal}) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 29why, one step back
29 await setTimeout(timeout, undefined, {signal});

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 30why, one step back
30 context.terminationReason ??= 'timeout';

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 31why, one step back
31 kill();

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 32–33why, one step back
32 throw new DiscardedError();
33};