The whatwg-url npm package and URL class in Chrome/Safari perform the following with file:// URLs:
const url = new URL('file:///private/abc/music.aac');
console.log(url.origin); // -> 'file://'
However, the built-in 'url' module and Firefox perform the following:
const url = new URL('file:///private/abc/music.aac');
console.log(url.origin); // -> null
There is an inconsistency here. I had a look at the internal url.js and I'm not sure if the specification allows an opaque origin in this situation. I'd thought I report in case someone more familiar with the specification can determine if the inconsistency is allowed by the specification.
/cc @nodejs/url
This is unfortunately one of the unspecified areas in the URL Standard. See https://url.spec.whatwg.org/#concept-url-origin:
A URL鈥檚 origin is the origin returned by running these steps, switching on URL鈥檚 scheme:
- "
file"- Unfortunate as it is, this is left as an exercise to the reader. When in doubt, return a new opaque origin.
We followed the "when in doubt" instruction, but it did come as a surprise to me to find that whatwg-url doesn't.
Firefox also returns null. Safari and Chrome both return "file://". Ugh.
Yup, seems like NodeJS is more inline with the specification. Is it worth moving upstream to Safari, Chrome, WHATWG URL to see what they think? I imagine there would be resistance to change as it might break backwards compatibility and, as it reads, they are compliant. I feel like the spec should really be tightened up there and just make everything return a null; you could return anything you like as a result of the exercise.
Have updated the OP with the new information about the inconsistencies.
Relevant issues:
I don't think getting browsers to align with each other is within the scope of this project. I am pretty certain what we do is a fair behavior, and I'm going to file an issue at whatwg-url encouraging them to do the same as we do (pretty sure they'd agree), and then close this issue.
How's that sound?
Yup, totally on-board with that.
Most helpful comment
I don't think getting browsers to align with each other is within the scope of this project. I am pretty certain what we do is a fair behavior, and I'm going to file an issue at whatwg-url encouraging them to do the same as we do (pretty sure they'd agree), and then close this issue.
How's that sound?