File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ } )
Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ import { renderDockImportsMap } from './plugins/server'
2323import { createDevToolsMiddleware } from './server'
2424import { startStandaloneDevTools } from './standalone'
2525import { collectStaticRpcDump } from './static-dump'
26+ import { normalizeHttpServerUrl } from './utils'
2627
2728export 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
Original file line number Diff line number Diff line change 1+ import { isIP } from 'node:net'
2+
13export 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+ }
You can’t perform that action at this time.
0 commit comments