From 136cf5446565371a4980bceeadd355a68c3cf584 Mon Sep 17 00:00:00 2001 From: Brandon Cheng Date: Wed, 25 Nov 2020 17:09:18 -0500 Subject: [PATCH] feat(client): allow sock host to use browser location's host This is templated off of commit 0835a19c/#2341, but for the browser location's hostname instead of its port. Useful when webpack-dev-server is being accessed behind a reverse proxy. --- client-src/default/utils/createSocketUrl.js | 5 ++++- test/client/utils/createSocketUrl.test.js | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/client-src/default/utils/createSocketUrl.js b/client-src/default/utils/createSocketUrl.js index 4f5ebdd592..72c1ab815a 100644 --- a/client-src/default/utils/createSocketUrl.js +++ b/client-src/default/utils/createSocketUrl.js @@ -74,10 +74,13 @@ function getSocketUrl(urlParts, loc) { // all of these sock url params are optionally passed in through // resourceQuery, so we need to fall back to the default if // they are not provided - const sockHost = query.sockHost || hostname; + let sockHost = query.sockHost || hostname; const sockPath = query.sockPath || '/sockjs-node'; let sockPort = query.sockPort || port; + if (sockHost === 'location') { + sockHost = loc.hostname; + } if (sockPort === 'location') { sockPort = loc.port; } diff --git a/test/client/utils/createSocketUrl.test.js b/test/client/utils/createSocketUrl.test.js index ab42ac67d7..e0e7ebd4d7 100644 --- a/test/client/utils/createSocketUrl.test.js +++ b/test/client/utils/createSocketUrl.test.js @@ -126,6 +126,16 @@ describe('createSocketUrl', () => { 'http://something.com', 'https://asdf/sockjs-node', ], + [ + '?http://example.com:8096&sockHost=location', + 'http://something.com', + 'http://something.com:8096/sockjs-node', + ], + [ + '?http://0.0.0.0:8096&sockHost=location', + 'http://something.com', + 'http://something.com:8096/sockjs-node', + ], [ '?https://example.com?sockPort=34', 'http://something.com', @@ -151,6 +161,11 @@ describe('createSocketUrl', () => { 'http://localhost:3000', 'http://localhost:3000/sockjs-node', ], + [ + '?http://example.com:8096&sockHost=location&sockPort=location', + 'http://something.com:3000', + 'http://something.com:3000/sockjs-node', + ], ]; samples3.forEach(([scriptSrc, loc, expected]) => { test(`should return socket ${expected} for query ${scriptSrc} and location ${loc}`, () => {