Installations
npm install http-terminator
Developer
gajus
Developer Guide
Module System
CommonJS
Min. Node Version
>=14
Typescript Support
No
Node Version
14.19.0
NPM Version
7.24.2
Statistics
324 Stars
61 Commits
26 Forks
5 Watching
1 Branches
6 Contributors
Updated on 25 Sept 2024
Languages
TypeScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
40,367,768
Last day
-56.2%
104,745
Compared to previous day
Last week
2%
1,035,634
Compared to previous week
Last month
5.7%
4,007,287
Compared to previous month
Last year
78.1%
20,882,391
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
http-terminator 🦾
Gracefully terminates HTTP(S) server.
Behaviour
When you call server.close()
, it stops the server from accepting new connections, but it keeps the existing connections open indefinitely. This can result in your server hanging indefinitely due to keep-alive connections or because of the ongoing requests that do not produce a response. Therefore, in order to close the server, you must track creation of all connections and terminate them yourself.
http-terminator implements the logic for tracking all connections and their termination upon a timeout. http-terminator also ensures graceful communication of the server intention to shutdown to any clients that are currently receiving response from this server.
API
1import { 2 createHttpTerminator, 3} from 'http-terminator'; 4 5/** 6 * @property gracefulTerminationTimeout Number of milliseconds to allow for the active sockets to complete serving the response (default: 5000). 7 * @property server Instance of http.Server. 8 */ 9type HttpTerminatorConfigurationInputType = {| 10 +gracefulTerminationTimeout?: number, 11 +server: Server, 12|}; 13 14/** 15 * @property terminate Terminates HTTP server. 16 */ 17type HttpTerminatorType = {| 18 +terminate: () => Promise<void>, 19|}; 20 21 22const httpTerminator: HttpTerminatorType = createHttpTerminator( 23 configuration: HttpTerminatorConfigurationInputType 24); 25
Usage
Use createHttpTerminator
to create an instance of http-terminator and instead of using server.close()
, use httpTerminator.terminate()
, e.g.
1import http from 'http'; 2import { 3 createHttpTerminator, 4} from 'http-terminator'; 5 6const server = http.createServer(); 7 8const httpTerminator = createHttpTerminator({ 9 server, 10}); 11 12await httpTerminator.terminate(); 13
Usage with Express
Usage with Express example:
1import express from 'express'; 2import { 3 createHttpTerminator, 4} from 'http-terminator'; 5 6const app = express(); 7 8const server = app.listen(); 9 10const httpTerminator = createHttpTerminator({ 11 server, 12}); 13 14await httpTerminator.terminate(); 15
Usage with Fastify
Usage with Fastify example:
1import fastify from 'fastify'; 2import { 3 createHttpTerminator, 4} from 'http-terminator'; 5 6const app = fastify(); 7 8void app.listen(0); 9 10const httpTerminator = createHttpTerminator({ 11 server: app.server, 12}); 13 14await httpTerminator.terminate(); 15
Usage with Koa
Usage with Koa example:
1import Koa from 'koa'; 2import { 3 createHttpTerminator, 4} from 'http-terminator'; 5 6const app = new Koa(); 7 8const server = app.listen(); 9 10const httpTerminator = createHttpTerminator({ 11 server, 12}); 13 14await httpTerminator.terminate(); 15
Usage with other HTTP frameworks
As it should be clear from the usage examples for Node.js HTTP server, Express and Koa, http-terminator works by accessing an instance of a Node.js http.Server
. To understand how to use http-terminator with your framework, identify how to access an instance of http.Server
and use it to create a http-terminator instance.
Alternative libraries
There are several alternative libraries that implement comparable functionality, e.g.
- https://github.com/hunterloftis/stoppable
- https://github.com/thedillonb/http-shutdown
- https://github.com/tellnes/http-close
- https://github.com/sebhildebrandt/http-graceful-shutdown
The main benefit of http-terminator is that:
- it does not monkey-patch Node.js API
- it immediately destroys all sockets without an attached HTTP request
- it allows graceful timeout to sockets with ongoing HTTP requests
- it properly handles HTTPS connections
- it informs connections using keep-alive that server is shutting down by setting a
connection: close
header - it does not terminate the Node.js process
FAQ
What is the use case for http-terminator?
To gracefully terminate a HTTP server.
We say that a service is gracefully terminated when service stops accepting new clients, but allows time to complete the existing requests.
There are several reasons to terminate services gracefully:
- Terminating a service gracefully ensures that the client experience is not affected (assuming the service is load-balanced).
- If your application is stateful, then when services are not terminated gracefully, you are risking data corruption.
- Forcing termination of the service with a timeout ensures timely termination of the service (otherwise the service can remain hanging indefinitely).
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Warn: project license file does not contain an FSF or OSI license.
Reason
Found 3/22 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 11 are checked with a SAST tool
Score
3.1
/10
Last Scanned on 2024-11-25
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