Skip to content

Commit f47dff2

Browse files
knagaitsevevilebottnawi
authored andcommittedMay 13, 2019
feat: added sockHost option (#1858)
1 parent decec40 commit f47dff2

File tree

6 files changed

+111
-11
lines changed

6 files changed

+111
-11
lines changed
 

‎client-src/default/index.js

+23-9
Original file line numberDiff line numberDiff line change
@@ -220,21 +220,35 @@ if (
220220
) {
221221
protocol = self.location.protocol;
222222
}
223+
224+
// default values of the sock url if they are not provided
225+
let sockHost = hostname;
226+
let sockPath = '/sockjs-node';
227+
let sockPort = urlParts.port;
228+
if (
229+
urlParts.path !== null &&
230+
// eslint-disable-next-line no-undefined
231+
urlParts.path !== undefined &&
232+
urlParts.path !== '/'
233+
) {
234+
const parsedQuery = querystring.parse(urlParts.path);
235+
// all of these sock url params are optionally passed in through
236+
// __resourceQuery, so we need to fall back to the default if
237+
// they are not provided
238+
sockHost = parsedQuery.sockHost || sockHost;
239+
sockPath = parsedQuery.sockPath || sockPath;
240+
sockPort = parsedQuery.sockPort || sockPort;
241+
}
242+
223243
const socketUrl = url.format({
224244
protocol,
225245
auth: urlParts.auth,
226-
hostname,
227-
port:
228-
urlParts.path == null || urlParts.path === '/'
229-
? urlParts.port
230-
: querystring.parse(urlParts.path).sockPort || urlParts.port,
246+
hostname: sockHost,
247+
port: sockPort,
231248
// If sockPath is provided it'll be passed in via the __resourceQuery as a
232249
// query param so it has to be parsed out of the querystring in order for the
233250
// client to open the socket to the correct location.
234-
pathname:
235-
urlParts.path == null || urlParts.path === '/'
236-
? '/sockjs-node'
237-
: querystring.parse(urlParts.path).sockPath || urlParts.path,
251+
pathname: sockPath,
238252
});
239253

240254
socket(socketUrl, onSocketMsg);

‎lib/options.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@
288288
"setup": {
289289
"instanceof": "Function"
290290
},
291+
"sockHost": {
292+
"type": "string"
293+
},
291294
"sockPath": {
292295
"type": "string"
293296
},
@@ -395,8 +398,9 @@
395398
"serveIndex": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserverserveindex)",
396399
"serverSideRender": "should be {Boolean} (https://github.com/webpack/webpack-dev-middleware#serversiderender)",
397400
"setup": "should be {Function} (https://webpack.js.org/configuration/dev-server/#devserversetup)",
401+
"sockHost": "should be {String|Null} (https://webpack.js.org/configuration/dev-server/#devserversockhost)",
398402
"sockPath": "should be {String} (https://webpack.js.org/configuration/dev-server/#devserversockpath)",
399-
"sockPort": "should be {Number|String|Null}",
403+
"sockPort": "should be {Number|String|Null} (https://webpack.js.org/configuration/dev-server/#devserversockport)",
400404
"socket": "should be {String} (https://webpack.js.org/configuration/dev-server/#devserversocket)",
401405
"staticOptions": "should be {Object} (https://webpack.js.org/configuration/dev-server/#devserverstaticoptions)",
402406
"stats": "should be {Object|Boolean} (https://webpack.js.org/configuration/dev-server/#devserverstats-)",

‎lib/utils/addEntries.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ function addEntries(config, options, server) {
1616
};
1717

1818
const domain = createDomain(options, app);
19+
const sockHost = options.sockHost ? `&sockHost=${options.sockHost}` : '';
1920
const sockPath = options.sockPath ? `&sockPath=${options.sockPath}` : '';
2021
const sockPort = options.sockPort ? `&sockPort=${options.sockPort}` : '';
2122
const clientEntry = `${require.resolve(
2223
'../../client/'
23-
)}?${domain}${sockPath}${sockPort}`;
24+
)}?${domain}${sockHost}${sockPath}${sockPort}`;
2425
let hotEntry;
2526

2627
if (options.hotOnly) {

‎lib/utils/createConfig.js

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ function createConfig(config, argv, { port }) {
3030
options.socket = argv.socket;
3131
}
3232

33+
if (argv.sockHost) {
34+
options.sockHost = argv.sockHost;
35+
}
36+
3337
if (argv.sockPath) {
3438
options.sockPath = argv.sockPath;
3539
}

‎test/Client.test.js

+73
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,76 @@ describe('Client complex inline script path with sockPort', () => {
156156
});
157157
});
158158
});
159+
160+
// previously, using sockPort without sockPath had the ability
161+
// to alter the sockPath (based on a bug in client-src/index.js)
162+
// so we need to make sure sockPath is not altered in this case
163+
describe('Client complex inline script path with sockPort, no sockPath', () => {
164+
beforeAll((done) => {
165+
const options = {
166+
port: 9000,
167+
host: '0.0.0.0',
168+
inline: true,
169+
watchOptions: {
170+
poll: true,
171+
},
172+
sockPort: 8080,
173+
};
174+
helper.startAwaitingCompilation(config, options, done);
175+
});
176+
177+
afterAll(helper.close);
178+
179+
describe('browser client', () => {
180+
jest.setTimeout(30000);
181+
182+
it('uses the correct sockPort and sockPath', (done) => {
183+
runBrowser().then(({ page, browser }) => {
184+
page
185+
.waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/))
186+
.then((requestObj) => {
187+
expect(requestObj.url()).toMatch(
188+
/^http:\/\/localhost:8080\/sockjs-node/
189+
);
190+
browser.close().then(done);
191+
});
192+
page.goto('http://localhost:9000/main');
193+
});
194+
});
195+
});
196+
});
197+
198+
describe('Client complex inline script path with sockHost', () => {
199+
beforeAll((done) => {
200+
const options = {
201+
port: 9000,
202+
host: '0.0.0.0',
203+
inline: true,
204+
watchOptions: {
205+
poll: true,
206+
},
207+
sockHost: 'myhost.test',
208+
};
209+
helper.startAwaitingCompilation(config, options, done);
210+
});
211+
212+
afterAll(helper.close);
213+
214+
describe('browser client', () => {
215+
jest.setTimeout(30000);
216+
217+
it('uses the correct sockHost', (done) => {
218+
runBrowser().then(({ page, browser }) => {
219+
page
220+
.waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/))
221+
.then((requestObj) => {
222+
expect(requestObj.url()).toMatch(
223+
/^http:\/\/myhost\.test:9000\/sockjs-node/
224+
);
225+
browser.close().then(done);
226+
});
227+
page.goto('http://localhost:9000/main');
228+
});
229+
});
230+
});
231+
});

‎test/options.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ describe('options', () => {
337337
success: [''],
338338
failure: [false],
339339
},
340+
sockHost: {
341+
success: [''],
342+
failure: [false],
343+
},
340344
sockPath: {
341345
success: [''],
342346
failure: [false],

0 commit comments

Comments
 (0)
Please sign in to comment.