Gathering detailed insights and metrics for @ladjs/graceful
Gathering detailed insights and metrics for @ladjs/graceful
Gathering detailed insights and metrics for @ladjs/graceful
Gathering detailed insights and metrics for @ladjs/graceful
Gracefully exit HTTP servers (Express/Koa/Fastify/etc), databases (Mongo/Mongoose), Redis clients, Bree job schedulers, and custom handlers.
npm install @ladjs/graceful
Typescript
Module System
Min. Node Version
Node Version
NPM Version
94.6
Supply Chain
100
Quality
83.8
Maintenance
100
Vulnerability
100
License
JavaScript (98.79%)
Shell (1.21%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
74 Stars
68 Commits
8 Forks
3 Watchers
2 Branches
7 Contributors
Updated on Jan 07, 2025
Latest Version
4.2.0
Package Id
@ladjs/graceful@4.2.0
Unpacked Size
26.06 kB
Size
6.82 kB
File Count
5
NPM Version
10.7.0
Node Version
18.20.4
Published on
Jan 07, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Gracefully exit HTTP servers (Express/Koa/Fastify/etc), databases (Mongo/Mongoose), Redis clients, Bree job schedulers, and custom handlers.
npm:
1npm install @ladjs/graceful
See the Express, Koa, Fastify, or Other code snippet examples and Instance Options below.
You can pass Instance Options to customize your graceful handler (e.g. if you have more than one server, or wish to close both a Redis connection and a server at the same time).
1const Graceful = require('@ladjs/graceful'); 2 3// 4// ... 5// 6 7// 8// see Instance Options in the README below and examples for different projects (e.g. Koa or Express) 9// 10const graceful = new Graceful({ 11 // 12 // http or net servers 13 // (this supports Express/Koa/Fastify/etc) 14 // (basically anything created with http.createServer or net.createServer) 15 // <https://github.com/expressjs/express> 16 // <https://github.com/koajs/koa> 17 // <https://github.com/fastify/fastify> 18 // 19 servers: [], 20 21 // bree clients 22 // <https://github.com/breejs/bree> 23 brees: [], 24 25 // redis clients 26 // <https://github.com/luin/ioredis> 27 // <https://github.com/redis/node-redis> 28 redisClients: [], 29 30 // mongoose clients 31 // <https://github.com/Automattic/mongoose> 32 mongooses: [], 33 34 // custom handlers to invoke upon shutdown 35 customHandlers: [], 36 37 // logger 38 logger: console, 39 40 // how long to wait in ms for exit to finish 41 timeoutMs: 5000, 42 43 // options to pass to `lil-http-terminator` to override defaults 44 lilHttpTerminator: {}, 45 46 // 47 // appends a `true` boolean value to a property of this name in the logger meta object 48 // (this is useful for Cabin/Axe as it will prevent a log from being created in MongoDB) 49 // (and instead of having a DB log created upon graceful exit, it will simply log to console) 50 // (defer to the Forward Email codebase, specifically the logger helper) 51 // 52 // NOTE: if you set this to `false` then this will be ignored and no meta property will be populated 53 // 54 ignoreHook: 'ignore_hook', 55 56 // 57 // appends a `true` boolean value to a property of this name in the logger meta object 58 // (this is useful for Cabin/Axe as it will prevent the meta object from being outputted to the logger) 59 // 60 hideMeta: 'hide_meta' 61}); 62 63// 64// NOTE: YOU MUST INVOKE `graceful.listen()` IN ORDER FOR THIS TO WORK! 65// 66graceful.listen();
Using this package will bind process event listeners when graceful.listen()
is called:
process.on('warning')
- will output via config.logger.warn
process.on('unhandledRejection')
- bubbles up to uncaughtException
(will output via config.logger.error
and process.exit(1)
(does not exit gracefully)process.once('uncaughtException')
- will output via config.logger.error
and process.exit(1)
(does not exit gracefully)process.on('message')
- support Windows (e.g. signals not available) and listen for message of shutdown
and then exit gracefullyprocess.once('SIGTERM')
- will exit gracefullyprocess.once('SIGHUP')
- will exit gracefullyprocess.once('SIGINT')
- will exit gracefullyThis package also prevents multiple process/SIG events from triggering multiple graceful exits. Only one graceful exit can occur at a time.
For servers
passed, we use lil-http-terminator under the hood. Default configuration options can be overridden by passing a lilHttpTerminator
configuration object. See index.js for more insight.
1const express = require('express'); 2const Graceful = require('@ladjs/graceful'); 3 4const app = express(); 5const server = app.listen(); 6const graceful = new Graceful({ servers: [server] }); 7graceful.listen();
1const Koa = require('koa'); 2const Graceful = require('@ladjs/graceful'); 3 4const app = new Koa(); 5const server = app.listen(); 6const graceful = new Graceful({ servers: [server] }); 7graceful.listen();
1const fastify = require('fastify'); 2const Graceful = require('@ladjs/graceful'); 3 4const app = fastify(); 5app.listen(); 6 7// 8// NOTE: @ladjs/graceful is smart and detects `app.server` automatically 9// 10const graceful = new Graceful({ servers: [app] }); 11graceful.listen();
This package works with any server created with http.createServer
or net.createServer
(Node's internal HTTP and Net packages).
Please defer to the test folder files for example usage.
Here is the full list of options and their defaults. See index.js for more insight if necessary.
Property | Type | Default Value | Description |
---|---|---|---|
servers | Array | [] | An array of HTTP or NET servers to gracefully close on exit |
brees | Array | [] | An array of Bree instances to gracefully exit |
redisClients | Array | [] | An array of Redis client instances to gracefully exit |
mongooses | Array | [] | An array of Mongoose connections to gracefully exit |
customHandlers | Array | [] | An array of functions (custom handlers) to invoke upon graceful exit |
logger | Object | console | This is the default logger. We recommend using Cabin instead of using console as your default logger. Set this value to false to disable logging entirely (uses noop function) |
timeoutMs | Number | 5000 | A number in milliseconds for how long to wait to gracefully exit |
lilHttpTerminator | Object | {} | An object of options to pass to lil-http-terminator to override default options provided |
ignoreHook | String or false Boolean | "ignore_hook" | Appends a true boolean property to a property with this value in logs, e.g. console.log('graceful exiting', { ignore_hook: true }); which is useful for preventing logs from being written to a database in hooks (this is meant for usage with Cabin and Axe and made for Forward Email). If you pass a false value then this property will not get populated. |
hideMeta | String or false Boolean | "hide_meta" | Appends a true boolean property to a property with this value in logs, e.g. console.log('graceful exiting', { hide_meta: true }); which is useful for preventing metadata object from being invoked as the second argument (this is meant for usage with Cabin and Axe and made for Forward Email). If you pass a false value then this property will not get populated. |
You can refer Forward Email for more complex usage:
Additionally you can also refer to Lad usage:
You can also read more about Bree at https://github.com/breejs/bree.
Name | Website |
---|---|
Nick Baugh | http://niftylettuce.com/ |
Felix Mosheev | https://github.com/felixmosh |
Nicholai Nissen | https://nicholai.dev |
Spencer Snyder | https://spencersnyder.io |
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
Found 1/29 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More