Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added sockHost, changed how sock URL is built #1858

Merged
merged 5 commits into from May 13, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 18 additions & 9 deletions client-src/default/index.js
Expand Up @@ -220,21 +220,30 @@ 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 && urlParts.path !== '/') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use strict equality operator. Or are you intentionally doing so to include undefined?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's how it was here: https://github.com/webpack/webpack-dev-server/blob/master/client-src/default/index.js#L235. I kept it that way under the assumption that it might need to catch undefined.

Copy link
Member

@hiroppy hiroppy May 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I didn't know that it was already used...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Loonride i think we should fix using != (==` and etc) in our code base

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evilebottnawi should I fix it here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to change !==, but its change is the breaking change, so please rewrite as below.

if ((urlParts.path !== null || urlParts.path !== undefined) && urlParts.path !== '/') {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think urlParts.path != null would turn into (urlParts.path !== null && urlParts.path !== undefined)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. My mistake.

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
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
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
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
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
Expand Up @@ -337,6 +337,10 @@ describe('options', () => {
success: [''],
failure: [false],
},
sockHost: {
success: [''],
failure: [false],
},
sockPath: {
success: [''],
failure: [false],
Expand Down