Skip to content

Commit e3f366f

Browse files
authored
fix!: rename methods (#3381)
BRAKING CHANGE: `sockWrite` was renamed to `sendMessage`
1 parent 59196e8 commit e3f366f

File tree

3 files changed

+48
-40
lines changed

3 files changed

+48
-40
lines changed

lib/Server.js

+35-27
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Server {
5353

5454
this.setupHooks();
5555
this.setupApp();
56-
this.setupCheckHostRoute();
56+
this.setupHostHeaderCheck();
5757
this.setupDevMiddleware();
5858

5959
// Should be after `webpack-dev-middleware`, otherwise other middlewares might rewrite response
@@ -99,7 +99,7 @@ class Server {
9999
msg = `${msg} (${addInfo})`;
100100
}
101101

102-
this.sockWrite(this.sockets, 'progress-update', {
102+
this.sendMessage(this.sockets, '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.sockWrite(this.sockets, 'invalid');
123+
this.sendMessage(this.sockets, 'invalid');
124124
};
125125

126126
const addHooks = (compiler) => {
@@ -141,9 +141,9 @@ class Server {
141141
}
142142
}
143143

144-
setupCheckHostRoute() {
144+
setupHostHeaderCheck() {
145145
this.app.all('*', (req, res, next) => {
146-
if (this.checkHost(req.headers)) {
146+
if (this.checkHostHeader(req.headers)) {
147147
return next();
148148
}
149149

@@ -464,7 +464,7 @@ class Server {
464464
});
465465
}
466466

467-
createSocketServer() {
467+
createWebSocketServer() {
468468
this.socketServer = new this.SocketServerImplementation(this);
469469

470470
this.socketServer.onConnection((connection, headers) => {
@@ -479,8 +479,12 @@ class Server {
479479
);
480480
}
481481

482-
if (!headers || !this.checkHost(headers) || !this.checkOrigin(headers)) {
483-
this.sockWrite([connection], 'error', 'Invalid Host/Origin header');
482+
if (
483+
!headers ||
484+
!this.checkHostHeader(headers) ||
485+
!this.checkOriginHeader(headers)
486+
) {
487+
this.sendMessage([connection], 'error', 'Invalid Host/Origin header');
484488

485489
this.socketServer.close(connection);
486490

@@ -498,19 +502,23 @@ class Server {
498502
});
499503

500504
if (this.options.hot === true || this.options.hot === 'only') {
501-
this.sockWrite([connection], 'hot');
505+
this.sendMessage([connection], 'hot');
502506
}
503507

504508
if (this.options.liveReload) {
505-
this.sockWrite([connection], 'liveReload');
509+
this.sendMessage([connection], 'liveReload');
506510
}
507511

508512
if (this.options.client.progress) {
509-
this.sockWrite([connection], 'progress', this.options.client.progress);
513+
this.sendMessage(
514+
[connection],
515+
'progress',
516+
this.options.client.progress
517+
);
510518
}
511519

512520
if (this.options.client.overlay) {
513-
this.sockWrite([connection], 'overlay', this.options.client.overlay);
521+
this.sendMessage([connection], 'overlay', this.options.client.overlay);
514522
}
515523

516524
if (!this.stats) {
@@ -521,7 +529,7 @@ class Server {
521529
});
522530
}
523531

524-
showStatus() {
532+
logStatus() {
525533
const useColor = getColorsOption(getCompilerConfigArray(this.compiler));
526534
const protocol = this.options.https ? 'https' : 'http';
527535
const { address, port } = this.server.address();
@@ -702,7 +710,7 @@ class Server {
702710
this.options.host,
703711
(error) => {
704712
if (this.options.hot || this.options.liveReload) {
705-
this.createSocketServer();
713+
this.createWebSocketServer();
706714
}
707715

708716
if (this.options.bonjour) {
@@ -711,7 +719,7 @@ class Server {
711719
runBonjour(this.options);
712720
}
713721

714-
this.showStatus();
722+
this.logStatus();
715723

716724
if (fn) {
717725
fn.call(this.server, error);
@@ -823,15 +831,15 @@ class Server {
823831
next();
824832
}
825833

826-
checkHost(headers) {
827-
return this.checkHeaders(headers, 'host');
834+
checkHostHeader(headers) {
835+
return this.checkHeader(headers, 'host');
828836
}
829837

830-
checkOrigin(headers) {
831-
return this.checkHeaders(headers, 'origin');
838+
checkOriginHeader(headers) {
839+
return this.checkHeader(headers, 'origin');
832840
}
833841

834-
checkHeaders(headers, headerToCheck) {
842+
checkHeader(headers, headerToCheck) {
835843
// allow user to opt out of this security check, at their own risk
836844
// by explicitly enabling allowedHosts
837845
if (this.options.allowedHosts === 'all') {
@@ -912,7 +920,7 @@ class Server {
912920
return false;
913921
}
914922

915-
sockWrite(sockets, type, data) {
923+
sendMessage(sockets, type, data) {
916924
sockets.forEach((socket) => {
917925
this.socketServer.send(socket, JSON.stringify({ type, data }));
918926
});
@@ -952,19 +960,19 @@ class Server {
952960
stats.assets.every((asset) => !asset.emitted);
953961

954962
if (shouldEmit) {
955-
this.sockWrite(sockets, 'still-ok');
963+
this.sendMessage(sockets, 'still-ok');
956964

957965
return;
958966
}
959967

960-
this.sockWrite(sockets, 'hash', stats.hash);
968+
this.sendMessage(sockets, 'hash', stats.hash);
961969

962970
if (stats.errors.length > 0) {
963-
this.sockWrite(sockets, 'errors', stats.errors);
971+
this.sendMessage(sockets, 'errors', stats.errors);
964972
} else if (stats.warnings.length > 0) {
965-
this.sockWrite(sockets, 'warnings', stats.warnings);
973+
this.sendMessage(sockets, 'warnings', stats.warnings);
966974
} else {
967-
this.sockWrite(sockets, 'ok');
975+
this.sendMessage(sockets, 'ok');
968976
}
969977
}
970978

@@ -1005,7 +1013,7 @@ class Server {
10051013
// disabling refreshing on changing the content
10061014
if (this.options.liveReload) {
10071015
watcher.on('change', () => {
1008-
this.sockWrite(this.sockets, 'static-changed', watchPath);
1016+
this.sendMessage(this.sockets, 'static-changed', watchPath);
10091017
});
10101018
}
10111019

test/server/Server.test.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ describe('Server', () => {
287287
});
288288
});
289289

290-
describe('checkHost', () => {
290+
describe('checkHostHeader', () => {
291291
let compiler;
292292
let server;
293293

@@ -311,7 +311,7 @@ describe('Server', () => {
311311
host: 'localhost',
312312
};
313313
server = createServer(compiler, options);
314-
if (!server.checkHost(headers)) {
314+
if (!server.checkHostHeader(headers)) {
315315
throw new Error("Validation didn't fail");
316316
}
317317
});
@@ -329,7 +329,7 @@ describe('Server', () => {
329329

330330
server = createServer(compiler, options);
331331

332-
if (!server.checkHost(headers)) {
332+
if (!server.checkHostHeader(headers)) {
333333
throw new Error("Validation didn't fail");
334334
}
335335
});
@@ -351,7 +351,7 @@ describe('Server', () => {
351351
tests.forEach((test) => {
352352
const headers = { host: test };
353353

354-
if (!server.checkHost(headers)) {
354+
if (!server.checkHostHeader(headers)) {
355355
throw new Error("Validation didn't pass");
356356
}
357357
});
@@ -370,7 +370,7 @@ describe('Server', () => {
370370

371371
server = createServer(compiler, options);
372372

373-
if (server.checkHost(headers)) {
373+
if (server.checkHostHeader(headers)) {
374374
throw new Error("Validation didn't fail");
375375
}
376376
});
@@ -387,7 +387,7 @@ describe('Server', () => {
387387

388388
server = createServer(compiler, options);
389389

390-
if (!server.checkOrigin(headers)) {
390+
if (!server.checkOriginHeader(headers)) {
391391
throw new Error("Validation didn't fail");
392392
}
393393
});
@@ -406,7 +406,7 @@ describe('Server', () => {
406406

407407
server = createServer(compiler, options);
408408

409-
if (!server.checkOrigin(headers)) {
409+
if (!server.checkOriginHeader(headers)) {
410410
throw new Error("Validation didn't fail");
411411
}
412412
});

test/server/allowedHosts-option.test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('allowedHosts', () => {
3131

3232
server = createServer(compiler, options);
3333

34-
if (!server.checkHost(headers)) {
34+
if (!server.checkHostHeader(headers)) {
3535
throw new Error("Validation didn't fail");
3636
}
3737
});
@@ -47,7 +47,7 @@ describe('allowedHosts', () => {
4747

4848
server = createServer(compiler, options);
4949

50-
if (!server.checkHost(headers)) {
50+
if (!server.checkHostHeader(headers)) {
5151
throw new Error("Validation didn't fail");
5252
}
5353
});
@@ -65,7 +65,7 @@ describe('allowedHosts', () => {
6565

6666
server = createServer(compiler, options);
6767

68-
if (!server.checkHost(headers)) {
68+
if (!server.checkHostHeader(headers)) {
6969
throw new Error("Validation didn't fail");
7070
}
7171
});
@@ -80,7 +80,7 @@ describe('allowedHosts', () => {
8080

8181
server = createServer(compiler, options);
8282

83-
if (!server.checkHost(headers)) {
83+
if (!server.checkHostHeader(headers)) {
8484
throw new Error("Validation didn't fail");
8585
}
8686
});
@@ -93,7 +93,7 @@ describe('allowedHosts', () => {
9393
tests.forEach((test) => {
9494
const headers = { host: test };
9595

96-
if (!server.checkHost(headers)) {
96+
if (!server.checkHostHeader(headers)) {
9797
throw new Error("Validation didn't fail");
9898
}
9999
});
@@ -116,7 +116,7 @@ describe('allowedHosts', () => {
116116
tests.forEach((test) => {
117117
const headers = { host: test };
118118

119-
if (!server.checkHost(headers)) {
119+
if (!server.checkHostHeader(headers)) {
120120
throw new Error("Validation didn't fail");
121121
}
122122
});

0 commit comments

Comments
 (0)