util: add convertProcessSignalToExitCode utility · nodejs/node@8e900af · GitHub
Skip to content

Commit 8e900af

Browse files
ErickWendeladuh95
authored andcommitted
util: add convertProcessSignalToExitCode utility
Add convertProcessSignalToExitCode() to convert signal names to POSIX exit codes (128 + signal number). Exposed in public util API. Refs: #60720 PR-URL: #60963 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent cb54b3c commit 8e900af

5 files changed

Lines changed: 125 additions & 0 deletions

File tree

doc/api/child_process.md

Lines changed: 16 additions & 0 deletions

doc/api/util.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,38 @@ callbackFunction((err, ret) => {
8989
});
9090
```
9191

92+
## `util.convertProcessSignalToExitCode(signalCode)`
93+
94+
<!-- YAML
95+
added: REPLACEME
96+
-->
97+
98+
* `signalCode` {string} A signal name (e.g., `'SIGTERM'`, `'SIGKILL'`).
99+
* Returns: {number|null} The exit code, or `null` if the signal is invalid.
100+
101+
The `util.convertProcessSignalToExitCode()` method converts a signal name to its
102+
corresponding POSIX exit code. Following the POSIX standard, the exit code
103+
for a process terminated by a signal is calculated as `128 + signal number`.
104+
105+
```mjs
106+
import { convertProcessSignalToExitCode } from 'node:util';
107+
108+
console.log(convertProcessSignalToExitCode('SIGTERM')); // 143 (128 + 15)
109+
console.log(convertProcessSignalToExitCode('SIGKILL')); // 137 (128 + 9)
110+
console.log(convertProcessSignalToExitCode('INVALID')); // null
111+
```
112+
113+
```cjs
114+
const { convertProcessSignalToExitCode } = require('node:util');
115+
116+
console.log(convertProcessSignalToExitCode('SIGTERM')); // 143 (128 + 15)
117+
console.log(convertProcessSignalToExitCode('SIGKILL')); // 137 (128 + 9)
118+
console.log(convertProcessSignalToExitCode('INVALID')); // null
119+
```
120+
121+
This is particularly useful when working with processes to determine
122+
the exit code based on the signal that terminated the process.
123+
92124
## `util.debuglog(section[, callback])`
93125

94126
<!-- YAML

lib/internal/util.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const {
1616
ObjectGetOwnPropertyDescriptor,
1717
ObjectGetOwnPropertyDescriptors,
1818
ObjectGetPrototypeOf,
19+
ObjectKeys,
1920
ObjectPrototypeHasOwnProperty,
2021
ObjectSetPrototypeOf,
2122
ObjectValues,
@@ -108,6 +109,7 @@ function isError(e) {
108109
const codesWarned = new SafeSet();
109110

110111
let validateString;
112+
let validateOneOf;
111113

112114
function getDeprecationWarningEmitter(
113115
code, msg, deprecated, useEmitSync,
@@ -397,6 +399,17 @@ function convertToValidSignal(signal) {
397399
throw new ERR_UNKNOWN_SIGNAL(signal);
398400
}
399401

402+
function convertProcessSignalToExitCode(signalCode) {
403+
// Lazy-load to avoid a circular dependency.
404+
if (validateOneOf === undefined)
405+
({ validateOneOf } = require('internal/validators'));
406+
407+
validateOneOf(signalCode, 'signalCode', ObjectKeys(signals));
408+
409+
// POSIX standard: exit code for signal termination is 128 + signal number.
410+
return 128 + signals[signalCode];
411+
}
412+
400413
function getConstructorOf(obj) {
401414
while (obj) {
402415
const descriptor = ObjectGetOwnPropertyDescriptor(obj, 'constructor');
@@ -960,6 +973,7 @@ module.exports = {
960973
assignFunctionName,
961974
cachedResult,
962975
constructSharedArrayBuffer,
976+
convertProcessSignalToExitCode,
963977
convertToValidSignal,
964978
createClassWrapper,
965979
decorateErrorStack,

lib/util.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ const { getOptionValue } = require('internal/options');
8484
const binding = internalBinding('util');
8585

8686
const {
87+
convertProcessSignalToExitCode,
8788
deprecate: internalDeprecate,
8889
getLazy,
8990
getSystemErrorMap,
@@ -472,6 +473,7 @@ module.exports = {
472473
'The `util._extend` API is deprecated. Please use Object.assign() instead.',
473474
'DEP0060'),
474475
callbackify,
476+
convertProcessSignalToExitCode,
475477
debug: debuglog,
476478
debuglog,
477479
deprecate,
Lines changed: 61 additions & 0 deletions

0 commit comments

Comments
 (0)