Skip to content

Commit 78accc4

Browse files
authored
feat(dev): label network URLs with their interface name (#22830)
1 parent 05302b0 commit 78accc4

7 files changed

Lines changed: 201 additions & 22 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`printServerUrls > aligns interface names into a column 1`] = `
4+
"
5+
➜ Network: http://172.18.0.1:5173/ eth0
6+
➜ Network: http://10.0.0.2:5173/ wlan0
7+
"
8+
`;
9+
10+
exports[`printServerUrls > appends the network interface name to each network URL 1`] = `
11+
"
12+
➜ Local: http://localhost:5173/
13+
➜ Network: http://172.18.0.1:5173/ eth0
14+
➜ Network: http://10.0.0.2:5173/ wlan0
15+
"
16+
`;
17+
18+
exports[`printServerUrls > omits the annotation when the interface name is unknown 1`] = `
19+
"
20+
➜ Network: http://10.0.0.2:5173/
21+
"
22+
`;
23+
24+
exports[`printServerUrls > truncates an over-long interface name 1`] = `
25+
"
26+
➜ Network: http://10.0.0.2:5173/ vEthernet (WSL (Hyp…
27+
"
28+
`;
29+
30+
exports[`printServerUrls > works when networkInterfaceNames is absent 1`] = `
31+
"
32+
➜ Network: http://10.0.0.2:5173/
33+
"
34+
`;

‎packages/vite/src/node/__tests__/dev.spec.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ describe('the dev server', () => {
6969
expect(urls).toStrictEqual({
7070
local: ['http://localhost:5013/'],
7171
network: [],
72+
networkInterfaceNames: [],
7273
})
7374
})
7475
})
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { stripVTControlCharacters } from 'node:util'
2+
import { describe, expect, test } from 'vitest'
3+
import { printServerUrls } from '../logger'
4+
import type { ResolvedServerUrls } from '../server'
5+
6+
function collectServerUrls(urls: ResolvedServerUrls): string {
7+
const messages: string[] = []
8+
printServerUrls(urls, undefined, (msg) =>
9+
messages.push(stripVTControlCharacters(msg)),
10+
)
11+
return '\n' + messages.join('\n') + '\n'
12+
}
13+
14+
describe('printServerUrls', () => {
15+
test('appends the network interface name to each network URL', () => {
16+
const messages = collectServerUrls({
17+
local: ['http://localhost:5173/'],
18+
network: ['http://172.18.0.1:5173/', 'http://10.0.0.2:5173/'],
19+
networkInterfaceNames: ['eth0', 'wlan0'],
20+
})
21+
expect(messages).toMatchSnapshot()
22+
})
23+
24+
test('aligns interface names into a column', () => {
25+
const messages = collectServerUrls({
26+
local: [],
27+
network: ['http://172.18.0.1:5173/', 'http://10.0.0.2:5173/'],
28+
networkInterfaceNames: ['eth0', 'wlan0'],
29+
})
30+
expect(messages).toMatchSnapshot()
31+
})
32+
33+
test('truncates an over-long interface name', () => {
34+
const messages = collectServerUrls({
35+
local: [],
36+
network: ['http://10.0.0.2:5173/'],
37+
networkInterfaceNames: ['vEthernet (WSL (Hyper-V firewall))'],
38+
})
39+
expect(messages).toMatchSnapshot()
40+
})
41+
42+
test('omits the annotation when the interface name is unknown', () => {
43+
const messages = collectServerUrls({
44+
local: [],
45+
network: ['http://10.0.0.2:5173/'],
46+
networkInterfaceNames: [undefined],
47+
})
48+
expect(messages).toMatchSnapshot()
49+
})
50+
51+
test('works when networkInterfaceNames is absent', () => {
52+
const messages = collectServerUrls({
53+
local: [],
54+
network: ['http://10.0.0.2:5173/'],
55+
})
56+
expect(messages).toMatchSnapshot()
57+
})
58+
})

