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

Add fileURLToPath #59

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

mischnic
Copy link

@mischnic mischnic commented Dec 7, 2021

This adds fileURLToPath.

In Node, this uses the current platform's file path format (windows vs posix). Since https://github.com/browserify/path-browserify also only implements posix, I think it makes sense to do the same here.

I've copied the implementations and the majority of the test cases from Node itself. I think this makes sense to ensure compatibility, but I'm not sure if this poses problems regarding the license

@ljharb
Copy link
Collaborator

ljharb commented Dec 8, 2021

This seems great. Can you confirm that this implementation/tests match every version of node that has this function?

@codecov-commenter
Copy link

codecov-commenter commented Dec 8, 2021

Codecov Report

Merging #59 (5a3f3e4) into master (b449442) will increase coverage by 0.16%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #59      +/-   ##
==========================================
+ Coverage   97.00%   97.16%   +0.16%     
==========================================
  Files           1        1              
  Lines         367      388      +21     
  Branches      133      140       +7     
==========================================
+ Hits          356      377      +21     
  Misses         11       11              
Impacted Files Coverage Δ
url.js 97.16% <100.00%> (+0.16%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b449442...5a3f3e4. Read the comment docs.

test/index.js Outdated
Comment on lines 2108 to 2109
var fromURL = url.fileURLToPath(new URL(fileURLToPathTestCase.fileURL));
assert.strictEqual(fromURL, fileURLToPathTestCase.path);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test case needs to be skipped when typeof URL !== 'function', for older nodes

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed it is; we can’t add anything that depends on URL without a full polyfill for it.

Copy link
Author

@mischnic mischnic Dec 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using url.parse instead might be problematic because that behaves differently to the WHATWG one

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed it to use url.parse instead now. Only protocol, pathname and hostname are used. So I hope his is fine

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately that particular polyfill doesn't work on old enough engines to be viable. We'd need to locate one that works down to node 0.4 and equivalent browsers.

Copy link
Author

@mischnic mischnic Dec 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! That implies it's possible. We'd need to build our own implementation tho, based on that one is fine, since that's too large a single package to depend on.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So even using require("core-js-pure/web/url") would be too large to install/load?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there's a ton of reasons I don't want that particular dependency.

test/index.js Outdated Show resolved Hide resolved
@mischnic
Copy link
Author

mischnic commented Dec 8, 2021

Can you confirm that this implementation/tests match every version of node that has this function?

The function was added in Node 10.12.0. All of the fileURLToPath-related tests I added pass with Node 10.24.1, 12.22.7, 14.18.2, 16.13.1, 17.2.0. (By changing the test script to var url = require('url'); and running it with that version)

@JumpLink

This comment was marked as outdated.

@ljharb

This comment was marked as resolved.

@starwiz-7
Copy link

I've been using a package which uses fileURLToPath internally for a bunch of things inside webpack 5 and using this package to polyfill url. I have been receving build errors for this.

Any chance we can merge this implementation?

@Cavdy
Copy link

Cavdy commented Nov 14, 2023

yeah I have same experience as @starwiz-7, would be great to have this merged

@cascornelissen
Copy link

I don't want to add to the pile of +1's, but we are in a similar situation. I'm willing to help out, is there anything we can do to help get this merged and released?

@niksy
Copy link

niksy commented May 4, 2024

If you’re looking for solution where you can alias url to userland implementation (e.g. bundler alias option), node-stdlib-browser (note: I’m the author of the module) adds additional exports to the ones provided by node-url.

@ljharb
Copy link
Collaborator

ljharb commented May 4, 2024

@niksy this package is that solution; if yours has functionality this one is missing, it’d be great to PR that in.

@niksy
Copy link

niksy commented May 4, 2024

Absolutely! I’ve done the changes from my side since at the time it was easier to just patch it, but I would definitely like it if it’s done upstream.

From what I can see in this PR, everything is implemented, but from the comments I see that major blocker is URL implementation that works all the way down to Node 0.4 and it isn’t big of a dependancy?

@ljharb
Copy link
Collaborator

ljharb commented May 4, 2024

Yep, that’s right.

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

Successfully merging this pull request may close these issues.

8 participants