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

Code before:
    }
  } 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(':')) {
          host = `[${host}]`
        }
        const url = `${protocol}://${host}:${port}${base}`
        if (detail.address.includes('127.0.0.1')) {
          local.push(url)
        } else {
          network.push(url)
        }
      })
  }

  const certs = httpsOptions?.cert ? arraify(httpsOptions.cert) : []
  const hostnameFromCert = certs
    .map((cert) => {
      try {
        const buf = bufferify(cert)
        return new crypto.X509Certificate(buf)
      } catch {
        return null
      }
    })
    .flatMap((cert) =>
      cert && cert.subjectAltName
        ? extractHostnamesFromSubjectAltName(cert.subjectAltName)
        : [],
    )

  if (hostnameFromCert.length > 0) {
    const existings = new Set([...local, ...network])
    local.push(
      ...hostnameFromCert
        .map((hostname) => `${protocol}://${hostname}:${port}${base}`)
        .filter((url) => !existings.has(url)),
    )
  }

  return { local, network }
}

export function extractHostnamesFromSubjectAltName(
  subjectAltName: string,
): string[] {

Review comment (suggestion):
```suggestion
        return new crypto.X509Certificate(cert)
```
I think this conversion is handled internally by `X509Certificate`.


Code after:

  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(':')) {
          host = `[${host}]`
        }
        const url = `${protocol}://${host}:${port}${base}`
        if (detail.address.includes('127.0.0.1')) {
          local.push(url)
        } else {
          network.push(url)
        }
      })
  }

  const hostnamesFromCert = extractHostnamesFromCerts(httpsOptions?.cert)
  if (hostnamesFromCert.length > 0) {
    const existings = new Set([...local, ...network])
    local.push(
      ...hostnamesFromCert
        .map((hostname) => `${protocol}://${hostname}:${port}${base}`)
        .filter((url) => !existings.has(url)),
    )
  }

  return { local, network }
}