Skip to content

fix: migrate wsUrl host option to hostname #3456

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

Merged
merged 9 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Options:
--client-transport <value> Allows to set custom transport to communicate with dev server.
--client-web-socket-url <value> Allows to specify URL to web socket server (useful when you're proxying dev server and client script does not
always know where to connect to).
--client-web-socket-url-host <value> Tells clients connected to devServer to use the provided host.
--client-web-socket-url-hostname <value> Tells clients connected to devServer to use the provided host.
--client-web-socket-url-path <value> Tells clients connected to devServer to use the provided path to connect.
--client-web-socket-url-port <value> Tells clients connected to devServer to use the provided port.
--client-web-socket-url-protocol <value> Tells clients connected to devServer to use the provided protocol.
Expand Down
8 changes: 4 additions & 4 deletions bin/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,18 @@ module.exports = {
simpleType: 'string',
multiple: false,
},
'client-web-socket-url-host': {
'client-web-socket-url-hostname': {
configs: [
{
type: 'string',
multiple: false,
description:
'Tells clients connected to devServer to use the provided host.',
path: 'client.webSocketURL.host',
'Tells clients connected to devServer to use the provided hostname.',
path: 'client.webSocketURL.hostname',
},
],
description:
'Tells clients connected to devServer to use the provided host.',
'Tells clients connected to devServer to use the provided hostname.',
simpleType: 'string',
multiple: false,
},
Expand Down
4 changes: 2 additions & 2 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -921,9 +921,9 @@ class Server {
}
}

// Also allow if `client.webSocketURL.host` provided
// Also allow if `client.webSocketURL.hostname` provided
if (typeof this.options.client.webSocketURL !== 'undefined') {
return this.options.client.webSocketURL.host === hostname;
return this.options.client.webSocketURL.hostname === hostname;
}

