Skip to content

Commit 8e51f68

Browse files
committed
fix(utils): use server address if available in createDomain
1 parent ecf2fe1 commit 8e51f68

File tree

5 files changed

+38
-18
lines changed

5 files changed

+38
-18
lines changed

lib/utils/createDomain.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ const ip = require('internal-ip');
55

66
function createDomain(options, server) {
77
const protocol = options.https ? 'https' : 'http';
8-
const hostname = options.useLocalIp
9-
? ip.v4.sync() || 'localhost'
10-
: options.host || 'localhost';
8+
let hostname;
9+
if (options.useLocalIp) {
10+
hostname = ip.v4.sync() || 'localhost';
11+
} else if (server) {
12+
hostname = server.address().address;
13+
} else {
14+
hostname = 'localhost';
15+
}
1116
const port = server ? server.address().port : 0;
1217
// use explicitly defined public url
1318
// (prefix with protocol if not explicitly given)

test/cli/__snapshots__/cli.test.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`CLI --hot webpack 4 1`] = `
4-
"<i> [webpack-dev-server] Project is running at http://localhost:8080/
4+
"<i> [webpack-dev-server] Project is running at http://127.0.0.1:8080/
55
<i> [webpack-dev-middleware] Hash: X
66
<i> Version: webpack x.x.x Time: Xms
77
<i> Built at: Thu Jan 01 1970 <CLR=BOLD>00:00:00</CLR> GMT
@@ -28,7 +28,7 @@ exports[`CLI --hot webpack 4 1`] = `
2828
`;
2929
3030
exports[`CLI --hot webpack 5 1`] = `
31-
"<i> [webpack-dev-server] Project is running at http://localhost:8080/
31+
"<i> [webpack-dev-server] Project is running at http://127.0.0.1:8080/
3232
<i> [webpack-dev-middleware] asset main.js X KiB [emitted] (name: main)
3333
<i> runtime modules X KiB 10 modules
3434
<i> cacheable modules X KiB
@@ -46,7 +46,7 @@ exports[`CLI --hot webpack 5 1`] = `
4646
`;
4747
4848
exports[`CLI --no-hot webpack 4 1`] = `
49-
"<i> [webpack-dev-server] Project is running at http://localhost:8080/
49+
"<i> [webpack-dev-server] Project is running at http://127.0.0.1:8080/
5050
<i> [webpack-dev-middleware] Hash: X
5151
<i> Version: webpack x.x.x Time: Xms
5252
<i> Built at: Thu Jan 01 1970 <CLR=BOLD>00:00:00</CLR> GMT
@@ -73,7 +73,7 @@ exports[`CLI --no-hot webpack 4 1`] = `
7373
`;
7474
7575
exports[`CLI --no-hot webpack 5 1`] = `
76-
"<i> [webpack-dev-server] Project is running at http://localhost:8080/
76+
"<i> [webpack-dev-server] Project is running at http://127.0.0.1:8080/
7777
<i> [webpack-dev-middleware] asset main.js X KiB [emitted] (name: main)
7878
<i> runtime modules X bytes 3 modules
7979
<i> cacheable modules X KiB

test/cli/cli.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ runCLITest('CLI', () => {
121121
it('unspecified port', (done) => {
122122
testBin('')
123123
.then((output) => {
124-
expect(/http:\/\/localhost:[0-9]+/.test(output.stderr)).toEqual(true);
124+
expect(/http:\/\/127\.0\.0\.1:[0-9]+/.test(output.stderr)).toEqual(
125+
true
126+
);
125127
done();
126128
})
127129
.catch(done);

test/server/open-option.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('open option', () => {
2727
server.close(() => {
2828
expect(open.mock.calls[0]).toMatchInlineSnapshot(`
2929
Array [
30-
"http://localhost:8117/",
30+
"http://127.0.0.1:8117/",
3131
Object {
3232
"wait": false,
3333
},

test/server/utils/createDomain.test.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,18 @@ describe('createDomain', () => {
2626
host: 'localhost',
2727
port: port1,
2828
},
29-
expected: `http://localhost:${port1}`,
29+
expected: [
30+
`http://localhost:${port1}`,
31+
`http://127.0.0.1:${port1}`,
32+
`http://[::1]:${port1}`,
33+
],
3034
},
3135
{
3236
name: 'no host option',
3337
options: {
3438
port: port1,
3539
},
36-
expected: `http://localhost:${port1}`,
40+
expected: [`http://0.0.0.0:${port1}`, `http://[::]:${port1}`],
3741
},
3842
{
3943
name: 'https',
@@ -42,7 +46,11 @@ describe('createDomain', () => {
4246
port: port1,
4347
https: true,
4448
},
45-
expected: `https://localhost:${port1}`,
49+
expected: [
50+
`https://localhost:${port1}`,
51+
`https://127.0.0.1:${port1}`,
52+
`https://[::1]:${port1}`,
53+
],
4654
timeout: 60000,
4755
},
4856
{
@@ -52,7 +60,7 @@ describe('createDomain', () => {
5260
port: port1,
5361
public: 'myhost.test',
5462
},
55-
expected: 'http://myhost.test',
63+
expected: ['http://myhost.test'],
5664
},
5765
{
5866
name: 'override with public (port)',
@@ -61,7 +69,7 @@ describe('createDomain', () => {
6169
port: port1,
6270
public: `myhost.test:${port2}`,
6371
},
64-
expected: `http://myhost.test:${port2}`,
72+
expected: [`http://myhost.test:${port2}`],
6573
},
6674
{
6775
name: 'override with public (protocol)',
@@ -70,7 +78,7 @@ describe('createDomain', () => {
7078
port: port1,
7179
public: 'https://myhost.test',
7280
},
73-
expected: 'https://myhost.test',
81+
expected: ['https://myhost.test'],
7482
},
7583
{
7684
name: 'override with public (protocol + port)',
@@ -79,15 +87,20 @@ describe('createDomain', () => {
7987
port: port1,
8088
public: `https://myhost.test:${port2}`,
8189
},
82-
expected: `https://myhost.test:${port2}`,
90+
expected: [`https://myhost.test:${port2}`],
8391
},
8492
{
8593
name: 'localIp',
8694
options: {
8795
useLocalIp: true,
8896
port: port1,
8997
},
90-
expected: `http://${internalIp.v4.sync() || 'localhost'}:${port1}`,
98+
expected: [
99+
`http://${internalIp.v4.sync()}:${port1}`,
100+
`https://localhost:${port1}`,
101+
`https://127.0.0.1:${port1}`,
102+
`https://[::1]:${port1}`,
103+
],
91104
},
92105
];
93106

@@ -105,7 +118,7 @@ describe('createDomain', () => {
105118

106119
const domain = createDomain(options, server.listeningApp);
107120

108-
if (domain !== expected) {
121+
if (!expected.includes(domain)) {
109122
done(`generated domain ${domain} doesn't match expected ${expected}`);
110123
} else {
111124
done();

0 commit comments

Comments
 (0)