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')
7 var EE = require('events')
8 /* istanbul ignore if */
9 if (typeof EE !== 'function') {
14 if (process.__signal_exit_emitter__) {
15 emitter = process.__signal_exit_emitter__
17 emitter = process.__signal_exit_emitter__ = new EE()
22 // Because this emitter is a global, we have to check to see if a
23 // previous version of this library failed to enable infinite listeners.
24 // I know what you're about to say. But literally everything about
25 // signal-exit is a compromise with evil. Get used to it.
26 if (!emitter.infinite) {
27 emitter.setMaxListeners(Infinity)
28 emitter.infinite = true
31 module.exports = function (cb, opts) {
32 assert.equal(typeof cb, 'function', 'a callback must be provided for exit handler')
34 if (loaded === false) {
39 if (opts && opts.alwaysLast) {
43 var remove = function () {
44 emitter.removeListener(ev, cb)
45 if (emitter.listeners('exit').length === 0 &&
46 emitter.listeners('afterexit').length === 0) {
55 module.exports.unload = unload
62 signals.forEach(function (sig) {
64 process.removeListener(sig, sigListeners[sig])
67 process.emit = originalProcessEmit
68 process.reallyExit = originalProcessReallyExit
72 function emit (event, code, signal) {
73 if (emitter.emitted[event]) {
76 emitter.emitted[event] = true
77 emitter.emit(event, code, signal)
80 // { <signal>: <listener fn>, ... }
82 signals.forEach(function (sig) {
83 sigListeners[sig] = function listener () {
84 // If there are no other listeners, an exit is coming!
85 // Simplest way: remove us and then re-send the signal.
86 // We know that this will kill the process, so we can
88 var listeners = process.listeners(sig)
89 if (listeners.length === emitter.count) {
91 emit('exit', null, sig)
92 /* istanbul ignore next */
93 emit('afterexit', null, sig)
94 /* istanbul ignore next */
95 process.kill(process.pid, sig)
100 module.exports.signals = function () {
104 module.exports.load = load
114 // This is the number of onSignalExit's that are in play.
115 // It's important so that we can count the correct number of
116 // listeners on signals, and don't wait for the other one to
117 // handle it instead of us.
120 signals = signals.filter(function (sig) {
122 process.on(sig, sigListeners[sig])
129 process.emit = processEmit
130 process.reallyExit = processReallyExit
133 var originalProcessReallyExit = process.reallyExit
134 function processReallyExit (code) {
135 process.exitCode = code || 0
136 emit('exit', process.exitCode, null)
137 /* istanbul ignore next */
138 emit('afterexit', process.exitCode, null)
139 /* istanbul ignore next */
140 originalProcessReallyExit.call(process, process.exitCode)
143 var originalProcessEmit = process.emit
144 function processEmit (ev, arg) {
146 if (arg !== undefined) {
147 process.exitCode = arg
149 var ret = originalProcessEmit.apply(this, arguments)
150 emit('exit', process.exitCode, null)
151 /* istanbul ignore next */
152 emit('afterexit', process.exitCode, null)
155 return originalProcessEmit.apply(this, arguments)