11import os from 'node:os' ;
22import process from 'node:process' ;
33
4- const isWindows = os . platform ( ) === 'win32' ;
5-
64// POSIX shells convention: exit status = 128 + signal number
75// https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html#:~:text=When%20a%20command%20terminates%20on%20a%20fatal%20signal%20whose%20number%20is%20N%2C%20Bash%20uses%20the%20value%20128%2BN%20as%20the%20exit%20status.
86const UNIX_SIGNAL_EXIT_CODE_OFFSET = 128 ;
97const unixSignalExitCode = ( signalNumber : number ) =>
108 UNIX_SIGNAL_EXIT_CODE_OFFSET + signalNumber ;
119
1210const SIGINT_CODE = 2 ;
11+ const SIGTERM_CODE = 15 ;
12+ const SIGQUIT_CODE = 3 ;
1313
1414export const SIGNAL_EXIT_CODES = ( ) : Record < SignalName , number > => {
1515 const isWindowsRuntime = os . platform ( ) === 'win32' ;
1616 return {
1717 SIGINT : isWindowsRuntime ? SIGINT_CODE : unixSignalExitCode ( SIGINT_CODE ) ,
18- SIGTERM : unixSignalExitCode ( 15 ) ,
19- SIGQUIT : unixSignalExitCode ( 3 ) ,
18+ SIGTERM : unixSignalExitCode ( SIGTERM_CODE ) ,
19+ SIGQUIT : unixSignalExitCode ( SIGQUIT_CODE ) ,
2020 } ;
2121} ;
2222
@@ -39,6 +39,7 @@ export type ExitHandlerOptions = {
3939} ;
4040
4141export function installExitHandlers ( options : ExitHandlerOptions = { } ) : void {
42+ // eslint-disable-next-line functional/no-let
4243 let closedReason : CloseReason | undefined ;
4344 const {
4445 onClose,
@@ -49,40 +50,47 @@ export function installExitHandlers(options: ExitHandlerOptions = {}): void {
4950 } = options ;
5051
5152 const close = ( code : number , reason : CloseReason ) => {
52- if ( closedReason ) return ;
53+ if ( closedReason ) {
54+ return ;
55+ }
5356 closedReason = reason ;
5457 onClose ?.( code , reason ) ;
5558 } ;
5659
5760 process . on ( 'uncaughtException' , err => {
5861 onError ?.( err , 'uncaughtException' ) ;
59- if ( fatalExit )
62+ if ( fatalExit ) {
6063 close ( fatalExitCode , {
6164 kind : 'fatal' ,
6265 fatal : 'uncaughtException' ,
6366 } ) ;
67+ }
6468 } ) ;
6569
6670 process . on ( 'unhandledRejection' , reason => {
6771 onError ?.( reason , 'unhandledRejection' ) ;
68- if ( fatalExit )
72+ if ( fatalExit ) {
6973 close ( fatalExitCode , {
7074 kind : 'fatal' ,
7175 fatal : 'unhandledRejection' ,
7276 } ) ;
77+ }
7378 } ) ;
7479
7580 ( [ 'SIGINT' , 'SIGTERM' , 'SIGQUIT' ] as const ) . forEach ( signal => {
7681 process . on ( signal , ( ) => {
7782 close ( SIGNAL_EXIT_CODES ( ) [ signal ] , { kind : 'signal' , signal } ) ;
7883 if ( signalExit ) {
84+ // eslint-disable-next-line n/no-process-exit
7985 process . exit ( SIGNAL_EXIT_CODES ( ) [ signal ] ) ;
8086 }
8187 } ) ;
8288 } ) ;
8389
8490 process . on ( 'exit' , code => {
85- if ( closedReason ) return ;
91+ if ( closedReason ) {
92+ return ;
93+ }
8694 close ( code , { kind : 'exit' } ) ;
8795 } ) ;
8896}
0 commit comments