From 8e51f68cdddb7131b8c4b23e3903cf71e85c5c05 Mon Sep 17 00:00:00 2001 From: Young Min Kin Date: Fri, 27 Nov 2020 21:00:20 +0900 Subject: [PATCH 1/2] fix(utils): use server address if available in createDomain --- lib/utils/createDomain.js | 11 ++++++--- test/cli/__snapshots__/cli.test.js.snap | 8 +++---- test/cli/cli.test.js | 4 +++- test/server/open-option.test.js | 2 +- test/server/utils/createDomain.test.js | 31 ++++++++++++++++++------- 5 files changed, 38 insertions(+), 18 deletions(-) diff --git a/lib/utils/createDomain.js b/lib/utils/createDomain.js index c718296fb3..2e3cb0f4b1 100644 --- a/lib/utils/createDomain.js +++ b/lib/utils/createDomain.js @@ -5,9 +5,14 @@ const ip = require('internal-ip'); function createDomain(options, server) { const protocol = options.https ? 'https' : 'http'; - const hostname = options.useLocalIp - ? ip.v4.sync() || 'localhost' - : options.host || 'localhost'; + let hostname; + if (options.useLocalIp) { + hostname = ip.v4.sync() || 'localhost'; + } else if (server) { + hostname = server.address().address; + } else { + hostname = 'localhost'; + } const port = server ? server.address().port : 0; // use explicitly defined public url // (prefix with protocol if not explicitly given) diff --git a/test/cli/__snapshots__/cli.test.js.snap b/test/cli/__snapshots__/cli.test.js.snap index 330cff62e6..2807d86871 100644 --- a/test/cli/__snapshots__/cli.test.js.snap +++ b/test/cli/__snapshots__/cli.test.js.snap @@ -1,7 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`CLI --hot webpack 4 1`] = ` -" [webpack-dev-server] Project is running at http://localhost:8080/ +" [webpack-dev-server] Project is running at http://127.0.0.1:8080/ [webpack-dev-middleware] Hash: X Version: webpack x.x.x Time: Xms Built at: Thu Jan 01 1970 00:00:00 GMT @@ -28,7 +28,7 @@ exports[`CLI --hot webpack 4 1`] = ` `; exports[`CLI --hot webpack 5 1`] = ` -" [webpack-dev-server] Project is running at http://localhost:8080/ +" [webpack-dev-server] Project is running at http://127.0.0.1:8080/ [webpack-dev-middleware] asset main.js X KiB [emitted] (name: main) runtime modules X KiB 10 modules cacheable modules X KiB @@ -46,7 +46,7 @@ exports[`CLI --hot webpack 5 1`] = ` `; exports[`CLI --no-hot webpack 4 1`] = ` -" [webpack-dev-server] Project is running at http://localhost:8080/ +" [webpack-dev-server] Project is running at http://127.0.0.1:8080/ [webpack-dev-middleware] Hash: X Version: webpack x.x.x Time: Xms Built at: Thu Jan 01 1970 00:00:00 GMT @@ -73,7 +73,7 @@ exports[`CLI --no-hot webpack 4 1`] = ` `; exports[`CLI --no-hot webpack 5 1`] = ` -" [webpack-dev-server] Project is running at http://localhost:8080/ +" [webpack-dev-server] Project is running at http://127.0.0.1:8080/ [webpack-dev-middleware] asset main.js X KiB [emitted] (name: main) runtime modules X bytes 3 modules cacheable modules X KiB diff --git a/test/cli/cli.test.js b/test/cli/cli.test.js index 02f0db0f89..05746d19ce 100644 --- a/test/cli/cli.test.js +++ b/test/cli/cli.test.js @@ -121,7 +121,9 @@ runCLITest('CLI', () => { it('unspecified port', (done) => { testBin('') .then((output) => { - expect(/http:\/\/localhost:[0-9]+/.test(output.stderr)).toEqual(true); + expect(/http:\/\/127\.0\.0\.1:[0-9]+/.test(output.stderr)).toEqual( + true + ); done(); }) .catch(done); diff --git a/test/server/open-option.test.js b/test/server/open-option.test.js index 57da231042..7fd3a42229 100644 --- a/test/server/open-option.test.js +++ b/test/server/open-option.test.js @@ -27,7 +27,7 @@ describe('open option', () => { server.close(() => { expect(open.mock.calls[0]).toMatchInlineSnapshot(` Array [ - "http://localhost:8117/", + "http://127.0.0.1:8117/", Object { "wait": false, }, diff --git a/test/server/utils/createDomain.test.js b/test/server/utils/createDomain.test.js index 327fc4a184..303ab6b7f9 100644 --- a/test/server/utils/createDomain.test.js +++ b/test/server/utils/createDomain.test.js @@ -26,14 +26,18 @@ describe('createDomain', () => { host: 'localhost', port: port1, }, - expected: `http://localhost:${port1}`, + expected: [ + `http://localhost:${port1}`, + `http://127.0.0.1:${port1}`, + `http://[::1]:${port1}`, + ], }, { name: 'no host option', options: { port: port1, }, - expected: `http://localhost:${port1}`, + expected: [`http://0.0.0.0:${port1}`, `http://[::]:${port1}`], }, { name: 'https', @@ -42,7 +46,11 @@ describe('createDomain', () => { port: port1, https: true, }, - expected: `https://localhost:${port1}`, + expected: [ + `https://localhost:${port1}`, + `https://127.0.0.1:${port1}`, + `https://[::1]:${port1}`, + ], timeout: 60000, }, { @@ -52,7 +60,7 @@ describe('createDomain', () => { port: port1, public: 'myhost.test', }, - expected: 'http://myhost.test', + expected: ['http://myhost.test'], }, { name: 'override with public (port)', @@ -61,7 +69,7 @@ describe('createDomain', () => { port: port1, public: `myhost.test:${port2}`, }, - expected: `http://myhost.test:${port2}`, + expected: [`http://myhost.test:${port2}`], }, { name: 'override with public (protocol)', @@ -70,7 +78,7 @@ describe('createDomain', () => { port: port1, public: 'https://myhost.test', }, - expected: 'https://myhost.test', + expected: ['https://myhost.test'], }, { name: 'override with public (protocol + port)', @@ -79,7 +87,7 @@ describe('createDomain', () => { port: port1, public: `https://myhost.test:${port2}`, }, - expected: `https://myhost.test:${port2}`, + expected: [`https://myhost.test:${port2}`], }, { name: 'localIp', @@ -87,7 +95,12 @@ describe('createDomain', () => { useLocalIp: true, port: port1, }, - expected: `http://${internalIp.v4.sync() || 'localhost'}:${port1}`, + expected: [ + `http://${internalIp.v4.sync()}:${port1}`, + `https://localhost:${port1}`, + `https://127.0.0.1:${port1}`, + `https://[::1]:${port1}`, + ], }, ]; @@ -105,7 +118,7 @@ describe('createDomain', () => { const domain = createDomain(options, server.listeningApp); - if (domain !== expected) { + if (!expected.includes(domain)) { done(`generated domain ${domain} doesn't match expected ${expected}`); } else { done(); From c9839d4b408a8a11e0fdaf02a06d2451b23a984a Mon Sep 17 00:00:00 2001 From: Young Min Kin Date: Fri, 27 Nov 2020 21:36:05 +0900 Subject: [PATCH 2/2] fix: use location.hostname as the client host by default --- lib/utils/createDomain.js | 6 ++++-- test/cli/__snapshots__/cli.test.js.snap | 8 ++++---- test/server/__snapshots__/Server.test.js.snap | 4 ++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/utils/createDomain.js b/lib/utils/createDomain.js index 2e3cb0f4b1..7248e7ad4e 100644 --- a/lib/utils/createDomain.js +++ b/lib/utils/createDomain.js @@ -5,13 +5,15 @@ const ip = require('internal-ip'); function createDomain(options, server) { const protocol = options.https ? 'https' : 'http'; + // use location hostname and port by default in createSocketUrl + // ipv6 detection is not required as 0.0.0.0 is just used as a placeholder let hostname; if (options.useLocalIp) { - hostname = ip.v4.sync() || 'localhost'; + hostname = ip.v4.sync() || '0.0.0.0'; } else if (server) { hostname = server.address().address; } else { - hostname = 'localhost'; + hostname = '0.0.0.0'; } const port = server ? server.address().port : 0; // use explicitly defined public url diff --git a/test/cli/__snapshots__/cli.test.js.snap b/test/cli/__snapshots__/cli.test.js.snap index 2807d86871..c73d2cb1ae 100644 --- a/test/cli/__snapshots__/cli.test.js.snap +++ b/test/cli/__snapshots__/cli.test.js.snap @@ -8,8 +8,8 @@ exports[`CLI --hot webpack 4 1`] = ` Asset Size Chunks Chunk Names main.js X KiB main [emitted] main Entrypoint main = main.js - [0] multi Xdir/client/default?http://localhost (webpack)/hot/dev-server.js ./foo.js X bytes {main} [built] - [../../../client/default/index.js?http://localhost] Xdir/client/default?http://localhost X KiB {main} [built] + [0] multi Xdir/client/default?http://0.0.0.0 (webpack)/hot/dev-server.js ./foo.js X bytes {main} [built] + [../../../client/default/index.js?http://0.0.0.0] Xdir/client/default?http://0.0.0.0 X KiB {main} [built] [../../../client/default/overlay.js] Xdir/client/default/overlay.js X KiB {main} [built] [../../../client/default/socket.js] Xdir/client/default/socket.js X KiB {main} [built] [../../../client/default/utils/createSocketUrl.js] Xdir/client/default/utils/createSocketUrl.js X KiB {main} [built] @@ -53,9 +53,9 @@ exports[`CLI --no-hot webpack 4 1`] = ` Asset Size Chunks Chunk Names main.js X KiB main [emitted] main Entrypoint main = main.js - [0] multi Xdir/client/default?http://localhost ./foo.js X bytes {main} [built] + [0] multi Xdir/client/default?http://0.0.0.0 ./foo.js X bytes {main} [built] [../../../client/clients/WebsocketClient.js] Xdir/client/clients/WebsocketClient.js X KiB {main} [built] - [../../../client/default/index.js?http://localhost] Xdir/client/default?http://localhost X KiB {main} [built] + [../../../client/default/index.js?http://0.0.0.0] Xdir/client/default?http://0.0.0.0 X KiB {main} [built] [../../../client/default/overlay.js] Xdir/client/default/overlay.js X KiB {main} [built] [../../../client/default/socket.js] Xdir/client/default/socket.js X KiB {main} [built] [../../../client/default/utils/createSocketUrl.js] Xdir/client/default/utils/createSocketUrl.js X KiB {main} [built] diff --git a/test/server/__snapshots__/Server.test.js.snap b/test/server/__snapshots__/Server.test.js.snap index eb0628a2c5..e0793a9f8f 100644 --- a/test/server/__snapshots__/Server.test.js.snap +++ b/test/server/__snapshots__/Server.test.js.snap @@ -6,7 +6,7 @@ Array [ "client", "default", "index.js?http:", - "localhost", + "0.0.0.0", ], Array [ "node_modules", @@ -26,7 +26,7 @@ Array [ "client", "default", "index.js?http:", - "localhost", + "0.0.0.0", ], Array [ "node_modules",