Permalink
Browse files

fs: allow WHATWG URL and file: URLs as paths

Updates the fs module APIs to allow 'file://' URL objects
to be passed as the path.

For example:

```js
const URL = require('url').URL;
const myURL = new URL('file:///C:/path/to/file');
fs.readFile(myURL, (err, data) => {});
```

On Windows, file: URLs with a hostname convert to UNC paths,
while file: URLs with drive letters convert to local absolute
paths:

```
file://hostname/a/b/c => \\hostname\a\b\c
file:///c:/a/b/c => c:\a\b\c
```

On all other platforms, file: URLs with a hostname are unsupported
and will result in a throw:

```
file://hostname/a/b/c => throw!
file:///a/b/c => /a/b/c
```

The documentation for the fs API is intentionally not updated in
this commit because the URL API is still considered experimental
and is not officially documented *at this time*

Note that file: URLs are *required* by spec to always be absolute
paths from the file system root.

This is a semver-major commit because it changes error handling
on the fs APIs.

PR-URL: #10739
Ref: #10703
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
  • Loading branch information...
jasnell authored and italoacasas committed Jan 11, 2017
1 parent f096235 commit 6cf06cf5187b3eb9b9eaf0c2cbace346b695e2a4
Showing with 283 additions and 3 deletions.
  1. +89 −3 lib/fs.js
  2. +64 −0 lib/internal/url.js
  3. +60 −0 test/parallel/test-fs-null-bytes.js
  4. +70 −0 test/parallel/test-fs-whatwg-url.js
Oops, something went wrong.

0 comments on commit 6cf06cf

Please sign in to comment.