Skip to content

Commit

Permalink
feat: added sockHost option (#1858)
Browse files Browse the repository at this point in the history
  • Loading branch information
knagaitsev authored and evilebottnawi committed May 13, 2019
1 parent decec40 commit f47dff2
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 11 deletions.
32 changes: 23 additions & 9 deletions client-src/default/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,21 +220,35 @@ if (
) {
protocol = self.location.protocol;
}

// default values of the sock url if they are not provided
let sockHost = hostname;
let sockPath = '/sockjs-node';
let sockPort = urlParts.port;
if (
urlParts.path !== null &&
// eslint-disable-next-line no-undefined
urlParts.path !== undefined &&
urlParts.path !== '/'
) {
const parsedQuery = querystring.parse(urlParts.path);
// 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
sockHost = parsedQuery.sockHost || sockHost;
sockPath = parsedQuery.sockPath || sockPath;
sockPort = parsedQuery.sockPort || sockPort;
}

const socketUrl = url.format({
protocol,
auth: urlParts.auth,
hostname,
port:
urlParts.path == null || urlParts.path === '/'
? urlParts.port
: querystring.parse(urlParts.path).sockPort || urlParts.port,
hostname: sockHost,
port: sockPort,
// If sockPath is provided it'll be passed in via the __resourceQuery as a
// query param so it has to be parsed out of the querystring in order for the
// client to open the socket to the correct location.
pathname:
urlParts.path == null || urlParts.path === '/'
? '/sockjs-node'
: querystring.parse(urlParts.path).sockPath || urlParts.path,
pathname: sockPath,
});

socket(socketUrl, onSocketMsg);
Expand Down
6 changes: 5 additions & 1 deletion lib/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@
"setup": {
"instanceof": "Function"
},
"sockHost": {
"type": "string"
},
"sockPath": {
"type": "string"
},
Expand Down Expand Up @@ -395,8 +398,9 @@
"serveIndex": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserverserveindex)",
"serverSideRender": "should be {Boolean} (https://github.com/webpack/webpack-dev-middleware#serversiderender)",
"setup": "should be {Function} (https://webpack.js.org/configuration/dev-server/#devserversetup)",
"sockHost": "should be {String|Null} (https://webpack.js.org/configuration/dev-server/#devserversockhost)",
"sockPath": "should be {String} (https://webpack.js.org/configuration/dev-server/#devserversockpath)",
"sockPort": "should be {Number|String|Null}",
"sockPort": "should be {Number|String|Null} (https://webpack.js.org/configuration/dev-server/#devserversockport)",
"socket": "should be {String} (https://webpack.js.org/configuration/dev-server/#devserversocket)",
"staticOptions": "should be {Object} (https://webpack.js.org/configuration/dev-server/#devserverstaticoptions)",
"stats": "should be {Object|Boolean} (https://webpack.js.org/configuration/dev-server/#devserverstats-)",
Expand Down
3 changes: 2 additions & 1 deletion lib/utils/addEntries.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ function addEntries(config, options, server) {
};

const domain = createDomain(options, app);
const sockHost = options.sockHost ? `&sockHost=${options.sockHost}` : '';
const sockPath = options.sockPath ? `&sockPath=${options.sockPath}` : '';
const sockPort = options.sockPort ? `&sockPort=${options.sockPort}` : '';
const clientEntry = `${require.resolve(
'../../client/'
)}?${domain}${sockPath}${sockPort}`;
)}?${domain}${sockHost}${sockPath}${sockPort}`;
let hotEntry;

if (options.hotOnly) {
Expand Down
4 changes: 4 additions & 0 deletions lib/utils/createConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ function createConfig(config, argv, { port }) {
options.socket = argv.socket;
}

if (argv.sockHost) {
options.sockHost = argv.sockHost;
}

if (argv.sockPath) {
options.sockPath = argv.sockPath;
}
Expand Down
73 changes: 73 additions & 0 deletions test/Client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,76 @@ describe('Client complex inline script path with sockPort', () => {
});
});
});

// previously, using sockPort without sockPath had the ability
// to alter the sockPath (based on a bug in client-src/index.js)
// so we need to make sure sockPath is not altered in this case
describe('Client complex inline script path with sockPort, no sockPath', () => {
beforeAll((done) => {
const options = {
port: 9000,
host: '0.0.0.0',
inline: true,
watchOptions: {
poll: true,
},
sockPort: 8080,
};
helper.startAwaitingCompilation(config, options, done);
});

afterAll(helper.close);

describe('browser client', () => {
jest.setTimeout(30000);

it('uses the correct sockPort and sockPath', (done) => {
runBrowser().then(({ page, browser }) => {
page
.waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/))
.then((requestObj) => {
expect(requestObj.url()).toMatch(
/^http:\/\/localhost:8080\/sockjs-node/
);
browser.close().then(done);
});
page.goto('http://localhost:9000/main');
});
});
});
});

describe('Client complex inline script path with sockHost', () => {
beforeAll((done) => {
const options = {
port: 9000,
host: '0.0.0.0',
inline: true,
watchOptions: {
poll: true,
},
sockHost: 'myhost.test',
};
helper.startAwaitingCompilation(config, options, done);
});

afterAll(helper.close);

describe('browser client', () => {
jest.setTimeout(30000);

it('uses the correct sockHost', (done) => {
runBrowser().then(({ page, browser }) => {
page
.waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/))
.then((requestObj) => {
expect(requestObj.url()).toMatch(
/^http:\/\/myhost\.test:9000\/sockjs-node/
);
browser.close().then(done);
});
page.goto('http://localhost:9000/main');
});
});
});
});
4 changes: 4 additions & 0 deletions test/options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@ describe('options', () => {
success: [''],
failure: [false],
},
sockHost: {
success: [''],
failure: [false],
},
sockPath: {
success: [''],
failure: [false],
Expand Down

0 comments on commit f47dff2

Please sign in to comment.