import os from 'node:os';
import {setTimeout as pSetTimeout} from 'node:timers/promises';
import {onExit} from 'signal-exit';
const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;
context.isCanceled = true;
}
};
const timeoutKill = (spawned, signal, reject) => {
spawned.kill(signal);
reject(Object.assign(new Error('Timed out'), {timedOut: true, signal}));
const killAfterTimeout = async (spawned, timeout, killSignal, controller) => {
await pSetTimeout(timeout, undefined, {ref: false, signal: controller.signal});
spawned.kill(killSignal);
throw Object.assign(new Error('Timed out'), {timedOut: true, signal: killSignal});
};
// `timeout` option handling
export const setupTimeout = (spawned, {timeout, killSignal = 'SIGTERM'}, spawnedPromise) => {
export const setupTimeout = async (spawned, {timeout, killSignal = 'SIGTERM'}, spawnedPromise) => {
if (timeout === 0 || timeout === undefined) {
return spawnedPromise;
}
let timeoutId;
const timeoutPromise = new Promise((resolve, reject) => {
timeoutId = setTimeout(() => {
timeoutKill(spawned, killSignal, reject);
}, timeout);
});
const safeSpawnedPromise = spawnedPromise.finally(() => {
clearTimeout(timeoutId);
});
return Promise.race([timeoutPromise, safeSpawnedPromise]);
const controller = new AbortController();
try {
return await Promise.race([
spawnedPromise,
killAfterTimeout(spawned, timeout, killSignal, controller),
]);
} finally {
controller.abort();
}
};
export const validateTimeout = ({timeout}) => {
if (timeout !== undefined && (!Number.isFinite(timeout) || timeout < 0)) {