Skip to content

fs: allow WHATWG URL and file: URLs as paths

Rodrigo Muino Tomonari requested to merge github/fork/jasnell/url-path into master

Updates the fs module APIs to allow 'file://' URL strings and WHATWG URL objects using 'file:' protocol to be passed as the path.

For example:

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

// or
// EDIT(addaleax): this was removed from this PR
fs.readFile('file:///C:/path/to/file', (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.

Refs: https://github.com/nodejs/node/issues/10703

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

fs, url

Merge request reports

Loading