Skip to content

Commit 83e09b5

Browse files
authored
fix(core): support IPv6 in devtools server link (#204)
1 parent 9840419 commit 83e09b5

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { normalizeHttpServerUrl } from '../utils'
3+
4+
describe('normalizeHttpServerUrl', () => {
5+
it('formats ipv4 localhost as localhost', () => {
6+
expect(normalizeHttpServerUrl('127.0.0.1', 9999)).toBe('http://localhost:9999')
7+
})
8+
9+
it('wraps ipv6 hosts in brackets', () => {
10+
expect(normalizeHttpServerUrl('::1', 9999)).toBe('http://[::1]:9999')
11+
})
12+
13+
it('preserves non-ip hosts', () => {
14+
expect(normalizeHttpServerUrl('localhost', 9999)).toBe('http://localhost:9999')
15+
})
16+
})

packages/core/src/node/cli-commands.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { renderDockImportsMap } from './plugins/server'
2323
import { createDevToolsMiddleware } from './server'
2424
import { startStandaloneDevTools } from './standalone'
2525
import { collectStaticRpcDump } from './static-dump'
26+
import { normalizeHttpServerUrl } from './utils'
2627

2728
export interface StartOptions {
2829
root?: string
@@ -68,9 +69,10 @@ export async function start(options: StartOptions) {
6869
const server = createServer(toNodeListener(app))
6970

7071
server.listen(port, host, async () => {
71-
console.log(c.green`${MARK_NODE} Vite DevTools started at`, c.green(`http://${host === '127.0.0.1' ? 'localhost' : host}:${port}`), '\n')
72+
const url = normalizeHttpServerUrl(host, port)
73+
console.log(c.green`${MARK_NODE} Vite DevTools started at`, c.green(url), '\n')
7274
if (options.open)
73-
await open(`http://${host === '127.0.0.1' ? 'localhost' : host}:${port}`)
75+
await open(url)
7476
})
7577
}
7678

packages/core/src/node/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
import { isIP } from 'node:net'
2+
13
export function isObject(value: unknown): value is Record<string, any> {
24
return Object.prototype.toString.call(value) === '[object Object]'
35
}
6+
7+
export function normalizeHttpServerUrl(host: string, port: number | string): string {
8+
const normalizedHost
9+
= host === '127.0.0.1'
10+
? 'localhost'
11+
: isIP(host) === 6
12+
? `[${host}]`
13+
: host
14+
15+
return `http://${normalizedHost}:${port}`
16+
}

0 commit comments

Comments
 (0)