| | |
| | | // ignored, since we can never get coverage for them. |
| | | // grab a reference to node's real process object right away |
| | | var process = global.process |
| | | |
| | | const processOk = function (process) { |
| | | return process && |
| | | typeof process === 'object' && |
| | | typeof process.removeListener === 'function' && |
| | | typeof process.emit === 'function' && |
| | | typeof process.reallyExit === 'function' && |
| | | typeof process.listeners === 'function' && |
| | | typeof process.kill === 'function' && |
| | | typeof process.pid === 'number' && |
| | | typeof process.on === 'function' |
| | | } |
| | | |
| | | // some kind of non-node environment, just no-op |
| | | if (typeof process !== 'object' || !process) { |
| | | module.exports = function () {} |
| | | /* istanbul ignore if */ |
| | | if (!processOk(process)) { |
| | | module.exports = function () { |
| | | return function () {} |
| | | } |
| | | } else { |
| | | var assert = require('assert') |
| | | var signals = require('./signals.js') |
| | |
| | | } |
| | | |
| | | module.exports = function (cb, opts) { |
| | | if (global.process !== process) { |
| | | return |
| | | /* istanbul ignore if */ |
| | | if (!processOk(global.process)) { |
| | | return function () {} |
| | | } |
| | | assert.equal(typeof cb, 'function', 'a callback must be provided for exit handler') |
| | | |
| | |
| | | } |
| | | |
| | | var unload = function unload () { |
| | | if (!loaded || global.process !== process) { |
| | | if (!loaded || !processOk(global.process)) { |
| | | return |
| | | } |
| | | loaded = false |
| | |
| | | module.exports.unload = unload |
| | | |
| | | var emit = function emit (event, code, signal) { |
| | | /* istanbul ignore if */ |
| | | if (emitter.emitted[event]) { |
| | | return |
| | | } |
| | |
| | | var sigListeners = {} |
| | | signals.forEach(function (sig) { |
| | | sigListeners[sig] = function listener () { |
| | | if (process !== global.process) { |
| | | /* istanbul ignore if */ |
| | | if (!processOk(global.process)) { |
| | | return |
| | | } |
| | | // If there are no other listeners, an exit is coming! |
| | |
| | | // so use a supported signal instead |
| | | sig = 'SIGINT' |
| | | } |
| | | /* istanbul ignore next */ |
| | | process.kill(process.pid, sig) |
| | | } |
| | | } |
| | |
| | | var loaded = false |
| | | |
| | | var load = function load () { |
| | | if (loaded || process !== global.process) { |
| | | if (loaded || !processOk(global.process)) { |
| | | return |
| | | } |
| | | loaded = true |
| | |
| | | |
| | | var originalProcessReallyExit = process.reallyExit |
| | | var processReallyExit = function processReallyExit (code) { |
| | | if (process !== global.process) { |
| | | /* istanbul ignore if */ |
| | | if (!processOk(global.process)) { |
| | | return |
| | | } |
| | | process.exitCode = code || 0 |
| | | process.exitCode = code || /* istanbul ignore next */ 0 |
| | | emit('exit', process.exitCode, null) |
| | | /* istanbul ignore next */ |
| | | emit('afterexit', process.exitCode, null) |
| | |
| | | |
| | | var originalProcessEmit = process.emit |
| | | var processEmit = function processEmit (ev, arg) { |
| | | if (ev === 'exit' && process === global.process) { |
| | | if (ev === 'exit' && processOk(global.process)) { |
| | | /* istanbul ignore else */ |
| | | if (arg !== undefined) { |
| | | process.exitCode = arg |
| | | } |
| | | var ret = originalProcessEmit.apply(this, arguments) |
| | | /* istanbul ignore next */ |
| | | emit('exit', process.exitCode, null) |
| | | /* istanbul ignore next */ |
| | | emit('afterexit', process.exitCode, null) |
| | | /* istanbul ignore next */ |
| | | return ret |
| | | } else { |
| | | return originalProcessEmit.apply(this, arguments) |