From 990babcee9b6b786afce1fa6afa2a263ed5742fe Mon Sep 17 00:00:00 2001 From: ehmicky Date: Sun, 23 May 2021 21:28:15 +0200 Subject: [PATCH] Fix `timeout` option validation --- index.js | 4 +++- lib/kill.js | 11 +++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 695fcb9be3..bbf0d2921d 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,7 @@ const npmRunPath = require('npm-run-path'); const onetime = require('onetime'); const makeError = require('./lib/error'); const normalizeStdio = require('./lib/stdio'); -const {spawnedKill, spawnedCancel, setupTimeout, setExitHandler} = require('./lib/kill'); +const {spawnedKill, spawnedCancel, setupTimeout, validateTimeout, setExitHandler} = require('./lib/kill'); const {handleInput, getSpawnedResult, makeAllStream, validateInputSync} = require('./lib/stream'); const {mergePromise, getSpawnedPromise} = require('./lib/promise'); const {joinCommand, parseCommand} = require('./lib/command'); @@ -75,6 +75,8 @@ const execa = (file, args, options) => { const parsed = handleArguments(file, args, options); const command = joinCommand(file, args); + validateTimeout(parsed.options); + let spawned; try { spawned = childProcess.spawn(parsed.file, parsed.args, parsed.options); diff --git a/lib/kill.js b/lib/kill.js index 7f6f8dd7f2..287a14238e 100644 --- a/lib/kill.js +++ b/lib/kill.js @@ -71,10 +71,6 @@ const setupTimeout = (spawned, {timeout, killSignal = 'SIGTERM'}, spawnedPromise return spawnedPromise; } - if (!Number.isFinite(timeout) || timeout < 0) { - throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`); - } - let timeoutId; const timeoutPromise = new Promise((resolve, reject) => { timeoutId = setTimeout(() => { @@ -89,6 +85,12 @@ const setupTimeout = (spawned, {timeout, killSignal = 'SIGTERM'}, spawnedPromise return Promise.race([timeoutPromise, safeSpawnedPromise]); }; +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})`); + } +}; + // `cleanup` option handling const setExitHandler = async (spawned, {cleanup, detached}, timedPromise) => { if (!cleanup || detached) { @@ -108,5 +110,6 @@ module.exports = { spawnedKill, spawnedCancel, setupTimeout, + validateTimeout, setExitHandler };