UNPKG

1.6 kBJavaScriptView Raw
1var assert = require('assert')
2
3module.exports = function (cb) {
4 assert.equal(typeof cb, 'function', 'a callback must be provided for exit handler')
5
6 var emittedExit = false
7
8 Object.keys(signals).forEach(function (sig) {
9 var listener = function () {
10 // If there are no other listeners, do the default action.
11 if (process.listeners(sig).length === 1) {
12 process.removeListener(sig, listener)
13 cb(process.exitCode || signals[sig], sig)
14 process.kill(process.pid, sig)
15 }
16 }
17
18 try {
19 process.on(sig, listener)
20 } catch (er) {}
21 })
22
23 process.on('exit', function (code) {
24 if (emittedExit) return
25 emittedExit = true
26 return cb(code, undefined)
27 })
28}
29
30var signals = {
31 'SIGALRM': 142,
32 'SIGBUS': 138,
33 'SIGCLD': undefined,
34 'SIGEMT': undefined,
35 'SIGFPE': 136,
36 'SIGHUP': 129,
37 'SIGILL': 132,
38 'SIGINFO': undefined,
39 'SIGINT': 130, // CTRL^C
40 'SIGIOT': 134,
41 'SIGKILL': 137, // can't be caught, but let's keep it here.
42 'SIGLOST': undefined,
43 'SIGPIPE': 141,
44 'SIGPOLL': undefined,
45 'SIGPROF': 155,
46 'SIGPWR': undefined,
47 'SIGQUIT': 131,
48 'SIGSEGV': 139,
49 'SIGSTKFLT': undefined,
50 'SIGSYS': 140,
51 'SIGTERM': 143, // polite exit code, can be caught.
52 'SIGTRAP': 133,
53 'SIGTSTP': 146,
54 'SIGTTIN': 149,
55 'SIGTTOU': 150,
56 'SIGUNUSED': undefined,
57 'SIGUSR2': 159,
58 'SIGVTALRM': 154,
59 'SIGXCPU': 152,
60 'SIGXFSZ': 153
61}
62
63var nonFatalSignals = [
64 'SIGWINCH', // resize window.
65 'SIGUSR1', // debugger.
66 'SIGCHLD',
67 'SIGSTOP',
68 'SIGCONT',
69 'SIGIO',
70 'SIGURG',
71 'SIGABRT',
72 'SIGURG' // out of band data.
73]