-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
168 lines (168 loc) · 6.49 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// +----------------------------------------------------------------------+
// | node-graceful v3 (https://github.com/mrbar42/node-graceful) |
// | Graceful process exit manager. |
// |----------------------------------------------------------------------|
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
var Graceful = /** @class */ (function () {
function Graceful() {
}
Object.defineProperty(Graceful, "captureExceptions", {
get: function () {
return Graceful._captureExceptions;
},
set: function (newValue) {
if (Graceful._captureExceptions === newValue)
return;
Graceful._captureExceptions = newValue;
if (Graceful._captureExceptions) {
process.on('uncaughtException', Graceful.exceptionListener);
}
else {
process.removeListener('uncaughtException', Graceful.exceptionListener);
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(Graceful, "captureRejections", {
get: function () {
return Graceful._captureRejections;
},
set: function (newValue) {
if (Graceful._captureRejections === newValue)
return;
Graceful._captureRejections = newValue;
if (Graceful._captureRejections) {
process.on('unhandledRejection', Graceful.rejectionListener);
}
else {
process.removeListener('unhandledRejection', Graceful.rejectionListener);
}
},
enumerable: false,
configurable: true
});
Graceful.on = function (signal, listener) {
if (signal !== 'exit')
throw new Error('Only supports \'exit\' signal');
Graceful.listeners.push(listener);
Graceful.updateRegistration();
return function () { return Graceful.off('exit', listener); };
};
Graceful.off = function (signal, listener) {
if (signal !== 'exit')
throw new Error('Only supports \'exit\' signal');
var index = Graceful.listeners.indexOf(listener);
if (index !== -1)
Graceful.listeners.splice(index, 1);
Graceful.updateRegistration();
};
Graceful.clear = function () {
Graceful.listeners.splice(0, Infinity);
Graceful.updateRegistration();
};
Graceful.exit = function (code, signal) {
if (signal === void 0) { signal = 'SIGTERM'; }
var exitSignal = typeof code === 'string' ? code : signal;
if (typeof code === 'number') {
process.exitCode = code;
}
Graceful.onDeadlyEvent(exitSignal, { reason: 'Manual call to Graceful.exit()' });
};
Graceful.onDeadlyEvent = function (signal, details) {
// console.log(signal, details);
if (Graceful.isExiting) {
if (Graceful.exitOnDouble)
Graceful.killProcess(true);
return;
}
var listeners = Graceful.listeners.slice(0);
Graceful.isExiting = true;
var completedListeners = 0;
var done = function () {
completedListeners++;
if (completedListeners === listeners.length) {
Graceful.killProcess(false);
}
};
if (Number(Graceful.timeout)) {
var timeoutRef = setTimeout(function () { return Graceful.killProcess(true); }, Graceful.timeout);
if (timeoutRef && timeoutRef.unref)
timeoutRef.unref();
}
for (var _i = 0, listeners_1 = listeners; _i < listeners_1.length; _i++) {
var listener = listeners_1[_i];
Graceful.invokeListener(listener, done, signal, details);
}
};
Graceful.invokeListener = function (listener, done, signal, details) {
var invoked = false;
var listenerDone = function () {
if (!invoked) {
invoked = true;
done();
}
};
var retVal = listener(signal, details);
// allow returning a promise
if (retVal && typeof retVal.then === 'function') {
retVal.then(listenerDone, listenerDone);
}
else {
listenerDone();
}
};
Graceful.updateRegistration = function () {
if (Graceful.listeners.length && !Graceful.isRegistered) {
var _loop_1 = function (deadlySignal) {
var listener = function () { return Graceful.onDeadlyEvent(deadlySignal); };
Graceful.signalsListeners[deadlySignal] = listener;
process.on(deadlySignal, listener);
};
for (var _i = 0, _a = Graceful.DEADLY_SIGNALS; _i < _a.length; _i++) {
var deadlySignal = _a[_i];
_loop_1(deadlySignal);
}
Graceful.isRegistered = true;
}
else if (!Graceful.listeners.length && Graceful.isRegistered) {
for (var _b = 0, _c = Graceful.DEADLY_SIGNALS; _b < _c.length; _b++) {
var deadlySignal = _c[_b];
var listener = Graceful.signalsListeners[deadlySignal];
if (listener) {
process.removeListener(deadlySignal, listener);
delete Graceful.signalsListeners[deadlySignal];
}
}
Graceful.isRegistered = false;
}
};
Graceful.killProcess = function (force) {
process.exit(process.exitCode || (force ? 1 : 0));
};
Graceful.DEADLY_SIGNALS = ['SIGTERM', 'SIGINT', 'SIGBREAK', 'SIGHUP'];
Graceful.exitOnDouble = true;
Graceful.timeout = 30000;
Graceful._captureExceptions = false;
Graceful._captureRejections = false;
Graceful.listeners = [];
Graceful.isRegistered = false;
Graceful.isExiting = false;
Graceful.exceptionListener = function (event) {
process.exitCode = 1;
Graceful.onDeadlyEvent('uncaughtException', event);
};
Graceful.rejectionListener = function (event) {
process.exitCode = 1;
Graceful.onDeadlyEvent('unhandledRejection', event);
};
Graceful.signalsListeners = {};
return Graceful;
}());
exports.default = Graceful;
// @ts-ignore - simulate default export in way Typescript can't understand
Graceful.default = exports.default;
// Support all the possible commonjs variations including Typescript
module.exports = Graceful;
Object.defineProperty(Graceful, "__esModule", { value: true });