Skip to content

Commit 427c5c2

Browse files
authoredMay 30, 2021
Fix timeout option validation (#463)
1 parent edf28a6 commit 427c5c2

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed
 

‎index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const npmRunPath = require('npm-run-path');
77
const onetime = require('onetime');
88
const makeError = require('./lib/error');
99
const normalizeStdio = require('./lib/stdio');
10-
const {spawnedKill, spawnedCancel, setupTimeout, setExitHandler} = require('./lib/kill');
10+
const {spawnedKill, spawnedCancel, setupTimeout, validateTimeout, setExitHandler} = require('./lib/kill');
1111
const {handleInput, getSpawnedResult, makeAllStream, validateInputSync} = require('./lib/stream');
1212
const {mergePromise, getSpawnedPromise} = require('./lib/promise');
1313
const {joinCommand, parseCommand} = require('./lib/command');
@@ -75,6 +75,8 @@ const execa = (file, args, options) => {
7575
const parsed = handleArguments(file, args, options);
7676
const command = joinCommand(file, args);
7777

78+
validateTimeout(parsed.options);
79+
7880
let spawned;
7981
try {
8082
spawned = childProcess.spawn(parsed.file, parsed.args, parsed.options);

‎lib/kill.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ const setupTimeout = (spawned, {timeout, killSignal = 'SIGTERM'}, spawnedPromise
7171
return spawnedPromise;
7272
}
7373

74-
if (!Number.isFinite(timeout) || timeout < 0) {
75-
throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`);
76-
}
77-
7874
let timeoutId;
7975
const timeoutPromise = new Promise((resolve, reject) => {
8076
timeoutId = setTimeout(() => {
@@ -89,6 +85,12 @@ const setupTimeout = (spawned, {timeout, killSignal = 'SIGTERM'}, spawnedPromise
8985
return Promise.race([timeoutPromise, safeSpawnedPromise]);
9086
};
9187

88+
const validateTimeout = ({timeout}) => {
89+
if (timeout !== undefined && (!Number.isFinite(timeout) || timeout < 0)) {
90+
throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`);
91+
}
92+
};
93+
9294
// `cleanup` option handling
9395
const setExitHandler = async (spawned, {cleanup, detached}, timedPromise) => {
9496
if (!cleanup || detached) {
@@ -108,5 +110,6 @@ module.exports = {
108110
spawnedKill,
109111
spawnedCancel,
110112
setupTimeout,
113+
validateTimeout,
111114
setExitHandler
112115
};

0 commit comments

Comments
 (0)
Please sign in to comment.