function normalizeUrl(u) {
  if (!(u instanceof URL)) {
    if (u instanceof LegacyUrl) {
      return normalizeUrl(new URL(url.format(u)))
    }
    // If the url is invalid, let the URL library report it
    return normalizeUrl(new URL(u))
  }

  if (!/https?:/.test(u.protocol)) {
    throw new TypeError(
      `Protocol '${u.protocol}' not recognized. This commonly occurs when a hostname and port are included without a protocol, producing a URL that is valid but confusing, and probably not what you want.`,
    )
  }

  return {
    href: u.href,
    origin: u.origin,
    protocol: u.protocol,
    username: u.username,
    password: u.password,
    host: u.host,
    hostname:
      // strip brackets from IPv6
      typeof u.hostname === 'string' && u.hostname.startsWith('[')
        ? u.hostname.slice(1, -1)
        : u.hostname,
    port: u.port || (u.protocol === 'http:' ? 80 : 443),
    pathname: u.pathname,
    search: u.search,
    searchParams: u.searchParams,
    hash: u.hash,
  }
}
Normalizes the passed url for consistent internal processing
@param {string|LegacyUrl|URL} u