Skip to content
Myr

signal.js

This file

Path

lib/terminate/signal.js

70 lines · 2.4 KiB · JavaScript

Last changed by

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

History of this file

Take it

Improve validation and typing of signals (#1025)ehmickybefore adoption7213287

line 1why, one step back
1import {constants} from 'node:os';

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 2why, one step back
2import {signalsByName} from 'human-signals';

Improve validation and typing of signals (#1025)ehmickybefore adoption7213287

lines 3–67why, one step back
3
4// Normalize signals for comparison purpose.5// Also validate the signal exists.6export const normalizeKillSignal = killSignal => {7 const optionName = 'option `killSignal`';8 if (killSignal === 0) {9 throw new TypeError(`Invalid ${optionName}: 0 cannot be used.`);10 }1112 return normalizeSignal(killSignal, optionName);13};1415export const normalizeSignalArgument = signal => signal === 016 ? signal17 : normalizeSignal(signal, '`subprocess.kill()`\'s argument');1819const normalizeSignal = (signalNameOrInteger, optionName) => {20 if (Number.isInteger(signalNameOrInteger)) {21 return normalizeSignalInteger(signalNameOrInteger, optionName);22 }2324 if (typeof signalNameOrInteger === 'string') {25 return normalizeSignalName(signalNameOrInteger, optionName);26 }2728 throw new TypeError(`Invalid ${optionName} ${String(signalNameOrInteger)}: it must be a string or an integer.\n${getAvailableSignals()}`);29};3031const normalizeSignalInteger = (signalInteger, optionName) => {32 if (signalsIntegerToName.has(signalInteger)) {33 return signalsIntegerToName.get(signalInteger);34 }3536 throw new TypeError(`Invalid ${optionName} ${signalInteger}: this signal integer does not exist.\n${getAvailableSignals()}`);37};3839const getSignalsIntegerToName = () => new Map(Object.entries(constants.signals)40 .reverse()41 .map(([signalName, signalInteger]) => [signalInteger, signalName]));4243const signalsIntegerToName = getSignalsIntegerToName();4445const normalizeSignalName = (signalName, optionName) => {46 if (signalName in constants.signals) {47 return signalName;48 }4950 if (signalName.toUpperCase() in constants.signals) {51 throw new TypeError(`Invalid ${optionName} '${signalName}': please rename it to '${signalName.toUpperCase()}'.`);52 }5354 throw new TypeError(`Invalid ${optionName} '${signalName}': this signal name does not exist.\n${getAvailableSignals()}`);55};5657const getAvailableSignals = () => `Available signal names: ${getAvailableSignalNames()}.58Available signal numbers: ${getAvailableSignalIntegers()}.`;5960const getAvailableSignalNames = () => Object.keys(constants.signals)61 .sort()62 .map(signalName => `'${signalName}'`)63 .join(', ');6465const getAvailableSignalIntegers = () => [...new Set(Object.values(constants.signals)66 .sort((signalInteger, signalIntegerTwo) => signalInteger - signalIntegerTwo))]67 .join(', ');

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

lines 68–70why, one step back
68
69// Human-friendly description of a signal70export const getSignalDescription = signal => signalsByName[signal].description;