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 // grab a reference to node's real process object right away
5 var process = global.process
7 const processOk = function (process) {
9 typeof process === 'object' &&
10 typeof process.removeListener === 'function' &&
11 typeof process.emit === 'function' &&
12 typeof process.reallyExit === 'function' &&
13 typeof process.listeners === 'function' &&
14 typeof process.kill === 'function' &&
15 typeof process.pid === 'number' &&
16 typeof process.on === 'function'
19 // some kind of non-node environment, just no-op
20 /* istanbul ignore if */
21 if (!processOk(process)) {
22 module.exports = function () {}
24 var assert = require('assert')
25 var signals = require('./signals.js')
26 var isWin = /^win/i.test(process.platform)
28 var EE = require('events')
29 /* istanbul ignore if */
30 if (typeof EE !== 'function') {
35 if (process.__signal_exit_emitter__) {
36 emitter = process.__signal_exit_emitter__
38 emitter = process.__signal_exit_emitter__ = new EE()
43 // Because this emitter is a global, we have to check to see if a
44 // previous version of this library failed to enable infinite listeners.
45 // I know what you're about to say. But literally everything about
46 // signal-exit is a compromise with evil. Get used to it.
47 if (!emitter.infinite) {
48 emitter.setMaxListeners(Infinity)
49 emitter.infinite = true
52 module.exports = function (cb, opts) {
53 /* istanbul ignore if */
54 if (!processOk(global.process)) {
57 assert.equal(typeof cb, 'function', 'a callback must be provided for exit handler')
59 if (loaded === false) {
64 if (opts && opts.alwaysLast) {
68 var remove = function () {
69 emitter.removeListener(ev, cb)
70 if (emitter.listeners('exit').length === 0 &&
71 emitter.listeners('afterexit').length === 0) {
80 var unload = function unload () {
81 if (!loaded || !processOk(global.process)) {
86 signals.forEach(function (sig) {
88 process.removeListener(sig, sigListeners[sig])
91 process.emit = originalProcessEmit
92 process.reallyExit = originalProcessReallyExit
95 module.exports.unload = unload
97 var emit = function emit (event, code, signal) {
98 /* istanbul ignore if */
99 if (emitter.emitted[event]) {
102 emitter.emitted[event] = true
103 emitter.emit(event, code, signal)
106 // { <signal>: <listener fn>, ... }
107 var sigListeners = {}
108 signals.forEach(function (sig) {
109 sigListeners[sig] = function listener () {
110 /* istanbul ignore if */
111 if (!processOk(global.process)) {
114 // If there are no other listeners, an exit is coming!
115 // Simplest way: remove us and then re-send the signal.
116 // We know that this will kill the process, so we can
118 var listeners = process.listeners(sig)
119 if (listeners.length === emitter.count) {
121 emit('exit', null, sig)
122 /* istanbul ignore next */
123 emit('afterexit', null, sig)
124 /* istanbul ignore next */
125 if (isWin && sig === 'SIGHUP') {
126 // "SIGHUP" throws an `ENOSYS` error on Windows,
127 // so use a supported signal instead
130 /* istanbul ignore next */
131 process.kill(process.pid, sig)
136 module.exports.signals = function () {
142 var load = function load () {
143 if (loaded || !processOk(global.process)) {
148 // This is the number of onSignalExit's that are in play.
149 // It's important so that we can count the correct number of
150 // listeners on signals, and don't wait for the other one to
151 // handle it instead of us.
154 signals = signals.filter(function (sig) {
156 process.on(sig, sigListeners[sig])
163 process.emit = processEmit
164 process.reallyExit = processReallyExit
166 module.exports.load = load
168 var originalProcessReallyExit = process.reallyExit
169 var processReallyExit = function processReallyExit (code) {
170 /* istanbul ignore if */
171 if (!processOk(global.process)) {
174 process.exitCode = code || /* istanbul ignore next */ 0
175 emit('exit', process.exitCode, null)
176 /* istanbul ignore next */
177 emit('afterexit', process.exitCode, null)
178 /* istanbul ignore next */
179 originalProcessReallyExit.call(process, process.exitCode)
182 var originalProcessEmit = process.emit
183 var processEmit = function processEmit (ev, arg) {
184 if (ev === 'exit' && processOk(global.process)) {
185 /* istanbul ignore else */
186 if (arg !== undefined) {
187 process.exitCode = arg
189 var ret = originalProcessEmit.apply(this, arguments)
190 /* istanbul ignore next */
191 emit('exit', process.exitCode, null)
192 /* istanbul ignore next */
193 emit('afterexit', process.exitCode, null)
194 /* istanbul ignore next */
197 return originalProcessEmit.apply(this, arguments)