Skip to content

refactor: rename properties #3445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 33 additions & 25 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Server {
this.compiler = compiler;
this.options = options;
this.logger = this.compiler.getInfrastructureLogger('webpack-dev-server');
this.sockets = [];
this.webSocketConnections = [];
this.staticWatchers = [];
// Keep track of websocket proxies for external websocket upgrade.
this.webSocketProxies = [];
Expand All @@ -43,7 +43,7 @@ class Server {

this.applyDevServerPlugin();

this.SocketServerImplementation = getSocketServerImplementation(
this.webSocketServerImplementation = getSocketServerImplementation(
this.options
);

Expand Down Expand Up @@ -99,7 +99,7 @@ class Server {
msg = `${msg} (${addInfo})`;
}

this.sendMessage(this.sockets, 'progress-update', {
this.sendMessage(this.webSocketConnections, 'progress-update', {
percent,
msg,
pluginName,
Expand All @@ -120,7 +120,7 @@ class Server {
setupHooks() {
// Listening for events
const invalidPlugin = () => {
this.sendMessage(this.sockets, 'invalid');
this.sendMessage(this.webSocketConnections, 'invalid');
};

const addHooks = (compiler) => {
Expand All @@ -129,7 +129,7 @@ class Server {
compile.tap('webpack-dev-server', invalidPlugin);
invalid.tap('webpack-dev-server', invalidPlugin);
done.tap('webpack-dev-server', (stats) => {
this.sendStats(this.sockets, this.getStats(stats));
this.sendStats(this.webSocketConnections, this.getStats(stats));
this.stats = stats;
});
};
Expand Down Expand Up @@ -474,9 +474,10 @@ class Server {
}

createWebSocketServer() {
this.socketServer = new this.SocketServerImplementation(this);
// eslint-disable-next-line new-cap
this.webSocketServer = new this.webSocketServerImplementation(this);

this.socketServer.onConnection((connection, headers) => {
this.webSocketServer.onConnection((connection, headers) => {
if (!connection) {
return;
}
Expand All @@ -495,18 +496,18 @@ class Server {
) {
this.sendMessage([connection], 'error', 'Invalid Host/Origin header');

this.socketServer.closeConnection(connection);
this.webSocketServer.closeConnection(connection);

return;
}

this.sockets.push(connection);
this.webSocketConnections.push(connection);

this.socketServer.onConnectionClose(connection, () => {
const idx = this.sockets.indexOf(connection);
this.webSocketServer.onConnectionClose(connection, () => {
const idx = this.webSocketConnections.indexOf(connection);

if (idx >= 0) {
this.sockets.splice(idx, 1);
this.webSocketConnections.splice(idx, 1);
}
});

Expand Down Expand Up @@ -748,9 +749,9 @@ class Server {
}

close(callback) {
if (this.socketServer) {
this.socketServer.close();
this.sockets = [];
if (this.webSocketServer) {
this.webSocketServer.close();
this.webSocketConnections = [];
}

const prom = Promise.all(
Expand Down Expand Up @@ -928,9 +929,12 @@ class Server {
return false;
}

sendMessage(sockets, type, data) {
sockets.forEach((socket) => {
this.socketServer.send(socket, JSON.stringify({ type, data }));
sendMessage(webSocketConnections, type, data) {
webSocketConnections.forEach((webSocketConnection) => {
this.webSocketServer.send(
webSocketConnection,
JSON.stringify({ type, data })
);
});
}

Expand Down Expand Up @@ -960,7 +964,7 @@ class Server {
}

// Send stats to a socket or multiple sockets
sendStats(sockets, stats, force) {
sendStats(webSocketConnections, stats, force) {
const shouldEmit =
!force &&
stats &&
Expand All @@ -970,23 +974,23 @@ class Server {
stats.assets.every((asset) => !asset.emitted);

if (shouldEmit) {
this.sendMessage(sockets, 'still-ok');
this.sendMessage(webSocketConnections, 'still-ok');

return;
}

this.sendMessage(sockets, 'hash', stats.hash);
this.sendMessage(webSocketConnections, 'hash', stats.hash);

if (stats.errors.length > 0 || stats.warnings.length > 0) {
if (stats.errors.length > 0) {
this.sendMessage(sockets, 'errors', stats.errors);
this.sendMessage(webSocketConnections, 'errors', stats.errors);
}

if (stats.warnings.length > 0) {
this.sendMessage(sockets, 'warnings', stats.warnings);
this.sendMessage(webSocketConnections, 'warnings', stats.warnings);
}
} else {
this.sendMessage(sockets, 'ok');
this.sendMessage(webSocketConnections, 'ok');
}
}

Expand Down Expand Up @@ -1027,7 +1031,11 @@ class Server {
// disabling refreshing on changing the content
if (this.options.liveReload) {
watcher.on('change', (item) => {
this.sendMessage(this.sockets, 'static-changed', item);
this.sendMessage(
this.webSocketConnections,
'static-changed',
item
);
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/servers/SockJSServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ module.exports = class SockJSServer extends BaseServer {
}

close(callback) {
[...this.server.sockets].forEach((socket) => {
[...this.server.webSocketConnections].forEach((socket) => {
this.closeConnection(socket);
});

Expand Down
14 changes: 7 additions & 7 deletions test/server/webSocketServer-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ describe('webSocketServer', () => {
}

close(callback) {
[...this.server.sockets].forEach((socket) => {
[...this.server.webSocketConnections].forEach((socket) => {
this.closeConnection(socket);
});

Expand Down Expand Up @@ -314,7 +314,7 @@ describe('webSocketServer', () => {
}

close(callback) {
[...this.server.sockets].forEach((socket) => {
[...this.server.webSocketConnections].forEach((socket) => {
this.closeConnection(socket);
});

Expand Down Expand Up @@ -424,7 +424,7 @@ describe('webSocketServer', () => {
}

close(callback) {
[...this.server.sockets].forEach((socket) => {
[...this.server.webSocketConnections].forEach((socket) => {
this.closeConnection(socket);
});

Expand Down Expand Up @@ -515,13 +515,13 @@ describe('webSocketServer', () => {
origin: `http://localhost:${port}`,
});

expect(server.sockets.length).toEqual(1);
expect(server.sockets).toMatchSnapshot();
expect(server.webSocketConnections.length).toEqual(1);
expect(server.webSocketConnections).toMatchSnapshot();

// this simulates a client leaving the server
mockServerInstance.onConnectionClose.mock.calls[0][1](connectionObj);

expect(server.sockets.length).toEqual(0);
expect(server.webSocketConnections.length).toEqual(0);

// check that the dev server was passed to the socket server implementation constructor
expect(MockWebsocketServer.mock.calls[0].length).toEqual(1);
Expand Down Expand Up @@ -556,7 +556,7 @@ describe('webSocketServer', () => {
host: null,
}
);
expect(server.sockets.length).toEqual(0);
expect(server.webSocketConnections.length).toEqual(0);
expect(MockWebsocketServer.mock.calls[0].length).toEqual(1);
expect(MockWebsocketServer.mock.calls[0][0].options.port).toEqual(port);
expect(mockServerInstance.onConnection.mock.calls).toMatchSnapshot();
Expand Down