@breadc/death 0.9.4
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/LICENSE +21 -0
- package/README.md +47 -0
- package/dist/index.cjs +64 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.mjs +62 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 XLor
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# @breadc/death
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@breadc/death) [](https://github.com/yjl9903/Breadc/actions/workflows/ci.yml)
|
|
4
|
+
|
|
5
|
+
Easily register termination signals callbacks.
|
|
6
|
+
|
|
7
|
+
It will listen `SIGINT`, `SIGTERM`, `SIGQUIT` these three signals. When receive any one of them, it will invoke all the callbacks **in the reverse order** (it also **await the asynchronous callbacks**), and finally send the original signal again.
|
|
8
|
+
|
|
9
|
+
> In [Signal events](https://nodejs.org/dist/latest-v20.x/docs/api/process.html#signal-events), it says that `SIGTERM` and `SIGINT` have default handlers on non-Windows platforms that reset the terminal mode before exiting with code 128 + signal number. **If one of these signals has a listener installed, its default behavior will be removed (Node.js will no longer exit).**
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm i @breadc/death
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { onDeath } from '@breadc/death'
|
|
21
|
+
|
|
22
|
+
onDeath((signal) => {
|
|
23
|
+
console.log(`Receive signal: ${signal}`);
|
|
24
|
+
console.log('Process is being killed');
|
|
25
|
+
})
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
You can also use `process.exit` instead of `process.kill`, or disable terminating the current process, which is the default behavior of this package. Note that **the context object is shared between different callbacks**.
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
onDeath((signal, context) => {
|
|
32
|
+
// Use process.exit(1)
|
|
33
|
+
context.terminate = 'exit'
|
|
34
|
+
context.exit = 1
|
|
35
|
+
})
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
onDeath((signal, context) => {
|
|
40
|
+
// Disable terminate
|
|
41
|
+
context.terminate = false
|
|
42
|
+
})
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## License
|
|
46
|
+
|
|
47
|
+
MIT License © 2023 [XLor](https://github.com/yjl9903)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const node_events = require('node:events');
|
|
4
|
+
|
|
5
|
+
const emitter = new node_events.EventEmitter();
|
|
6
|
+
const handlers = {
|
|
7
|
+
SIGINT: makeHandler(),
|
|
8
|
+
SIGTERM: makeHandler(),
|
|
9
|
+
SIGQUIT: makeHandler()
|
|
10
|
+
};
|
|
11
|
+
function onDeath(callback, { SIGINT = true, SIGTERM = true, SIGQUIT = true } = {}) {
|
|
12
|
+
const cleanUps = [];
|
|
13
|
+
if (SIGINT) {
|
|
14
|
+
registerCallback("SIGINT", handlers.SIGINT);
|
|
15
|
+
emitter.addListener("SIGINT", callback);
|
|
16
|
+
cleanUps.push(() => emitter.removeListener("SIGINT", callback));
|
|
17
|
+
}
|
|
18
|
+
if (SIGTERM) {
|
|
19
|
+
registerCallback("SIGTERM", handlers.SIGTERM);
|
|
20
|
+
emitter.addListener("SIGTERM", callback);
|
|
21
|
+
cleanUps.push(() => emitter.removeListener("SIGTERM", callback));
|
|
22
|
+
}
|
|
23
|
+
if (SIGQUIT) {
|
|
24
|
+
registerCallback("SIGQUIT", handlers.SIGQUIT);
|
|
25
|
+
emitter.addListener("SIGQUIT", callback);
|
|
26
|
+
cleanUps.push(() => emitter.removeListener("SIGQUIT", callback));
|
|
27
|
+
}
|
|
28
|
+
return () => {
|
|
29
|
+
for (const cleanUp of cleanUps) {
|
|
30
|
+
cleanUp();
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function registerCallback(signal, callback) {
|
|
35
|
+
process.on(signal, callback);
|
|
36
|
+
return () => {
|
|
37
|
+
process.off(signal, callback);
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function makeHandler(signal) {
|
|
41
|
+
return async (signal2) => {
|
|
42
|
+
const listeners = emitter.listeners(signal2);
|
|
43
|
+
const context = {
|
|
44
|
+
terminate: "kill",
|
|
45
|
+
exit: void 0,
|
|
46
|
+
kill: signal2
|
|
47
|
+
};
|
|
48
|
+
for (const listener of listeners.reverse()) {
|
|
49
|
+
await listener(signal2, context);
|
|
50
|
+
}
|
|
51
|
+
if (context.terminate === "kill" || context.terminate === "exit") {
|
|
52
|
+
process.removeListener("SIGINT", handlers.SIGINT);
|
|
53
|
+
process.removeListener("SIGTERM", handlers.SIGTERM);
|
|
54
|
+
process.removeListener("SIGQUIT", handlers.SIGQUIT);
|
|
55
|
+
if (context.terminate === "kill") {
|
|
56
|
+
process.kill(process.pid, context.kill);
|
|
57
|
+
} else {
|
|
58
|
+
process.exit(context.exit);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
exports.onDeath = onDeath;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
type DeathSignals = 'SIGINT' | 'SIGTERM' | 'SIGQUIT';
|
|
2
|
+
interface OnDeathContext {
|
|
3
|
+
terminate: 'exit' | 'kill' | false;
|
|
4
|
+
exit: number | undefined;
|
|
5
|
+
kill: NodeJS.Signals | undefined;
|
|
6
|
+
}
|
|
7
|
+
type OnDeathCallback = (signal: DeathSignals, context: OnDeathContext) => unknown | Promise<unknown>;
|
|
8
|
+
interface OnDeathOptions {
|
|
9
|
+
SIGINT?: boolean;
|
|
10
|
+
SIGTERM?: boolean;
|
|
11
|
+
SIGQUIT?: boolean;
|
|
12
|
+
}
|
|
13
|
+
declare function onDeath(callback: OnDeathCallback, { SIGINT, SIGTERM, SIGQUIT }?: OnDeathOptions): () => void;
|
|
14
|
+
|
|
15
|
+
export { DeathSignals, OnDeathCallback, OnDeathContext, OnDeathOptions, onDeath };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
|
|
3
|
+
const emitter = new EventEmitter();
|
|
4
|
+
const handlers = {
|
|
5
|
+
SIGINT: makeHandler(),
|
|
6
|
+
SIGTERM: makeHandler(),
|
|
7
|
+
SIGQUIT: makeHandler()
|
|
8
|
+
};
|
|
9
|
+
function onDeath(callback, { SIGINT = true, SIGTERM = true, SIGQUIT = true } = {}) {
|
|
10
|
+
const cleanUps = [];
|
|
11
|
+
if (SIGINT) {
|
|
12
|
+
registerCallback("SIGINT", handlers.SIGINT);
|
|
13
|
+
emitter.addListener("SIGINT", callback);
|
|
14
|
+
cleanUps.push(() => emitter.removeListener("SIGINT", callback));
|
|
15
|
+
}
|
|
16
|
+
if (SIGTERM) {
|
|
17
|
+
registerCallback("SIGTERM", handlers.SIGTERM);
|
|
18
|
+
emitter.addListener("SIGTERM", callback);
|
|
19
|
+
cleanUps.push(() => emitter.removeListener("SIGTERM", callback));
|
|
20
|
+
}
|
|
21
|
+
if (SIGQUIT) {
|
|
22
|
+
registerCallback("SIGQUIT", handlers.SIGQUIT);
|
|
23
|
+
emitter.addListener("SIGQUIT", callback);
|
|
24
|
+
cleanUps.push(() => emitter.removeListener("SIGQUIT", callback));
|
|
25
|
+
}
|
|
26
|
+
return () => {
|
|
27
|
+
for (const cleanUp of cleanUps) {
|
|
28
|
+
cleanUp();
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function registerCallback(signal, callback) {
|
|
33
|
+
process.on(signal, callback);
|
|
34
|
+
return () => {
|
|
35
|
+
process.off(signal, callback);
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function makeHandler(signal) {
|
|
39
|
+
return async (signal2) => {
|
|
40
|
+
const listeners = emitter.listeners(signal2);
|
|
41
|
+
const context = {
|
|
42
|
+
terminate: "kill",
|
|
43
|
+
exit: void 0,
|
|
44
|
+
kill: signal2
|
|
45
|
+
};
|
|
46
|
+
for (const listener of listeners.reverse()) {
|
|
47
|
+
await listener(signal2, context);
|
|
48
|
+
}
|
|
49
|
+
if (context.terminate === "kill" || context.terminate === "exit") {
|
|
50
|
+
process.removeListener("SIGINT", handlers.SIGINT);
|
|
51
|
+
process.removeListener("SIGTERM", handlers.SIGTERM);
|
|
52
|
+
process.removeListener("SIGQUIT", handlers.SIGQUIT);
|
|
53
|
+
if (context.terminate === "kill") {
|
|
54
|
+
process.kill(process.pid, context.kill);
|
|
55
|
+
} else {
|
|
56
|
+
process.exit(context.exit);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export { onDeath };
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@breadc/death",
|
|
3
|
+
"version": "0.9.4",
|
|
4
|
+
"description": "Easily register termination signals callbacks.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"breadc",
|
|
7
|
+
"death",
|
|
8
|
+
"terminal",
|
|
9
|
+
"signal",
|
|
10
|
+
"cli",
|
|
11
|
+
"command-line"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/yjl9903/Breadc/tree/main/packages/death#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/yjl9903/Breadc/issues"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/yjl9903/Breadc.git",
|
|
20
|
+
"directory": "packages/death"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"author": "XLor",
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"type": "module",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"require": "./dist/index.cjs",
|
|
29
|
+
"import": "./dist/index.mjs",
|
|
30
|
+
"types": "./dist/index.d.ts"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"main": "dist/index.cjs",
|
|
34
|
+
"module": "dist/index.mjs",
|
|
35
|
+
"types": "dist/index.d.ts",
|
|
36
|
+
"files": [
|
|
37
|
+
"dist"
|
|
38
|
+
],
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/node": "^18.16.7",
|
|
41
|
+
"vitest": "^0.31.0"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "unbuild",
|
|
45
|
+
"format": "prettier --write src/**/*.ts test/*.ts",
|
|
46
|
+
"test": "vitest",
|
|
47
|
+
"test:ci": "vitest --run",
|
|
48
|
+
"typecheck": "tsc --noEmit"
|
|
49
|
+
}
|
|
50
|
+
}
|