signal-exit
Advanced tools
Comparing version 1.2.0 to 1.3.0
53
index.js
var assert = require('assert') | ||
module.exports = function (cb) { | ||
module.exports = function (cb, opts) { | ||
assert.equal(typeof cb, 'function', 'a callback must be provided for exit handler') | ||
var emittedExit = false | ||
var emittedExit = false, | ||
listenerMap = {}, | ||
listeners = [] | ||
opts = opts || {} | ||
opts.minimumListeners = opts.maxListeners || 1 | ||
Object.keys(signals).forEach(function (sig) { | ||
var listener = function () { | ||
// If there are no other listeners, do the default action. | ||
if (process.listeners(sig).length === 1) { | ||
if (process.listeners(sig).length <= opts.maxListeners) { | ||
process.removeListener(sig, listener) | ||
@@ -18,2 +23,5 @@ cb(process.exitCode || signals[sig], sig) | ||
listenerMap[sig] = listener | ||
listeners.push(listener) | ||
try { | ||
@@ -24,9 +32,40 @@ process.on(sig, listener) | ||
process.on('exit', function (code) { | ||
var listener = function (code) { | ||
if (emittedExit) return | ||
emittedExit = true | ||
return cb(code, undefined) | ||
}) | ||
} | ||
listenerMap['exit'] = listener | ||
listeners.push(listener) | ||
process.on('exit', listener) | ||
if (opts.alwaysLast) monkeyPatchAddListener(listenerMap, listeners) | ||
} | ||
// in some cases we always want to ensure that the | ||
// onExit handler registered with signal-exit is | ||
// the last event handler to fire, e.g, for code coverage. | ||
function monkeyPatchAddListener (listenerMap, listeners) { | ||
var events = Object.keys(listenerMap), | ||
listener | ||
process.on = process.addListener = (function (on) { | ||
return function (ev, fn) { | ||
for (var i = 0, event; (event = events[i]) !== undefined; i++) { | ||
listener = listenerMap[event] | ||
if (ev === event && listeners.indexOf(fn) === -1) { | ||
process.removeListener(ev, listener) | ||
var ret = on.call(process, ev, fn) | ||
on.call(process, ev, listener) | ||
return ret | ||
} | ||
} | ||
return on.apply(this, arguments) | ||
} | ||
})(process.on) | ||
} | ||
var signals = { | ||
@@ -65,3 +104,3 @@ 'SIGALRM': 142, | ||
var nonFatalSignals = [ | ||
/* var nonFatalSignals = [ | ||
'SIGWINCH', // resize window. | ||
@@ -76,2 +115,2 @@ 'SIGUSR1', // debugger. | ||
'SIGURG' // out of band data. | ||
] | ||
] */ |
{ | ||
"name": "signal-exit", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "when you want process.on('exit') to fire when a process is killed with a signal.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "nyc tap ./test/*.js" | ||
"test": "standard && nyc tap ./test/*.js" | ||
}, | ||
@@ -25,3 +25,3 @@ "repository": { | ||
"chai": "^2.3.0", | ||
"nyc": "^2.0.0", | ||
"nyc": "^2.0.4", | ||
"standard": "^3.9.0", | ||
@@ -28,0 +28,0 @@ "tap": "^1.0.4" |
@@ -5,4 +5,4 @@ var onSignalExit = require('../../') | ||
console.log('exited with sigint, ' + code + ', ' + signal) | ||
}) | ||
}, {maxListeners: 2}) | ||
process.kill(process.pid, 'SIGINT') |
@@ -7,3 +7,3 @@ var onSignalExit = require('../../') | ||
calledListener, code, signal) | ||
}) | ||
}, {maxListeners: 2}) | ||
@@ -10,0 +10,0 @@ process.on('SIGHUP', listener) |
@@ -5,4 +5,4 @@ var onSignalExit = require('../../') | ||
console.log('exited with sigterm, ' + code + ', ' + signal) | ||
}) | ||
}, {maxListeners: 2}) | ||
process.kill(process.pid, 'SIGTERM') |
@@ -51,2 +51,12 @@ /* global describe, it */ | ||
}) | ||
it('ensures that if alwaysLast=true, the handler is run last', function (done) { | ||
exec(process.execPath + ' ./test/fixtures/signal-last.js', function (err, stdout, stderr) { | ||
assert.equal(err.code, null) | ||
assert.equal(err.signal, 'SIGHUP') | ||
stdout.should.match(/first counter=1/) | ||
stdout.should.match(/last counter=2/) | ||
done() | ||
}) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8340
12
199