@breadc/death 0.9.7-beta.0 → 1.0.0-beta.1

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.
@@ -0,0 +1,24 @@
1
+ //#region src/death.d.ts
2
+ type DeathSignals = 'SIGINT' | 'SIGTERM' | 'SIGQUIT';
3
+ interface OnDeathContext {
4
+ triggered: Date | undefined;
5
+ terminate: 'exit' | 'kill' | false;
6
+ exit: number | undefined;
7
+ kill: NodeJS.Signals | undefined;
8
+ }
9
+ type OnDeathCallback = (signal: DeathSignals, context: OnDeathContext) => unknown | Promise<unknown>;
10
+ interface OnDeathOptions {
11
+ SIGINT?: boolean;
12
+ SIGTERM?: boolean;
13
+ SIGQUIT?: boolean;
14
+ }
15
+ declare function onDeath(callback: OnDeathCallback, {
16
+ SIGINT,
17
+ SIGTERM,
18
+ SIGQUIT
19
+ }?: OnDeathOptions): () => void;
20
+ declare function useDeath(callback: OnDeathCallback, options?: OnDeathOptions): {
21
+ [Symbol.dispose]: () => void;
22
+ };
23
+ //#endregion
24
+ export { DeathSignals, OnDeathCallback, OnDeathContext, OnDeathOptions, onDeath, useDeath };
package/dist/index.mjs CHANGED
@@ -1,92 +1,94 @@
1
- import { EventEmitter } from 'node:events';
1
+ import { EventEmitter } from "node:events";
2
2
 
3
+ //#region src/death.ts
3
4
  const emitter = new EventEmitter();
4
5
  const context = {
5
- triggered: false,
6
- terminate: "kill",
7
- exit: void 0,
8
- kill: void 0
6
+ triggered: void 0,
7
+ terminate: "kill",
8
+ exit: void 0,
9
+ kill: void 0
9
10
  };
