destroyable-server
Advanced tools
Comparing version
@@ -57,5 +57,4 @@ "use strict"; | ||
reject(err); | ||
else | ||
resolve(); | ||
}); | ||
const closePromises = []; | ||
for (let key in connectionDict) { | ||
@@ -66,5 +65,19 @@ const connections = connectionDict[key]; | ||
for (let i = connections.length - 1; i >= 0; i--) { | ||
connections[i].destroy(); | ||
const conn = connections[i]; | ||
closePromises.push(new Promise((resolve) => { | ||
if (conn.closed || conn.destroyed) | ||
return resolve(); | ||
conn.on('close', resolve); | ||
})); | ||
conn.destroy(); | ||
} | ||
} | ||
// Wait for all connections to actually close: | ||
Promise.all(closePromises).then(() => { | ||
// We defer this fractionally, so that any localhost sockets have a | ||
// chance to process the other end of this socket closure. Without | ||
// this, you can see client conns still open after this promise | ||
// resolved, which can cause problems for connection reuse. | ||
setImmediate(() => resolve()); | ||
}); | ||
}); | ||
@@ -71,0 +84,0 @@ } |
19
index.ts
@@ -66,5 +66,6 @@ import * as net from 'net'; | ||
if (err) reject(err); | ||
else resolve(); | ||
}); | ||
const closePromises: Array<Promise<void>> = []; | ||
for (let key in connectionDict) { | ||
@@ -75,5 +76,19 @@ const connections = connectionDict[key]; | ||
for (let i = connections.length - 1; i >= 0; i--) { | ||
connections[i].destroy(); | ||
const conn = connections[i]; | ||
closePromises.push(new Promise((resolve) => { | ||
if (conn.closed || conn.destroyed) return resolve(); | ||
conn.on('close', resolve); | ||
})); | ||
conn.destroy(); | ||
} | ||
} | ||
// Wait for all connections to actually close: | ||
Promise.all(closePromises).then(() => { | ||
// We defer this fractionally, so that any localhost sockets have a | ||
// chance to process the other end of this socket closure. Without | ||
// this, you can see client conns still open after this promise | ||
// resolved, which can cause problems for connection reuse. | ||
setImmediate(() => resolve()); | ||
}); | ||
}); | ||
@@ -80,0 +95,0 @@ } |
{ | ||
"name": "destroyable-server", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"author": "Tim Perry <tim@httptoolkit.com>", | ||
@@ -5,0 +5,0 @@ "description": "A tiny Node.js module to make any server force-closeable", |
@@ -9,3 +9,3 @@ # Destroyable-Server [](https://github.com/httptoolkit/destroyable-server/actions) [](https://npmjs.com/package/destroyable-server) | ||
With this module, you can call `server.destroy()` to forcibly shut down all existing sockets in addition to closing the server, to immediately & completely end all connections. | ||
With this module, you can call `server.destroy()` to forcibly shut down all existing sockets in addition to closing the server, to immediately & completely (but cleanly) end all connections. Unlike `server.closeIdleConnections()` and similar, this is available on all net servers (not just HTTP) closes all connections with no possibility of race conditions as new connections appear, and provides a promise you can wait on to ensure all connection closure has fully completed before your code continues. | ||
@@ -12,0 +12,0 @@ This works for HTTP, TLS, bare sockets, whatever. Anything that extends `net.Server` or follows its patterns should work correctly. |
Sorry, the diff of this file is not supported yet
24487
11.1%189
15.95%