Repository: vitejs/vite
File: packages/vite/src/node/utils.ts (TypeScript)

Code before:
    // See #8647 for more details.
    const localhostAddr = await getLocalhostAddressIfDiffersFromDNS()
    if (localhostAddr) {
      name = localhostAddr
    }
  }

  return { host, name }
}

export function resolveServerUrls(
  server: Server,
  options: CommonServerOptions,
  hostname: Hostname,
  httpsOptions: HttpsServerOptions | undefined,
  config: ResolvedConfig,
): ResolvedServerUrls {
  const address = server.address()

  const isAddressInfo = (x: any): x is AddressInfo => x?.address
  if (!isAddressInfo(address)) {
    return { local: [], network: [] }
  }

  const local: string[] = []
  const network: string[] = []
  const protocol = options.https ? 'https' : 'http'
  const port = address.port
  const base =
    config.rawBase === './' || config.rawBase === '' ? '/' : config.rawBase

  if (hostname.host !== undefined && !wildcardHosts.has(hostname.host)) {
    let hostnameName = hostname.name
    // ipv6 host
    if (hostnameName.includes(':')) {
      hostnameName = `[${hostnameName}]`
    }
    const address = `${protocol}://${hostnameName}:${port}${base}`
    if (loopbackHosts.has(hostname.host)) {
      local.push(address)
    } else {
      network.push(address)
    }
  } else {
    Object.values(os.networkInterfaces())
      .flatMap((nInterface) => nInterface ?? [])
      .filter((detail) => detail.address && detail.family === 'IPv4')
      .forEach((detail) => {
        let host = detail.address.replace('127.0.0.1', hostname.name)
        // ipv6 host
        if (host.includes(':')) {

Review comment (suggestion):
For whatever reason this took me a minute to parse.

Have you considered moving from the ternary `.filter((cert) => Boolean(cert?.subjectAltName))`?

Quick test to show that should cover all the cases:
```
let c = [{subjectAltName: 'asdf'}, {}, null, {subjectAltName: null}, {subjectAltName: ''}];
c.filter((cert) => Boolean(cert?.subjectAltName)).length === 1
```

Code after:
    // See #8647 for more details.
    const localhostAddr = await getLocalhostAddressIfDiffersFromDNS()
    if (localhostAddr) {
      name = localhostAddr
    }
  }

  return { host, name }
}

export function extractHostnamesFromCerts(
  certs: HttpsServerOptions['cert'] | undefined,
): string[] {
  const certList = certs ? arraify(certs) : []
  if (certList.length === 0) return []

  const hostnames = certList
    .map((cert) => {
      try {
        return new crypto.X509Certificate(cert)
      } catch {
        return null
      }
    })
    .flatMap((cert) =>
      cert?.subjectAltName
        ? extractHostnamesFromSubjectAltName(cert.subjectAltName)
        : [],
    )

  return unique(hostnames)
}

export function resolveServerUrls(
  server: Server,
  options: CommonServerOptions,
  hostname: Hostname,
  httpsOptions: HttpsServerOptions | undefined,
  config: ResolvedConfig,
): ResolvedServerUrls {
  const address = server.address()

  const isAddressInfo = (x: any): x is AddressInfo => x?.address
  if (!isAddressInfo(address)) {
    return { local: [], network: [] }
  }

  const local: string[] = []
  const network: string[] = []
  const protocol = options.https ? 'https' : 'http'
  const port = address.port