capture-exit
Advanced tools
Comparing version 1.0.5 to 1.0.6
14
index.js
@@ -53,2 +53,4 @@ var RSVP = require('rsvp'); | ||
var firstExitCode; | ||
module.exports.captureExit = function() { | ||
@@ -81,12 +83,16 @@ if (exit) { | ||
var own = lastTime = module.exports._flush(lastTime, code) | ||
if (firstExitCode === undefined) { | ||
firstExitCode = code; | ||
} | ||
var own = lastTime = module.exports._flush(lastTime, firstExitCode) | ||
.then(function() { | ||
// if another chain has started, let it exit | ||
if (own !== lastTime) { return; } | ||
exit.call(process, code); | ||
exit.call(process, firstExitCode); | ||
}) | ||
.catch(function(error) { | ||
// if another chain has started, let it exit | ||
if (own !== lastTime) { return; } | ||
console.error(error); | ||
if (own !== lastTime) { | ||
throw error; | ||
} | ||
exit.call(process, 1); | ||
@@ -93,0 +99,0 @@ }); |
{ | ||
"name": "capture-exit", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "safely cleanup in signal handlers", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
67
test.js
@@ -83,20 +83,30 @@ var expect = require('chai').expect; | ||
it('works (multiple exists)', function() { | ||
it('works (multiple exits)', function() { | ||
var exitWasCalled = 0; | ||
var onExitWasCalled = 0; | ||
var deferred; | ||
var lastDeferred = RSVP.defer(); | ||
process.exit = function stubExit(code) { | ||
exitWasCalled++; | ||
expect(code).to.equal('the expected code'); | ||
try { | ||
expect(code).to.equal('the expected code'); | ||
lastDeferred.resolve(); | ||
} catch(e) { | ||
lastDeferred.reject(e); | ||
} | ||
}; | ||
var deferred; | ||
exit.captureExit(); | ||
exit.onExit(function() { | ||
exit.onExit(function(code) { | ||
onExitWasCalled++; | ||
deferred = RSVP.defer(); | ||
expect(code).to.equal('the expected code'); | ||
return deferred.promise; | ||
}); | ||
process.exit('the expected code'); | ||
process.exit('NOT the expected code'); | ||
process.exit('the expected code'); | ||
@@ -114,4 +124,51 @@ expect(exitWasCalled).to.equal(0); | ||
expect(onExitWasCalled).to.equal(1); | ||
return lastDeferred.promise; | ||
}); | ||
}); | ||
it('exits with 1 if a prior exit handler throws', function() { | ||
var deferred; | ||
var lastDeferred = RSVP.defer(); | ||
var exitWasCalled = 0; | ||
var onExitWasCalled = 0; | ||
process.exit = function stubExit(code) { | ||
exitWasCalled++; | ||
try { | ||
expect(code).to.equal(1); | ||
lastDeferred.resolve(); | ||
} catch(e) { | ||
lastDeferred.reject(e); | ||
} | ||
}; | ||
exit.captureExit(); | ||
exit.onExit(function(code) { | ||
onExitWasCalled++; | ||
deferred = RSVP.defer(); | ||
throw new Error('bad things are bad'); | ||
}); | ||
process.exit('NOT the expected code'); | ||
process.exit('NOT the expected code'); | ||
expect(exitWasCalled).to.equal(0); | ||
expect(onExitWasCalled).to.equal(0); | ||
return delay(100).then(function() { | ||
deferred.resolve(); | ||
return deferred.promise.then(function() { | ||
expect(onExitWasCalled).to.equal(1); | ||
}); | ||
}).finally(function() { | ||
expect(onExitWasCalled).to.equal(1); | ||
return lastDeferred.promise; | ||
}).finally(function () { | ||
expect(exitWasCalled).to.equal(1); | ||
}); | ||
}); | ||
}); | ||
@@ -118,0 +175,0 @@ }); |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
26627
12
448
1