util: add convertProcessSignalToExitCode utility

Add convertProcessSignalToExitCode() to convert signal names to POSIX
exit codes (128 + signal number). Exposed in public util API.

Refs: https://github.com/nodejs/node/pull/60720
PR-URL: https://github.com/nodejs/node/pull/60963
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
This commit is contained in:
Erick Wendel
2025-12-11 10:25:21 -03:00
committed by GitHub
parent 180c717714
commit e705603a6f
5 changed files with 125 additions and 0 deletions

View File

@@ -1557,6 +1557,10 @@ re-raise the handled signal.
See waitpid(2).
When `code` is `null` due to signal termination, you can use
[`util.convertProcessSignalToExitCode()`][] to convert the signal to a POSIX
exit code.
### Event: `'message'`
<!-- YAML
@@ -1671,6 +1675,11 @@ within the child process to close the IPC channel as well.
The `subprocess.exitCode` property indicates the exit code of the child process.
If the child process is still running, the field will be `null`.
When the child process is terminated by a signal, `subprocess.exitCode` will be
`null` and [`subprocess.signalCode`][] will be set. To get the corresponding
POSIX exit code, use
[`util.convertProcessSignalToExitCode(subprocess.signalCode)`][`util.convertProcessSignalToExitCode()`].
### `subprocess.kill([signal])`
<!-- YAML
@@ -2107,6 +2116,10 @@ connection to the child.
The `subprocess.signalCode` property indicates the signal received by
the child process if any, else `null`.
When the child process is terminated by a signal, [`subprocess.exitCode`][] will be `null`.
To get the corresponding POSIX exit code, use
[`util.convertProcessSignalToExitCode(subprocess.signalCode)`][`util.convertProcessSignalToExitCode()`].
### `subprocess.spawnargs`
* Type: {Array}
@@ -2387,12 +2400,15 @@ or [`child_process.fork()`][].
[`stdio`]: #optionsstdio
[`subprocess.connected`]: #subprocessconnected
[`subprocess.disconnect()`]: #subprocessdisconnect
[`subprocess.exitCode`]: #subprocessexitcode
[`subprocess.kill()`]: #subprocesskillsignal
[`subprocess.send()`]: #subprocesssendmessage-sendhandle-options-callback
[`subprocess.signalCode`]: #subprocesssignalcode
[`subprocess.stderr`]: #subprocessstderr
[`subprocess.stdin`]: #subprocessstdin
[`subprocess.stdio`]: #subprocessstdio
[`subprocess.stdout`]: #subprocessstdout
[`util.convertProcessSignalToExitCode()`]: util.md#utilconvertprocesssignaltoexitcodesignalcode
[`util.promisify()`]: util.md#utilpromisifyoriginal
[synchronous counterparts]: #synchronous-process-creation
[v8.serdes]: v8.md#serialization-api

View File

@@ -89,6 +89,38 @@ callbackFunction((err, ret) => {
});
```
## `util.convertProcessSignalToExitCode(signalCode)`
<!-- YAML
added: REPLACEME
-->
* `signalCode` {string} A signal name (e.g., `'SIGTERM'`, `'SIGKILL'`).
* Returns: {number|null} The exit code, or `null` if the signal is invalid.
The `util.convertProcessSignalToExitCode()` method converts a signal name to its
corresponding POSIX exit code. Following the POSIX standard, the exit code
for a process terminated by a signal is calculated as `128 + signal number`.
```mjs
import { convertProcessSignalToExitCode } from 'node:util';
console.log(convertProcessSignalToExitCode('SIGTERM')); // 143 (128 + 15)
console.log(convertProcessSignalToExitCode('SIGKILL')); // 137 (128 + 9)
console.log(convertProcessSignalToExitCode('INVALID')); // null
```
```cjs
const { convertProcessSignalToExitCode } = require('node:util');
console.log(convertProcessSignalToExitCode('SIGTERM')); // 143 (128 + 15)
console.log(convertProcessSignalToExitCode('SIGKILL')); // 137 (128 + 9)
console.log(convertProcessSignalToExitCode('INVALID')); // null
```
This is particularly useful when working with processes to determine
the exit code based on the signal that terminated the process.
## `util.debuglog(section[, callback])`
<!-- YAML