Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

url: inconsistence with docs on slashed protocols #4097

Closed
claudiorodriguez opened this issue Dec 1, 2015 · 1 comment
Closed

url: inconsistence with docs on slashed protocols #4097

claudiorodriguez opened this issue Dec 1, 2015 · 1 comment

Comments

@claudiorodriguez
Copy link
Contributor

In the docs, at https://nodejs.org/api/url.html#url_url_format_urlobj it says:

"protocol" is treated the same with or without the trailing : (colon).
The protocols http, https, ftp, gopher, file will be postfixed with :// (colon-slash-slash).
All other protocols mailto, xmpp, aim, sftp, foo, etc will be postfixed with : (colon).
"slashes" set to true if the protocol requires :// (colon-slash-slash)
Only needs to be set for protocols not previously listed as requiring slashes, such as mongodb://localhost:8000/.

This is not how the code works. If you look at https://github.com/nodejs/node/blob/master/lib/url.js you see:

  // only the slashedProtocols get the //.  Not mailto:, xmpp:, etc.
  // unless they had them to begin with.
  if (this.slashes ||
      (!protocol || slashedProtocol[protocol]) && host !== false) {
    host = '//' + (host || '');
    if (pathname && pathname.charAt(0) !== '/') pathname = '/' + pathname;
  } else if (!host) {
    host = '';
  }

So the documented behaviour only happens when host/hostname are set.
You can check this with the following snippet:

var url = require('url');

var unslashedUri = url.parse('/no-slashes');
var slashedUriByHost = url.parse('/slashes-host');
var slashedUriForced = url.parse('/slashes-forced');

unslashedUri.protocol = 'file';
console.log(url.format(unslashedUri)); // file:/no-slashes

slashedUriByHost.protocol = 'file';
slashedUriByHost.host = 'localhost';
console.log(url.format(slashedUriByHost)); // file://localhost/slashes-host

slashedUriForced.protocol = 'file';
slashedUriForced.slashes = true
console.log(url.format(slashedUriForced)); // file:///slashes-forced

I can make a PR correcting either the docs, or the url module, but which one is the intended behaviour?

@claudiorodriguez
Copy link
Contributor Author

Duplicate of #3361 actually

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant