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