openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
38 lines (37 loc) • 940 B
JavaScript
import process from "node:process";
//#region src/process/child-process-bridge.ts
const defaultSignals = process.platform === "win32" ? [
"SIGTERM",
"SIGINT",
"SIGBREAK"
] : [
"SIGTERM",
"SIGINT",
"SIGHUP",
"SIGQUIT"
];
/** Forwards process termination signals to a child and detaches on child exit/error. */
function attachChildProcessBridge(child, { signals = defaultSignals, onSignal } = {}) {
const listeners = /* @__PURE__ */ new Map();
for (const signal of signals) {
const listener = () => {
onSignal?.(signal);
try {
child.kill(signal);
} catch {}
};
try {
process.on(signal, listener);
listeners.set(signal, listener);
} catch {}
}
const detach = () => {
for (const [signal, listener] of listeners) process.off(signal, listener);
listeners.clear();
};
child.once("exit", detach);
child.once("error", detach);
return { detach };
}
//#endregion
export { attachChildProcessBridge as t };