destroyable-server
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -16,8 +16,28 @@ "use strict"; | ||
function makeDestroyable(server) { | ||
const connections = {}; | ||
const connectionDict = {}; | ||
const addConnection = (key, conn) => { | ||
if (connectionDict[key]) { | ||
connectionDict[key].push(conn); | ||
} | ||
else { | ||
connectionDict[key] = [conn]; | ||
} | ||
}; | ||
const removeConnection = (key, conn) => { | ||
const conns = connectionDict[key]; | ||
if (!conns) | ||
return; | ||
const index = conns.indexOf(conn); | ||
if (conns.length === 1 && index === 0) { | ||
delete connectionDict[key]; | ||
} | ||
else if (index !== -1) { | ||
conns.splice(index, 1); | ||
} | ||
}; | ||
server.on('connection', function (conn) { | ||
const key = conn.remoteAddress + ':' + conn.remotePort; | ||
connections[key] = conn; | ||
addConnection(key, conn); | ||
conn.on('close', function () { | ||
delete connections[key]; | ||
removeConnection(key, conn); | ||
}); | ||
@@ -27,5 +47,5 @@ }); | ||
const key = conn.remoteAddress + ':' + conn.remotePort; | ||
connections[key] = conn; | ||
addConnection(key, conn); | ||
conn.on('close', function () { | ||
delete connections[key]; | ||
removeConnection(key, conn); | ||
}); | ||
@@ -42,4 +62,9 @@ }); | ||
}); | ||
for (let key in connections) { | ||
connections[key].destroy(); | ||
for (let key in connectionDict) { | ||
const connections = connectionDict[key]; | ||
// Shut them down in reverse order (most recent first) to try to | ||
// reduce issues with layered connections (like tunnels) | ||
for (let i = connections.length - 1; i >= 0; i--) { | ||
connections[i].destroy(); | ||
} | ||
} | ||
@@ -46,0 +71,0 @@ }); |
39
index.ts
@@ -23,9 +23,29 @@ import * as net from 'net'; | ||
export function makeDestroyable<S extends net.Server>(server: S): DestroyableServer<S> { | ||
const connections: { [key: string]: net.Socket } = {}; | ||
const connectionDict: { [key: string]: Array<net.Socket> } = {}; | ||
const addConnection = (key: string, conn: net.Socket) => { | ||
if (connectionDict[key]) { | ||
connectionDict[key].push(conn); | ||
} else { | ||
connectionDict[key] = [conn]; | ||
} | ||
} | ||
const removeConnection = (key: string, conn: net.Socket) => { | ||
const conns = connectionDict[key]; | ||
if (!conns) return; | ||
const index = conns.indexOf(conn); | ||
if (conns.length === 1 && index === 0) { | ||
delete connectionDict[key]; | ||
} else if (index !== -1) { | ||
conns.splice(index, 1); | ||
} | ||
}; | ||
server.on('connection', function(conn: net.Socket) { | ||
const key = conn.remoteAddress + ':' + conn.remotePort; | ||
connections[key] = conn; | ||
addConnection(key, conn); | ||
conn.on('close', function() { | ||
delete connections[key]; | ||
removeConnection(key, conn); | ||
}); | ||
@@ -36,5 +56,5 @@ }); | ||
const key = conn.remoteAddress + ':' + conn.remotePort; | ||
connections[key] = conn; | ||
addConnection(key, conn); | ||
conn.on('close', function() { | ||
delete connections[key]; | ||
removeConnection(key, conn); | ||
}); | ||
@@ -51,4 +71,9 @@ }); | ||
for (let key in connections) { | ||
connections[key].destroy(); | ||
for (let key in connectionDict) { | ||
const connections = connectionDict[key]; | ||
// Shut them down in reverse order (most recent first) to try to | ||
// reduce issues with layered connections (like tunnels) | ||
for (let i = connections.length - 1; i >= 0; i--) { | ||
connections[i].destroy(); | ||
} | ||
} | ||
@@ -55,0 +80,0 @@ }); |
{ | ||
"name": "destroyable-server", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"author": "Tim Perry <tim@httptoolkit.com>", | ||
@@ -5,0 +5,0 @@ "description": "A tiny Node.js module to make any server force-closeable", |
# Destroyable-Server [![Build Status](https://github.com/httptoolkit/destroyable-server/workflows/CI/badge.svg)](https://github.com/httptoolkit/destroyable-server/actions) [![Available on NPM](https://img.shields.io/npm/v/destroyable-server.svg)](https://npmjs.com/package/destroyable-server) | ||
> _Part of [HTTP Toolkit](https://httptoolkit.tech): powerful tools for building, testing & debugging HTTP(S)_ | ||
> _Part of [HTTP Toolkit](https://httptoolkit.com/): powerful tools for building, testing & debugging HTTP(S)_ | ||
@@ -5,0 +5,0 @@ A tiny Node.js module to make any server force-closeable. |
Sorry, the diff of this file is not supported yet
22041
57460
163