Skip to content

Commit

Permalink
fix: hide process signal handler (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Oct 19, 2022
1 parent 38c7c79 commit b7dddfb
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/preflight.cts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,24 @@ if (process.send) {
* Since we're setting a custom signal handler, we need to emulate the
* default behavior when there are no other handlers set
*/
if (process.rawListeners(signal).length === 1) {
process.stdout.write('\n');

if (process.listenerCount(signal) === 0) {
// eslint-disable-next-line unicorn/no-process-exit
process.exit(128 + osConstants.signals[signal]);
}
}

process.on('SIGINT', relaySignal);
process.on('SIGTERM', relaySignal);
const relaySignals = ['SIGINT', 'SIGTERM'] as const;
for (const signal of relaySignals) {
process.on(signal, relaySignal);
}

// Reduce the listenerCount to hide the one set above
const { listenerCount } = process;
process.listenerCount = function (eventName) {
let count = Reflect.apply(listenerCount, this, arguments);
if (relaySignals.includes(eventName as any)) {
count -= 1;
}
return count;
};
}

0 comments on commit b7dddfb

Please sign in to comment.