source: trip-planner-front/node_modules/signal-exit/index.js@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.9 KB
RevLine 
[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
5var process = global.process
6// some kind of non-node environment, just no-op
7if (typeof process !== 'object' || !process) {
8 module.exports = function () {}
9} else {
10 var assert = require('assert')
11 var signals = require('./signals.js')
12 var isWin = /^win/i.test(process.platform)
13
14 var EE = require('events')
15 /* istanbul ignore if */
16 if (typeof EE !== 'function') {
17 EE = EE.EventEmitter
18 }
19
20 var emitter
21 if (process.__signal_exit_emitter__) {
22 emitter = process.__signal_exit_emitter__
23 } else {
24 emitter = process.__signal_exit_emitter__ = new EE()
25 emitter.count = 0
26 emitter.emitted = {}
27 }
28
29 // Because this emitter is a global, we have to check to see if a
30 // previous version of this library failed to enable infinite listeners.
31 // I know what you're about to say. But literally everything about
32 // signal-exit is a compromise with evil. Get used to it.
33 if (!emitter.infinite) {
34 emitter.setMaxListeners(Infinity)
35 emitter.infinite = true
36 }
37
38 module.exports = function (cb, opts) {
39 if (global.process !== process) {
40 return
41 }
42 assert.equal(typeof cb, 'function', 'a callback must be provided for exit handler')
43
44 if (loaded === false) {
45 load()
46 }
47
48 var ev = 'exit'
49 if (opts && opts.alwaysLast) {
50 ev = 'afterexit'
51 }
52
53 var remove = function () {
54 emitter.removeListener(ev, cb)
55 if (emitter.listeners('exit').length === 0 &&
56 emitter.listeners('afterexit').length === 0) {
57 unload()
58 }
59 }
60 emitter.on(ev, cb)
61
62 return remove
63 }
64
65 var unload = function unload () {
66 if (!loaded || global.process !== process) {
67 return
68 }
69 loaded = false
70
71 signals.forEach(function (sig) {
72 try {
73 process.removeListener(sig, sigListeners[sig])
74 } catch (er) {}
75 })
76 process.emit = originalProcessEmit
77 process.reallyExit = originalProcessReallyExit
78 emitter.count -= 1
79 }
80 module.exports.unload = unload
81
82 var emit = function emit (event, code, signal) {
83 if (emitter.emitted[event]) {
84 return
85 }
86 emitter.emitted[event] = true
87 emitter.emit(event, code, signal)
88 }
89
90 // { <signal>: <listener fn>, ... }
91 var sigListeners = {}
92 signals.forEach(function (sig) {
93 sigListeners[sig] = function listener () {
94 if (process !== global.process) {
95 return
96 }
97 // If there are no other listeners, an exit is coming!
98 // Simplest way: remove us and then re-send the signal.
99 // We know that this will kill the process, so we can
100 // safely emit now.
101 var listeners = process.listeners(sig)
102 if (listeners.length === emitter.count) {
103 unload()
104 emit('exit', null, sig)
105 /* istanbul ignore next */
106 emit('afterexit', null, sig)
107 /* istanbul ignore next */
108 if (isWin && sig === 'SIGHUP') {
109 // "SIGHUP" throws an `ENOSYS` error on Windows,
110 // so use a supported signal instead
111 sig = 'SIGINT'
112 }
113 process.kill(process.pid, sig)
114 }
115 }
116 })
117
118 module.exports.signals = function () {
119 return signals
120 }
121
122 var loaded = false
123
124 var load = function load () {
125 if (loaded || process !== global.process) {
126 return
127 }
128 loaded = true
129
130 // This is the number of onSignalExit's that are in play.
131 // It's important so that we can count the correct number of
132 // listeners on signals, and don't wait for the other one to
133 // handle it instead of us.
134 emitter.count += 1
135
136 signals = signals.filter(function (sig) {
137 try {
138 process.on(sig, sigListeners[sig])
139 return true
140 } catch (er) {
141 return false
142 }
143 })
144
145 process.emit = processEmit
146 process.reallyExit = processReallyExit
147 }
148 module.exports.load = load
149
150 var originalProcessReallyExit = process.reallyExit
151 var processReallyExit = function processReallyExit (code) {
152 if (process !== global.process) {
153 return
154 }
155 process.exitCode = code || 0
156 emit('exit', process.exitCode, null)
157 /* istanbul ignore next */
158 emit('afterexit', process.exitCode, null)
159 /* istanbul ignore next */
160 originalProcessReallyExit.call(process, process.exitCode)
161 }
162
163 var originalProcessEmit = process.emit
164 var processEmit = function processEmit (ev, arg) {
165 if (ev === 'exit' && process === global.process) {
166 if (arg !== undefined) {
167 process.exitCode = arg
168 }
169 var ret = originalProcessEmit.apply(this, arguments)
170 emit('exit', process.exitCode, null)
171 /* istanbul ignore next */
172 emit('afterexit', process.exitCode, null)
173 return ret
174 } else {
175 return originalProcessEmit.apply(this, arguments)
176 }
177 }
178}
Note: See TracBrowser for help on using the repository browser.