Skip to content

Commit 22b1414

Browse files
authored
refactor: remove killable (#3657)
1 parent 75bafbf commit 22b1414

8 files changed

+44
-51
lines changed

lib/Server.js

+35-18
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const util = require("util");
77
const fs = require("graceful-fs");
88
const ipaddr = require("ipaddr.js");
99
const internalIp = require("internal-ip");
10-
const killable = require("killable");
1110
const express = require("express");
1211
const { validate } = require("schema-utils");
1312
const schema = require("./options.json");
@@ -35,6 +34,7 @@ class Server {
3534
this.staticWatchers = [];
3635
// Keep track of websocket proxies for external websocket upgrade.
3736
this.webSocketProxies = [];
37+
this.sockets = [];
3838
this.compiler = compiler;
3939
}
4040

@@ -682,8 +682,6 @@ class Server {
682682
this.setupFeatures();
683683
this.createServer();
684684

685-
killable(this.server);
686-
687685
if (this.options.setupExitSignals) {
688686
const signals = ["SIGINT", "SIGTERM"];
689687

@@ -1144,9 +1142,6 @@ class Server {
11441142
}
11451143

11461144
createServer() {
1147-
const https = require("https");
1148-
const http = require("http");
1149-
11501145
if (this.options.https) {
11511146
if (this.options.http2) {
11521147
// TODO: we need to replace spdy with http2 which is an internal module
@@ -1160,12 +1155,26 @@ class Server {
11601155
this.app
11611156
);
11621157
} else {
1158+
const https = require("https");
1159+
11631160
this.server = https.createServer(this.options.https, this.app);
11641161
}
11651162
} else {
1163+
const http = require("http");
1164+
11661165
this.server = http.createServer(this.app);
11671166
}
11681167

1168+
this.server.on("connection", (socket) => {
1169+
// Add socket to list
1170+
this.sockets.push(socket);
1171+
1172+
socket.once("close", () => {
1173+
// Remove socket from list
1174+
this.sockets.splice(this.sockets.indexOf(socket), 1);
1175+
});
1176+
});
1177+
11691178
this.server.on("error", (error) => {
11701179
throw error;
11711180
});
@@ -1775,34 +1784,42 @@ class Server {
17751784
}
17761785

17771786
async stop() {
1778-
if (this.webSocketProxies.length > 0) {
1779-
this.webSocketProxies = [];
1780-
}
1787+
this.webSocketProxies = [];
17811788

1782-
if (this.staticWatchers.length > 0) {
1783-
await Promise.all(this.staticWatchers.map((watcher) => watcher.close()));
1789+
await Promise.all(this.staticWatchers.map((watcher) => watcher.close()));
17841790

1785-
this.staticWatchers = [];
1786-
}
1791+
this.staticWatchers = [];
17871792

17881793
if (this.webSocketServer) {
17891794
await new Promise((resolve) => {
17901795
this.webSocketServer.implementation.close(() => {
1796+
this.webSocketServer = null;
1797+
17911798
resolve();
17921799
});
1793-
});
17941800

1795-
this.webSocketServer = null;
1801+
for (const client of this.webSocketServer.clients) {
1802+
client.terminate();
1803+
}
1804+
1805+
this.webSocketServer.clients = [];
1806+
});
17961807
}
17971808

17981809
if (this.server) {
17991810
await new Promise((resolve) => {
1800-
this.server.kill(() => {
1811+
this.server.close(() => {
1812+
this.server = null;
1813+
18011814
resolve();
18021815
});
1803-
});
18041816

1805-
this.server = null;
1817+
for (const socket of this.sockets) {
1818+
socket.destroy();
1819+
}
1820+
1821+
this.sockets = [];
1822+
});
18061823

18071824
if (this.middleware) {
18081825
await new Promise((resolve, reject) => {

lib/servers/BaseServer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
module.exports = class BaseServer {
66
constructor(server) {
77
this.server = server;
8-
this.clients = new Set();
8+
this.clients = [];
99
}
1010
};

lib/servers/SockJSServer.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,14 @@ module.exports = class SockJSServer extends BaseServer {
6767
client.send = client.write;
6868
client.terminate = client.close;
6969

70-
this.clients.add(client);
70+
this.clients.push(client);
7171

7272
client.on("close", () => {
73-
this.clients.delete(client);
73+
this.clients.splice(this.clients.indexOf(client), 1);
7474
});
7575
});
7676

7777
this.implementation.close = (callback) => {
78-
for (const client of this.clients) {
79-
client.close();
80-
}
81-
82-
this.clients.clear();
83-
8478
callback();
8579
};
8680
}

lib/servers/WebsocketServer.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ module.exports = class WebsocketServer extends BaseServer {
5454
}, WebsocketServer.heartbeatInterval);
5555

5656
this.implementation.on("connection", (client) => {
57-
this.clients.add(client);
57+
this.clients.push(client);
5858

5959
client.isAlive = true;
6060

@@ -63,18 +63,12 @@ module.exports = class WebsocketServer extends BaseServer {
6363
});
6464

6565
client.on("close", () => {
66-
this.clients.delete(client);
66+
this.clients.splice(this.clients.indexOf(client), 1);
6767
});
6868
});
6969

7070
this.implementation.on("close", () => {
7171
clearInterval(interval);
72-
73-
for (const ws of this.clients) {
74-
ws.terminate();
75-
}
76-
77-
this.clients.clear();
7872
});
7973
}
8074
};

package-lock.json

-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
"http-proxy-middleware": "^2.0.0",
4646
"internal-ip": "^6.2.0",
4747
"ipaddr.js": "^2.0.1",
48-
"killable": "^1.0.1",
4948
"open": "^8.0.9",
5049
"p-retry": "^4.5.0",
5150
"portfinder": "^1.0.28",

test/e2e/web-socket-communication.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ describe("web socket communication", () => {
9696
}, 200);
9797
});
9898

99-
expect(server.webSocketServer.clients.size).toBe(0);
99+
expect(server.webSocketServer.clients.length).toBe(0);
100100
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
101101
"console messages"
102102
);

test/server/setupExitSignals-option.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const port = require("../ports-map")["setup-exit-signals-option"];
88
describe("setupExitSignals option", () => {
99
let server;
1010
let exitSpy;
11-
let killSpy;
11+
let stopCallbackSpy;
1212
let stdinResumeSpy;
1313

1414
const signals = ["SIGINT", "SIGTERM"];
@@ -30,7 +30,7 @@ describe("setupExitSignals option", () => {
3030
stdinResumeSpy = jest
3131
.spyOn(process.stdin, "resume")
3232
.mockImplementation(() => {});
33-
killSpy = jest.spyOn(server.server, "kill");
33+
stopCallbackSpy = jest.spyOn(server, "stopCallback");
3434
});
3535

3636
afterEach(async () => {
@@ -48,7 +48,7 @@ describe("setupExitSignals option", () => {
4848
process.emit(signal);
4949

5050
setTimeout(() => {
51-
expect(killSpy.mock.calls.length).toEqual(1);
51+
expect(stopCallbackSpy.mock.calls.length).toEqual(1);
5252
expect(exitSpy.mock.calls.length).toEqual(1);
5353

5454
done();

0 commit comments

Comments
 (0)