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 b9d0d4b
Showing 1 changed file with 21 additions and 27 deletions.
48 changes: 21 additions & 27 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,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 +681,6 @@ class Server {
this.setupFeatures();
this.createServer();

Server.killable(this.server);

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

Expand Down Expand Up @@ -1762,6 +1735,27 @@ class Server {

await this.initialize();

let sockets = [];

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

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

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

const listenOptions = this.options.ipc
? { path: this.options.ipc }
: { host: this.options.host, port: this.options.port };
Expand Down

0 comments on commit b9d0d4b

Please sign in to comment.