Skip to content

Commit 2d68701

Browse files
authored
feat: add webSocketURL option (#3309)
1 parent ed6d27d commit 2d68701

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+792
-735
lines changed

bin/cli-flags.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -394,16 +394,6 @@ module.exports = {
394394
negatedDescription: 'Disable gzip compression.',
395395
negative: true,
396396
},
397-
{
398-
name: 'public',
399-
type: String,
400-
configs: [
401-
{
402-
type: 'string',
403-
},
404-
],
405-
description: 'The public hostname/ip address of the server.',
406-
},
407397
{
408398
name: 'firewall',
409399
type: [Boolean, String],

client-src/utils/createSocketURL.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ const url = require('url');
77
function createSocketURL(parsedURL) {
88
let { auth, hostname, protocol, port } = parsedURL;
99

10-
const getURLSearchParam = (name) => {
11-
if (parsedURL.searchParams) {
12-
return parsedURL.searchParams.get(name);
13-
}
14-
15-
return parsedURL.query && parsedURL.query[name];
16-
};
17-
1810
// Node.js module parses it as `::`
1911
// `new URL(urlString, [baseURLstring])` parses it as '[::]'
2012
const isInAddrAny =
@@ -66,22 +58,25 @@ function createSocketURL(parsedURL) {
6658
//
6759
// All of these sock url params are optionally passed in through resourceQuery,
6860
// so we need to fall back to the default if they are not provided
69-
const socketURLHostname = (
70-
getURLSearchParam('host') ||
71-
hostname ||
72-
'localhost'
73-
).replace(/^\[(.*)\]$/, '$1');
61+
const socketURLHostname = (hostname || 'localhost').replace(
62+
/^\[(.*)\]$/,
63+
'$1'
64+
);
7465

7566
if (!port || port === '0') {
7667
port = self.location.port;
7768
}
7869

79-
const socketURLPort = getURLSearchParam('port') || port;
70+
const socketURLPort = port;
8071

8172
// If path is provided it'll be passed in via the resourceQuery as a
8273
// query param so it has to be parsed out of the querystring in order for the
8374
// client to open the socket to the correct location.
84-
const socketURLPathname = getURLSearchParam('path') || '/ws';
75+
let socketURLPathname = '/ws';
76+
77+
if (parsedURL.pathname && !parsedURL.fromCurrentScript) {
78+
socketURLPathname = parsedURL.pathname;
79+
}
8580

8681
return url.format({
8782
protocol: socketURLProtocol,

client-src/utils/parseURL.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@ function parseURL(resourceQuery) {
88

99
if (typeof resourceQuery === 'string' && resourceQuery !== '') {
1010
// If this bundle is inlined, use the resource query to get the correct url.
11-
// format is like `?http://0.0.0.0:8096&port=8097&host=localhost`
11+
// for backward compatibility we supports:
12+
// - ?ws://0.0.0.0:8096&3Flogging=info
13+
// - ?ws%3A%2F%2F192.168.0.5%3A8080%2F%3Flogging%3Dinfo
14+
// Also we support `http` and `https` for backward compatibility too
1215
options = url.parse(
13-
resourceQuery
14-
// strip leading `?` from query string to get a valid URL
15-
.substr(1)
16-
// replace first `&` with `?` to have a valid query string
17-
.replace('&', '?'),
16+
decodeURIComponent(
17+
resourceQuery
18+
// strip leading `?` from query string to get a valid URL
19+
.substr(1)
20+
// replace first `&` with `?` to have a valid query string
21+
.replace('&', '?')
22+
),
1823
true
1924
);
2025
} else {
@@ -36,9 +41,11 @@ function parseURL(resourceQuery) {
3641

3742
if (scriptSourceURL) {
3843
options = scriptSourceURL;
44+
options.fromCurrentScript = true;
3945
}
4046
} else {
4147
options = url.parse(self.location.href, true, true);
48+
options.fromCurrentScript = true;
4249
}
4350
}
4451

examples/cli/public-protocol/app.js

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# CLI: Public Option
1+
# CLI: Web Socket URL
22

33
```console
4-
npx webpack serve --open-target --host 0.0.0.0 --public <insert local ip>:8080
4+
npx webpack serve --open-target --host 0.0.0.0 --web-socket-url <insert-host>:8080
55
```
66

77
_NOTE: replace `<insert local ip>` with your local IP Address._
88

99
In order to make the server publicly accessible the client needs to know with
1010
what host to connect to the server. If `--host 0.0.0.0` is given, the client
11-
would try to connect to `0.0.0.0`. With the `--public` option it is possible to
11+
would try to connect to `0.0.0.0`. With the `--web-socket-url` options it is possible to
1212
override this.
1313

1414
## What Should Happen
@@ -18,4 +18,4 @@ override this.
1818
3. Open the console in your browser's devtools.
1919
4. Select the 'Network' tab.
2020
5. Select the 'WS' or 'WebSockets' sub-tab.
21-
6. Verify that the WebSocket is connecting to `<insert local ip>:8080`.
21+
6. Verify that the WebSocket is connecting to `<insert-host>:8080`.
File renamed without changes.

examples/cli/web-socket-url/app.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
const target = document.querySelector('#target');
4+
5+
target.innerHTML =
6+
'Please check the ws request in devtools, it should try to connect to the protocol + server defined in the webSocketURL setting.';

examples/cli/public-protocol/webpack.config.js renamed to examples/cli/web-socket-url/webpack.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ module.exports = setup({
99
entry: './app.js',
1010
devServer: {
1111
host: '0.0.0.0',
12-
public: 'https://localhost:8080',
12+
client: {
13+
webSocketURL: 'ws://localhost:8080',
14+
},
1315
firewall: false,
1416
},
1517
});

lib/Server.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -969,17 +969,9 @@ class Server {
969969
}
970970
}
971971

972-
// also allow public hostname if provided
973-
if (typeof this.options.public === 'string') {
974-
const idxPublic = this.options.public.indexOf(':');
975-
const publicHostname =
976-
idxPublic >= 0
977-
? this.options.public.substr(0, idxPublic)
978-
: this.options.public;
979-
980-
if (hostname === publicHostname) {
981-
return true;
982-
}
972+
// Also allow if `client.webSocketURL.host` provided
973+
if (typeof this.options.client.webSocketURL !== 'undefined') {
974+
return this.options.client.webSocketURL.host === hostname;
983975
}
984976

985977
// disallow

lib/options.json

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -229,28 +229,6 @@
229229
],
230230
"description": "Allows to set custom transport to communicate with server."
231231
},
232-
"host": {
233-
"type": "string",
234-
"description": "Tells clients connected to devServer to use the provided host."
235-
},
236-
"path": {
237-
"type": "string",
238-
"description": "Tells clients connected to devServer to use the provided path to connect."
239-
},
240-
"port": {
241-
"anyOf": [
242-
{
243-
"type": "number"
244-
},
245-
{
246-
"type": "string"
247-
},
248-
{
249-
"enum": ["auto"]
250-
}
251-
],
252-
"description": "Tells clients connected to devServer to use the provided port."
253-
},
254232
"logging": {
255233
"enum": ["none", "error", "warn", "info", "log", "verbose"],
256234
"decription": "Log level in the browser."
@@ -301,6 +279,34 @@
301279
}
302280
],
303281
"description": "Tells devServer to inject a Hot Module Replacement entry."
282+
},
283+
"webSocketURL": {
284+
"anyOf": [
285+
{
286+
"type": "string",
287+
"minLength": 1
288+
},
289+
{
290+
"type": "object",
291+
"additionalProperties": false,
292+
"properties": {
293+
"host": {
294+
"type": "string",
295+
"minLength": 1,
296+
"description": "Tells clients connected to devServer to use the provided host."
297+
},
298+
"port": {
299+
"type": "number",
300+
"description": "Tells clients connected to devServer to use the provided port."
301+
},
302+
"path": {
303+
"type": "string",
304+
"description": "Tells clients connected to devServer to use the provided path to connect."
305+
}
306+
}
307+
}
308+
],
309+
"description": "When using dev server and you're proxying dev-server, the client script does not always know where to connect to."
304310
}
305311
},
306312
"description": "Specifies client properties. https://webpack.js.org/configuration/dev-server/#devserverclient",
@@ -540,10 +546,6 @@
540546
],
541547
"description": "Proxying some URLs can be useful when you have a separate API backend development server and you want to send API requests on the same domain. https://webpack.js.org/configuration/dev-server/#devserverproxy"
542548
},
543-
"public": {
544-
"type": "string",
545-
"description": "When using dev server and you're proxying dev-server, the client script does not always know where to connect to. It will try to guess the URL of the server based on window.location, but if that fails you'll need to use this. https://webpack.js.org/configuration/dev-server/#devserverpublic"
546-
},
547549
"setupExitSignals": {
548550
"type": "boolean",
549551
"description": "It takes a boolean and if true (default on CLI), the server will close and exit the process on SIGINT and SIGTERM. https://webpack.js.org/configuration/dev-server/#devserversetupexitsignals"

0 commit comments

Comments
 (0)