unshiftio / url-parse

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

URLs with no hostname not parsed properly in v1.5.0 #205

Closed jonomuller closed 3 years ago

jonomuller commented 3 years ago

Hi there, I'm using this library to parse URLs with a custom protocol like so:

my-custom-protocol:///path-to/image.jpg

The behaviour has changed in v1.5.0 so that part of the pathname is being treated as the host. I believe this is a bug and goes against the browser implementation of URL, which produces a similar output similar to v1.4.7 with the hostname unset. I think it's due to the change in regex match here.

Output in v1.4.7:

{
  slashes: true,
  protocol: 'my-custom-protocol:',
  hash: '',
  query: '',
  pathname: '/path-to/image.jpg',
  auth: '',
  host: '',
  port: '',
  hostname: '',
  password: '',
  username: '',
  origin: 'null',
  href: 'my-custom-protocol:///path-to/image.jpg'
}

Output in v1.5.0:

{
  slashes: true,
  protocol: 'my-custom-protocol:',
  hash: '',
  query: '',
  pathname: '/image.jpg',
  auth: '',
  host: 'path-to',
  port: '',
  hostname: 'path-to',
  password: '',
  username: '',
  origin: 'my-custom-protocol://path-to',
  href: 'my-custom-protocol://path-to/image.jpg'
}

Let me know if you need any more details. Thanks!

lpinca commented 3 years ago

It seems similar to https://github.com/unshiftio/url-parse/issues/203. I think we should do something similar to https://github.com/unshiftio/url-parse/pull/204 for all non special schemes.