

























@@ -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 = {
2223SIGHUP: 129,
@@ -65,6 +66,23 @@ function resolveTimeoutMs(envName, defaultValue) {
6566return parsed;
6667}
676869+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) {
6987const value = argv[index + 1];
7088if (value === undefined || value === "" || value.startsWith("-")) {
@@ -149,6 +167,11 @@ export function parseArgs(argv) {
149167150168function run(command, args, cwd, options = {}) {
151169return new Promise((resolve, reject) => {
170+const resolvedTimeoutMs = resolveOptionalTimerTimeoutMs(options.timeoutMs);
171+const resolvedKillAfterMs = resolveTimerTimeoutMs(
172+options.killAfterMs,
173+DEFAULT_TIMEOUT_KILL_AFTER_MS,
174+);
152175const useProcessGroup = process.platform !== "win32";
153176const child = spawn(command, args, {
154177 cwd,
@@ -230,21 +253,21 @@ function run(command, args, cwd, options = {}) {
230253return;
231254}
232255killChild("SIGKILL");
233-}, options.killAfterMs ?? DEFAULT_TIMEOUT_KILL_AFTER_MS);
256+}, resolvedKillAfterMs);
234257forceKillTimeout.unref?.();
235258};
236259ACTIVE_CHILD_KILLERS.add(killChild);
237260const timeout =
238-options.timeoutMs === undefined
261+resolvedTimeoutMs === undefined
239262 ? undefined
240263 : setTimeout(() => {
241264timedOut = true;
242265terminateChild();
243-}, options.timeoutMs);
266+}, resolvedTimeoutMs);
244267timeout?.unref?.();
245268const finishAfterTeardown = async (error, value = "") => {
246269if (processGroupAlive()) {
247-await waitForProcessGroupExit(options.killAfterMs ?? DEFAULT_TIMEOUT_KILL_AFTER_MS);
270+await waitForProcessGroupExit(resolvedKillAfterMs);
248271}
249272if (processGroupAlive()) {
250273killChild("SIGKILL");
@@ -275,7 +298,7 @@ function run(command, args, cwd, options = {}) {
275298child.on("close", (status, signal) => {
276299if (timedOut) {
277300void 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);
280303return;
281304}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。