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

Support url *strings* in fs APIs #17658

Closed
jkrems opened this issue Dec 13, 2017 · 9 comments
Closed

Support url *strings* in fs APIs #17658

jkrems opened this issue Dec 13, 2017 · 9 comments
Labels
feature request Issues that request new features to be added to Node.js. fs Issues and PRs related to the fs subsystem / file system. whatwg-url Issues and PRs related to the WHATWG URL implementation.

Comments

@jkrems
Copy link
Contributor

jkrems commented Dec 13, 2017

  • Version: 7.6.0+
  • Platform: all
  • Subsystem: fs

Right now the FS APIs do support URLs - but only if they are passed as URL objects. This seems to go against the use of URLs in DOM APIs where methods tend to accept strings. E.g. fetch only "accepts" URL objects because they happen to stringify to their .href property.

Would it be acceptable to extend the current support for file URLs to "any string starting with file:// will be treated like a URL object"? This would remove the need for people to manually create import and then instantiate a URL object if they already have a valid file URL (e.g. via import.meta.url).

@addaleax addaleax added feature request Issues that request new features to be added to Node.js. fs Issues and PRs related to the fs subsystem / file system. whatwg-url Issues and PRs related to the WHATWG URL implementation. labels Dec 13, 2017
@TimothyGu
Copy link
Member

I'm fine with it if it's shown that performance of fs APIs given regular path strings does not decrease because of this. Checking for a URL object is much simpler than checking for the first 7 characters, though I agree that having to reparse URLs is a hassle. I also understand that accepting URL objects is not recommended by the URL Standard in the first place.

@addaleax
Copy link
Member

We discussed this in the original PR a bit (at first, it contained support for that): #10739 (comment)

@jkrems
Copy link
Contributor Author

jkrems commented Dec 13, 2017 via email

@jasnell
Copy link
Member

jasnell commented Dec 13, 2017

There are a couple of issues here:

  1. Allowing URL strings means we would have to parse all string inputs through new URL(), which is definite performance hit.
  2. It also means that we risk breaking existing cases where file names have a : in them

At this point I'm -1 on modifying the existing APIs further to support this case, tho I would not be opposed to alternate methods that support it. e.g., instead of fs.readFile('file://whatever'), something like a fs.ureadFile('file://whatever'). This would follow the pattern established for methods that accept `fd's rather than paths, so there is precedent.

@jkrems
Copy link
Contributor Author

jkrems commented Dec 13, 2017

Interesting. I agree that continuously overloading the meaning of the existing APIs isn't great. Originally I had something like "only if the string starts exactly with 'file://'" which might address the performance or existing filename concerns. Still, leaves the security concerns and a potentially confusing API ("here's the 20 different things that might be passed in as a filename").

I could see fs.u* or a similar convention for URL-only file system methods. Super tempted to suggest making them async/promise-returning from the start. Not sure if that would make it a lot harder to add.

@jasnell
Copy link
Member

jasnell commented Dec 13, 2017

I've already got an effort under way to work up some Promise-based fs APIs. The PR has been sitting there for a bit without updating, but it will be advancing. There are definitely going to be some differences between it and the current API so I'm weighing just how much different it should be.

@jkrems
Copy link
Contributor Author

jkrems commented Dec 13, 2017

Thanks, I'll have a look at #15485!

@pauldraper
Copy link

The real issue IMO, is that this is simply incorrect.

Files may contain : on many file systems, and repeated slashes are permitted in paths.

If it's a string, it should be treated as a path. If it's a URL, it should be treated as a URL.

@TimothyGu
Copy link
Member

@pauldraper's comment is a dealbreaker for me:

$ mkdir -p file\:/abc
$ echo 'hello world' > file\:/abc/abc
$ node
> fs.readFileSync('file:///abc/abc', 'utf8')
'hello world\n'

I'm going to close this as something that can't reasonably be accomplished.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. fs Issues and PRs related to the fs subsystem / file system. whatwg-url Issues and PRs related to the WHATWG URL implementation.
Projects
None yet
Development

No branches or pull requests

5 participants