‎packages/vite/src/node/__tests__/utils.spec.ts‎

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import fs from 'node:fs'
22
import path from 'node:path'
33
import crypto from 'node:crypto'
4-
import { describe, expect, test } from 'vitest'
4+
import os, { type NetworkInterfaceInfoIPv4 } from 'node:os'
5+
import { describe, expect, test, vi, onTestFinished } from 'vitest'
56
import { fileURLToPath } from 'mlly'
67
import {
78
asyncFlatten,
@@ -1096,4 +1097,59 @@ describe('resolveServerUrls', () => {
10961097

10971098
expect(result.local).toContain('https://localhost:3000/')
10981099
})
1100+
1101+
test('captures interface names aligned with the network URLs', () => {
1102+
const mockServer = createMockServer('IPv4', '0.0.0.0')
1103+
const networkInterfacesSpy = vi
1104+
.spyOn(os, 'networkInterfaces')
1105+
.mockReturnValue({
1106+
lo: [
1107+
{ address: '127.0.0.1', family: 'IPv4' } as NetworkInterfaceInfoIPv4,
1108+
],
1109+
eth0: [
1110+
{
1111+
address: '192.168.1.10',
1112+
family: 'IPv4',
1113+
} as NetworkInterfaceInfoIPv4,
1114+
],
1115+
wlan0: [
1116+
{ address: '10.0.0.5', family: 'IPv4' } as NetworkInterfaceInfoIPv4,
1117+
],
1118+
})
1119+
onTestFinished(() => {
1120+
networkInterfacesSpy.mockRestore()
1121+
})
1122+
1123+
const result = resolveServerUrls(
1124+
mockServer,
1125+
{ https: false } as any,
1126+
{ host: undefined, name: 'localhost' } as any,
1127+
{},
1128+
{ rawBase: '/' } as any,
1129+
)
1130+
1131+
expect(result.network).toEqual([
1132+
'http://192.168.1.10:3000/',
1133+
'http://10.0.0.5:3000/',
1134+
])
1135+
expect(result.networkInterfaceNames).toStrictEqual(['eth0', 'wlan0'])
1136+
expect(result.networkInterfaceNames).toHaveLength(result.network.length)
1137+
// The loopback interface is reported as a local URL, not a network one.
1138+
expect(result.local).toContain('http://localhost:3000/')
1139+
})
1140+
1141+
test('uses an undefined interface name for an explicit network host', () => {
1142+
const mockServer = createMockServer('IPv4', '0.0.0.0')
1143+
1144+
const result = resolveServerUrls(
1145+
mockServer,
1146+
{ https: false } as any,
1147+
{ host: 'example.com', name: 'example.com' } as any,
1148+
{},
1149+
{ rawBase: '/' } as any,
1150+
)
1151+
1152+
expect(result.network).toStrictEqual(['http://example.com:3000/'])
1153+
expect(result.networkInterfaceNames).toStrictEqual([undefined])
1154+
})
10991155
})

‎packages/vite/src/node/logger.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ export function createLogger(
165165
return logger
166166
}
167167

