From 54035dacc583dbd29166778e7c3a5afd1848e28e Mon Sep 17 00:00:00 2001 From: Erick Wendel Date: Fri, 14 Nov 2025 17:56:08 -0300 Subject: [PATCH 1/6] 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 --- doc/api/util.md | 32 ++++++++ lib/internal/util.js | 13 ++++ lib/util.js | 2 + .../test-util-convert-signal-to-exit-code.mjs | 76 +++++++++++++++++++ 4 files changed, 123 insertions(+) create mode 100644 test/parallel/test-util-convert-signal-to-exit-code.mjs diff --git a/doc/api/util.md b/doc/api/util.md index 9a02eb43c45b76..75ebe7130e93dd 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -89,6 +89,38 @@ callbackFunction((err, ret) => { }); ``` +## `util.convertProcessSignalToExitCode(signalCode)` + + + +* `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])`