timeout.js
This file
Path
lib/terminate/timeout.js
21 lines · 832 bytes · JavaScript
Last changed by
Add more code comments (#984)ehmickybefore adoptionfdd8a9f
History of this fileTake it
12345678910111213141516171819202122import {setTimeout} from 'node:timers/promises';import {DiscardedError} from '../return/final-error.js';
// Validate `timeout` optionexport const validateTimeout = ({timeout}) => { if (timeout !== undefined && (!Number.isFinite(timeout) || timeout < 0)) { throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`); }};
// Fails when the `timeout` option is exceededexport const throwOnTimeout = (subprocess, timeout, context, controller) => timeout === 0 || timeout === undefined ? [] : [killAfterTimeout(subprocess, timeout, context, controller)];
const killAfterTimeout = async (subprocess, timeout, context, {signal}) => { await setTimeout(timeout, undefined, {signal}); context.timedOut = true; subprocess.kill(); throw new DiscardedError();};