[Pkg-javascript-commits] [node-death] 01/04: Import Upstream version 1.0.0
Paolo Greppi
paolog-guest at moszumanska.debian.org
Tue Nov 29 10:59:45 UTC 2016
This is an automated email from the git hooks/post-receive script.
paolog-guest pushed a commit to branch master
in repository node-death.
commit 09bf71b0c1cf6847a1f6ce880f15fa56b074d013
Author: Paolo Greppi <paolo.greppi at libpf.com>
Date: Tue Nov 29 08:18:22 2016 +0000
Import Upstream version 1.0.0
---
.gitignore | 1 +
CHANGELOG.md | 17 ++++
LICENSE | 15 +++
README.md | 96 +++++++++++++++++++
app.js | 17 ++++
lib/death.js | 52 +++++++++++
package.json | 38 ++++++++
test/death.test.js | 165 +++++++++++++++++++++++++++++++++
test/mocha.opts | 3 +
test/resources/default | 17 ++++
test/resources/disable | 18 ++++
test/resources/sighup | 17 ++++
test/resources/uncaughtException-false | 15 +++
test/resources/uncaughtException-true | 15 +++
14 files changed, 486 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..40b878d
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+node_modules/
\ No newline at end of file
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..1331865
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,17 @@
+1.0.0 / 2015-03-17
+------------------
+- removed `homepage` field in `package.json`
+- small formatting changes
+
+0.1.0 / 2013-02-18
+------------------
+* Fixed bugs due to setting of `uncaughtException`. Closes #1
+* Changed default of `uncaughtException` from `true` to `false`.
+* Removed aliases for `uncaughtException`.
+* Fixed bug that when a key is set to false, it's still caught.
+* Passed signal to callback.
+
+
+0.0.1 / 2012-12-01
+------------------
+* Initial release.
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..83642bf
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,15 @@
+(The MIT License)
+
+Copyright (c) 2012, JP Richardson <jprichardson at gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
+(the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
+ merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
+OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..0da744c
--- /dev/null
+++ b/README.md
@@ -0,0 +1,96 @@
+Node.js - death
+================
+
+Gracefully cleanup when termination signals are sent to your process.
+
+
+Why?
+----
+
+Because adding clean up callbacks for `uncaughtException`, `SIGINT`, and `SIGTERM` is annoying. Ideally, you can
+use this package to put your cleanup code in one place and exit gracefully if you need to.
+
+
+Operating System Compatibility
+------------------------------
+
+It's only been tested on POSIX compatible systems. [Here's a nice discussion](https://github.com/joyent/node/issues/1553) on Windows signals, apparently, this has been fixed/mapped.
+
+
+Installation
+------------
+
+ npm install death
+
+
+
+Example
+------
+
+```js
+var ON_DEATH = require('death'); //this is intentionally ugly
+
+ON_DEATH(function(signal, err) {
+ //clean up code here
+})
+```
+
+
+Usage
+-----
+
+By default, it sets the callback on `SIGINT`, `SIGQUIT`, and `SIGTERM`.
+
+### Signals
+- **SIGINT**: Sent from CTRL-C
+- **SIGQUIT**: Sent from keyboard quit action.
+- **SIGTERM**: Sent from operating system `kill`.
+
+More discussion and detail: http://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html and http://pubs.opengroup.org/onlinepubs/009695399/basedefs/signal.h.html and http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap11.html.
+
+AS they pertain to Node.js: http://dailyjs.com/2012/03/15/unix-node-signals/
+
+
+#### Want to catch uncaughtException?
+
+No problem, do this:
+
+```js
+var ON_DEATH = require('death')({uncaughtException: true})
+```
+
+#### Want to know which signals are being caught?
+
+Do this:
+
+```js
+var ON_DEATH = require('death')({debug: true})
+```
+
+Your process will then log anytime it catches these signals.
+
+#### Want to catch SIGHUP?
+
+Be careful with this one though. Typically this is fired if your SSH connection dies, but can
+also be fired if the program is made a daemon.
+
+Do this:
+
+```js
+var ON_DEATH = require('death')({SIGHUP: true})
+```
+
+#### Why choose the ugly "ON_DEATH"?
+
+Name it whatever you want. I like `ON_DEATH` because it stands out like a sore thumb in my code.
+
+
+
+License
+-------
+
+(MIT License)
+
+Copyright 2012, JP Richardson <jprichardson at gmail.com>
+
+
diff --git a/app.js b/app.js
new file mode 100644
index 0000000..40e9bf0
--- /dev/null
+++ b/app.js
@@ -0,0 +1,17 @@
+var ON_DEATH = require('./lib/death')({debug: true})
+
+//to kill this, call `kill -9 pid`
+
+process.stdin.resume()
+
+ON_DEATH(function(err) {
+ if (!err)
+ console.log('Caught foo')
+ else
+ console.error('We got an uncaught exception! ' + err)
+})
+
+setTimeout(function() {
+ throw new Error('stupid exception')
+}, 5000)
+
diff --git a/lib/death.js b/lib/death.js
new file mode 100644
index 0000000..753c81d
--- /dev/null
+++ b/lib/death.js
@@ -0,0 +1,52 @@
+
+var defaultConfig = {
+ uncaughtException: false,
+ SIGINT: true,
+ SIGTERM: true,
+ SIGQUIT: true
+}
+
+var DEBUG = false
+
+function ON_DEATH (callback) {
+ Object.keys(defaultConfig).forEach(function(key) {
+ var val = defaultConfig[key]
+ if (val) {
+ if (DEBUG)
+ process.on(key, function() {
+ var args = Array.prototype.slice.call(arguments, 0)
+ args.unshift(key)
+ console.log('Trapped ' + key)
+ callback.apply(null, args)
+ })
+ else
+ process.on(key, function() {
+ var args = Array.prototype.slice.call(arguments, 0)
+ args.unshift(key)
+ callback.apply(null, args)
+ })
+ }
+ })
+}
+
+module.exports = function (arg) {
+ if (typeof arg === 'object') {
+ if (arg['debug'])
+ DEBUG = arg.debug
+ if (arg['DEBUG'])
+ DEBUG = arg.DEBUG
+ delete arg.debug; delete arg.DEBUG;
+
+ Object.keys(arg).forEach(function(key) {
+ defaultConfig[key] = arg[key]
+ })
+
+ if (DEBUG)
+ console.log('ON_DEATH: debug mode enabled for pid [%d]', process.pid)
+
+ return ON_DEATH
+ } else if (typeof arg === 'function') {
+ ON_DEATH(arg)
+ }
+}
+
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..6bb1eaf
--- /dev/null
+++ b/package.json
@@ -0,0 +1,38 @@
+{
+ "name": "death",
+ "version": "1.0.0",
+ "description": "Gracefully cleanup when termination signals are sent to your process.",
+ "repository": {
+ "type": "git",
+ "url": "git at github.com:jprichardson/node-death.git"
+ },
+ "keywords": [
+ "sigint",
+ "sigterm",
+ "sigkill",
+ "sigquit",
+ "exception",
+ "kill",
+ "terminate",
+ "process",
+ "clean"
+ ],
+ "author": "JP Richardson <jprichardson at gmail.com>",
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": ""
+ }
+ ],
+ "dependencies": {},
+ "devDependencies": {
+ "win-spawn": "~1.1.1",
+ "autoresolve": "0.0.3",
+ "testutil": "~0.4.0",
+ "colors": "~0.6.0-1"
+ },
+ "main": "./lib/death.js",
+ "scripts": {
+ "test": "mocha test"
+ }
+}
diff --git a/test/death.test.js b/test/death.test.js
new file mode 100644
index 0000000..d669f02
--- /dev/null
+++ b/test/death.test.js
@@ -0,0 +1,165 @@
+var spawn = require('win-spawn')
+var P = require('autoresolve')
+var testutil = require('testutil')
+var colors = require('colors')
+
+/* global describe, it, T, EQ */
+
+describe('death', function() {
+ describe('default behavior', function() {
+ it('should catch SIGINT, SIGTERM, and SIGQUIT and return 3', function(done) {
+ var signals = []
+ var progPath = P('test/resources/default')
+ var prog = spawn(progPath, [])
+ //console.dir(prog)
+
+ prog.stdout.on('data', function(data) {
+ //console.log(colors.cyan(data.toString()))
+ })
+
+ prog.stderr.on('data', function(data) {
+ //console.error(colors.red(data.toString()))
+ signals = signals.concat(data.toString().trim().split('\n'))
+ })
+
+ prog.on('exit', function(code) {
+ EQ (code, 3)
+ //console.dir(signals)
+ T (signals.indexOf('SIGQUIT') >= 0)
+ T (signals.indexOf('SIGTERM') >= 0)
+ T (signals.indexOf('SIGINT') >= 0)
+ done()
+ })
+
+ setTimeout(function() {
+ prog.kill('SIGINT')
+ process.kill(prog.pid, 'SIGTERM')
+ prog.kill('SIGQUIT')
+ }, 100)
+
+ })
+ })
+
+ describe('other signal', function() {
+ it('should catch SIGINT, SIGTERM, SIGQUIT, and SIGHUP and return 4', function(done) {
+ var signals = []
+ var progPath = P('test/resources/sighup')
+ var prog = spawn(progPath, [])
+ //console.dir(prog)
+
+ prog.stdout.on('data', function(data) {
+ //console.log(colors.cyan(data.toString()))
+ })
+
+ prog.stderr.on('data', function(data) {
+ //console.error(colors.red(data.toString()))
+ signals = signals.concat(data.toString().trim().split('\n'))
+ })
+
+ prog.on('exit', function(code) {
+ EQ (code, 4)
+ //console.dir(signals)
+ T (signals.indexOf('SIGQUIT') >= 0)
+ T (signals.indexOf('SIGTERM') >= 0)
+ T (signals.indexOf('SIGINT') >= 0)
+ T (signals.indexOf('SIGHUP') >= 0)
+ done()
+ })
+
+ setTimeout(function() {
+ prog.kill('SIGINT')
+ process.kill(prog.pid, 'SIGTERM')
+ prog.kill('SIGQUIT')
+ prog.kill('SIGHUP')
+ }, 100)
+
+ })
+ })
+
+ describe('disable signal', function() {
+ it('should catch SIGINT and SIGTERM', function(done) {
+ var signals = []
+ var progPath = P('test/resources/disable')
+ var prog = spawn(progPath, [])
+ //console.dir(prog)
+
+ prog.stdout.on('data', function(data) {
+ //console.log(colors.cyan(data.toString()))
+ })
+
+ prog.stderr.on('data', function(data) {
+ //console.error(colors.red(data.toString()))
+ signals = signals.concat(data.toString().trim().split('\n'))
+ })
+
+ prog.on('exit', function(code) {
+ T (signals.indexOf('SIGQUIT') < 0)
+ T (signals.indexOf('SIGTERM') >= 0)
+ T (signals.indexOf('SIGINT') >= 0)
+ done()
+ })
+
+ setTimeout(function() {
+ prog.kill('SIGINT')
+ prog.kill('SIGTERM')
+ setTimeout(function() {
+ prog.kill('SIGQUIT') //this actually kills it since we disabled it
+ },10)
+ }, 100)
+
+ })
+ })
+
+ describe('uncaughException', function() {
+ describe('> when set to true', function() {
+ it('should catch uncaughtException', function(done) {
+ var errData = ''
+ var progPath = P('test/resources/uncaughtException-true')
+ var prog = spawn(progPath, [])
+ //console.dir(prog)
+
+ prog.stdout.on('data', function(data) {
+ //console.log(colors.cyan(data.toString()))
+ })
+
+ prog.stderr.on('data', function(data) {
+ //console.error(colors.red(data.toString()))
+ errData += data.toString().trim()
+ })
+
+ prog.on('exit', function(code) {
+ EQ (code, 70)
+ T (errData.indexOf('uncaughtException') >= 0)
+ T (errData.indexOf('UNCAUGHT SELF') >= 0)
+ done()
+ })
+ })
+ })
+
+ describe('> when set to false', function() {
+ it('should catch uncaughtException', function(done) {
+ var errData = ''
+ var progPath = P('test/resources/uncaughtException-false')
+ var prog = spawn(progPath, [])
+ //console.dir(prog)
+
+ prog.stdout.on('data', function(data) {
+ //console.log(colors.cyan(data.toString()))
+ })
+
+ prog.stderr.on('data', function(data) {
+ //console.error(colors.red(data.toString()))
+ errData += data.toString().trim()
+ })
+
+ prog.on('exit', function(code) {
+ EQ (code, 1)
+ T (errData.indexOf('CAUGHT: uncaughtException') < 0)
+ T (errData.indexOf('UNCAUGHT SELF') >= 0)
+ done()
+ })
+ })
+ })
+ })
+})
+
diff --git a/test/mocha.opts b/test/mocha.opts
new file mode 100644
index 0000000..5fa33d2
--- /dev/null
+++ b/test/mocha.opts
@@ -0,0 +1,3 @@
+--reporter spec
+--ui bdd
+--timeout 2000
\ No newline at end of file
diff --git a/test/resources/default b/test/resources/default
new file mode 100755
index 0000000..10b4b93
--- /dev/null
+++ b/test/resources/default
@@ -0,0 +1,17 @@
+#!/usr/bin/env node
+
+var P = require('autoresolve')
+ , ON_DEATH = require(P('lib/death'))
+
+var ret = 0x0
+console.log('HI FROM PROGRAM')
+
+ON_DEATH(function(signal, err) {
+ console.error(signal)
+ ret += 1
+})
+
+setTimeout(function() {
+ //twiddle thumbs
+ process.exit(ret)
+}, 500)
\ No newline at end of file
diff --git a/test/resources/disable b/test/resources/disable
new file mode 100755
index 0000000..1e0902e
--- /dev/null
+++ b/test/resources/disable
@@ -0,0 +1,18 @@
+#!/usr/bin/env node
+
+var P = require('autoresolve')
+ , ON_DEATH = require(P('lib/death'))({SIGQUIT: false})
+
+var ret = 0x0
+console.log('HI FROM PROGRAM')
+
+ON_DEATH(function(signal, err) {
+ console.error(signal)
+ ret += 1
+})
+
+setTimeout(function() {
+ //twiddle thumbls
+ process.exit(ret)
+}, 30000)
+
diff --git a/test/resources/sighup b/test/resources/sighup
new file mode 100755
index 0000000..b5fa6b8
--- /dev/null
+++ b/test/resources/sighup
@@ -0,0 +1,17 @@
+#!/usr/bin/env node
+
+var P = require('autoresolve')
+ , ON_DEATH = require(P('lib/death'))({SIGHUP: true})
+
+var ret = 0x0
+console.log('HI FROM PROGRAM')
+
+ON_DEATH(function(signal, err) {
+ console.error(signal)
+ ret += 1
+})
+
+setTimeout(function() {
+ //twiddle thumbs
+ process.exit(ret)
+}, 500)
\ No newline at end of file
diff --git a/test/resources/uncaughtException-false b/test/resources/uncaughtException-false
new file mode 100755
index 0000000..64ecda9
--- /dev/null
+++ b/test/resources/uncaughtException-false
@@ -0,0 +1,15 @@
+#!/usr/bin/env node
+
+var P = require('autoresolve')
+ , ON_DEATH = require(P('lib/death'))({uncaughtException: false})
+
+var ret = 0x0
+console.log('HI FROM PROGRAM')
+
+ON_DEATH(function(signal, err) {
+ console.error('CAUGHT: ' + signal)
+ console.error(err.message)
+ process.exit(70)
+})
+
+throw new Error('UNCAUGHT SELF')
\ No newline at end of file
diff --git a/test/resources/uncaughtException-true b/test/resources/uncaughtException-true
new file mode 100755
index 0000000..20a1d5a
--- /dev/null
+++ b/test/resources/uncaughtException-true
@@ -0,0 +1,15 @@
+#!/usr/bin/env node
+
+var P = require('autoresolve')
+ , ON_DEATH = require(P('lib/death'))({uncaughtException: true})
+
+var ret = 0x0
+console.log('HI FROM PROGRAM')
+
+ON_DEATH(function(signal, err) {
+ console.error(signal)
+ console.error(err.message)
+ process.exit(70)
+})
+
+throw new Error('UNCAUGHT SELF')
\ No newline at end of file
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-death.git
More information about the Pkg-javascript-commits
mailing list