@alwatr/exit-hook 1.0.0
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/CHANGELOG.md +10 -0
- package/LICENSE +21 -0
- package/README.md +21 -0
- package/dist/main.cjs +3 -0
- package/dist/main.cjs.map +7 -0
- package/dist/main.d.ts +16 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.mjs +3 -0
- package/dist/main.mjs.map +7 -0
- package/package.json +75 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
# 1.0.0 (2024-01-03)
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* **exit-hook:** new package ([a41b3a0](https://github.com/Alwatr/nanolib/commit/a41b3a01a4e6af595521e506326678eb96491a11)) by @njfamirm
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 S. Ali Mihandoost
|
|
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,21 @@
|
|
|
1
|
+
# Exit Hook
|
|
2
|
+
|
|
3
|
+
A utility for registering exit handlers in Node.js.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
yarn add @alwatr/exit-hook
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import {exitHook} from '@alwatr/exit-hook';
|
|
15
|
+
|
|
16
|
+
const saveAllData = () => {
|
|
17
|
+
// save all data
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
exitHook(saveAllData);
|
|
21
|
+
```
|
package/dist/main.cjs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/* @alwatr/exit-hook v1.0.0 */
|
|
2
|
+
"use strict";var n=Object.defineProperty;var a=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var p=(o,e)=>{for(var r in e)n(o,r,{get:e[r],enumerable:!0})},u=(o,e,r,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let c of f(e))!x.call(o,c)&&c!==r&&n(o,c,{get:()=>e[c],enumerable:!(s=a(e,c))||s.enumerable});return o};var k=o=>u(n({},"__esModule",{value:!0}),o);var E={};p(E,{existHook:()=>b});module.exports=k(E);var i=[],l=!1;function b(o){i.push(o)}function t(){if(l!==!0){l=!0;for(let o of i)try{o()}catch(e){console.error("Error in exit hook callback:",e)}}}process.once("beforeExit",t),process.once("exit",t),process.once("SIGTERM",t),process.once("SIGINT",t);0&&(module.exports={existHook});
|
|
3
|
+
//# sourceMappingURL=main.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/main.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Array of callback functions to be called when the process is exiting.\n */\nconst callbacks: (() => void)[] = [];\n\n/**\n * True whether the process is exiting to prevent calling the callbacks more than once.\n */\nlet exiting = false;\n\n/**\n * Add a callback function to be called when the process is exiting.\n *\n * @param callback The callback function to be called when the process is exiting.\n *\n * @example\n * ```typescript\n * const saveAllData = () => {\n * // save all data\n * };\n *\n * existHook(saveAllData);\n * ```\n */\nexport function existHook(callback: () => void): void {\n callbacks.push(callback);\n}\n\n/**\n * A once callback to be called on process exit event.\n */\nfunction onExit_() {\n if (exiting === true) return;\n exiting = true;\n\n for (const callback of callbacks) {\n try {\n callback();\n }\n catch (error) {\n console.error('Error in exit hook callback:', error);\n }\n }\n}\n\n/**\n * This event emitted when Node.js empties its event loop and has no additional work to schedule.\n * Normally, the Node.js process will exit when there is no work scheduled,\n * but a listener registered on the 'beforeExit' event can make asynchronous calls, and thereby cause the Node.js process to continue.\n *\n * @see https://nodejs.org/api/process.html#event-beforeexit\n */\nprocess.once('beforeExit', onExit_);\n\n/**\n * This event is emitted when the Node.js process is about to exit as a result of either:\n * 1- The `process.exit()` method being called explicitly.\n * 2- The Node.js event loop no longer having any additional work to perform.\n *\n * @see https://nodejs.org/api/process.html#event-exit\n */\nprocess.once('exit', onExit_);\n\n/**\n * This event is emitted in terminal mode before exiting with code 128 + signal number.\n *\n * @see https://nodejs.org/api/process.html#signal-events\n */\nprocess.once('SIGTERM', onExit_);\n\n/**\n * This event is emitted when `Ctrl+C` is pressed.\n *\n * @see https://nodejs.org/api/process.html#signal-events\n */\nprocess.once('SIGINT', onExit_);\n"],
|
|
5
|
+
"mappings": ";yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,eAAAE,IAAA,eAAAC,EAAAH,GAGA,IAAMI,EAA4B,CAAC,EAK/BC,EAAU,GAgBP,SAASH,EAAUI,EAA4B,CACpDF,EAAU,KAAKE,CAAQ,CACzB,CAKA,SAASC,GAAU,CACjB,GAAIF,IAAY,GAChB,CAAAA,EAAU,GAEV,QAAWC,KAAYF,EACrB,GAAI,CACFE,EAAS,CACX,OACOE,EAAO,CACZ,QAAQ,MAAM,+BAAgCA,CAAK,CACrD,EAEJ,CASA,QAAQ,KAAK,aAAcD,CAAO,EASlC,QAAQ,KAAK,OAAQA,CAAO,EAO5B,QAAQ,KAAK,UAAWA,CAAO,EAO/B,QAAQ,KAAK,SAAUA,CAAO",
|
|
6
|
+
"names": ["main_exports", "__export", "existHook", "__toCommonJS", "callbacks", "exiting", "callback", "onExit_", "error"]
|
|
7
|
+
}
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Add a callback function to be called when the process is exiting.
|
|
3
|
+
*
|
|
4
|
+
* @param callback The callback function to be called when the process is exiting.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* const saveAllData = () => {
|
|
9
|
+
* // save all data
|
|
10
|
+
* };
|
|
11
|
+
*
|
|
12
|
+
* existHook(saveAllData);
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare function existHook(callback: () => void): void;
|
|
16
|
+
//# sourceMappingURL=main.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAUA;;;;;;;;;;;;;GAaG;AACH,wBAAgB,SAAS,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,IAAI,CAEpD"}
|
package/dist/main.mjs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/* @alwatr/exit-hook v1.0.0 */
|
|
2
|
+
var c=[],r=!1;function n(e){c.push(e)}function o(){if(r!==!0){r=!0;for(let e of c)try{e()}catch(t){console.error("Error in exit hook callback:",t)}}}process.once("beforeExit",o),process.once("exit",o),process.once("SIGTERM",o),process.once("SIGINT",o);export{n as existHook};
|
|
3
|
+
//# sourceMappingURL=main.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/main.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Array of callback functions to be called when the process is exiting.\n */\nconst callbacks: (() => void)[] = [];\n\n/**\n * True whether the process is exiting to prevent calling the callbacks more than once.\n */\nlet exiting = false;\n\n/**\n * Add a callback function to be called when the process is exiting.\n *\n * @param callback The callback function to be called when the process is exiting.\n *\n * @example\n * ```typescript\n * const saveAllData = () => {\n * // save all data\n * };\n *\n * existHook(saveAllData);\n * ```\n */\nexport function existHook(callback: () => void): void {\n callbacks.push(callback);\n}\n\n/**\n * A once callback to be called on process exit event.\n */\nfunction onExit_() {\n if (exiting === true) return;\n exiting = true;\n\n for (const callback of callbacks) {\n try {\n callback();\n }\n catch (error) {\n console.error('Error in exit hook callback:', error);\n }\n }\n}\n\n/**\n * This event emitted when Node.js empties its event loop and has no additional work to schedule.\n * Normally, the Node.js process will exit when there is no work scheduled,\n * but a listener registered on the 'beforeExit' event can make asynchronous calls, and thereby cause the Node.js process to continue.\n *\n * @see https://nodejs.org/api/process.html#event-beforeexit\n */\nprocess.once('beforeExit', onExit_);\n\n/**\n * This event is emitted when the Node.js process is about to exit as a result of either:\n * 1- The `process.exit()` method being called explicitly.\n * 2- The Node.js event loop no longer having any additional work to perform.\n *\n * @see https://nodejs.org/api/process.html#event-exit\n */\nprocess.once('exit', onExit_);\n\n/**\n * This event is emitted in terminal mode before exiting with code 128 + signal number.\n *\n * @see https://nodejs.org/api/process.html#signal-events\n */\nprocess.once('SIGTERM', onExit_);\n\n/**\n * This event is emitted when `Ctrl+C` is pressed.\n *\n * @see https://nodejs.org/api/process.html#signal-events\n */\nprocess.once('SIGINT', onExit_);\n"],
|
|
5
|
+
"mappings": ";AAGA,IAAMA,EAA4B,CAAC,EAK/BC,EAAU,GAgBP,SAASC,EAAUC,EAA4B,CACpDH,EAAU,KAAKG,CAAQ,CACzB,CAKA,SAASC,GAAU,CACjB,GAAIH,IAAY,GAChB,CAAAA,EAAU,GAEV,QAAWE,KAAYH,EACrB,GAAI,CACFG,EAAS,CACX,OACOE,EAAO,CACZ,QAAQ,MAAM,+BAAgCA,CAAK,CACrD,EAEJ,CASA,QAAQ,KAAK,aAAcD,CAAO,EASlC,QAAQ,KAAK,OAAQA,CAAO,EAO5B,QAAQ,KAAK,UAAWA,CAAO,EAO/B,QAAQ,KAAK,SAAUA,CAAO",
|
|
6
|
+
"names": ["callbacks", "exiting", "existHook", "callback", "onExit_", "error"]
|
|
7
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@alwatr/exit-hook",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A utility for registering exit handlers in Node.js.",
|
|
5
|
+
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"exit-hook",
|
|
8
|
+
"hook",
|
|
9
|
+
"exit",
|
|
10
|
+
"cross-platform",
|
|
11
|
+
"ECMAScript",
|
|
12
|
+
"typescript",
|
|
13
|
+
"javascript",
|
|
14
|
+
"node",
|
|
15
|
+
"nodejs",
|
|
16
|
+
"esm",
|
|
17
|
+
"module",
|
|
18
|
+
"utility",
|
|
19
|
+
"util",
|
|
20
|
+
"utils",
|
|
21
|
+
"nanolib",
|
|
22
|
+
"alwatr"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "./dist/main.cjs",
|
|
26
|
+
"module": "./dist/main.mjs",
|
|
27
|
+
"types": "./dist/main.d.ts",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"import": "./dist/main.mjs",
|
|
31
|
+
"require": "./dist/main.cjs",
|
|
32
|
+
"types": "./dist/main.d.ts"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"files": [
|
|
37
|
+
"**/*.{js,mjs,cjs,map,d.ts,html,md}",
|
|
38
|
+
"!demo/**/*"
|
|
39
|
+
],
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "https://github.com/Alwatr/nanolib",
|
|
46
|
+
"directory": "packages/exit-hook"
|
|
47
|
+
},
|
|
48
|
+
"homepage": "https://github.com/Alwatr/nanolib/tree/next/packages/exit-hook#readme",
|
|
49
|
+
"bugs": {
|
|
50
|
+
"url": "https://github.com/Alwatr/nanolib/issues"
|
|
51
|
+
},
|
|
52
|
+
"prettier": "@alwatr/prettier-config",
|
|
53
|
+
"scripts": {
|
|
54
|
+
"b": "yarn run build",
|
|
55
|
+
"w": "yarn run watch",
|
|
56
|
+
"c": "yarn run clean",
|
|
57
|
+
"cb": "yarn run clean && yarn run build",
|
|
58
|
+
"d": "yarn run build:es && ALWATR_DEBUG=1 yarn node",
|
|
59
|
+
"build": "yarn run build:ts & yarn run build:es",
|
|
60
|
+
"build:es": "nano-build --preset=module",
|
|
61
|
+
"build:ts": "tsc --build",
|
|
62
|
+
"watch": "yarn run watch:ts & yarn run watch:es",
|
|
63
|
+
"watch:es": "yarn run build:es --watch",
|
|
64
|
+
"watch:ts": "yarn run build:ts --watch --preserveWatchOutput",
|
|
65
|
+
"clean": "rm -rfv dist *.tsbuildinfo"
|
|
66
|
+
},
|
|
67
|
+
"devDependencies": {
|
|
68
|
+
"@alwatr/nano-build": "^1.2.3",
|
|
69
|
+
"@alwatr/prettier-config": "^1.0.4",
|
|
70
|
+
"@alwatr/tsconfig-base": "^1.1.0",
|
|
71
|
+
"@types/node": "^20.10.6",
|
|
72
|
+
"typescript": "^5.3.3"
|
|
73
|
+
},
|
|
74
|
+
"gitHead": "f78592294341aff359a6b67f703550d007056beb"
|
|
75
|
+
}
|