[6a3a178] | 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
|
---|
[e29cc2e] | 6 |
|
---|
| 7 | const processOk = function (process) {
|
---|
| 8 | return 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'
|
---|
| 17 | }
|
---|
| 18 |
|
---|
[6a3a178] | 19 | // some kind of non-node environment, just no-op
|
---|
[e29cc2e] | 20 | /* istanbul ignore if */
|
---|
| 21 | if (!processOk(process)) {
|
---|
[6a3a178] | 22 | module.exports = function () {}
|
---|
| 23 | } else {
|
---|
| 24 | var assert = require('assert')
|
---|
| 25 | var signals = require('./signals.js')
|
---|
| 26 | var isWin = /^win/i.test(process.platform)
|
---|
| 27 |
|
---|
| 28 | var EE = require('events')
|
---|
| 29 | /* istanbul ignore if */
|
---|
| 30 | if (typeof EE !== 'function') {
|
---|
| 31 | EE = EE.EventEmitter
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | var emitter
|
---|
| 35 | if (process.__signal_exit_emitter__) {
|
---|
| 36 | emitter = process.__signal_exit_emitter__
|
---|
| 37 | } else {
|
---|
| 38 | emitter = process.__signal_exit_emitter__ = new EE()
|
---|
| 39 | emitter.count = 0
|
---|
| 40 | emitter.emitted = {}
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 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
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | module.exports = function (cb, opts) {
|
---|
[e29cc2e] | 53 | /* istanbul ignore if */
|
---|
| 54 | if (!processOk(global.process)) {
|
---|
[6a3a178] | 55 | return
|
---|
| 56 | }
|
---|
| 57 | assert.equal(typeof cb, 'function', 'a callback must be provided for exit handler')
|
---|
| 58 |
|
---|
| 59 | if (loaded === false) {
|
---|
| 60 | load()
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | var ev = 'exit'
|
---|
| 64 | if (opts && opts.alwaysLast) {
|
---|
| 65 | ev = 'afterexit'
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | var remove = function () {
|
---|
| 69 | emitter.removeListener(ev, cb)
|
---|
| 70 | if (emitter.listeners('exit').length === 0 &&
|
---|
| 71 | emitter.listeners('afterexit').length === 0) {
|
---|
| 72 | unload()
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 | emitter.on(ev, cb)
|
---|
| 76 |
|
---|
| 77 | return remove
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | var unload = function unload () {
|
---|
[e29cc2e] | 81 | if (!loaded || !processOk(global.process)) {
|
---|
[6a3a178] | 82 | return
|
---|
| 83 | }
|
---|
| 84 | loaded = false
|
---|
| 85 |
|
---|
| 86 | signals.forEach(function (sig) {
|
---|
| 87 | try {
|
---|
| 88 | process.removeListener(sig, sigListeners[sig])
|
---|
| 89 | } catch (er) {}
|
---|
| 90 | })
|
---|
| 91 | process.emit = originalProcessEmit
|
---|
| 92 | process.reallyExit = originalProcessReallyExit
|
---|
| 93 | emitter.count -= 1
|
---|
| 94 | }
|
---|
| 95 | module.exports.unload = unload
|
---|
| 96 |
|
---|
| 97 | var emit = function emit (event, code, signal) {
|
---|
[e29cc2e] | 98 | /* istanbul ignore if */
|
---|
[6a3a178] | 99 | if (emitter.emitted[event]) {
|
---|
| 100 | return
|
---|
| 101 | }
|
---|
| 102 | emitter.emitted[event] = true
|
---|
| 103 | emitter.emit(event, code, signal)
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | // { <signal>: <listener fn>, ... }
|
---|
| 107 | var sigListeners = {}
|
---|
| 108 | signals.forEach(function (sig) {
|
---|
| 109 | sigListeners[sig] = function listener () {
|
---|
[e29cc2e] | 110 | /* istanbul ignore if */
|
---|
| 111 | if (!processOk(global.process)) {
|
---|
[6a3a178] | 112 | return
|
---|
| 113 | }
|
---|
| 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
|
---|
| 117 | // safely emit now.
|
---|
| 118 | var listeners = process.listeners(sig)
|
---|
| 119 | if (listeners.length === emitter.count) {
|
---|
| 120 | unload()
|
---|
| 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
|
---|
| 128 | sig = 'SIGINT'
|
---|
| 129 | }
|
---|
[e29cc2e] | 130 | /* istanbul ignore next */
|
---|
[6a3a178] | 131 | process.kill(process.pid, sig)
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 | })
|
---|
| 135 |
|
---|
| 136 | module.exports.signals = function () {
|
---|
| 137 | return signals
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | var loaded = false
|
---|
| 141 |
|
---|
| 142 | var load = function load () {
|
---|
[e29cc2e] | 143 | if (loaded || !processOk(global.process)) {
|
---|
[6a3a178] | 144 | return
|
---|
| 145 | }
|
---|
| 146 | loaded = true
|
---|
| 147 |
|
---|
| 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.
|
---|
| 152 | emitter.count += 1
|
---|
| 153 |
|
---|
| 154 | signals = signals.filter(function (sig) {
|
---|
| 155 | try {
|
---|
| 156 | process.on(sig, sigListeners[sig])
|
---|
| 157 | return true
|
---|
| 158 | } catch (er) {
|
---|
| 159 | return false
|
---|
| 160 | }
|
---|
| 161 | })
|
---|
| 162 |
|
---|
| 163 | process.emit = processEmit
|
---|
| 164 | process.reallyExit = processReallyExit
|
---|
| 165 | }
|
---|
| 166 | module.exports.load = load
|
---|
| 167 |
|
---|
| 168 | var originalProcessReallyExit = process.reallyExit
|
---|
| 169 | var processReallyExit = function processReallyExit (code) {
|
---|
[e29cc2e] | 170 | /* istanbul ignore if */
|
---|
| 171 | if (!processOk(global.process)) {
|
---|
[6a3a178] | 172 | return
|
---|
| 173 | }
|
---|
[e29cc2e] | 174 | process.exitCode = code || /* istanbul ignore next */ 0
|
---|
[6a3a178] | 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)
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | var originalProcessEmit = process.emit
|
---|
| 183 | var processEmit = function processEmit (ev, arg) {
|
---|
[e29cc2e] | 184 | if (ev === 'exit' && processOk(global.process)) {
|
---|
| 185 | /* istanbul ignore else */
|
---|
[6a3a178] | 186 | if (arg !== undefined) {
|
---|
| 187 | process.exitCode = arg
|
---|
| 188 | }
|
---|
| 189 | var ret = originalProcessEmit.apply(this, arguments)
|
---|
[e29cc2e] | 190 | /* istanbul ignore next */
|
---|
[6a3a178] | 191 | emit('exit', process.exitCode, null)
|
---|
| 192 | /* istanbul ignore next */
|
---|
| 193 | emit('afterexit', process.exitCode, null)
|
---|
[e29cc2e] | 194 | /* istanbul ignore next */
|
---|
[6a3a178] | 195 | return ret
|
---|
| 196 | } else {
|
---|
| 197 | return originalProcessEmit.apply(this, arguments)
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 | }
|
---|