npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

server.close

v0.2.0

Published

A graceful shutdown handler for Node.js HTTP/HTTP2 servers.

Readme

server.close

A graceful shutdown handler for Node.js HTTP/HTTP2 servers.

Features

  • 🚀 Support for HTTP and HTTP2 servers
  • ⏱️ Configurable timeout
  • 🔌 Automatic Connection: close header for active connections
  • 🎯 Custom signal support
  • 📦 TypeScript support
  • 🪶 Zero dependencies

Installation

npm install server.close
pnpm add server.close
yarn add server.close

Usage

Basic Usage

import { createServer } from 'node:http';
import { gracefulShutdown } from 'server.close';

const server = createServer((req, res) => {
  res.end('Hello World');
});

// Setup graceful shutdown
gracefulShutdown(server);

server.listen(3000, () => {
  console.log('Server listening on port 3000');
});

With Options

import { createServer } from 'node:http';
import { gracefulShutdown } from 'server.close';

const server = createServer((req, res) => {
  res.end('Hello World');
});

gracefulShutdown(server, {
  // Callback to execute before shutdown
  onShutdown: async (signal) => {
    console.log(`Received ${signal}, cleaning up...`);
    // Close database connections, clear caches, etc.
  },
  // Signals to listen for (default: ['SIGINT', 'SIGTERM'])
  signals: ['SIGINT', 'SIGTERM'],
  // Timeout in milliseconds (default: 10000)
  timeout: 15000,
});

server.listen(3000);

Manual Shutdown

import { createServer } from 'node:http';
import { gracefulShutdown } from 'server.close';

const server = createServer((req, res) => {
  res.end('Hello World');
});

// Get the shutdown function for manual invocation
const shutdown = gracefulShutdown(server);

server.listen(3000);

// Manually trigger shutdown when needed
await shutdown();

With HTTP2

import { createSecureServer } from 'node:http2';
import { readFileSync } from 'node:fs';
import { gracefulShutdown } from 'server.close';

const server = createSecureServer({
  key: readFileSync('server-key.pem'),
  cert: readFileSync('server-cert.pem'),
});

gracefulShutdown(server, {
  timeout: 20000,
});

server.listen(3000);

API

gracefulShutdown(server, options?)

Sets up graceful shutdown for the server.

Parameters

  • server: Server | Http2Server | Http2SecureServer - Node.js HTTP or HTTP2 server instance
  • options?: Options - Optional configuration

Options

| Option | Type | Default | Description | | ------------ | -------------------------------------------- | ----------------------- | ------------------------------------------- | | onShutdown | (signal?: string) => Promise<void> \| void | undefined | Callback to execute before server shutdown | | signals | string[] | ['SIGINT', 'SIGTERM'] | List of signals to listen for | | timeout | number | 10000 | Time to wait before forced shutdown (in ms) |

Returns

(signal?: string) => Promise<void> - Function to manually trigger shutdown

How It Works

  1. Signal Detection: Listens for specified signals (SIGINT, SIGTERM, etc.)
  2. Reject New Connections: Sets Connection: close header for new requests
  3. Handle Active Connections: Sets Connection: close header for in-flight requests
  4. Start Server Closure: Stops accepting new connections and waits for existing ones to complete
  5. Timeout: Forces shutdown if all connections don't close within the configured time
  6. Cleanup: Executes the onShutdown callback
  7. Process Exit: Safely terminates the process