Skip to content

Commit

Permalink
fix!: rename methods (#3381)
Browse files Browse the repository at this point in the history
BRAKING CHANGE: `sockWrite` was renamed to `sendMessage`
  • Loading branch information
anshumanv committed Jun 3, 2021
1 parent 59196e8 commit e3f366f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 40 deletions.
62 changes: 35 additions & 27 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Server {

this.setupHooks();
this.setupApp();
this.setupCheckHostRoute();
this.setupHostHeaderCheck();
this.setupDevMiddleware();

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

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

const addHooks = (compiler) => {
Expand All @@ -141,9 +141,9 @@ class Server {
}
}

setupCheckHostRoute() {
setupHostHeaderCheck() {
this.app.all('*', (req, res, next) => {
if (this.checkHost(req.headers)) {
if (this.checkHostHeader(req.headers)) {
return next();
}

Expand Down Expand Up @@ -464,7 +464,7 @@ class Server {
});
}

createSocketServer() {
createWebSocketServer() {
this.socketServer = new this.SocketServerImplementation(this);

this.socketServer.onConnection((connection, headers) => {
Expand All @@ -479,8 +479,12 @@ class Server {
);
}

if (!headers || !this.checkHost(headers) || !this.checkOrigin(headers)) {
this.sockWrite([connection], 'error', 'Invalid Host/Origin header');
if (
!headers ||
!this.checkHostHeader(headers) ||
!this.checkOriginHeader(headers)
) {
this.sendMessage([connection], 'error', 'Invalid Host/Origin header');

this.socketServer.close(connection);

Expand All @@ -498,19 +502,23 @@ class Server {
});

if (this.options.hot === true || this.options.hot === 'only') {
this.sockWrite([connection], 'hot');
this.sendMessage([connection], 'hot');
}

if (this.options.liveReload) {
this.sockWrite([connection], 'liveReload');
this.sendMessage([connection], 'liveReload');
}

if (this.options.client.progress) {
this.sockWrite([connection], 'progress', this.options.client.progress);
this.sendMessage(
[connection],
'progress',
this.options.client.progress
);
}

if (this.options.client.overlay) {
this.sockWrite([connection], 'overlay', this.options.client.overlay);
this.sendMessage([connection], 'overlay', this.options.client.overlay);
}

if (!this.stats) {
Expand All @@ -521,7 +529,7 @@ class Server {
});
}

showStatus() {
logStatus() {
const useColor = getColorsOption(getCompilerConfigArray(this.compiler));
const protocol = this.options.https ? 'https' : 'http';
const { address, port } = this.server.address();
Expand Down Expand Up @@ -702,7 +710,7 @@ class Server {
this.options.host,
(error) => {
if (this.options.hot || this.options.liveReload) {
this.createSocketServer();
this.createWebSocketServer();
}

if (this.options.bonjour) {
Expand All @@ -711,7 +719,7 @@ class Server {
runBonjour(this.options);
}

this.showStatus();
this.logStatus();

if (fn) {
fn.call(this.server, error);
Expand Down Expand Up @@ -823,15 +831,15 @@ class Server {
next();
}

checkHost(headers) {
return this.checkHeaders(headers, 'host');
checkHostHeader(headers) {
return this.checkHeader(headers, 'host');
}

checkOrigin(headers) {
return this.checkHeaders(headers, 'origin');
checkOriginHeader(headers) {
return this.checkHeader(headers, 'origin');
}

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

sockWrite(sockets, type, data) {
sendMessage(sockets, type, data) {
sockets.forEach((socket) => {
this.socketServer.send(socket, JSON.stringify({ type, data }));
});
Expand Down Expand Up @@ -952,19 +960,19 @@ class Server {
stats.assets.every((asset) => !asset.emitted);

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

return;
}

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

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

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

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

describe('checkHost', () => {
describe('checkHostHeader', () => {
let compiler;
let server;

Expand All @@ -311,7 +311,7 @@ describe('Server', () => {
host: 'localhost',
};
server = createServer(compiler, options);
if (!server.checkHost(headers)) {
if (!server.checkHostHeader(headers)) {
throw new Error("Validation didn't fail");
}
});
Expand All @@ -329,7 +329,7 @@ describe('Server', () => {

server = createServer(compiler, options);

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

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

server = createServer(compiler, options);

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

server = createServer(compiler, options);

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

server = createServer(compiler, options);

if (!server.checkOrigin(headers)) {
if (!server.checkOriginHeader(headers)) {
throw new Error("Validation didn't fail");
}
});
Expand Down
12 changes: 6 additions & 6 deletions test/server/allowedHosts-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('allowedHosts', () => {

server = createServer(compiler, options);

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

server = createServer(compiler, options);

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

server = createServer(compiler, options);

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

server = createServer(compiler, options);

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

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

if (!server.checkHost(headers)) {
if (!server.checkHostHeader(headers)) {
throw new Error("Validation didn't fail");
}
});
Expand Down

0 comments on commit e3f366f

Please sign in to comment.