1 // Note: since nyc uses this module to output coverage, any lines
2 // that are in the direct sync flow of nyc's outputCoverage are
3 // ignored, since we can never get coverage for them.
4 var assert = require('assert')
5 var signals = require('./signals.js')
6 var isWin = /^win/i.test(process.platform)
8 var EE = require('events')
9 /* istanbul ignore if */
10 if (typeof EE !== 'function') {
15 if (process.__signal_exit_emitter__) {
16 emitter = process.__signal_exit_emitter__
18 emitter = process.__signal_exit_emitter__ = new EE()
23 // Because this emitter is a global, we have to check to see if a
24 // previous version of this library failed to enable infinite listeners.
25 // I know what you're about to say. But literally everything about
26 // signal-exit is a compromise with evil. Get used to it.
27 if (!emitter.infinite) {
28 emitter.setMaxListeners(Infinity)
29 emitter.infinite = true
32 module.exports = function (cb, opts) {
33 assert.equal(typeof cb, 'function', 'a callback must be provided for exit handler')
35 if (loaded === false) {
40 if (opts && opts.alwaysLast) {
44 var remove = function () {
45 emitter.removeListener(ev, cb)
46 if (emitter.listeners('exit').length === 0 &&
47 emitter.listeners('afterexit').length === 0) {
56 module.exports.unload = unload
63 signals.forEach(function (sig) {
65 process.removeListener(sig, sigListeners[sig])
68 process.emit = originalProcessEmit
69 process.reallyExit = originalProcessReallyExit
73 function emit (event, code, signal) {
74 if (emitter.emitted[event]) {
77 emitter.emitted[event] = true
78 emitter.emit(event, code, signal)
81 // { <signal>: <listener fn>, ... }
83 signals.forEach(function (sig) {
84 sigListeners[sig] = function listener () {
85 // If there are no other listeners, an exit is coming!
86 // Simplest way: remove us and then re-send the signal.
87 // We know that this will kill the process, so we can
89 var listeners = process.listeners(sig)
90 if (listeners.length === emitter.count) {
92 emit('exit', null, sig)
93 /* istanbul ignore next */
94 emit('afterexit', null, sig)
95 /* istanbul ignore next */
96 if (isWin && sig === 'SIGHUP') {
97 // "SIGHUP" throws an `ENOSYS` error on Windows,
98 // so use a supported signal instead
101 process.kill(process.pid, sig)
106 module.exports.signals = function () {
110 module.exports.load = load
120 // This is the number of onSignalExit's that are in play.
121 // It's important so that we can count the correct number of
122 // listeners on signals, and don't wait for the other one to
123 // handle it instead of us.
126 signals = signals.filter(function (sig) {
128 process.on(sig, sigListeners[sig])
135 process.emit = processEmit
136 process.reallyExit = processReallyExit
139 var originalProcessReallyExit = process.reallyExit
140 function processReallyExit (code) {
141 process.exitCode = code || 0
142 emit('exit', process.exitCode, null)
143 /* istanbul ignore next */
144 emit('afterexit', process.exitCode, null)
145 /* istanbul ignore next */
146 originalProcessReallyExit.call(process, process.exitCode)
149 var originalProcessEmit = process.emit
150 function processEmit (ev, arg) {
152 if (arg !== undefined) {
153 process.exitCode = arg
155 var ret = originalProcessEmit.apply(this, arguments)
156 emit('exit', process.exitCode, null)
157 /* istanbul ignore next */
158 emit('afterexit', process.exitCode, null)
161 return originalProcessEmit.apply(this, arguments)