Files
openclaw/src/process/child-process-bridge.ts
Peter Steinberger e2ec8283c4 refactor(deadcode): trim mid-size src exports (#105888)
* refactor(deadcode): trim auto-reply and CLI exports

* refactor(deadcode): trim cron and task exports

* refactor(deadcode): trim fleet and process exports

* test(deadcode): exercise live task and process seams

* test(fleet): cover stream redaction through owner module

* refactor(security): trim dead internal exports

* refactor(secrets): trim dead internal exports

* refactor(deadcode): trim remaining src exports

* refactor(deadcode): remove test-only runtime exports

* refactor(deadcode): trim pairing test exports

* refactor(deadcode): reconcile refreshed baseline

* test(auto-reply): deduplicate queue state imports
2026-07-13 00:42:56 -07:00

51 lines
1.4 KiB
TypeScript

// Child process bridge adapts child process events into typed lifecycle callbacks.
import type { ChildProcess } from "node:child_process";
import process from "node:process";
/** Signal forwarding options for a child process bridge. */
type ChildProcessBridgeOptions = {
signals?: NodeJS.Signals[];
onSignal?: (signal: NodeJS.Signals) => void;
};
const defaultSignals: NodeJS.Signals[] =
process.platform === "win32"
? ["SIGTERM", "SIGINT", "SIGBREAK"]
: ["SIGTERM", "SIGINT", "SIGHUP", "SIGQUIT"];
/** Forwards process termination signals to a child and detaches on child exit/error. */
export function attachChildProcessBridge(
child: ChildProcess,
{ signals = defaultSignals, onSignal }: ChildProcessBridgeOptions = {},
): { detach: () => void } {
const listeners = new Map<NodeJS.Signals, () => void>();
for (const signal of signals) {
const listener = (): void => {
onSignal?.(signal);
try {
child.kill(signal);
} catch {
// ignore
}
};
try {
process.on(signal, listener);
listeners.set(signal, listener);
} catch {
// Unsupported signal on this platform.
}
}
const detach = (): void => {
for (const [signal, listener] of listeners) {
process.off(signal, listener);
}
listeners.clear();
};
child.once("exit", detach);
child.once("error", detach);
return { detach };
}