node-last-wish 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/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # node-last-wish
2
+
3
+ A tiny NestJS utility that registers process signal handlers for graceful shutdown.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install node-last-wish
9
+ ```
10
+
11
+ Requires `@nestjs/common` ≥ 9 as a peer dependency.
12
+
13
+ ## Usage
14
+
15
+ ```ts
16
+ import { registerCrashHandlers } from 'node-last-wish';
17
+
18
+ async function bootstrap() {
19
+ const app = await NestFactory.create(AppModule);
20
+ await app.listen(3000);
21
+
22
+ registerCrashHandlers(app, {
23
+ onShutdown: async () => {
24
+ // close DB connections, flush logs, etc.
25
+ },
26
+ });
27
+ }
28
+
29
+ bootstrap();
30
+ ```
31
+
32
+ ## API
33
+
34
+ ### `registerCrashHandlers(app, opts?)`
35
+
36
+ Registers handlers for `uncaughtException`, `unhandledRejection`, and `SIGTERM`.
37
+ On any of these events the process will:
38
+ 1. Run the optional `onShutdown` hook.
39
+ 2. Close the NestJS application (`app.close()`).
40
+ 3. Exit with code `1` (errors) or `0` (SIGTERM).
41
+
42
+ | Parameter | Type | Description |
43
+ |---|---|---|
44
+ | `app` | `INestApplication` | The NestJS application instance. |
45
+ | `opts.logger` | `{ error(...) }` | Custom logger. Defaults to `console`. |
46
+ | `opts.onShutdown` | `() => Promise<void>` | Optional cleanup hook run before closing the app. |
47
+
48
+ ## Build
49
+
50
+ ```bash
51
+ npm run build
52
+ ```
@@ -0,0 +1,8 @@
1
+ import { INestApplication } from "@nestjs/common";
2
+ export interface GracefulShutdownOptions {
3
+ logger?: {
4
+ error: (msg: string, ...args: any[]) => void;
5
+ };
6
+ onShutdown?: () => Promise<void>;
7
+ }
8
+ export declare function registerCrashHandlers(app: INestApplication, opts?: GracefulShutdownOptions): void;
package/dist/index.js ADDED
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.registerCrashHandlers = registerCrashHandlers;
4
+ async function gracefulShutdown(app, opts) {
5
+ var _a;
6
+ const log = (_a = opts === null || opts === void 0 ? void 0 : opts.logger) !== null && _a !== void 0 ? _a : console;
7
+ try {
8
+ if (opts === null || opts === void 0 ? void 0 : opts.onShutdown)
9
+ await opts.onShutdown();
10
+ await app.close();
11
+ }
12
+ catch (e) {
13
+ log.error("Error during graceful shutdown", e);
14
+ }
15
+ }
16
+ function registerCrashHandlers(app, opts) {
17
+ var _a;
18
+ const log = (_a = opts === null || opts === void 0 ? void 0 : opts.logger) !== null && _a !== void 0 ? _a : console;
19
+ process.on("uncaughtException", async (err) => {
20
+ log.error("uncaughtException", err);
21
+ await gracefulShutdown(app, opts);
22
+ process.exit(1);
23
+ });
24
+ process.on("unhandledRejection", async (reason) => {
25
+ log.error("unhandledRejection", reason);
26
+ await gracefulShutdown(app, opts);
27
+ process.exit(1);
28
+ });
29
+ process.on("SIGTERM", async () => {
30
+ log.error("SIGTERM received");
31
+ await gracefulShutdown(app, opts);
32
+ process.exit(0);
33
+ });
34
+ }
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "node-last-wish",
3
+ "version": "1.0.0",
4
+ "description": "Graceful shutdown handlers for NestJS — catches uncaughtException, unhandledRejection, and SIGTERM",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "keywords": [
11
+ "nestjs",
12
+ "graceful-shutdown",
13
+ "shutdown",
14
+ "sigterm",
15
+ "crash-handler"
16
+ ],
17
+ "author": "Nil Golan",
18
+ "license": "MIT",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "https://github.com/Nil9711/node-last-wish.git"
22
+ },
23
+ "bugs": {
24
+ "url": "https://github.com/Nil9711/node-last-wish/issues"
25
+ },
26
+ "homepage": "https://github.com/Nil9711/node-last-wish#readme",
27
+ "scripts": {
28
+ "build": "tsc"
29
+ },
30
+ "peerDependencies": {
31
+ "@nestjs/common": ">=9.0.0"
32
+ },
33
+ "devDependencies": {
34
+ "@nestjs/common": "^9.0.0",
35
+ "@types/node": "^25.2.3",
36
+ "typescript": "^5.9.3"
37
+ }
38
+ }