Skip to content

Commit

Permalink
chore: update dependencies, lint with xo
Browse files Browse the repository at this point in the history
  • Loading branch information
Tapani Moilanen committed Oct 13, 2016
1 parent 98b78cb commit 5a02a74
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 53 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
node_modules

/.idea

coverage
.nyc_output

npm-debug.log
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
sudo: false
language: node_js
node_js:
- '5'
- '6'
- '4'
- '0.12'
before_install:
Expand Down
31 changes: 16 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ function exit(exit, code, err) {
}
hooks.map(runHook.bind(null, 0, null));

if (!waitingFor) {
// No asynchronous hooks, exit immediately
doExit();
} else {
if (waitingFor) {
// Force exit after x ms (10000 by default), even if async hooks in progress
setTimeout(function() {
setTimeout(function () {
doExit();
}, asyncTimeoutMs);
} else {
// No asynchronous hooks, exit immediately
doExit();
}

// Runs a single hook
Expand All @@ -58,14 +58,15 @@ function exit(exit, code, err) {

// Async hook callback, decrements waiting counter
function stepTowardExit() {
process.nextTick(function() {
process.nextTick(function () {
if (--waitingFor === 0) {
doExit();
}
});
}

var doExitDone = false;

function doExit() {
if (doExitDone) {
return;
Expand Down Expand Up @@ -94,7 +95,7 @@ function add(hook) {
// PM2 Cluster shutdown message. Caught to support async handlers with pm2, needed because
// explicitly calling process.exit() doesn't trigger the beforeExit event, and the exit
// event cannot support async handlers, since the event loop is never called after it.
add.hookEvent('message', 0, function(msg) {
add.hookEvent('message', 0, function (msg) {
if (msg !== 'shutdown') {
return true;
}
Expand All @@ -103,14 +104,14 @@ function add(hook) {
}

// New signal / event to hook
add.hookEvent = function(event, code, filter) {
events[event] = function() {
add.hookEvent = function (event, code, filter) {
events[event] = function () {
for (var i = 0; i < filters.length; i++) {
if (filters[i].apply(this, arguments)) {
return;
}
}
exit(code != null, code);
exit(code !== undefined && code !== null, code);
};

if (!filters[event]) {
Expand All @@ -124,25 +125,25 @@ add.hookEvent = function(event, code, filter) {
};

// Unhook signal / event
add.unhookEvent = function(event) {
add.unhookEvent = function (event) {
process.removeListener(event, events[event]);
delete events[event];
delete filters[event];
};

// List hooked events
add.hookedEvents = function() {
add.hookedEvents = function () {
var ret = [];
for (var name in events) {
if (events.hasOwnProperty(name)) {
if ({}.hasOwnProperty.call(events, name)) {
ret.push(name);
}
}
return ret;
};

// Add an uncaught exception handler
add.uncaughtExceptionHandler = function(hook) {
add.uncaughtExceptionHandler = function (hook) {
errHooks.push(hook);

if (errHooks.length === 1) {
Expand All @@ -151,7 +152,7 @@ add.uncaughtExceptionHandler = function(hook) {
};

// Configure async force exit timeout
add.forceExitTimeout = function(ms) {
add.forceExitTimeout = function (ms) {
asyncTimeoutMs = ms;
};

Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "async-exit-hook",
"version": "1.0.2",
"description": "Run some code when the process exits (supports async hooks andpm2 clustering)",
"description": "Run some code when the process exits (supports async hooks and pm2 clustering)",
"license": "MIT",
"repository": {
"type": "git",
Expand All @@ -23,7 +23,7 @@
"node": ">=0.12.0"
},
"scripts": {
"test": "nyc ava"
"test": "xo && nyc ava"
},
"files": [
"index.js"
Expand All @@ -50,9 +50,11 @@
"event"
],
"devDependencies": {
"ava": "0.13.0",
"coveralls": "^2.11.8",
"nyc": "^6.0.0"
"ava": "^0.16.0",
"coveralls": "^2.11.14",
"nyc": "^8.3.1",
"standard-version": "^3.0.0",
"xo": "^0.16.0"
},
"ava": {
"files": [
Expand Down
12 changes: 6 additions & 6 deletions test/cases/asyncErr.js → test/cases/async-err.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
var exitHook = require('./../../index');
var stub = require('./stub');

exitHook(function(cb) {
setTimeout(function() {
exitHook(function (cb) {
setTimeout(function () {
stub.called();
cb();
}, 50);
stub.called();
});

exitHook(function() {
exitHook(function () {
stub.called();
});

exitHook.uncaughtExceptionHandler(function(err, cb) {
setTimeout(function() {
exitHook.uncaughtExceptionHandler(function (err, cb) {
setTimeout(function () {
stub.called();
cb();
}, 50);
Expand All @@ -25,7 +25,7 @@ exitHook.uncaughtExceptionHandler(function(err, cb) {
stub.called();
});

process.on('uncaughtException', function() {
process.on('uncaughtException', function () {
// All uncaught exception handlers should be called even though the exit hook handler was registered
stub.called();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
var exitHook = require('./../../index');
var stub = require('./stub');

exitHook(function(cb) {
setTimeout(function() {
exitHook(function (cb) {
setTimeout(function () {
stub.called();
cb();
}, 2000);
stub.called();
});

exitHook(function() {
exitHook(function () {
stub.called();
});

exitHook.uncaughtExceptionHandler(function(err, cb) {
setTimeout(function() {
// eslint-disable-next-line handle-callback-err
exitHook.uncaughtExceptionHandler(function (err, cb) {
setTimeout(function () {
stub.called();
cb();
}, 2000);
Expand Down
6 changes: 3 additions & 3 deletions test/cases/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
var exitHook = require('./../../index');
var stub = require('./stub');

exitHook(function(cb) {
setTimeout(function() {
exitHook(function (cb) {
setTimeout(function () {
stub.called();
cb();
}, 50);
stub.called();
});

exitHook(function() {
exitHook(function () {
stub.called();
});

Expand Down
24 changes: 13 additions & 11 deletions test/cases/stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,58 @@ var c = 0;
var noCallback = true;

// Increment the called count
exports.called = function() {
exports.called = function () {
c++;
};

// Exit with error
exports.reject = function(s, code) {
exports.reject = function (s, code) {
process.stdout.write('FAILURE: ' + s);
process.exit(code != null ? code : 1);
// eslint-disable-next-line xo/no-process-exit
process.exit(code === null || code === undefined ? 1 : code);
};

// Exit with success
exports.done = function() {
exports.done = function () {
process.stdout.write('SUCCESS');
// eslint-disable-next-line xo/no-process-exit
process.exit(0);
};

// Add the exit check with a specific expected called count
exports.addCheck = function(num) {
exports.addCheck = function (num) {
noCallback = false;

// Only call exit once, and save uncaught errors
var called = false;
var ucErr;

// Save errors that do not start with 'test'
process.on('uncaughtException', function(err) {
process.on('uncaughtException', function (err) {
if (err.message.indexOf('test') !== 0) {
ucErr = err;
}
});

// Check that there were no unexpected errors and all callbacks were called
process.once('exit', function() {
process.once('exit', function () {
if (called) {
return;
}
called = true;

if (ucErr) {
exports.reject(ucErr.stack);
} else if (c !== num) {
exports.reject('Expected ' + num + ' callback calls, but ' + c + ' received');
} else {
} else if (c === num) {
exports.done();
} else {
exports.reject('Expected ' + num + ' callback calls, but ' + c + ' received');
}
});
};

// If the check isn't added, throw on exit
process.once('exit', function() {
process.once('exit', function () {
if (noCallback) {
exports.reject('FAILURE, CHECK NOT ADDED');
}
Expand Down
2 changes: 1 addition & 1 deletion test/cases/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exitHook(function () {
stub.called();
});

process.on('exit', function() {
process.on('exit', function () {
stub.called();
});

Expand Down
14 changes: 8 additions & 6 deletions test/tests.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Tests have to happen in a subprocess to test the exit functionality
'use strict';

var test = require('ava');
var fork = require('child_process').fork;
var path = require('path');

var test = require('ava');

/**
* Starts a test file in a subprocess, returns a promise that resolves with the subprocess
* exit code and output in an array ([code, output])
Expand All @@ -26,11 +27,11 @@ function testInSub(test, signal) {

var output = '';

proc.stdout.on('data', function(data) {
proc.stdout.on('data', function (data) {
output += data.toString();
});

proc.stderr.on('data', function(data) {
proc.stderr.on('data', function (data) {
output += data.toString();
});

Expand All @@ -47,7 +48,8 @@ function testInSub(test, signal) {
}

test('API: test adding and removing and listing hooks', t => {
const exitHook = require('./../');
var exitHook = require('./../');

t.plan(3);

// Enable hooks
Expand Down Expand Up @@ -86,7 +88,7 @@ test('async handlers', t => {

test('async uncaught exception handler', t => {
t.plan(2);
return testInSub('asyncErr')
return testInSub('async-err')
.then(([code, output]) => {
t.is(output, 'SUCCESS');
t.is(code, 0);
Expand All @@ -95,7 +97,7 @@ test('async uncaught exception handler', t => {

test('async exit timeout', t => {
t.plan(2);
return testInSub('asyncExitTimeout')
return testInSub('async-exit-timeout')
.then(([code, output]) => {
t.is(output, 'SUCCESS');
t.is(code, 0);
Expand Down

0 comments on commit 5a02a74

Please sign in to comment.