// disallow
Expand Down
4 changes: 2 additions & 2 deletions lib/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@
"type": "object",
"additionalProperties": false,
"properties": {
"host": {
"description": "Tells clients connected to devServer to use the provided host.",
"hostname": {
"description": "Tells clients connected to devServer to use the provided hostname.",
"type": "string",
"minLength": 1
},
Expand Down
22 changes: 11 additions & 11 deletions lib/utils/DevServerPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,38 @@ class DevServerPlugin {
/** @type {"ws:" | "wss:" | "http:" | "https:" | "auto:"} */
let protocol;

// We are proxying dev server and need to specify custom `host`
// We are proxying dev server and need to specify custom `hostname`
if (typeof options.client.webSocketURL.protocol !== 'undefined') {
protocol = options.client.webSocketURL.protocol;
} else {
protocol = options.https ? 'wss:' : 'ws:';
}

/** @type {string} */
let host;
let hostname;

// SockJS is not supported server mode, so `host` and `port` can't specified, let's ignore them
// SockJS is not supported server mode, so `hostname` and `port` can't specified, let's ignore them
// TODO show warning about this
const isSockJSType = options.webSocketServer.type === 'sockjs';

// We are proxying dev server and need to specify custom `host`
if (typeof options.client.webSocketURL.host !== 'undefined') {
host = options.client.webSocketURL.host;
// We are proxying dev server and need to specify custom `hostname`
if (typeof options.client.webSocketURL.hostname !== 'undefined') {
hostname = options.client.webSocketURL.hostname;
}
// Web socket server works on custom `host`, only for `ws` because `sock-js` is not support custom `host`
// Web socket server works on custom `hostname`, only for `ws` because `sock-js` is not support custom `hostname`
else if (
typeof options.webSocketServer.options.host !== 'undefined' &&
!isSockJSType
) {
host = options.webSocketServer.options.host;
hostname = options.webSocketServer.options.host;
}
// The `host` option is specified
else if (typeof this.options.host !== 'undefined') {
host = this.options.host;
hostname = this.options.host;
}
// The `port` option is not specified
else {
host = '0.0.0.0';
hostname = '0.0.0.0';
}

/** @type {number | string} */
Expand Down Expand Up @@ -130,7 +130,7 @@ class DevServerPlugin {
new URL(
`${protocol}//${username}${password ? `:${password}` : ''}${
username || password ? `@` : ''
}${ipaddr.IPv6.isIPv6(host) ? `[${host}]` : host}${
}${ipaddr.IPv6.isIPv6(hostname) ? `[${hostname}]` : hostname}${
port ? `:${port}` : ''
}${path || '/'}${searchParamsString ? `?${searchParamsString}` : ''}`
).toString()
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/normalizeOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function normalizeOptions(compiler, options, logger) {

options.client.webSocketURL = {
protocol: parsedURL.protocol,
host: parsedURL.hostname,
hostname: parsedURL.hostname,
port: parsedURL.port.length > 0 ? Number(parsedURL.port) : '',
path: parsedURL.pathname,
username: parsedURL.username,
Expand Down Expand Up @@ -151,7 +151,7 @@ function normalizeOptions(compiler, options, logger) {

if (typeof options.allowedHosts === 'undefined') {
// allowedHosts allows some default hosts picked from
// `options.host` or `webSocketURL.host` and `localhost`
// `options.host` or `webSocketURL.hostname` and `localhost`
options.allowedHosts = 'auto';
}
if (
Expand Down
16 changes: 8 additions & 8 deletions test/__snapshots__/validate-options.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,16 @@ exports[`options validate should throw an error on the "client" option with '{"u
-> Allows to specify options for client script in the browser. https://webpack.js.org/configuration/dev-server/#devserverclient"
`;

exports[`options validate should throw an error on the "client" option with '{"webSocketURL":{"host":""}}' value 1`] = `
exports[`options validate should throw an error on the "client" option with '{"webSocketURL":{"hostname":""}}' value 1`] = `
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
- options.client.webSocketURL.host should be an non-empty string.
-> Tells clients connected to devServer to use the provided host."
- options.client.webSocketURL.hostname should be an non-empty string.
-> Tells clients connected to devServer to use the provided hostname."
`;

exports[`options validate should throw an error on the "client" option with '{"webSocketURL":{"host":true,"path":"","port":8080}}' value 1`] = `
exports[`options validate should throw an error on the "client" option with '{"webSocketURL":{"hostname":true,"path":"","port":8080}}' value 1`] = `
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
- options.client.webSocketURL.host should be a non-empty string.
-> Tells clients connected to devServer to use the provided host."
- options.client.webSocketURL.hostname should be a non-empty string.
-> Tells clients connected to devServer to use the provided hostname."
`;

exports[`options validate should throw an error on the "client" option with '{"webSocketURL":{"path":true}}' value 1`] = `
Expand All @@ -172,7 +172,7 @@ exports[`options validate should throw an error on the "client" option with '{"w
exports[`options validate should throw an error on the "client" option with '{"webSocketURL":{"port":true}}' value 1`] = `
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
- options.client.webSocketURL should be one of these:
non-empty string | object { host?, path?, password?, port?, protocol?, username? }
non-empty string | object { hostname?, path?, password?, port?, protocol?, username? }
-> Allows to specify URL to web socket server (useful when you're proxying dev server and client script does not always know where to connect to).
Details:
* options.client.webSocketURL.port should be one of these:
Expand All @@ -186,7 +186,7 @@ exports[`options validate should throw an error on the "client" option with '{"w
exports[`options validate should throw an error on the "client" option with '{"webSocketURL":{"username":123,"password":976}}' value 1`] = `
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
- options.client.webSocketURL should be one of these:
non-empty string | object { host?, path?, password?, port?, protocol?, username? }
non-empty string | object { hostname?, path?, password?, port?, protocol?, username? }
-> Allows to specify URL to web socket server (useful when you're proxying dev server and client script does not always know where to connect to).
Details:
* options.client.webSocketURL.password should be a string.
Expand Down
16 changes: 8 additions & 8 deletions test/__snapshots__/validate-options.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,16 @@ exports[`options validate should throw an error on the "client" option with '{"u
-> Allows to specify options for client script in the browser. https://webpack.js.org/configuration/dev-server/#devserverclient"
`;

exports[`options validate should throw an error on the "client" option with '{"webSocketURL":{"host":""}}' value 1`] = `
exports[`options validate should throw an error on the "client" option with '{"webSocketURL":{"hostname":""}}' value 1`] = `
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
- options.client.webSocketURL.host should be an non-empty string.
-> Tells clients connected to devServer to use the provided host."
- options.client.webSocketURL.hostname should be an non-empty string.
-> Tells clients connected to devServer to use the provided hostname."
`;

exports[`options validate should throw an error on the "client" option with '{"webSocketURL":{"host":true,"path":"","port":8080}}' value 1`] = `
exports[`options validate should throw an error on the "client" option with '{"webSocketURL":{"hostname":true,"path":"","port":8080}}' value 1`] = `
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
- options.client.webSocketURL.host should be a non-empty string.
-> Tells clients connected to devServer to use the provided host."
- options.client.webSocketURL.hostname should be a non-empty string.
-> Tells clients connected to devServer to use the provided hostname."
`;

exports[`options validate should throw an error on the "client" option with '{"webSocketURL":{"path":true}}' value 1`] = `
Expand All @@ -172,7 +172,7 @@ exports[`options validate should throw an error on the "client" option with '{"w
exports[`options validate should throw an error on the "client" option with '{"webSocketURL":{"port":true}}' value 1`] = `
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
- options.client.webSocketURL should be one of these:
non-empty string | object { host?, path?, password?, port?, protocol?, username? }
non-empty string | object { hostname?, path?, password?, port?, protocol?, username? }
-> Allows to specify URL to web socket server (useful when you're proxying dev server and client script does not always know where to connect to).
Details:
* options.client.webSocketURL.port should be one of these:
Expand All @@ -186,7 +186,7 @@ exports[`options validate should throw an error on the "client" option with '{"w
exports[`options validate should throw an error on the "client" option with '{"webSocketURL":{"username":123,"password":976}}' value 1`] = `
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
- options.client.webSocketURL should be one of these:
non-empty string | object { host?, path?, password?, port?, protocol?, username? }
non-empty string | object { hostname?, path?, password?, port?, protocol?, username? }
-> Allows to specify URL to web socket server (useful when you're proxying dev server and client script does not always know where to connect to).
Details:
* options.client.webSocketURL.password should be a string.
Expand Down
2 changes: 1 addition & 1 deletion test/cli/__snapshots__/basic.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Options:
--client-hot-entry Injects a Hot Module Replacement entry.
--no-client-hot-entry Does not injects a Hot Module Replacement entry.
--client-web-socket-url <value> Allows to specify URL to web socket server (useful when you're proxying dev server and client script does not always know where to connect to).
--client-web-socket-url-host <value> Tells clients connected to devServer to use the provided host.
--client-web-socket-url-hostname <value> Tells clients connected to devServer to use the provided hostname.
--client-web-socket-url-port <value> Tells clients connected to devServer to use the provided port.
--client-web-socket-url-path <value> Tells clients connected to devServer to use the provided path to connect.
--client-web-socket-url-protocol <value> Tells clients connected to devServer to use the provided protocol.
Expand Down
2 changes: 1 addition & 1 deletion test/cli/__snapshots__/basic.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Options:
--no-client-progress Negative 'client-progress' option.
--client-transport <value> Allows to set custom transport to communicate with dev server.
--client-web-socket-url <value> Allows to specify URL to web socket server (useful when you're proxying dev server and client script does not always know where to connect to).
--client-web-socket-url-host <value> Tells clients connected to devServer to use the provided host.
--client-web-socket-url-hostname <value> Tells clients connected to devServer to use the provided hostname.
--client-web-socket-url-path <value> Tells clients connected to devServer to use the provided path to connect.
--client-web-socket-url-password <value> Tells clients connected to devServer to use the provided password to authenticate.
--client-web-socket-url-port <value> Tells clients connected to devServer to use the provided port.
Expand Down
4 changes: 2 additions & 2 deletions test/cli/client-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ describe('"client" CLI option', () => {
expect(exitCode).toEqual(0);
});

it('should work using "--client-web-socket-url-host"', async () => {
it('should work using "--client-web-socket-url-hostname"', async () => {
const { exitCode } = await testBin([
'--client-web-socket-url-host',
'--client-web-socket-url-hostname',
'0.0.0.0',
]);

Expand Down
8 changes: 4 additions & 4 deletions test/e2e/web-socket-server-url.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ describe('web socket server URL', () => {
const devServerOptions = {
client: {
webSocketURL: {
host: devServerHost,
hostname: devServerHost,
},
},
webSocketServer,
Expand Down Expand Up @@ -595,7 +595,7 @@ describe('web socket server URL', () => {
const devServerOptions = {
client: {
webSocketURL: {
host: '127.0.0.1',
hostname: '127.0.0.1',
},
},
webSocketServer,
Expand Down Expand Up @@ -679,7 +679,7 @@ describe('web socket server URL', () => {
const devServerOptions = {
client: {
webSocketURL: {
host: '0.0.0.0',
hostname: '0.0.0.0',
},
},
webSocketServer,
Expand Down Expand Up @@ -1694,7 +1694,7 @@ describe('web socket server URL', () => {
client: {
webSocketURL: {
protocol: 'ws:',
host: '127.0.0.1',
hostname: '127.0.0.1',
port: port1,
path: '/ws',
},
Expand Down
2 changes: 1 addition & 1 deletion test/server/Server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ describe('Server', () => {
const options = {
client: {
webSocketURL: {
host: 'test.host',
hostname: 'test.host',
},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Object {
"hotEntry": true,
"overlay": true,
"webSocketURL": Object {
"host": "my.host",
"hostname": "my.host",
"port": 9000,
},
},
Expand Down Expand Up @@ -116,7 +116,7 @@ Object {
"hotEntry": true,
"overlay": true,
"webSocketURL": Object {
"host": "my.host",
"hostname": "my.host",
"port": 9000,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Object {
"hotEntry": true,
"overlay": true,
"webSocketURL": Object {
"host": "my.host",
"hostname": "my.host",
"port": 9000,
},
},
Expand Down Expand Up @@ -116,7 +116,7 @@ Object {
"hotEntry": true,
"overlay": true,
"webSocketURL": Object {
"host": "my.host",
"hostname": "my.host",
"port": 9000,
},
},
Expand Down
4 changes: 2 additions & 2 deletions test/server/utils/normalizeOptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('normalizeOptions', () => {
options: {
client: {
webSocketURL: {
host: 'my.host',
hostname: 'my.host',
port: 9000,
},
},
Expand All @@ -133,7 +133,7 @@ describe('normalizeOptions', () => {
options: {
client: {
webSocketURL: {
host: 'my.host',
hostname: 'my.host',
port: '9000',
},
},
Expand Down
8 changes: 4 additions & 4 deletions test/validate-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const tests = {
webSocketURL: 'ws://localhost:8080',
},
{
webSocketURL: { host: 'localhost' },
webSocketURL: { hostname: 'localhost' },
},
{
webSocketURL: { port: 8080 },
Expand All @@ -96,7 +96,7 @@ const tests = {
webSocketURL: { path: '/my-path/' },
},
{
webSocketURL: { host: 'localhost', port: 8080, path: '/my-path/' },
webSocketURL: { hostname: 'localhost', port: 8080, path: '/my-path/' },
},
{
webSocketURL: { username: 'zoro', password: 'roronoa' },
Expand Down Expand Up @@ -144,7 +144,7 @@ const tests = {
transport: true,
},
{
webSocketURL: { host: true, path: '', port: 8080 },
webSocketURL: { hostname: true, path: '', port: 8080 },
},
{
webSocketURL: { path: true },
Expand All @@ -153,7 +153,7 @@ const tests = {
webSocketURL: { port: true },
},
{
webSocketURL: { host: '' },
webSocketURL: { hostname: '' },
},
{
webSocketURL: { port: '' },
Expand Down