Skip to content

Commit 2f7f052

Browse files
rlamanaevilebottnawi
authored andcommittedMar 7, 2019
fix: use location.port when location.hostname is used to infer HMR socket URL (#1664)
1 parent cb10f83 commit 2f7f052

File tree

9 files changed

+273
-33
lines changed

9 files changed

+273
-33
lines changed
 

‎client-src/default/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ const onSocketMsg = {
181181

182182
let hostname = urlParts.hostname;
183183
let protocol = urlParts.protocol;
184+
let port = urlParts.port;
184185

185186
// check ipv4 and ipv6 `all hostname`
186187
if (hostname === '0.0.0.0' || hostname === '::') {
@@ -190,6 +191,7 @@ if (hostname === '0.0.0.0' || hostname === '::') {
190191
// eslint-disable-next-line no-bitwise
191192
if (self.location.hostname && !!~self.location.protocol.indexOf('http')) {
192193
hostname = self.location.hostname;
194+
port = self.location.port;
193195
}
194196
}
195197

@@ -208,7 +210,7 @@ const socketUrl = url.format({
208210
protocol,
209211
auth: urlParts.auth,
210212
hostname,
211-
port: urlParts.port,
213+
port,
212214
// If sockPath is provided it'll be passed in via the __resourceQuery as a
213215
// query param so it has to be parsed out of the querystring in order for the
214216
// client to open the socket to the correct location.

‎package-lock.json

+140-31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"marked": "^0.6.1",
8383
"nyc": "^13.3.0",
8484
"prettier": "^1.16.3",
85+
"puppeteer": "^1.12.2",
8586
"rimraf": "^2.6.2",
8687
"standard-version": "^5.0.0",
8788
"style-loader": "^0.23.1",

‎test/Client.test.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'use strict';
2+
3+
const express = require('express');
4+
const httpProxy = require('http-proxy-middleware');
5+
const request = require('supertest');
6+
const addEntries = require('../lib/utils/addEntries');
7+
const helper = require('./helper');
8+
const config = require('./fixtures/client-config/webpack.config');
9+
const runBrowser = require('./helpers/run-browser');
10+
11+
function startProxy(port) {
12+
const proxy = express();
13+
proxy.use(
14+
'/',
15+
httpProxy({
16+
target: 'http://localhost:9001',
17+
ws: true,
18+
changeOrigin: true,
19+
})
20+
);
21+
return proxy.listen(port);
22+
}
23+
24+
describe('Client code', () => {
25+
beforeAll((done) => {
26+
const options = {
27+
compress: true,
28+
port: 9001,
29+
host: '0.0.0.0',
30+
disableHostCheck: true,
31+
hot: true,
32+
watchOptions: {
33+
poll: true,
34+
},
35+
};
36+
addEntries(config, options);
37+
helper.start(config, options, done);
38+
});
39+
40+
afterAll(helper.close);
41+
42+
describe('behind a proxy', () => {
43+
let proxy;
44+
45+
jest.setTimeout(30000);
46+
47+
beforeAll(() => {
48+
proxy = startProxy(9000);
49+
});
50+
51+
afterAll(() => {
52+
proxy.close();
53+
});
54+
55+
it('responds with a 200', (done) => {
56+
const req = request('http://localhost:9000');
57+
req.get('/sockjs-node').expect(200, 'Welcome to SockJS!\n', done);
58+
});
59+
60+
it('requests websocket through the proxy with proper port number', (done) => {
61+
runBrowser().then(({ page, browser }) => {
62+
page
63+
.waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/))
64+
.then((requestObj) => {
65+
expect(requestObj.url()).toMatch(
66+
/^http:\/\/localhost:9000\/sockjs-node/
67+
);
68+
browser.close();
69+
done();
70+
});
71+
page.goto('http://localhost:9000/main');
72+
});
73+
});
74+
});
75+
});

‎test/fixtures/client-config/foo.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
console.log('Hey.');
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<script src="index.js"></script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
module.exports = {
4+
mode: 'development',
5+
context: __dirname,
6+
entry: './foo.js',
7+
output: {
8+
path: '/',
9+
},
10+
};

‎test/helper.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ module.exports = {
1414
const compiler = webpack(config);
1515
server = new Server(compiler, options);
1616

17-
server.listen(8080, 'localhost', (err) => {
17+
const port = options.port || 8080;
18+
const host = options.host || 'localhost';
19+
server.listen(port, host, (err) => {
1820
if (err) return done(err);
1921
done();
2022
});

‎test/helpers/run-browser.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
const puppeteer = require('puppeteer');
4+
5+
function runBrowser(config) {
6+
const options = {
7+
viewport: {
8+
width: 500,
9+
height: 500,
10+
},
11+
userAgent: '',
12+
...config,
13+
};
14+
15+
return new Promise((resolve, reject) => {
16+
let page;
17+
let browser;
18+
19+
puppeteer
20+
.launch({
21+
headless: true,
22+
args: ['--no-sandbox', '--disable-setuid-sandbox'],
23+
})
24+
.then((launchedBrowser) => {
25+
browser = launchedBrowser;
26+
return browser.newPage();
27+
})
28+
.then((newPage) => {
29+
page = newPage;
30+
page.emulate(options);
31+
resolve({ page, browser });
32+
})
33+
.catch(reject);
34+
});
35+
}
36+
37+
module.exports = runBrowser;

0 commit comments

Comments
 (0)
Please sign in to comment.