fix(node): improved exit hook

This commit is contained in:
alina 🌸 2024-07-01 01:50:43 +03:00
parent e22571b7c1
commit b3b29a1c67
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI

View file

@ -4,32 +4,33 @@ let installed = false
let handled = false let handled = false
const callbacks = new Set<() => void>() const callbacks = new Set<() => void>()
// eslint-disable-next-line func-call-spacing
const myHandlers = new Map<string, () => void>()
function exit(shouldManuallyExit: boolean, signal: number, event: string) { function register(shouldManuallyExit: boolean, signal: number, event: string) {
return function eventHandler() { function eventHandler() {
if (handled) { if (handled) {
return return
} }
handled = true handled = true
const exitCode = 128 + signal
for (const callback of callbacks) { for (const callback of callbacks) {
callback() callback()
} }
for (const [event, handler] of myHandlers) {
process.off(event, handler)
}
if (shouldManuallyExit) { if (shouldManuallyExit) {
// if the user has some custom handlers after us, we don't want to exit the process // send the signal again and let node handle it
process.kill(process.pid, signal)
}
}
const listeners = process.rawListeners(event) process.on(event, eventHandler)
const idx = listeners.indexOf(eventHandler) myHandlers.set(event, eventHandler)
if (idx === listeners.length - 1) {
process.exit(exitCode)
}
}
}
} }
export function beforeExit(fn: () => void): () => void { export function beforeExit(fn: () => void): () => void {
@ -39,10 +40,10 @@ export function beforeExit(fn: () => void): () => void {
if (!installed) { if (!installed) {
installed = true installed = true
process.on('beforeExit', exit(true, -128, 'beforeExit')) register(true, 0, 'beforeExit')
process.on('SIGINT', exit(true, 2, 'SIGINT')) register(true, 2, 'SIGINT')
process.on('SIGTERM', exit(true, 15, 'SIGINT')) register(true, 15, 'SIGTERM')
process.on('exit', exit(false, 15, 'exit')) register(false, 15, 'exit')
} }
callbacks.add(fn) callbacks.add(fn)