1
1
"use strict" ;
2
2
3
+ const http = require ( "http" ) ;
3
4
const webpack = require ( "webpack" ) ;
4
5
const Server = require ( "../../lib/Server" ) ;
5
6
const config = require ( "../fixtures/client-config/webpack.config" ) ;
6
7
const runBrowser = require ( "../helpers/run-browser" ) ;
7
8
const port = require ( "../ports-map" ) . host ;
8
9
9
- const ipv4 = Server . findIp ( "v4" ) ;
10
- const ipv6 = Server . findIp ( "v6" ) ;
11
- // macos requires root for using ip v6
12
- const isMacOS = process . platform === "darwin" ;
10
+ const ipv4 = Server . findIp ( "v4" , false ) ;
11
+ const ipv6 = Server . findIp ( "v6" , false ) ;
13
12
14
- function getAddress ( host , hostname ) {
13
+ async function getAddress ( host , hostname ) {
15
14
let address ;
16
15
17
16
if (
18
17
typeof host === "undefined" ||
19
- ( typeof host === "string" && host === "<not-specified>" )
18
+ ( typeof host === "string" && ( host === "<not-specified>" || host === "::" ) )
20
19
) {
21
20
address = "::" ;
22
- } else if ( typeof host === "string" && host === "0.0.0.0" ) {
21
+ } else if ( host === "0.0.0.0" ) {
23
22
address = "0.0.0.0" ;
24
- } else if ( typeof host === "string" && host === "localhost" ) {
25
- address = parseFloat ( process . versions . node ) >= 18 ? "::1" : "127.0.0.1" ;
23
+ } else if ( host === "::1" ) {
24
+ address = "::1" ;
25
+ } else if ( host === "localhost" ) {
26
+ // It can be `127.0.0.1` or `::1` on different OS
27
+ const server = http . createServer ( ( req , res ) => {
28
+ res . statusCode = 200 ;
29
+ res . setHeader ( "Content-Type" , "text/plain" ) ;
30
+ res . end ( "Hello World\n" ) ;
31
+ } ) ;
32
+
33
+ await new Promise ( ( resolve ) => {
34
+ server . listen ( { host : "localhost" , port : 23100 } , resolve ) ;
35
+ } ) ;
36
+
37
+ address = server . address ( ) . address ;
38
+
39
+ await new Promise ( ( resolve , reject ) => {
40
+ server . close ( ( err ) => {
41
+ if ( err ) {
42
+ reject ( err ) ;
43
+ return ;
44
+ }
45
+
46
+ resolve ( ) ;
47
+ } ) ;
48
+ } ) ;
49
+ } else if ( host === "local-ipv6" ) {
50
+ address = "::" ;
26
51
} else {
27
52
address = hostname ;
28
53
}
@@ -37,28 +62,17 @@ describe("host", () => {
37
62
undefined ,
38
63
"0.0.0.0" ,
39
64
"::" ,
40
- "localhost" ,
41
65
"::1" ,
66
+ "localhost" ,
42
67
"127.0.0.1" ,
43
68
"local-ip" ,
44
69
"local-ipv4" ,
45
70
"local-ipv6" ,
46
71
] ;
47
72
48
- for ( let host of hosts ) {
73
+ for ( const host of hosts ) {
49
74
it ( `should work using "${ host } " host and port as number` , async ( ) => {
50
75
const compiler = webpack ( config ) ;
51
-
52
- if ( ! ipv6 || isMacOS ) {
53
- if ( host === "::" ) {
54
- host = "127.0.0.1" ;
55
- } else if ( host === "::1" ) {
56
- host = "127.0.0.1" ;
57
- } else if ( host === "local-ipv6" ) {
58
- host = "127.0.0.1" ;
59
- }
60
- }
61
-
62
76
const devServerOptions = { port } ;
63
77
64
78
if ( host !== "<not-specified>" ) {
@@ -69,24 +83,28 @@ describe("host", () => {
69
83
70
84
let hostname = host ;
71
85
72
- if ( hostname === "0.0.0.0" ) {
73
- hostname = "127.0.0.1" ;
74
- } else if (
75
- hostname === "<not-specified>" ||
76
- typeof hostname === "undefined" ||
77
- hostname === "::" ||
78
- hostname === "::1"
79
- ) {
86
+ if ( hostname === "<not-specified>" || typeof hostname === "undefined" ) {
87
+ // If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available, or the unspecified IPv4 address (0.0.0.0) otherwise.
88
+ hostname = ipv6 ? `[${ ipv6 } ]` : ipv4 ;
89
+ } else if ( hostname === "0.0.0.0" ) {
90
+ hostname = ipv4 ;
91
+ } else if ( hostname === "::" ) {
92
+ // In most operating systems, listening to the unspecified IPv6 address (::) may cause the net.Server to also listen on the unspecified IPv4 address (0.0.0.0).
93
+ hostname = ipv6 ? `[${ ipv6 } ]` : ipv4 ;
94
+ } else if ( hostname === "::1" ) {
80
95
hostname = "[::1]" ;
81
96
} else if ( hostname === "local-ip" || hostname === "local-ipv4" ) {
82
97
hostname = ipv4 ;
83
98
} else if ( hostname === "local-ipv6" ) {
84
- hostname = `[${ ipv6 } ]` ;
99
+ // For test env where network ipv6 doesn't work
100
+ hostname = ipv6 ? `[${ ipv6 } ]` : "[::1]" ;
85
101
}
86
102
87
103
await server . start ( ) ;
88
104
89
- expect ( server . server . address ( ) ) . toMatchObject ( getAddress ( host , hostname ) ) ;
105
+ expect ( server . server . address ( ) ) . toMatchObject (
106
+ await getAddress ( host , hostname ) ,
107
+ ) ;
90
108
91
109
const { page, browser } = await runBrowser ( ) ;
92
110
@@ -121,17 +139,6 @@ describe("host", () => {
121
139
122
140
it ( `should work using "${ host } " host and port as string` , async ( ) => {
123
141
const compiler = webpack ( config ) ;
124
-
125
- if ( ! ipv6 || isMacOS ) {
126
- if ( host === "::" ) {
127
- host = "127.0.0.1" ;
128
- } else if ( host === "::1" ) {
129
- host = "127.0.0.1" ;
130
- } else if ( host === "local-ipv6" ) {
131
- host = "127.0.0.1" ;
132
- }
133
- }
134
-
135
142
const devServerOptions = { port : `${ port } ` } ;
136
143
137
144
if ( host !== "<not-specified>" ) {
@@ -142,24 +149,28 @@ describe("host", () => {
142
149
143
150
let hostname = host ;
144
151
145
- if ( hostname === "0.0.0.0" ) {
146
- hostname = "127.0.0.1" ;
147
- } else if (
148
- hostname === "<not-specified>" ||
149
- typeof hostname === "undefined" ||
150
- hostname === "::" ||
151
- hostname === "::1"
152
- ) {
152
+ if ( hostname === "<not-specified>" || typeof hostname === "undefined" ) {
153
+ // If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available, or the unspecified IPv4 address (0.0.0.0) otherwise.
154
+ hostname = ipv6 ? `[${ ipv6 } ]` : ipv4 ;
155
+ } else if ( hostname === "0.0.0.0" ) {
156
+ hostname = ipv4 ;
157
+ } else if ( hostname === "::" ) {
158
+ // In most operating systems, listening to the unspecified IPv6 address (::) may cause the net.Server to also listen on the unspecified IPv4 address (0.0.0.0).
159
+ hostname = ipv6 ? `[${ ipv6 } ]` : ipv4 ;
160
+ } else if ( hostname === "::1" ) {
153
161
hostname = "[::1]" ;
154
162
} else if ( hostname === "local-ip" || hostname === "local-ipv4" ) {
155
163
hostname = ipv4 ;
156
164
} else if ( hostname === "local-ipv6" ) {
157
- hostname = `[${ ipv6 } ]` ;
165
+ // For test env where network ipv6 doesn't work
166
+ hostname = ipv6 ? `[${ ipv6 } ]` : "[::1]" ;
158
167
}
159
168
160
169
await server . start ( ) ;
161
170
162
- expect ( server . server . address ( ) ) . toMatchObject ( getAddress ( host , hostname ) ) ;
171
+ expect ( server . server . address ( ) ) . toMatchObject (
172
+ await getAddress ( host , hostname ) ,
173
+ ) ;
163
174
164
175
const { page, browser } = await runBrowser ( ) ;
165
176
@@ -197,16 +208,6 @@ describe("host", () => {
197
208
198
209
process . env . WEBPACK_DEV_SERVER_BASE_PORT = port ;
199
210
200
- if ( ! ipv6 || isMacOS ) {
201
- if ( host === "::" ) {
202
- host = "127.0.0.1" ;
203
- } else if ( host === "::1" ) {
204
- host = "127.0.0.1" ;
205
- } else if ( host === "local-ipv6" ) {
206
- host = "127.0.0.1" ;
207
- }
208
- }
209
-
210
211
const devServerOptions = { port : "auto" } ;
211
212
212
213
if ( host !== "<not-specified>" ) {
@@ -217,24 +218,28 @@ describe("host", () => {
217
218
218
219
let hostname = host ;
219
220
220
- if ( hostname === "0.0.0.0" ) {
221
- hostname = "127.0.0.1" ;
222
- } else if (
223
- hostname === "<not-specified>" ||
224
- typeof hostname === "undefined" ||
225
- hostname === "::" ||
226
- hostname === "::1"
227
- ) {
221
+ if ( hostname === "<not-specified>" || typeof hostname === "undefined" ) {
222
+ // If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available, or the unspecified IPv4 address (0.0.0.0) otherwise.
223
+ hostname = ipv6 ? `[${ ipv6 } ]` : ipv4 ;
224
+ } else if ( hostname === "0.0.0.0" ) {
225
+ hostname = ipv4 ;
226
+ } else if ( hostname === "::" ) {
227
+ // In most operating systems, listening to the unspecified IPv6 address (::) may cause the net.Server to also listen on the unspecified IPv4 address (0.0.0.0).
228
+ hostname = ipv6 ? `[${ ipv6 } ]` : ipv4 ;
229
+ } else if ( hostname === "::1" ) {
228
230
hostname = "[::1]" ;
229
231
} else if ( hostname === "local-ip" || hostname === "local-ipv4" ) {
230
232
hostname = ipv4 ;
231
233
} else if ( hostname === "local-ipv6" ) {
232
- hostname = `[${ ipv6 } ]` ;
234
+ // For test env where network ipv6 doesn't work
235
+ hostname = ipv6 ? `[${ ipv6 } ]` : "[::1]" ;
233
236
}
234
237
235
238
await server . start ( ) ;
236
239
237
- expect ( server . server . address ( ) ) . toMatchObject ( getAddress ( host , hostname ) ) ;
240
+ expect ( server . server . address ( ) ) . toMatchObject (
241
+ await getAddress ( host , hostname ) ,
242
+ ) ;
238
243
239
244
const address = server . server . address ( ) ;
240
245
const { page, browser } = await runBrowser ( ) ;
0 commit comments