10
11
  const handlers = {
11
- SIGINT: makeHandler("SIGINT"),
12
- SIGTERM: makeHandler("SIGTERM"),
13
- SIGQUIT: makeHandler("SIGQUIT")
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
- const cleanUps = [];
17
- if (SIGINT) {
18
- registerCallback("SIGINT", handlers.SIGINT);
19
- cleanUps.push(handlers.SIGINT.addCallback(callback));
20
- }
21
- if (SIGTERM) {
22
- registerCallback("SIGTERM", handlers.SIGTERM);
23
- cleanUps.push(handlers.SIGTERM.addCallback(callback));
24
- }
25
- if (SIGQUIT) {
26
- registerCallback("SIGQUIT", handlers.SIGQUIT);
27
- cleanUps.push(handlers.SIGQUIT.addCallback(callback));
28
- }
29
- return () => {
30
- for (const cleanUp of cleanUps) {
31
- cleanUp();
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
+ }
35
+ function useDeath(callback, options = {}) {
36
+ const cancel = onDeath(callback, options);
37
+ return { [Symbol.dispose]: () => {
38
+ cancel();
39
+ } };
34
40
  }
35
41
  function registerCallback(signal, handler) {
36
- if (handler.count === 0) {
37
- handler.count += 1;
38
- process.addListener(signal, handler.listener);
39
- }
42
+ if (handler.count === 0) {
43
+ handler.count += 1;
44
+ process.addListener(signal, handler.listener);
45
+ }
40
46
  }
41
47
  function makeHandler(signal) {
42
- const callbacks = /* @__PURE__ */ new WeakMap();
43
- return {
44
- count: 0,
45
- callbacks,
46
- addCallback(callback) {
47
- {
48
- const count = callbacks.get(callback);
49
- if (count !== void 0) {
50
- callbacks.set(callback, count + 1);
51
- } else {
52
- callbacks.set(callback, 1);
53
- emitter.addListener(signal, callback);
54
- }
55
- }
56
- return () => {
57
- const count = callbacks.get(callback);
58
- if (count === void 0 || count <= 1) {
59
- callbacks.delete(callback);
60
- emitter.removeListener(signal, callback);
61
- } else {
62
- callbacks.set(callback, count - 1);
63
- }
64
- };
65
- },
66
- async listener(signal2) {
67
- if (context.triggered) {
68
- return;
69
- }
70
- context.triggered = true;
71
- context.kill = signal2;
72
- const listeners = [...emitter.listeners(signal2)];
73
- for (const listener of listeners.reverse()) {
74
- await listener(signal2, context);
75
- }
76
- if (context.terminate === "kill" || context.terminate === "exit") {
77
- process.removeListener("SIGINT", handlers.SIGINT.listener);
78
- process.removeListener("SIGTERM", handlers.SIGTERM.listener);
79
- process.removeListener("SIGQUIT", handlers.SIGQUIT.listener);
80
- if (context.terminate === "kill") {
81
- process.kill(process.pid, context.kill);
82
- } else {
83
- process.exit(context.exit);
84
- }
85
- }
86
- context.triggered = false;
87
- context.kill = void 0;
88
- }
89
- };
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
+ };
90
91
  }
91
92
 
92
- export { onDeath };
93
+ //#endregion
94
+ export { onDeath, useDeath };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@breadc/death",
3
- "version": "0.9.7-beta.0",
3
+ "version": "1.0.0-beta.1",
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
- "require": "./dist/index.cjs",
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.ts",
33
+ "types": "dist/index.d.mts",
36
34
  "files": [
37
35
  "dist"
38
36
  ],
39
- "devDependencies": {
40
- "@types/node": "^18.17.5",
41
- "vitest": "^0.33.0"
42
- },
43
37
  "scripts": {
44
- "build": "unbuild",
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,94 +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 registerCallback(signal, handler) {
38
- if (handler.count === 0) {
39
- handler.count += 1;
40
- process.addListener(signal, handler.listener);
41
- }
42
- }
43
- function makeHandler(signal) {
44
- const callbacks = /* @__PURE__ */ new WeakMap();
45
- return {
46
- count: 0,
47
- callbacks,
48
- addCallback(callback) {
49
- {
50
- const count = callbacks.get(callback);
51
- if (count !== void 0) {
52
- callbacks.set(callback, count + 1);
53
- } else {
54
- callbacks.set(callback, 1);
55
- emitter.addListener(signal, callback);
56
- }
57
- }
58
- return () => {
59
- const count = callbacks.get(callback);
60
- if (count === void 0 || count <= 1) {
61
- callbacks.delete(callback);
62
- emitter.removeListener(signal, callback);
63
- } else {
64
- callbacks.set(callback, count - 1);
65
- }
66
- };
67
- },
68
- async listener(signal2) {
69
- if (context.triggered) {
70
- return;
71
- }
72
- context.triggered = true;
73
- context.kill = signal2;
74
- const listeners = [...emitter.listeners(signal2)];
75
- for (const listener of listeners.reverse()) {
76
- await listener(signal2, context);
77
- }
78
- if (context.terminate === "kill" || context.terminate === "exit") {
79
- process.removeListener("SIGINT", handlers.SIGINT.listener);
80
- process.removeListener("SIGTERM", handlers.SIGTERM.listener);
81
- process.removeListener("SIGQUIT", handlers.SIGQUIT.listener);
82
- if (context.terminate === "kill") {
83
- process.kill(process.pid, context.kill);
84
- } else {
85
- process.exit(context.exit);
86
- }
87
- }
88
- context.triggered = false;
89
- context.kill = void 0;
90
- }
91
- };
92
- }
93
-
94
- exports.onDeath = onDeath;
package/dist/index.d.ts DELETED
@@ -1,16 +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
-
16
- export { DeathSignals, OnDeathCallback, OnDeathContext, OnDeathOptions, onDeath };