|
1 | 1 | // Note: since nyc uses this module to output coverage, any lines |
2 | 2 | // that are in the direct sync flow of nyc's outputCoverage are |
3 | 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) |
7 | | - |
8 | | -var EE = require('events') |
9 | | -/* istanbul ignore if */ |
10 | | -if (typeof EE !== 'function') { |
11 | | - EE = EE.EventEmitter |
| 4 | +// grab a reference to node's real process object right away |
| 5 | +var process = global.process |
| 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' |
12 | 17 | } |
13 | 18 |
|
14 | | -var emitter |
15 | | -if (process.__signal_exit_emitter__) { |
16 | | - emitter = process.__signal_exit_emitter__ |
| 19 | +// some kind of non-node environment, just no-op |
| 20 | +/* istanbul ignore if */ |
| 21 | +if (!processOk(process)) { |
| 22 | + module.exports = function () {} |
17 | 23 | } else { |
18 | | - emitter = process.__signal_exit_emitter__ = new EE() |
19 | | - emitter.count = 0 |
20 | | - emitter.emitted = {} |
21 | | -} |
22 | | - |
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 |
30 | | -} |
31 | | - |
32 | | -module.exports = function (cb, opts) { |
33 | | - assert.equal(typeof cb, 'function', 'a callback must be provided for exit handler') |
| 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 | + } |
34 | 33 |
|
35 | | - if (loaded === false) { |
36 | | - load() |
| 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 = {} |
37 | 41 | } |
38 | 42 |
|
39 | | - var ev = 'exit' |
40 | | - if (opts && opts.alwaysLast) { |
41 | | - ev = 'afterexit' |
| 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 |
42 | 50 | } |
43 | 51 |
|
44 | | - var remove = function () { |
45 | | - emitter.removeListener(ev, cb) |
46 | | - if (emitter.listeners('exit').length === 0 && |
47 | | - emitter.listeners('afterexit').length === 0) { |
48 | | - unload() |
| 52 | + module.exports = function (cb, opts) { |
| 53 | + /* istanbul ignore if */ |
| 54 | + if (!processOk(global.process)) { |
| 55 | + return |
49 | 56 | } |
50 | | - } |
51 | | - emitter.on(ev, cb) |
| 57 | + assert.equal(typeof cb, 'function', 'a callback must be provided for exit handler') |
52 | 58 |
|
53 | | - return remove |
54 | | -} |
| 59 | + if (loaded === false) { |
| 60 | + load() |
| 61 | + } |
55 | 62 |
|
56 | | -module.exports.unload = unload |
57 | | -function unload () { |
58 | | - if (!loaded) { |
59 | | - return |
60 | | - } |
61 | | - loaded = false |
| 63 | + var ev = 'exit' |
| 64 | + if (opts && opts.alwaysLast) { |
| 65 | + ev = 'afterexit' |
| 66 | + } |
62 | 67 |
|
63 | | - signals.forEach(function (sig) { |
64 | | - try { |
65 | | - process.removeListener(sig, sigListeners[sig]) |
66 | | - } catch (er) {} |
67 | | - }) |
68 | | - process.emit = originalProcessEmit |
69 | | - process.reallyExit = originalProcessReallyExit |
70 | | - emitter.count -= 1 |
71 | | -} |
| 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) |
72 | 76 |
|
73 | | -function emit (event, code, signal) { |
74 | | - if (emitter.emitted[event]) { |
75 | | - return |
| 77 | + return remove |
76 | 78 | } |
77 | | - emitter.emitted[event] = true |
78 | | - emitter.emit(event, code, signal) |
79 | | -} |
80 | 79 |
|
81 | | -// { <signal>: <listener fn>, ... } |
82 | | -var sigListeners = {} |
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 |
88 | | - // safely emit now. |
89 | | - var listeners = process.listeners(sig) |
90 | | - if (listeners.length === emitter.count) { |
91 | | - unload() |
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 |
99 | | - sig = 'SIGINT' |
100 | | - } |
101 | | - process.kill(process.pid, sig) |
| 80 | + var unload = function unload () { |
| 81 | + if (!loaded || !processOk(global.process)) { |
| 82 | + return |
102 | 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 |
103 | 94 | } |
104 | | -}) |
105 | | - |
106 | | -module.exports.signals = function () { |
107 | | - return signals |
108 | | -} |
| 95 | + module.exports.unload = unload |
109 | 96 |
|
110 | | -module.exports.load = load |
111 | | - |
112 | | -var loaded = false |
113 | | - |
114 | | -function load () { |
115 | | - if (loaded) { |
116 | | - return |
| 97 | + var emit = function emit (event, code, signal) { |
| 98 | + /* istanbul ignore if */ |
| 99 | + if (emitter.emitted[event]) { |
| 100 | + return |
| 101 | + } |
| 102 | + emitter.emitted[event] = true |
| 103 | + emitter.emit(event, code, signal) |
117 | 104 | } |
118 | | - loaded = true |
119 | | - |
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. |
124 | | - emitter.count += 1 |
125 | | - |
126 | | - signals = signals.filter(function (sig) { |
127 | | - try { |
128 | | - process.on(sig, sigListeners[sig]) |
129 | | - return true |
130 | | - } catch (er) { |
131 | | - return false |
| 105 | + |
| 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)) { |
| 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 | + } |
| 130 | + /* istanbul ignore next */ |
| 131 | + process.kill(process.pid, sig) |
| 132 | + } |
132 | 133 | } |
133 | 134 | }) |
134 | 135 |
|
135 | | - process.emit = processEmit |
136 | | - process.reallyExit = processReallyExit |
137 | | -} |
| 136 | + module.exports.signals = function () { |
| 137 | + return signals |
| 138 | + } |
138 | 139 |
|
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) |
147 | | -} |
| 140 | + var loaded = false |
| 141 | + |
| 142 | + var load = function load () { |
| 143 | + if (loaded || !processOk(global.process)) { |
| 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 |
148 | 167 |
|
149 | | -var originalProcessEmit = process.emit |
150 | | -function processEmit (ev, arg) { |
151 | | - if (ev === 'exit') { |
152 | | - if (arg !== undefined) { |
153 | | - process.exitCode = arg |
| 168 | + var originalProcessReallyExit = process.reallyExit |
| 169 | + var processReallyExit = function processReallyExit (code) { |
| 170 | + /* istanbul ignore if */ |
| 171 | + if (!processOk(global.process)) { |
| 172 | + return |
154 | 173 | } |
155 | | - var ret = originalProcessEmit.apply(this, arguments) |
| 174 | + process.exitCode = code || /* istanbul ignore next */ 0 |
156 | 175 | emit('exit', process.exitCode, null) |
157 | 176 | /* istanbul ignore next */ |
158 | 177 | emit('afterexit', process.exitCode, null) |
159 | | - return ret |
160 | | - } else { |
161 | | - return originalProcessEmit.apply(this, arguments) |
| 178 | + /* istanbul ignore next */ |
| 179 | + originalProcessReallyExit.call(process, process.exitCode) |
| 180 | + } |
| 181 | + |
| 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 |
| 188 | + } |
| 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 */ |
| 195 | + return ret |
| 196 | + } else { |
| 197 | + return originalProcessEmit.apply(this, arguments) |
| 198 | + } |
162 | 199 | } |
163 | 200 | } |
0 commit comments