Skip to content

Commit 639cd75

Browse files
refactor: rename properties (#3445)
1 parent 17aa345 commit 639cd75

File tree

3 files changed

+41
-33
lines changed

3 files changed

+41
-33
lines changed

lib/Server.js

+33-25
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Server {
3232
this.compiler = compiler;
3333
this.options = options;
3434
this.logger = this.compiler.getInfrastructureLogger('webpack-dev-server');
35-
this.sockets = [];
35+
this.webSocketConnections = [];
3636
this.staticWatchers = [];
3737
// Keep track of websocket proxies for external websocket upgrade.
3838
this.webSocketProxies = [];
@@ -43,7 +43,7 @@ class Server {
4343

4444
this.applyDevServerPlugin();
4545

46-
this.SocketServerImplementation = getSocketServerImplementation(
46+
this.webSocketServerImplementation = getSocketServerImplementation(
4747
this.options
4848
);
4949

@@ -99,7 +99,7 @@ class Server {
9999
msg = `${msg} (${addInfo})`;
100100
}
101101

102-
this.sendMessage(this.sockets, 'progress-update', {
102+
this.sendMessage(this.webSocketConnections, 'progress-update', {
103103
percent,
104104
msg,
105105
pluginName,
@@ -120,7 +120,7 @@ class Server {
120120
setupHooks() {
121121
// Listening for events
122122
const invalidPlugin = () => {
123-
this.sendMessage(this.sockets, 'invalid');
123+
this.sendMessage(this.webSocketConnections, 'invalid');
124124
};
125125

126126
const addHooks = (compiler) => {
@@ -129,7 +129,7 @@ class Server {
129129
compile.tap('webpack-dev-server', invalidPlugin);
130130
invalid.tap('webpack-dev-server', invalidPlugin);
131131
done.tap('webpack-dev-server', (stats) => {
132-
this.sendStats(this.sockets, this.getStats(stats));
132+
this.sendStats(this.webSocketConnections, this.getStats(stats));
133133
this.stats = stats;
134134
});
135135
};
@@ -474,9 +474,10 @@ class Server {
474474
}
475475

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

479-
this.socketServer.onConnection((connection, headers) => {
480+
this.webSocketServer.onConnection((connection, headers) => {
480481
if (!connection) {
481482
return;
482483
}
@@ -495,18 +496,18 @@ class Server {
495496
) {
496497
this.sendMessage([connection], 'error', 'Invalid Host/Origin header');
497498

498-
this.socketServer.closeConnection(connection);
499+
this.webSocketServer.closeConnection(connection);
499500

500501
return;
501502
}
502503

503-
this.sockets.push(connection);
504+
this.webSocketConnections.push(connection);
504505

505-
this.socketServer.onConnectionClose(connection, () => {
506-
const idx = this.sockets.indexOf(connection);
506+
this.webSocketServer.onConnectionClose(connection, () => {
507+
const idx = this.webSocketConnections.indexOf(connection);
507508

508509
if (idx >= 0) {
509-
this.sockets.splice(idx, 1);
510+
this.webSocketConnections.splice(idx, 1);
510511
}
511512
});
512513

@@ -748,9 +749,9 @@ class Server {
748749
}
749750

750751
close(callback) {
751-
if (this.socketServer) {
752-
this.socketServer.close();
753-
this.sockets = [];
752+
if (this.webSocketServer) {
753+
this.webSocketServer.close();
754+
this.webSocketConnections = [];
754755
}
755756

756757
const prom = Promise.all(
@@ -928,9 +929,12 @@ class Server {
928929
return false;
929930
}
930931

931-
sendMessage(sockets, type, data) {
932-
sockets.forEach((socket) => {
933-
this.socketServer.send(socket, JSON.stringify({ type, data }));
932+
sendMessage(webSocketConnections, type, data) {
933+
webSocketConnections.forEach((webSocketConnection) => {
934+
this.webSocketServer.send(
935+
webSocketConnection,
936+
JSON.stringify({ type, data })
937+
);
934938
});
935939
}
936940

@@ -960,7 +964,7 @@ class Server {
960964
}
961965

962966
// Send stats to a socket or multiple sockets
963-
sendStats(sockets, stats, force) {
967+
sendStats(webSocketConnections, stats, force) {
964968
const shouldEmit =
965969
!force &&
966970
stats &&
@@ -970,23 +974,23 @@ class Server {
970974
stats.assets.every((asset) => !asset.emitted);
971975

972976
if (shouldEmit) {
973-
this.sendMessage(sockets, 'still-ok');
977+
this.sendMessage(webSocketConnections, 'still-ok');
974978

975979
return;
976980
}
977981

978-
this.sendMessage(sockets, 'hash', stats.hash);
982+
this.sendMessage(webSocketConnections, 'hash', stats.hash);
979983

980984
if (stats.errors.length > 0 || stats.warnings.length > 0) {
981985
if (stats.errors.length > 0) {
982-
this.sendMessage(sockets, 'errors', stats.errors);
986+
this.sendMessage(webSocketConnections, 'errors', stats.errors);
983987
}
984988

985989
if (stats.warnings.length > 0) {
986-
this.sendMessage(sockets, 'warnings', stats.warnings);
990+
this.sendMessage(webSocketConnections, 'warnings', stats.warnings);
987991
}
988992
} else {
989-
this.sendMessage(sockets, 'ok');
993+
this.sendMessage(webSocketConnections, 'ok');
990994
}
991995
}
992996

@@ -1027,7 +1031,11 @@ class Server {
10271031
// disabling refreshing on changing the content
10281032
if (this.options.liveReload) {
10291033
watcher.on('change', (item) => {
1030-
this.sendMessage(this.sockets, 'static-changed', item);
1034+
this.sendMessage(
1035+
this.webSocketConnections,
1036+
'static-changed',
1037+
item
1038+
);
10311039
});
10321040
}
10331041

lib/servers/SockJSServer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ module.exports = class SockJSServer extends BaseServer {
7373
}
7474

7575
close(callback) {
76-
[...this.server.sockets].forEach((socket) => {
76+
[...this.server.webSocketConnections].forEach((socket) => {
7777
this.closeConnection(socket);
7878
});
7979

test/server/webSocketServer-option.test.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ describe('webSocketServer', () => {
223223
}
224224

225225
close(callback) {
226-
[...this.server.sockets].forEach((socket) => {
226+
[...this.server.webSocketConnections].forEach((socket) => {
227227
this.closeConnection(socket);
228228
});
229229

@@ -314,7 +314,7 @@ describe('webSocketServer', () => {
314314
}
315315

316316
close(callback) {
317-
[...this.server.sockets].forEach((socket) => {
317+
[...this.server.webSocketConnections].forEach((socket) => {
318318
this.closeConnection(socket);
319319
});
320320

@@ -424,7 +424,7 @@ describe('webSocketServer', () => {
424424
}
425425

426426
close(callback) {
427-
[...this.server.sockets].forEach((socket) => {
427+
[...this.server.webSocketConnections].forEach((socket) => {
428428
this.closeConnection(socket);
429429
});
430430

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

518-
expect(server.sockets.length).toEqual(1);
519-
expect(server.sockets).toMatchSnapshot();
518+
expect(server.webSocketConnections.length).toEqual(1);
519+
expect(server.webSocketConnections).toMatchSnapshot();
520520

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

524-
expect(server.sockets.length).toEqual(0);
524+
expect(server.webSocketConnections.length).toEqual(0);
525525

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

0 commit comments

Comments
 (0)