Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Aug 17, 2021
1 parent b7210e7 commit aea1b09
Showing 1 changed file with 18 additions and 28 deletions.
46 changes: 18 additions & 28 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Server {
this.staticWatchers = [];
// Keep track of websocket proxies for external websocket upgrade.
this.webSocketProxies = [];
this.sockets = [];
this.compiler = compiler;
}

Expand All @@ -60,31 +61,6 @@ class Server {
return /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(URL);
}

static killable(server) {
let sockets = [];

server.on("connection", (socket) => {
// add socket to list
sockets.push(socket);

socket.once("close", () => {
// remove socket from list
sockets.splice(sockets.indexOf(socket), 1);
});
});

server.kill = (cb) => {
server.close(cb);
sockets.forEach((socket) => {
socket.destroy();
});
// reset so the server can be restarted
sockets = [];
};

return server;
}

static async getHostname(hostname) {
if (hostname === "local-ip") {
return (await internalIp.v4()) || (await internalIp.v6()) || "0.0.0.0";
Expand Down Expand Up @@ -706,8 +682,6 @@ class Server {
this.setupFeatures();
this.createServer();

Server.killable(this.server);

if (this.options.setupExitSignals) {
const signals = ["SIGINT", "SIGTERM"];

Expand Down Expand Up @@ -1190,6 +1164,16 @@ class Server {
this.server = http.createServer(this.app);
}

this.server.on("connection", (socket) => {
// add socket to list
this.sockets.push(socket);

socket.once("close", () => {
// remove socket from list
this.sockets.splice(this.sockets.indexOf(socket), 1);
});
});

this.server.on("error", (error) => {
throw error;
});
Expand Down Expand Up @@ -1811,7 +1795,13 @@ class Server {

if (this.server) {
await new Promise((resolve) => {
this.server.kill(() => {
this.server.kill((cb) => {
this.server.stopCallback(cb);
this.sockets.forEach((socket) => {
socket.destroy();
});
// reset so the server can be restarted
this.sockets = [];
resolve();
});
});
Expand Down

0 comments on commit aea1b09

Please sign in to comment.