Skip to content

Commit 69924cf

Browse files
committed
fix: close open http server sockets immediatly
1 parent 9701fe1 commit 69924cf

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"scripts": {
2020
"build": "npm run clean && webpack && npm run build-copy-files",
2121
"clean": "shx rm -rf dist",
22-
"test": "jest --config jest.config.json --coverage --detectOpenHandles",
22+
"test": "jest --config jest.config.json --coverage --maxWorkers=15",
2323
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
2424
"lint": "tslint --project ./tsconfig.json",
2525
"coverage": "cat coverage/lcov.info | coveralls",

src/server.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { readFile } from 'fs';
22
import { createServer, IncomingMessage, Server as HttpServer, ServerResponse } from 'http';
3+
import { Socket } from 'net';
34
import opn from 'opn';
45
import { join } from 'path';
56
import { IPlotsContainer } from './models';
@@ -8,10 +9,21 @@ export class Server {
89
private instance: HttpServer;
910
private plotsContainer: IPlotsContainer = {};
1011
private port: number;
12+
private sockets: {[id: number]: Socket} = {};
13+
private nextSocketID = 0;
1114

1215
constructor(port: number) {
1316
this.port = port;
1417
this.instance = this.createServer();
18+
19+
this.instance.on('connection', (socket: Socket) => {
20+
const id = this.nextSocketID++;
21+
this.sockets[id] = socket;
22+
23+
socket.on('close', () => {
24+
delete this.sockets[id];
25+
});
26+
});
1527
}
1628

1729
/**
@@ -30,12 +42,18 @@ export class Server {
3042
}
3143

3244
/**
33-
* Closes the webserver and clears the plots container.
45+
* Closes the webserver, destroys all connected sockets
46+
* and clears the plots container.
3447
*/
3548
public clean() {
3649
if (this.instance.address()) {
3750
this.instance.close();
3851
}
52+
53+
for (const socket of Object.values(this.sockets)) {
54+
socket.destroy();
55+
}
56+
3957
this.plotsContainer = {};
4058
}
4159

@@ -86,7 +104,7 @@ export class Server {
86104
}
87105

88106
/**
89-
*
107+
* Serves the website at http://localhost:PORT/plots/:id/index.html
90108
* @param req
91109
* @param res
92110
*/

test/server.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,9 @@ describe('Server', () => {
116116
server.clean();
117117
server = null;
118118
});
119+
120+
// afterAll(() => {
121+
// console.log((process as any)._getActiveRequests());
122+
// console.log((process as any)._getActiveHandles()[0]);
123+
// });
119124
});

test/server.www.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,4 @@ describe('Server', () => {
7777
server.clean();
7878
server = null;
7979
});
80-
});
80+
});

0 commit comments

Comments
 (0)