Registry / http-networking / destroyable-server

destroyable-server

JSON →
library1.1.1jsnpmunverified

Destroyable-Server, currently at version 1.1.1, is a compact Node.js module designed to enhance the `net.Server` API by providing a reliable and immediate mechanism to terminate all active client connections. When `server.close()` is invoked, it merely ceases to accept new connections, often leaving existing ones open and preventing the Node.js process from exiting cleanly. This library introduces a `destroy()` method to server instances via its `makeDestroyable` utility, which not only stops listening but also forcibly closes every tracked socket. This capability is critical for scenarios requiring rapid server restarts, robust test suite teardowns, or graceful application shutdowns. It works across various `net.Server` subclasses, including HTTP and TLS, and uniquely offers a promise-based interface for awaiting complete connection termination, differentiating it from `server.closeIdleConnections()` which might miss active connections or not be universally available. The project maintains a stable, low-cadence release cycle, reflecting its focused utility and role as part of HTTP Toolkit.

npm install destroyable-server
INSTALL
IMPORT
SIG · DESTROYABLE-SERVER
D
destroyable-server
http-networkingjavascriptv1.1.1
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

makeDestroyable
✓ import { makeDestroyable } from 'destroyable-server';
✗ import makeDestroyable from 'destroyable-server';
This package uses named exports. A default import is incorrect.
makeDestroyable
✓ const { makeDestroyable } = require('destroyable-server');
✗ const makeDestroyable = require('destroyable-server');
CommonJS requires destructuring for the named export. Importing the entire module directly would result in an object containing the function.
MakeDestroyable
✓ import type { MakeDestroyable } from 'destroyable-server';
TypeScript users can import the `MakeDestroyable` type for explicit type annotations on server instances that have been made destroyable.

This quickstart demonstrates creating a Node.js TCP server, making it destroyable, and then gracefully shutting it down by forcibly closing all active connections after a delay.

import { createServer } from 'net'; import { makeDestroyable } from 'destroyable-server'; const PORT = process.env.PORT ?? 8000; // Create a basic TCP server let server = createServer((socket) => { console.log('Client connected.'); socket.write('Hello from destroyable-server!\n'); socket.on('end', () => { console.log('Client disconnected.'); }); // Example: close connection after 5 seconds setTimeout(() => { if (!socket.destroyed) { socket.end('Server closing connection.\n'); } }, 5000); }); // Make the server destroyable server = makeDestroyable(server); server.listen(PORT, () => { console.log(`Server listening on port ${PORT}.`); }); // Simulate a shutdown after 10 seconds setTimeout(async () => { console.log('Initiating server shutdown...'); try { await server.destroy(); console.log('Server and all connections destroyed successfully.'); } catch (error) { console.error('Error during server destruction:', error); } // In a real application, you might then exit the process: process.exit(0); }, 10000);
Debug
Known issues
gotchaThe `makeDestroyable` function mutates the server object passed to it, adding internal tracking for connections and the `destroy()` method directly to the instance. Be aware that the original server object reference will be modified.
fix
While the mutation is intended behavior, developers should treat the return value of `makeDestroyable` as the primary reference for the enhanced server, as shown in `server = makeDestroyable(server);`.
affects: >=1.0.0
gotchaThe `server.destroy()` method returns a Promise that resolves once all connections are terminated and the server is fully closed. Failing to `await` this promise can lead to premature process exits or other cleanup logic running before all resources are released, potentially causing resource leaks or 'address already in use' errors on restart.
fix
Always use `await server.destroy();` when initiating a shutdown to ensure complete resource cleanup before continuing or exiting the process.
affects: >=1.0.0
gotchaThis module is specifically designed for `net.Server` instances and objects following its patterns. Attempting to use `makeDestroyable` on objects that are not `net.Server` instances or lack the necessary underlying socket management may lead to unexpected behavior or errors.
fix
Ensure that the object passed to `makeDestroyable` is an instance of `net.Server` or a compatible derivative like `http.Server` or `https.Server`.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: server.destroy is not a function
You are attempting to call `destroy()` on a server object that has not been enhanced by `makeDestroyable`.
fix
Ensure you have called `server = makeDestroyable(server);` (or similar assignment) on your server instance before attempting to invoke `server.destroy()`.
Error: listen EADDRINUSE: address already in use :::PORT
A previous server instance on the same port was not fully shut down before a new one attempted to bind to the port. This often happens when `server.destroy()` was called but not awaited, or if the process exited before the cleanup completed.
fix
Always `await server.destroy()` during shutdown routines to guarantee that all connections are closed and the port is released before attempting to restart the server or exit the process.
Upgrade
Version history
1.1.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
OpenAI (training)
1
Resources
destroyable-server — npm install destroyable-server · libregistry