@breadc/death 0.9.7 → 1.0.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +16 -11
- package/dist/index.mjs +79 -85
- package/package.json +5 -12
- package/dist/index.cjs +0 -103
- package/dist/index.d.cts +0 -19
- package/dist/index.d.ts +0 -19
package/dist/index.d.mts
CHANGED
|
@@ -1,19 +1,24 @@
|
|
|
1
|
+
//#region src/death.d.ts
|
|
1
2
|
type DeathSignals = 'SIGINT' | 'SIGTERM' | 'SIGQUIT';
|
|
2
3
|
interface OnDeathContext {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
triggered: Date | undefined;
|
|
5
|
+
terminate: 'exit' | 'kill' | false;
|
|
6
|
+
exit: number | undefined;
|
|
7
|
+
kill: NodeJS.Signals | undefined;
|
|
7
8
|
}
|
|
8
9
|
type OnDeathCallback = (signal: DeathSignals, context: OnDeathContext) => unknown | Promise<unknown>;
|
|
9
10
|
interface OnDeathOptions {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
SIGINT?: boolean;
|
|
12
|
+
SIGTERM?: boolean;
|
|
13
|
+
SIGQUIT?: boolean;
|
|
13
14
|
}
|
|
14
|
-
declare function onDeath(callback: OnDeathCallback, {
|
|
15
|
+
declare function onDeath(callback: OnDeathCallback, {
|
|
16
|
+
SIGINT,
|
|
17
|
+
SIGTERM,
|
|
18
|
+
SIGQUIT
|
|
19
|
+
}?: OnDeathOptions): () => void;
|
|
15
20
|
declare function useDeath(callback: OnDeathCallback, options?: OnDeathOptions): {
|
|
16
|
-
|
|
21
|
+
[Symbol.dispose]: () => void;
|
|
17
22
|
};
|
|
18
|
-
|
|
19
|
-
export {
|
|
23
|
+
//#endregion
|
|
24
|
+
export { DeathSignals, OnDeathCallback, OnDeathContext, OnDeathOptions, onDeath, useDeath };
|
package/dist/index.mjs
CHANGED
|
@@ -1,100 +1,94 @@
|
|
|
1
|
-
import { EventEmitter } from
|
|
1
|
+
import { EventEmitter } from "node:events";
|
|
2
2
|
|
|
3
|
+
//#region src/death.ts
|
|
3
4
|
const emitter = new EventEmitter();
|
|
4
5
|
const context = {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
triggered: void 0,
|
|
7
|
+
terminate: "kill",
|
|
8
|
+
exit: void 0,
|
|
9
|
+
kill: void 0
|
|
9
10
|
};
|
|
10
11
|
const handlers = {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
SIGINT: makeHandler("SIGINT"),
|
|
13
|
+
SIGTERM: makeHandler("SIGTERM"),
|
|
14
|
+
SIGQUIT: makeHandler("SIGQUIT")
|
|
14
15
|
};
|
|
16
|
+
const FORCE_KILL_TIMEOUT = 3 * 1e3;
|
|
15
17
|
function onDeath(callback, { SIGINT = true, SIGTERM = true, SIGQUIT = true } = {}) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
};
|
|
18
|
+
const cleanUps = [];
|
|
19
|
+
if (SIGINT) {
|
|
20
|
+
registerCallback("SIGINT", handlers.SIGINT);
|
|
21
|
+
cleanUps.push(handlers.SIGINT.addCallback(callback));
|
|
22
|
+
}
|
|
23
|
+
if (SIGTERM) {
|
|
24
|
+
registerCallback("SIGTERM", handlers.SIGTERM);
|
|
25
|
+
cleanUps.push(handlers.SIGTERM.addCallback(callback));
|
|
26
|
+
}
|
|
27
|
+
if (SIGQUIT) {
|
|
28
|
+
registerCallback("SIGQUIT", handlers.SIGQUIT);
|
|
29
|
+
cleanUps.push(handlers.SIGQUIT.addCallback(callback));
|
|
30
|
+
}
|
|
31
|
+
return () => {
|
|
32
|
+
for (const cleanUp of cleanUps) cleanUp();
|
|
33
|
+
};
|
|
34
34
|
}
|
|
35
35
|
function useDeath(callback, options = {}) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
};
|
|
36
|
+
const cancel = onDeath(callback, options);
|
|
37
|
+
return { [Symbol.dispose]: () => {
|
|
38
|
+
cancel();
|
|
39
|
+
} };
|
|
42
40
|
}
|
|
43
41
|
function registerCallback(signal, handler) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
42
|
+
if (handler.count === 0) {
|
|
43
|
+
handler.count += 1;
|
|
44
|
+
process.addListener(signal, handler.listener);
|
|
45
|
+
}
|
|
48
46
|
}
|
|
49
47
|
function makeHandler(signal) {
|
|
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
|
-
context.triggered = false;
|
|
95
|
-
context.kill = void 0;
|
|
96
|
-
}
|
|
97
|
-
};
|
|
48
|
+
const callbacks = /* @__PURE__ */ new WeakMap();
|
|
49
|
+
return {
|
|
50
|
+
count: 0,
|
|
51
|
+
callbacks,
|
|
52
|
+
addCallback(callback) {
|
|
53
|
+
{
|
|
54
|
+
const count = callbacks.get(callback);
|
|
55
|
+
if (count !== void 0) callbacks.set(callback, count + 1);
|
|
56
|
+
else {
|
|
57
|
+
callbacks.set(callback, 1);
|
|
58
|
+
emitter.addListener(signal, callback);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return () => {
|
|
62
|
+
const count = callbacks.get(callback);
|
|
63
|
+
if (count === void 0 || count <= 1) {
|
|
64
|
+
callbacks.delete(callback);
|
|
65
|
+
emitter.removeListener(signal, callback);
|
|
66
|
+
} else callbacks.set(callback, count - 1);
|
|
67
|
+
};
|
|
68
|
+
},
|
|
69
|
+
async listener(signal) {
|
|
70
|
+
if (context.triggered) {
|
|
71
|
+
if ((/* @__PURE__ */ new Date()).getTime() - context.triggered.getTime() >= FORCE_KILL_TIMEOUT) process.exit(130);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
context.terminate = "kill";
|
|
75
|
+
context.kill = signal;
|
|
76
|
+
context.triggered = /* @__PURE__ */ new Date();
|
|
77
|
+
const listeners = [...emitter.listeners(signal)];
|
|
78
|
+
for (const listener of listeners.reverse()) await listener(signal, context);
|
|
79
|
+
if (context.terminate === "kill" || context.terminate === "exit") {
|
|
80
|
+
process.removeListener("SIGINT", handlers.SIGINT.listener);
|
|
81
|
+
process.removeListener("SIGTERM", handlers.SIGTERM.listener);
|
|
82
|
+
process.removeListener("SIGQUIT", handlers.SIGQUIT.listener);
|
|
83
|
+
if (context.terminate === "kill") process.kill(process.pid, context.kill);
|
|
84
|
+
else process.exit(context.exit);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
context.kill = void 0;
|
|
88
|
+
context.triggered = void 0;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
98
91
|
}
|
|
99
92
|
|
|
100
|
-
|
|
93
|
+
//#endregion
|
|
94
|
+
export { onDeath, useDeath };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@breadc/death",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0-beta.10",
|
|
4
4
|
"description": "Easily register termination signals callbacks.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"breadc",
|
|
@@ -25,24 +25,17 @@
|
|
|
25
25
|
"type": "module",
|
|
26
26
|
"exports": {
|
|
27
27
|
".": {
|
|
28
|
-
"
|
|
29
|
-
"import": "./dist/index.mjs"
|
|
30
|
-
"types": "./dist/index.d.ts"
|
|
28
|
+
"types": "./dist/index.d.mts",
|
|
29
|
+
"import": "./dist/index.mjs"
|
|
31
30
|
}
|
|
32
31
|
},
|
|
33
|
-
"main": "dist/index.cjs",
|
|
34
32
|
"module": "dist/index.mjs",
|
|
35
|
-
"types": "dist/index.d.
|
|
33
|
+
"types": "dist/index.d.mts",
|
|
36
34
|
"files": [
|
|
37
35
|
"dist"
|
|
38
36
|
],
|
|
39
|
-
"devDependencies": {
|
|
40
|
-
"@types/node": "^20.8.7",
|
|
41
|
-
"vitest": "^0.34.6"
|
|
42
|
-
},
|
|
43
37
|
"scripts": {
|
|
44
|
-
"build": "
|
|
45
|
-
"format": "prettier --write src/**/*.ts test/*.ts",
|
|
38
|
+
"build": "tsdown",
|
|
46
39
|
"test": "vitest",
|
|
47
40
|
"test:ci": "vitest --run",
|
|
48
41
|
"typecheck": "tsc --noEmit"
|
package/dist/index.cjs
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const node_events = require('node:events');
|
|
4
|
-
|
|
5
|
-
const emitter = new node_events.EventEmitter();
|
|
6
|
-
const context = {
|
|
7
|
-
triggered: false,
|
|
8
|
-
terminate: "kill",
|
|
9
|
-
exit: void 0,
|
|
10
|
-
kill: void 0
|
|
11
|
-
};
|
|
12
|
-
const handlers = {
|
|
13
|
-
SIGINT: makeHandler("SIGINT"),
|
|
14
|
-
SIGTERM: makeHandler("SIGTERM"),
|
|
15
|
-
SIGQUIT: makeHandler("SIGQUIT")
|
|
16
|
-
};
|
|
17
|
-
function onDeath(callback, { SIGINT = true, SIGTERM = true, SIGQUIT = true } = {}) {
|
|
18
|
-
const cleanUps = [];
|
|
19
|
-
if (SIGINT) {
|
|
20
|
-
registerCallback("SIGINT", handlers.SIGINT);
|
|
21
|
-
cleanUps.push(handlers.SIGINT.addCallback(callback));
|
|
22
|
-
}
|
|
23
|
-
if (SIGTERM) {
|
|
24
|
-
registerCallback("SIGTERM", handlers.SIGTERM);
|
|
25
|
-
cleanUps.push(handlers.SIGTERM.addCallback(callback));
|
|
26
|
-
}
|
|
27
|
-
if (SIGQUIT) {
|
|
28
|
-
registerCallback("SIGQUIT", handlers.SIGQUIT);
|
|
29
|
-
cleanUps.push(handlers.SIGQUIT.addCallback(callback));
|
|
30
|
-
}
|
|
31
|
-
return () => {
|
|
32
|
-
for (const cleanUp of cleanUps) {
|
|
33
|
-
cleanUp();
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
function useDeath(callback, options = {}) {
|
|
38
|
-
const cancel = onDeath(callback, options);
|
|
39
|
-
return {
|
|
40
|
-
[Symbol.dispose]: () => {
|
|
41
|
-
cancel();
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
function registerCallback(signal, handler) {
|
|
46
|
-
if (handler.count === 0) {
|
|
47
|
-
handler.count += 1;
|
|
48
|
-
process.addListener(signal, handler.listener);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
function makeHandler(signal) {
|
|
52
|
-
const callbacks = /* @__PURE__ */ new WeakMap();
|
|
53
|
-
return {
|
|
54
|
-
count: 0,
|
|
55
|
-
callbacks,
|
|
56
|
-
addCallback(callback) {
|
|
57
|
-
{
|
|
58
|
-
const count = callbacks.get(callback);
|
|
59
|
-
if (count !== void 0) {
|
|
60
|
-
callbacks.set(callback, count + 1);
|
|
61
|
-
} else {
|
|
62
|
-
callbacks.set(callback, 1);
|
|
63
|
-
emitter.addListener(signal, callback);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return () => {
|
|
67
|
-
const count = callbacks.get(callback);
|
|
68
|
-
if (count === void 0 || count <= 1) {
|
|
69
|
-
callbacks.delete(callback);
|
|
70
|
-
emitter.removeListener(signal, callback);
|
|
71
|
-
} else {
|
|
72
|
-
callbacks.set(callback, count - 1);
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
},
|
|
76
|
-
async listener(signal2) {
|
|
77
|
-
if (context.triggered) {
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
context.triggered = true;
|
|
81
|
-
context.kill = signal2;
|
|
82
|
-
const listeners = [...emitter.listeners(signal2)];
|
|
83
|
-
for (const listener of listeners.reverse()) {
|
|
84
|
-
await listener(signal2, context);
|
|
85
|
-
}
|
|
86
|
-
if (context.terminate === "kill" || context.terminate === "exit") {
|
|
87
|
-
process.removeListener("SIGINT", handlers.SIGINT.listener);
|
|
88
|
-
process.removeListener("SIGTERM", handlers.SIGTERM.listener);
|
|
89
|
-
process.removeListener("SIGQUIT", handlers.SIGQUIT.listener);
|
|
90
|
-
if (context.terminate === "kill") {
|
|
91
|
-
process.kill(process.pid, context.kill);
|
|
92
|
-
} else {
|
|
93
|
-
process.exit(context.exit);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
context.triggered = false;
|
|
97
|
-
context.kill = void 0;
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
exports.onDeath = onDeath;
|
|
103
|
-
exports.useDeath = useDeath;
|
package/dist/index.d.cts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
type DeathSignals = 'SIGINT' | 'SIGTERM' | 'SIGQUIT';
|
|
2
|
-
interface OnDeathContext {
|
|
3
|
-
triggered: boolean;
|
|
4
|
-
terminate: 'exit' | 'kill' | false;
|
|
5
|
-
exit: number | undefined;
|
|
6
|
-
kill: NodeJS.Signals | undefined;
|
|
7
|
-
}
|
|
8
|
-
type OnDeathCallback = (signal: DeathSignals, context: OnDeathContext) => unknown | Promise<unknown>;
|
|
9
|
-
interface OnDeathOptions {
|
|
10
|
-
SIGINT?: boolean;
|
|
11
|
-
SIGTERM?: boolean;
|
|
12
|
-
SIGQUIT?: boolean;
|
|
13
|
-
}
|
|
14
|
-
declare function onDeath(callback: OnDeathCallback, { SIGINT, SIGTERM, SIGQUIT }?: OnDeathOptions): () => void;
|
|
15
|
-
declare function useDeath(callback: OnDeathCallback, options?: OnDeathOptions): {
|
|
16
|
-
[Symbol.dispose]: () => void;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export { type DeathSignals, type OnDeathCallback, type OnDeathContext, type OnDeathOptions, onDeath, useDeath };
|
package/dist/index.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
type DeathSignals = 'SIGINT' | 'SIGTERM' | 'SIGQUIT';
|
|
2
|
-
interface OnDeathContext {
|
|
3
|
-
triggered: boolean;
|
|
4
|
-
terminate: 'exit' | 'kill' | false;
|
|
5
|
-
exit: number | undefined;
|
|
6
|
-
kill: NodeJS.Signals | undefined;
|
|
7
|
-
}
|
|
8
|
-
type OnDeathCallback = (signal: DeathSignals, context: OnDeathContext) => unknown | Promise<unknown>;
|
|
9
|
-
interface OnDeathOptions {
|
|
10
|
-
SIGINT?: boolean;
|
|
11
|
-
SIGTERM?: boolean;
|
|
12
|
-
SIGQUIT?: boolean;
|
|
13
|
-
}
|
|
14
|
-
declare function onDeath(callback: OnDeathCallback, { SIGINT, SIGTERM, SIGQUIT }?: OnDeathOptions): () => void;
|
|
15
|
-
declare function useDeath(callback: OnDeathCallback, options?: OnDeathOptions): {
|
|
16
|
-
[Symbol.dispose]: () => void;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export { type DeathSignals, type OnDeathCallback, type OnDeathContext, type OnDeathOptions, onDeath, useDeath };
|