unshiftio / url-parse

Small footprint URL parser that works seamlessly across Node.js and browser environments.
http://unshift.io
MIT License
1.03k stars 103 forks source link

Fix host parsing for file URLs #210

Closed lpinca closed 3 years ago

lpinca commented 3 years ago

Fixes: https://github.com/unshiftio/url-parse/pull/209#issuecomment-886235848

lpinca commented 3 years ago

Anyway url-parse does not do any validation:

> parse('file://host:123/foo/bar/baz')
{
  slashes: true,
  protocol: 'file:',
  hash: '',
  query: '',
  pathname: '/foo/bar/baz',
  auth: '',
  host: 'host',
  port: '',
  hostname: 'host',
  password: '',
  username: '',
  origin: 'null',
  href: 'file://host/foo/bar/baz'
}

The Node.js WHATWG URL parser throws an error for the same URL:

> new URL('file://host:123/foo/bar/baz')
Uncaught TypeError [ERR_INVALID_URL]: Invalid URL
    at __node_internal_captureLargerStackTrace (node:internal/errors:463:5)
    at new NodeError (node:internal/errors:370:5)
    at onParseError (node:internal/url:536:9)
    at new URL (node:internal/url:612:5) {
  input: 'file://host:123/foo/bar/baz',
  code: 'ERR_INVALID_URL'