@@ -17,6 +17,7 @@ const DEFAULT_TIMEOUT_KILL_AFTER_MS = 5_000;
1717const PROCESS_GROUP_EXIT_POLL_MS = 25 ;
1818const POST_FORCE_KILL_WAIT_MS = 1_000 ;
1919const DEFAULT_CAPTURED_STDOUT_MAX_BYTES = 1024 * 1024 ;
20+ const MAX_TIMER_TIMEOUT_MS = 2_147_000_000 ;
2021const ACTIVE_CHILD_KILLERS = new Set ( ) ;
2122const SIGNAL_EXIT_CODES = {
2223 SIGHUP : 129 ,
@@ -65,6 +66,23 @@ function resolveTimeoutMs(envName, defaultValue) {
6566 return parsed ;
6667}
6768
69+ function numericTimerValueMs ( valueMs ) {
70+ const value = Number ( valueMs ) ;
71+ return Number . isFinite ( value ) ? Math . floor ( value ) : undefined ;
72+ }
73+
74+ function resolveTimerTimeoutMs ( valueMs , fallbackMs = MAX_TIMER_TIMEOUT_MS ) {
75+ const value = numericTimerValueMs ( valueMs ) ?? numericTimerValueMs ( fallbackMs ) ;
76+ return Math . min ( Math . max ( value ?? MAX_TIMER_TIMEOUT_MS , 1 ) , MAX_TIMER_TIMEOUT_MS ) ;
77+ }
78+
79+ function resolveOptionalTimerTimeoutMs ( valueMs ) {
80+ if ( valueMs === undefined ) {
81+ return undefined ;
82+ }
83+ return resolveTimerTimeoutMs ( valueMs , 1 ) ;
84+ }
85+
6886function readOptionValue ( argv , index , optionName ) {
6987 const value = argv [ index + 1 ] ;
7088 if ( value === undefined || value === "" || value . startsWith ( "-" ) ) {
@@ -149,6 +167,11 @@ export function parseArgs(argv) {
149167
150168function run ( command , args , cwd , options = { } ) {
151169 return new Promise ( ( resolve , reject ) => {
170+ const resolvedTimeoutMs = resolveOptionalTimerTimeoutMs ( options . timeoutMs ) ;
171+ const resolvedKillAfterMs = resolveTimerTimeoutMs (
172+ options . killAfterMs ,
173+ DEFAULT_TIMEOUT_KILL_AFTER_MS ,
174+ ) ;
152175 const useProcessGroup = process . platform !== "win32" ;
153176 const child = spawn ( command , args , {
154177 cwd,
@@ -230,21 +253,21 @@ function run(command, args, cwd, options = {}) {
230253 return ;
231254 }
232255 killChild ( "SIGKILL" ) ;
233- } , options . killAfterMs ?? DEFAULT_TIMEOUT_KILL_AFTER_MS ) ;
256+ } , resolvedKillAfterMs ) ;
234257 forceKillTimeout . unref ?. ( ) ;
235258 } ;
236259 ACTIVE_CHILD_KILLERS . add ( killChild ) ;
237260 const timeout =
238- options . timeoutMs === undefined
261+ resolvedTimeoutMs === undefined
239262 ? undefined
240263 : setTimeout ( ( ) => {
241264 timedOut = true ;
242265 terminateChild ( ) ;
243- } , options . timeoutMs ) ;
266+ } , resolvedTimeoutMs ) ;
244267 timeout ?. unref ?. ( ) ;
245268 const finishAfterTeardown = async ( error , value = "" ) => {
246269 if ( processGroupAlive ( ) ) {
247- await waitForProcessGroupExit ( options . killAfterMs ?? DEFAULT_TIMEOUT_KILL_AFTER_MS ) ;
270+ await waitForProcessGroupExit ( resolvedKillAfterMs ) ;
248271 }
249272 if ( processGroupAlive ( ) ) {
250273 killChild ( "SIGKILL" ) ;
@@ -275,7 +298,7 @@ function run(command, args, cwd, options = {}) {
275298 child . on ( "close" , ( status , signal ) => {
276299 if ( timedOut ) {
277300 void finishAfterTeardown (
278- new Error ( `${ command } ${ args . join ( " " ) } timed out after ${ options . timeoutMs } ms` ) ,
301+ new Error ( `${ command } ${ args . join ( " " ) } timed out after ${ resolvedTimeoutMs } ms` ) ,
279302 ) ;
280303 return ;
281304 }
0 commit comments