chore: format
This commit is contained in:
parent
0093d426c6
commit
70fd552e88
2 changed files with 31 additions and 34 deletions
|
@ -5,43 +5,41 @@ interface NetworkAddressOpt {
|
||||||
}
|
}
|
||||||
|
|
||||||
const wildcardHosts = new Set([
|
const wildcardHosts = new Set([
|
||||||
'0.0.0.0',
|
'0.0.0.0',
|
||||||
'::',
|
'::',
|
||||||
'0000:0000:0000:0000:0000:0000:0000:0000',
|
'0000:0000:0000:0000:0000:0000:0000:0000',
|
||||||
])
|
])
|
||||||
type Protocol = 'http' | 'https'
|
type Protocol = 'http' | 'https'
|
||||||
|
|
||||||
// this code from vite https://github.com/vitejs/vite/blob/d09bbd093a4b893e78f0bbff5b17c7cf7821f403/packages/vite/src/node/utils.ts#L892-L914
|
// this code from vite https://github.com/vitejs/vite/blob/d09bbd093a4b893e78f0bbff5b17c7cf7821f403/packages/vite/src/node/utils.ts#L892-L914
|
||||||
export function getNetworkAddress(protocol: Protocol = 'http', hostname: string | undefined, port:number, base?:string) {
|
export function getNetworkAddress(protocol: Protocol = 'http', hostname: string | undefined, port: number, base?: string) {
|
||||||
|
const NetworkAddress: NetworkAddressOpt = {
|
||||||
const NetworkAddress:NetworkAddressOpt = {
|
|
||||||
local: [],
|
local: [],
|
||||||
network: []
|
network: []
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.values(os.networkInterfaces())
|
Object.values(os.networkInterfaces())
|
||||||
.flatMap((nInterface) => nInterface ?? [])
|
.flatMap((nInterface) => nInterface ?? [])
|
||||||
.filter(
|
.filter(
|
||||||
(detail) =>
|
(detail) =>
|
||||||
detail &&
|
detail &&
|
||||||
detail.address &&
|
detail.address &&
|
||||||
(detail.family === 'IPv4' ||
|
(detail.family === 'IPv4' ||
|
||||||
// @ts-expect-error Node 18.0 - 18.3 returns number
|
// @ts-expect-error Node 18.0 - 18.3 returns number
|
||||||
detail.family === 4),
|
detail.family === 4),
|
||||||
)
|
)
|
||||||
.forEach((detail) => {
|
.forEach((detail) => {
|
||||||
let host = detail.address.replace('127.0.0.1', hostname === undefined || wildcardHosts.has(hostname) ? 'localhost' : hostname)
|
let host = detail.address.replace('127.0.0.1', hostname === undefined || wildcardHosts.has(hostname) ? 'localhost' : hostname)
|
||||||
// ipv6 host
|
// ipv6 host
|
||||||
if (host.includes(':')) {
|
if (host.includes(':')) {
|
||||||
host = `[${host}]`
|
host = `[${host}]`
|
||||||
}
|
}
|
||||||
const url = `${protocol}://${host}:${port}${base ? base : ''}`
|
const url = `${protocol}://${host}:${port}${base ? base : ''}`
|
||||||
if (detail.address.includes('127.0.0.1')) {
|
if (detail.address.includes('127.0.0.1')) {
|
||||||
NetworkAddress.local.push(url)
|
NetworkAddress.local.push(url)
|
||||||
} else {
|
} else {
|
||||||
NetworkAddress.network.push(url)
|
NetworkAddress.network.push(url)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return NetworkAddress
|
return NetworkAddress
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -54,16 +54,15 @@ export default function startServer(app: NodeApp, options: Options) {
|
||||||
},
|
},
|
||||||
handler
|
handler
|
||||||
);
|
);
|
||||||
|
|
||||||
const protocol = server.server instanceof https.Server ? 'https' : 'http';
|
const protocol = server.server instanceof https.Server ? 'https' : 'http';
|
||||||
const address = getNetworkAddress(protocol, host!, port)
|
const address = getNetworkAddress(protocol, host!, port)
|
||||||
|
|
||||||
if(host === undefined ){
|
if (host === undefined) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log(
|
console.log(
|
||||||
`Preview server listening on \n local: ${address.local[0]} \t\n network: ${address.network[0]}
|
`Preview server listening on \n local: ${address.local[0]} \t\n network: ${address.network[0]}\n`);
|
||||||
` );
|
} else {
|
||||||
}else{
|
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log(`Preview server listening on ${address.local[0]}`);
|
console.log(`Preview server listening on ${address.local[0]}`);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue