signal-exit
Advanced tools
Comparing version 4.0.3 to 4.1.0
@@ -6,4 +6,15 @@ /// <reference types="node" /> | ||
* A function that takes an exit code and signal as arguments | ||
* | ||
* In the case of signal exits *only*, a return value of true | ||
* will indicate that the signal is being handled, and we should | ||
* not synthetically exit with the signal we received. Regardless | ||
* of the handler return value, the handler is unloaded when an | ||
* otherwise fatal signal is received, so you get exactly 1 shot | ||
* at it, unless you add another onExit handler at that point. | ||
* | ||
* In the case of numeric code exits, we may already have committed | ||
* to exiting the process, for example via a fatal exception or | ||
* unhandled promise rejection, so it is impossible to stop safely. | ||
*/ | ||
export type Handler = (code: number | null | undefined, signal: NodeJS.Signals | null) => any; | ||
export type Handler = (code: number | null | undefined, signal: NodeJS.Signals | null) => true | void; | ||
export declare const | ||
@@ -10,0 +21,0 @@ /** |
@@ -23,3 +23,3 @@ "use strict"; | ||
const ObjectDefineProperty = Object.defineProperty.bind(Object); | ||
// teeny tiny ee | ||
// teeny special purpose ee | ||
class Emitter { | ||
@@ -67,8 +67,13 @@ emitted = { | ||
if (this.emitted[ev]) { | ||
return; | ||
return false; | ||
} | ||
this.emitted[ev] = true; | ||
let ret = false; | ||
for (const fn of this.listeners[ev]) { | ||
fn(code, signal); | ||
ret = fn(code, signal) === true || ret; | ||
} | ||
if (ev === 'exit') { | ||
ret = this.emit('afterExit', code, signal) || ret; | ||
} | ||
return ret; | ||
} | ||
@@ -138,6 +143,7 @@ } | ||
this.unload(); | ||
this.#emitter.emit('exit', null, sig); | ||
this.#emitter.emit('afterExit', null, sig); | ||
const ret = this.#emitter.emit('exit', null, sig); | ||
/* c8 ignore start */ | ||
process.kill(process.pid, sig === 'SIGHUP' ? this.#hupSig : sig); | ||
const s = sig === 'SIGHUP' ? this.#hupSig : sig; | ||
if (!ret) | ||
process.kill(process.pid, s); | ||
/* c8 ignore stop */ | ||
@@ -225,3 +231,2 @@ } | ||
this.#emitter.emit('exit', this.#process.exitCode, null); | ||
this.#emitter.emit('afterExit', this.#process.exitCode, null); | ||
return this.#originalProcessReallyExit.call(this.#process, this.#process.exitCode); | ||
@@ -240,3 +245,2 @@ } | ||
this.#emitter.emit('exit', this.#process.exitCode, null); | ||
this.#emitter.emit('afterExit', this.#process.exitCode, null); | ||
/* c8 ignore stop */ | ||
@@ -243,0 +247,0 @@ return ret; |
@@ -6,4 +6,15 @@ /// <reference types="node" /> | ||
* A function that takes an exit code and signal as arguments | ||
* | ||
* In the case of signal exits *only*, a return value of true | ||
* will indicate that the signal is being handled, and we should | ||
* not synthetically exit with the signal we received. Regardless | ||
* of the handler return value, the handler is unloaded when an | ||
* otherwise fatal signal is received, so you get exactly 1 shot | ||
* at it, unless you add another onExit handler at that point. | ||
* | ||
* In the case of numeric code exits, we may already have committed | ||
* to exiting the process, for example via a fatal exception or | ||
* unhandled promise rejection, so it is impossible to stop safely. | ||
*/ | ||
export type Handler = (code: number | null | undefined, signal: NodeJS.Signals | null) => any; | ||
export type Handler = (code: number | null | undefined, signal: NodeJS.Signals | null) => true | void; | ||
export declare const | ||
@@ -10,0 +21,0 @@ /** |
@@ -19,3 +19,3 @@ // Note: since nyc uses this module to output coverage, any lines | ||
const ObjectDefineProperty = Object.defineProperty.bind(Object); | ||
// teeny tiny ee | ||
// teeny special purpose ee | ||
class Emitter { | ||
@@ -63,8 +63,13 @@ emitted = { | ||
if (this.emitted[ev]) { | ||
return; | ||
return false; | ||
} | ||
this.emitted[ev] = true; | ||
let ret = false; | ||
for (const fn of this.listeners[ev]) { | ||
fn(code, signal); | ||
ret = fn(code, signal) === true || ret; | ||
} | ||
if (ev === 'exit') { | ||
ret = this.emit('afterExit', code, signal) || ret; | ||
} | ||
return ret; | ||
} | ||
@@ -134,6 +139,7 @@ } | ||
this.unload(); | ||
this.#emitter.emit('exit', null, sig); | ||
this.#emitter.emit('afterExit', null, sig); | ||
const ret = this.#emitter.emit('exit', null, sig); | ||
/* c8 ignore start */ | ||
process.kill(process.pid, sig === 'SIGHUP' ? this.#hupSig : sig); | ||
const s = sig === 'SIGHUP' ? this.#hupSig : sig; | ||
if (!ret) | ||
process.kill(process.pid, s); | ||
/* c8 ignore stop */ | ||
@@ -221,3 +227,2 @@ } | ||
this.#emitter.emit('exit', this.#process.exitCode, null); | ||
this.#emitter.emit('afterExit', this.#process.exitCode, null); | ||
return this.#originalProcessReallyExit.call(this.#process, this.#process.exitCode); | ||
@@ -236,3 +241,2 @@ } | ||
this.#emitter.emit('exit', this.#process.exitCode, null); | ||
this.#emitter.emit('afterExit', this.#process.exitCode, null); | ||
/* c8 ignore stop */ | ||
@@ -239,0 +243,0 @@ return ret; |
{ | ||
"name": "signal-exit", | ||
"version": "4.0.3", | ||
"version": "4.1.0", | ||
"description": "when you want to fire an event no matter how a process exits.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/cjs/index.js", |
@@ -43,2 +43,25 @@ # signal-exit | ||
### Capturing Signal Exits | ||
If the handler returns an exact boolean `true`, and the exit is a | ||
due to signal, then the signal will be considered handled, and | ||
will _not_ trigger a synthetic `process.kill(process.pid, | ||
signal)` after firing the `onExit` handlers. | ||
In this case, it your responsibility as the caller to exit with a | ||
signal (for example, by calling `process.kill()`) if you wish to | ||
preserve the same exit status that would otherwise have occurred. | ||
If you do not, then the process will likely exit gracefully with | ||
status 0 at some point, assuming that no other terminating signal | ||
or other exit trigger occurs. | ||
Prior to calling handlers, the `onExit` machinery is unloaded, so | ||
any subsequent exits or signals will not be handled, even if the | ||
signal is captured and the exit is thus prevented. | ||
Note that numeric code exits may indicate that the process is | ||
already committed to exiting, for example due to a fatal | ||
exception or unhandled promise rejection, and so there is no way to | ||
prevent it safely. | ||
### Browser Fallback | ||
@@ -45,0 +68,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
76966
815
75