Skip to content

Commit

Permalink
refactor: move wsHeartbeatinterval to socket server (#3468)
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanv committed Jun 23, 2021
1 parent 80087de commit 3247a07
Show file tree
Hide file tree
Showing 24 changed files with 1,381 additions and 708 deletions.
1 change: 1 addition & 0 deletions .eslintignore
@@ -1,4 +1,5 @@
/client
/dist
!/test/client
coverage
node_modules
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -2,6 +2,7 @@

module.exports = {
extends: ['webpack', 'prettier'],
parser: '@babel/eslint-parser',
parserOptions: {
sourceType: 'script',
ecmaVersion: 2018,
Expand Down
2 changes: 0 additions & 2 deletions lib/Server.js
Expand Up @@ -36,8 +36,6 @@ class Server {
this.staticWatchers = [];
// Keep track of websocket proxies for external websocket upgrade.
this.webSocketProxies = [];
// this value of web socket can be overwritten for tests
this.webSocketHeartbeatInterval = 30000;

normalizeOptions(
this.compiler,
Expand Down
6 changes: 2 additions & 4 deletions lib/servers/SockJSServer.js
Expand Up @@ -65,11 +65,9 @@ module.exports = class SockJSServer extends BaseServer {

send(connection, message) {
// prevent cases where the server is trying to send data while connection is closing
if (connection.readyState !== 1) {
return;
if (connection.readyState === 1) {
connection.write(message);
}

connection.write(message);
}

close(callback) {
Expand Down
16 changes: 8 additions & 8 deletions lib/servers/WebsocketServer.js
Expand Up @@ -3,14 +3,16 @@
/* eslint-disable
class-methods-use-this
*/
const ws = require('ws');
const WebSocket = require('ws');
const BaseServer = require('./BaseServer');

module.exports = class WebsocketServer extends BaseServer {
static heartbeatInterval = 30000;

constructor(server) {
super(server);

this.implementation = new ws.Server({
this.implementation = new WebSocket.Server({
...this.server.options.webSocketServer.options,
noServer: true,
});
Expand Down Expand Up @@ -40,20 +42,18 @@ module.exports = class WebsocketServer extends BaseServer {
socket.isAlive = false;
socket.ping(noop);
});
}, this.server.webSocketHeartbeatInterval);
}, WebsocketServer.heartbeatInterval);

this.implementation.on('close', () => {
clearInterval(interval);
});
}

send(connection, message) {
// prevent cases where the server is trying to send data while connection is closing
if (connection.readyState !== 1) {
return;
// Prevent cases where the server is trying to send data while connection is closing
if (connection.readyState === WebSocket.OPEN) {
connection.send(message);
}

connection.send(message);
}

close(callback) {
Expand Down
34 changes: 32 additions & 2 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -60,7 +60,8 @@
},
"devDependencies": {
"@babel/cli": "^7.14.5",
"@babel/core": "^7.14.5",
"@babel/core": "^7.14.6",
"@babel/eslint-parser": "^7.14.7",
"@babel/plugin-transform-object-assign": "^7.14.5",
"@babel/plugin-transform-runtime": "^7.14.5",
"@babel/preset-env": "^7.14.5",
Expand All @@ -73,7 +74,7 @@
"body-parser": "^1.19.0",
"core-js": "^3.12.1",
"css-loader": "^5.2.4",
"eslint": "^7.28.0",
"eslint": "^7.29.0",
"eslint-config-prettier": "^8.3.0",
"eslint-config-webpack": "^1.2.5",
"eslint-plugin-import": "^2.23.2",
Expand Down
135 changes: 0 additions & 135 deletions test/e2e/__snapshots__/logging.test.js.snap.webpack4
@@ -1,11 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`logging should work and do not log messages about hot and live reloading is enabled (default) 1`] = `
Array [
"Hey.",
]
`;

exports[`logging should work and do not log messages about hot and live reloading is enabled (sockjs) 1`] = `
Array [
"Hey.",
Expand All @@ -18,17 +12,6 @@ Array [
]
`;

exports[`logging should work and log errors by default (default) 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
"[webpack-dev-server] Errors while compiling. Reload prevented.",
"[webpack-dev-server] Error from compilation",
]
`;

exports[`logging should work and log errors by default (sockjs) 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
Expand All @@ -51,13 +34,6 @@ Array [
]
`;

exports[`logging should work and log message about live reloading is enabled (default) 1`] = `
Array [
"Hey.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`logging should work and log message about live reloading is enabled (sockjs) 1`] = `
Array [
"Hey.",
Expand All @@ -72,33 +48,6 @@ Array [
]
`;

exports[`logging should work and log messages about hot and live reloading is enabled (default) 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`logging should work and log messages about hot and live reloading is enabled (default) 2`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`logging should work and log messages about hot and live reloading is enabled (default) 3`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`logging should work and log messages about hot and live reloading is enabled (sockjs) 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
Expand Down Expand Up @@ -153,14 +102,6 @@ Array [
]
`;

exports[`logging should work and log messages about hot is enabled (default) 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
]
`;

exports[`logging should work and log messages about hot is enabled (sockjs) 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
Expand All @@ -177,14 +118,6 @@ Array [
]
`;

exports[`logging should work and log only error (default) 1`] = `
Array [
"Hey.",
"[webpack-dev-server] Errors while compiling. Reload prevented.",
"[webpack-dev-server] Error from compilation",
]
`;

exports[`logging should work and log only error (sockjs) 1`] = `
Array [
"Hey.",
Expand All @@ -201,20 +134,6 @@ Array [
]
`;

exports[`logging should work and log static changes (default) 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
"[webpack-dev-server] \\"<cwd>/test/fixtures/client-config/static/foo.txt\\" from static directory was changed. Reloading...",
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`logging should work and log static changes (sockjs) 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
Expand Down Expand Up @@ -243,16 +162,6 @@ Array [
]
`;

exports[`logging should work and log warning and errors (default) 1`] = `
Array [
"Hey.",
"[webpack-dev-server] Warnings while compiling.",
"[webpack-dev-server] Warning from compilation",
"[webpack-dev-server] Errors while compiling. Reload prevented.",
"[webpack-dev-server] Error from compilation",
]
`;

exports[`logging should work and log warning and errors (sockjs) 1`] = `
Array [
"Hey.",
Expand All @@ -273,17 +182,6 @@ Array [
]
`;

exports[`logging should work and log warnings by default (default) 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
"[webpack-dev-server] Warnings while compiling.",
"[webpack-dev-server] Warning from compilation",
]
`;

exports[`logging should work and log warnings by default (sockjs) 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
Expand All @@ -306,15 +204,6 @@ Array [
]
`;

exports[`logging should work when the "client.logging" is "info" (default) 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`logging should work when the "client.logging" is "info" (sockjs) 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
Expand All @@ -333,15 +222,6 @@ Array [
]
`;

exports[`logging should work when the "client.logging" is "log" (default) 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`logging should work when the "client.logging" is "log" (sockjs) 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
Expand All @@ -360,12 +240,6 @@ Array [
]
`;

exports[`logging should work when the "client.logging" is "none" (default) 1`] = `
Array [
"Hey.",
]
`;

exports[`logging should work when the "client.logging" is "none" (sockjs) 1`] = `
Array [
"Hey.",
Expand All @@ -378,15 +252,6 @@ Array [
]
`;

exports[`logging should work when the "client.logging" is "verbose" (default) 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`logging should work when the "client.logging" is "verbose" (sockjs) 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
Expand Down

0 comments on commit 3247a07

Please sign in to comment.