Skip to content

Commit 0fa8fea

Browse files
authored
Fix Broken Socket on Client for Custom/Random Port Numbers (#1060)
* moving polyfills to the top, sockjs should init only after server has completed listen * fixes #1059: custom/random ports break sockjs url in the client
1 parent 1201ac1 commit 0fa8fea

File tree

3 files changed

+48
-35
lines changed

3 files changed

+48
-35
lines changed

bin/webpack-dev-server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
'use strict';
44

55
/* eslint global-require: off, import/order: off, no-console: off */
6+
require('../lib/polyfills');
67

78
const fs = require('fs');
89
const net = require('net');

client/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ if (typeof __resourceQuery === 'string' && __resourceQuery) {
3333
urlParts = url.parse((scriptHost || '/'), false, true);
3434
}
3535

36+
if (!urlParts.port || urlParts.port === '0') {
37+
urlParts.port = self.location.port;
38+
}
39+
3640
let hot = false;
3741
let initial = true;
3842
let currentHash = '';
@@ -176,7 +180,7 @@ const socketUrl = url.format({
176180
protocol,
177181
auth: urlParts.auth,
178182
hostname,
179-
port: (urlParts.port === '0') ? self.location.port : urlParts.port,
183+
port: urlParts.port,
180184
pathname: urlParts.path == null || urlParts.path === '/' ? '/sockjs-node' : urlParts.path
181185
});
182186

lib/Server.js

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
/* eslint no-console: off, func-names: off */
4+
require('./polyfills');
45

56
const fs = require('fs');
67
const http = require('http');
@@ -22,8 +23,6 @@ const webpackDevMiddleware = require('webpack-dev-middleware');
2223
const OptionsValidationError = require('./OptionsValidationError');
2324
const optionsSchema = require('./optionsSchema.json');
2425

25-
require('./polyfills');
26-
2726
const clientStats = { errorDetails: false };
2827

2928
function Server(compiler, options) {
@@ -483,49 +482,58 @@ Server.prototype.checkHost = function (headers) {
483482
};
484483

485484
// delegate listen call and init sockjs
486-
Server.prototype.listen = function (port, hostname) {
485+
Server.prototype.listen = function (port, hostname, fn) {
487486
this.listenHostname = hostname;
488487
// eslint-disable-next-line
489-
const returnValue = this.listeningApp.listen.apply(this.listeningApp, arguments);
490-
const sockServer = sockjs.createServer({
491-
// Use provided up-to-date sockjs-client
492-
sockjs_url: '/__webpack_dev_server__/sockjs.bundle.js',
493-
// Limit useless logs
494-
log(severity, line) {
495-
if (severity === 'error') {
496-
console.log(line);
497-
}
498-
}
499-
});
500-
sockServer.on('connection', (conn) => {
501-
if (!conn) return;
502-
if (!this.checkHost(conn.headers)) {
503-
this.sockWrite([conn], 'error', 'Invalid Host header');
504-
conn.close();
505-
return;
506-
}
507-
this.sockets.push(conn);
508488

509-
conn.on('close', () => {
510-
const connIndex = this.sockets.indexOf(conn);
511-
if (connIndex >= 0) {
512-
this.sockets.splice(connIndex, 1);
489+
const returnValue = this.listeningApp.listen(port, hostname, (err) => {
490+
const sockServer = sockjs.createServer({
491+
// Use provided up-to-date sockjs-client
492+
sockjs_url: '/__webpack_dev_server__/sockjs.bundle.js',
493+
// Limit useless logs
494+
log(severity, line) {
495+
if (severity === 'error') {
496+
console.log(line);
497+
}
513498
}
514499
});
515500

516-
if (this.clientLogLevel) { this.sockWrite([conn], 'log-level', this.clientLogLevel); }
501+
sockServer.on('connection', (conn) => {
502+
console.log('connection.remotePort', conn.remotePort);
503+
if (!conn) return;
504+
if (!this.checkHost(conn.headers)) {
505+
this.sockWrite([conn], 'error', 'Invalid Host header');
506+
conn.close();
507+
return;
508+
}
509+
this.sockets.push(conn);
517510

518-
if (this.clientOverlay) { this.sockWrite([conn], 'overlay', this.clientOverlay); }
511+
conn.on('close', () => {
512+
const connIndex = this.sockets.indexOf(conn);
513+
if (connIndex >= 0) {
514+
this.sockets.splice(connIndex, 1);
515+
}
516+
});
519517

520-
if (this.hot) this.sockWrite([conn], 'hot');
518+
if (this.clientLogLevel) { this.sockWrite([conn], 'log-level', this.clientLogLevel); }
521519

522-
if (!this._stats) return;
523-
this._sendStats([conn], this._stats.toJson(clientStats), true);
524-
});
520+
if (this.clientOverlay) { this.sockWrite([conn], 'overlay', this.clientOverlay); }
521+
522+
if (this.hot) this.sockWrite([conn], 'hot');
523+
524+
if (!this._stats) return;
525+
this._sendStats([conn], this._stats.toJson(clientStats), true);
526+
});
525527

526-
sockServer.installHandlers(this.listeningApp, {
527-
prefix: '/sockjs-node'
528+
sockServer.installHandlers(this.listeningApp, {
529+
prefix: '/sockjs-node'
530+
});
531+
532+
if (fn) {
533+
fn.call(this.listeningApp, err);
534+
}
528535
});
536+
529537
return returnValue;
530538
};
531539

0 commit comments

Comments
 (0)