medialize / URI.js

Javascript URL mutation library
http://medialize.github.io/URI.js/
MIT License
6.26k stars 476 forks source link

Extra slash added to url on version 1.19.2 #390

Closed mansilladev closed 4 years ago

mansilladev commented 4 years ago

https://github.com/medialize/URI.js/commit/4ced30a1c5ad278eae70bb06dee8d7886e68052b

The commit above (handle relative paths) on version 1.19.2 causes an extra slash to be added.

var URI = require('urijs');
var url = new URI("atlassian.com");
url.protocol("https");
console.log(url.toString())

Output is:

https:///atlassian.com

mansilladev commented 4 years ago

Perhaps line 686 was meant to be:

if (parts.path.charAt(0) !== '/' && typeof parts.hostname === 'string' && requireAbsolutePath)
rodneyrehm commented 4 years ago

you're seeing the result of #387

because your url atlassian.com is missing a protocol, it's not parsed as a hostname, but a relative path.

var url = new URI("atlassian.com");
console.log(JSON.stringify(url._parts, null, 2))

outputs

{
  "protocol": null,
  "username": null,
  "password": null,
  "hostname": null,
  "urn": null,
  "port": null,
  "path": "atlassian.com",
  "query": null,
  "fragment": null,
  "preventInvalidHostname": false,
  "duplicateQueryParameters": false,
  "escapeQuerySpace": true
}

Maybe it's time to revisit #260 (and the unfinished attempt at providing a solution: #374) and provide a proper API for what you're trying to do… Are you looking to get your hands dirty with this?

mansilladev commented 4 years ago

Given that we consistently have just a raw hostname (no proto/scheme/port), perhaps it’s best to start with a blank object, and fill in the parts, eh?

Our library/app (atlassian-connect-express) has been using it the way I described for years, and with recent urijs updates, we started getting reports of the triple slash.

I’ll take a look at #374 and see how dirty I’d like to get my hands. But for the short term, I believe we’ve got a fix. Thanks for your help today, and for your continued support on this library.