UNPKG

@mesh-tech/mesh-cli

Version:

CLI for Mesh platform development utilities

36 lines (35 loc) 1.54 kB
import { MeshCliError } from "./errors.js"; const MIN_SIGNALABLE_PID = 2; function unsignalableReason(pid) { if (typeof pid !== "number") { return `expected an integer, got ${typeof pid} (${String(pid)})`; } if (!Number.isInteger(pid)) { return `expected an integer, got ${String(pid)}`; } if (pid === 0) { return "0 is not a pid — kill(0) signals every process in this shell's own process group"; } if (pid === 1) { return "1 is init — and teardown negates first, so kill(-1) would signal every process you can signal"; } if (pid < MIN_SIGNALABLE_PID) { return `${pid} is negative — that is already a process-group id, not a pid`; } if (pid === process.pid) { return `${pid} is this CLI process itself — signalling it (or its group) would kill the command doing the teardown`; } if (pid === process.ppid) { return `${pid} is this CLI's parent (your shell) — signalling its group would kill your terminal session`; } return null; } export function assertSignalablePid(pid, source) { const reason = unsignalableReason(pid); if (reason === null) return; throw new MeshCliError(`Refusing to signal ${source.what}${reason}. ` + (source.file ? `The recorded value is corrupt or stale; signalling it could terminate unrelated processes.` : `Signalling it could terminate unrelated processes.`), source.file ? { remediation: { command: `rm ${source.file}` } } : {}); }