Skip to content

Commit

Permalink
chore(deps): update ws (#3652)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Aug 17, 2021
1 parent ae8c523 commit 493ccbd
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 42 deletions.
44 changes: 30 additions & 14 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1775,15 +1775,25 @@ class Server {
}

async stop() {
if (this.webSocketServer) {
process.nextTick(() => {
this.webSocketServer.implementation.close();
});
if (this.webSocketProxies.length > 0) {
this.webSocketProxies = [];
}

await Promise.all(this.staticWatchers.map((watcher) => watcher.close()));
if (this.staticWatchers.length > 0) {
await Promise.all(this.staticWatchers.map((watcher) => watcher.close()));

this.staticWatchers = [];
this.staticWatchers = [];
}

if (this.webSocketServer) {
await new Promise((resolve) => {
this.webSocketServer.implementation.close(() => {
resolve();
});
});

this.webSocketServer = null;
}

if (this.server) {
await new Promise((resolve) => {
Expand All @@ -1792,17 +1802,23 @@ class Server {
});
});

await new Promise((resolve, reject) => {
this.middleware.close((error) => {
if (error) {
reject(error);
this.server = null;

return;
}
if (this.middleware) {
await new Promise((resolve, reject) => {
this.middleware.close((error) => {
if (error) {
reject(error);

resolve();
return;
}

resolve();
});
});
});

this.middleware = null;
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion lib/servers/SockJSServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@ module.exports = class SockJSServer extends BaseServer {
});
});

this.implementation.close = () => {
this.implementation.close = (callback) => {
for (const client of this.clients) {
client.close();
}

this.clients.clear();

callback();
};
}
};
4 changes: 4 additions & 0 deletions lib/servers/WebsocketServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ module.exports = class WebsocketServer extends BaseServer {
this.implementation.on("close", () => {
clearInterval(interval);

for (const ws of this.clients) {
ws.terminate();
}

this.clients.clear();
});
}
Expand Down
48 changes: 39 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"strip-ansi": "^7.0.0",
"url": "^0.11.0",
"webpack-dev-middleware": "^5.0.0",
"ws": "^7.5.3"
"ws": "^8.1.0"
},
"devDependencies": {
"@babel/cli": "^7.14.5",
Expand Down Expand Up @@ -104,7 +104,7 @@
"tcp-port-used": "^1.0.2",
"typescript": "^4.2.4",
"url-loader": "^4.1.1",
"webpack": "^5.48.0",
"webpack": "^5.50.0",
"webpack-cli": "^4.7.2",
"webpack-merge": "^5.8.0"
},
Expand Down
10 changes: 4 additions & 6 deletions test/e2e/web-socket-communication.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe("web socket communication", () => {

page
.on("console", (message) => {
consoleMessages.push(message);
consoleMessages.push(message.text());
})
.on("pageerror", (error) => {
pageErrors.push(error);
Expand All @@ -45,20 +45,18 @@ describe("web socket communication", () => {
await server.stop();
await new Promise((resolve) => {
const interval = setInterval(() => {
if (server.webSocketServer.clients.size === 0) {
if (consoleMessages.includes("[webpack-dev-server] Disconnected!")) {
clearInterval(interval);

resolve();
}
}, 100);
});

expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
"console messages"
);
expect(consoleMessages).toMatchSnapshot("console messages");
expect(pageErrors).toMatchSnapshot("page errors");

await browser.close();
await server.stop();
});

it(`should work and terminate client that is not alive ("${websocketServer}")`, async () => {
Expand Down
12 changes: 9 additions & 3 deletions test/server/Server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ describe("Server", () => {
options: {
webSocketServer: class CustomServerImplementation {
constructor() {
this.implementation = { close: () => {} };
this.implementation = {
close: (callback) => {
callback();
},
};
}
},
},
Expand All @@ -192,7 +196,8 @@ describe("Server", () => {
type: "ws",
options: {
host: "127.0.0.1",
port: 43334,
// TODO `jest` is freeze here
// port: 43334,
pathname: "/ws",
},
},
Expand All @@ -210,7 +215,8 @@ describe("Server", () => {
type: "ws",
options: {
host: "127.0.0.1",
port: "43335",
// TODO `jest` is freeze here
// port: "43335",
pathname: "/ws",
},
},
Expand Down
2 changes: 0 additions & 2 deletions test/server/__snapshots__/Server.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,6 @@ Object {
"host": "127.0.0.1",
"path": "/ws",
"pathname": "/ws",
"port": 43334,
},
"type": "ws",
},
Expand Down Expand Up @@ -523,7 +522,6 @@ Object {
"host": "127.0.0.1",
"path": "/ws",
"pathname": "/ws",
"port": 43335,
},
"type": "ws",
},
Expand Down
2 changes: 0 additions & 2 deletions test/server/__snapshots__/Server.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,6 @@ Object {
"host": "127.0.0.1",
"path": "/ws",
"pathname": "/ws",
"port": 43334,
},
"type": "ws",
},
Expand Down Expand Up @@ -523,7 +522,6 @@ Object {
"host": "127.0.0.1",
"path": "/ws",
"pathname": "/ws",
"port": 43335,
},
"type": "ws",
},
Expand Down
1 change: 1 addition & 0 deletions test/server/http2-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ describe('"http2" option', () => {
});
http2Req.on("end", () => {
expect(data).toEqual(expect.stringMatching(/Heyo/));
client.close();
done();
});
http2Req.end();
Expand Down
6 changes: 5 additions & 1 deletion test/server/proxy-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ describe("proxy option", () => {
ws = new WebSocket(`ws://localhost:${port3}/proxy3/socket`);

ws.on("message", (message) => {
responseMessage = message;
responseMessage = message.toString();
done();
});

Expand All @@ -600,6 +600,10 @@ describe("proxy option", () => {
afterAll(async () => {
webSocketServer.close();

for (const client of webSocketServer.clients) {
client.terminate();
}

await server.stop();
});
});
Expand Down
6 changes: 4 additions & 2 deletions test/server/setupExitSignals-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ describe("setupExitSignals option", () => {

it.each(signals)("should close and exit on %s", (signal, done) => {
process.emit(signal);
process.nextTick(() => {

setTimeout(() => {
expect(killSpy.mock.calls.length).toEqual(1);
expect(exitSpy.mock.calls.length).toEqual(1);

done();
});
}, 1000);
});
});

0 comments on commit 493ccbd

Please sign in to comment.