168+
const maxNetworkInterfaceNameLength = 20
169+
168170
export function printServerUrls(
169171
urls: ResolvedServerUrls,
170172
optionsHost: string | boolean | undefined,
@@ -175,9 +177,25 @@ export function printServerUrls(
175177
for (const url of urls.local) {
176178
info(` ${colors.green('➜')} ${colors.bold('Local')}: ${colorUrl(url)}`)
177179
}
178-
for (const url of urls.network) {
179-
info(` ${colors.green('➜')} ${colors.bold('Network')}: ${colorUrl(url)}`)
180-
}
180+
const networkUrlMaxLength = Math.max(
181+
...urls.network.map((url) => url.length),
182+
0,
183+
)
184+
urls.network.forEach((url, index) => {
185+
const interfaceName = urls.networkInterfaceNames?.[index]
186+
let suffix = ''
187+
if (interfaceName) {
188+
const label =
189+
interfaceName.length > maxNetworkInterfaceNameLength
190+
? `${interfaceName.slice(0, maxNetworkInterfaceNameLength - 1)}…`
191+
: interfaceName
192+
suffix =
193+
' '.repeat(networkUrlMaxLength - url.length + 2) + colors.dim(label)
194+
}
195+
info(
196+
` ${colors.green('➜')} ${colors.bold('Network')}: ${colorUrl(url)}${suffix}`,
197+
)
198+
})
181199
if (urls.network.length === 0 && optionsHost === undefined) {
182200
info(
183201
colors.dim(` ${colors.green('➜')} ${colors.bold('Network')}: use `) +

‎packages/vite/src/node/server/index.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,13 @@ export interface ViteDevServer {
468468
export interface ResolvedServerUrls {
469469
local: string[]
470470
network: string[]
471+
/**
472+
* Names of the network interface that each {@link ResolvedServerUrls.network}
473+
* URL is bound to, in the same order as `network`. An entry is `undefined`
474+
* when the interface name is not known, for example when an explicit `host`
475+
* is set.
476+
*/
477+
networkInterfaceNames?: (string | undefined)[]
471478
}
472479

473480
export function createServer(

‎packages/vite/src/node/utils.ts‎

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,11 +1043,13 @@ export function resolveServerUrls(
10431043

10441044
const isAddressInfo = (x: any): x is AddressInfo => x?.address
10451045
if (!isAddressInfo(address)) {
1046-
return { local: [], network: [] }
1046+
return { local: [], network: [], networkInterfaceNames: [] }
10471047
}
10481048

10491049
const local: string[] = []
10501050
const network: string[] = []
1051+
// Interface name for each `network` URL, kept in the same order as `network`.
1052+
const networkInterfaceNames: (string | undefined)[] = []
10511053
const protocol = options.https ? 'https' : 'http'
10521054
const port = address.port
10531055
const base =
@@ -1064,24 +1066,27 @@ export function resolveServerUrls(
10641066
local.push(address)
10651067
} else {
10661068
network.push(address)
1069+
networkInterfaceNames.push(undefined)
10671070
}
10681071
} else {
1069-
Object.values(os.networkInterfaces())
1070-
.flatMap((nInterface) => nInterface ?? [])
1071-
.filter((detail) => detail.address && detail.family === 'IPv4')
1072-
.forEach((detail) => {
1073-
let host = detail.address.replace('127.0.0.1', hostname.name)
1074-
// ipv6 host
1075-
if (host.includes(':')) {
1076-
host = `[${host}]`
1077-
}
1078-
const url = `${protocol}://${host}:${port}${base}`
1079-
if (detail.address.includes('127.0.0.1')) {
1080-
local.push(url)
1081-
} else {
1082-
network.push(url)
1083-
}
1084-
})
1072+
Object.entries(os.networkInterfaces()).forEach(([name, nInterface]) => {
1073+
;(nInterface ?? [])
1074+
.filter((detail) => detail.address && detail.family === 'IPv4')
1075+
.forEach((detail) => {
1076+
let host = detail.address.replace('127.0.0.1', hostname.name)
1077+
// ipv6 host
1078+
if (host.includes(':')) {
1079+
host = `[${host}]`
1080+
}
1081+
const url = `${protocol}://${host}:${port}${base}`
1082+
if (detail.address.includes('127.0.0.1')) {
1083+
local.push(url)
1084+
} else {
1085+
network.push(url)
1086+
networkInterfaceNames.push(name)
1087+
}
1088+
})
1089+
})
10851090
}
10861091

10871092
const hostnamesFromCert = extractHostnamesFromCerts(httpsOptions?.cert)
@@ -1094,7 +1099,7 @@ export function resolveServerUrls(
10941099
)
10951100
}
10961101

1097-
return { local, network }
1102+
return { local, network, networkInterfaceNames }
10981103
}
10991104

11001105
export function extractHostnamesFromSubjectAltName(

0 commit comments

Comments
 (0)