diff options
author | James M Snell <jasnell@gmail.com> | 2017-01-11 07:02:40 -0800 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2017-02-03 13:59:06 -0800 |
commit | 79400bfbfdea32e861a30cadeb2f7363e99bcaca (patch) | |
tree | 29e5ecdf28ffcca63eb6571eb8912e2d8f6eced9 /test/parallel/test-fs-null-bytes.js | |
parent | 0792d452e86e88e44dc77d983e9daad50fb2bbd0 (diff) | |
download | android-node-v8-79400bfbfdea32e861a30cadeb2f7363e99bcaca.tar.gz android-node-v8-79400bfbfdea32e861a30cadeb2f7363e99bcaca.tar.bz2 android-node-v8-79400bfbfdea32e861a30cadeb2f7363e99bcaca.zip |
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: https://github.com/nodejs/node/pull/10739
Ref: https://github.com/nodejs/node/issues/10703
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Diffstat (limited to 'test/parallel/test-fs-null-bytes.js')
-rw-r--r-- | test/parallel/test-fs-null-bytes.js | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/test/parallel/test-fs-null-bytes.js b/test/parallel/test-fs-null-bytes.js index fa29e64c9b..39768489fb 100644 --- a/test/parallel/test-fs-null-bytes.js +++ b/test/parallel/test-fs-null-bytes.js @@ -2,6 +2,7 @@ const common = require('../common'); const assert = require('assert'); const fs = require('fs'); +const URL = require('url').URL; function check(async, sync) { const expected = /Path must be a string without null bytes/; @@ -48,6 +49,62 @@ check(null, fs.watch, 'foo\u0000bar', common.fail); check(null, fs.watchFile, 'foo\u0000bar', common.fail); check(fs.writeFile, fs.writeFileSync, 'foo\u0000bar'); +check(fs.access, fs.accessSync, new URL('file:///foo\u0000bar')); +check(fs.access, fs.accessSync, new URL('file:///foo\u0000bar'), fs.F_OK); +check(fs.appendFile, fs.appendFileSync, new URL('file:///foo\u0000bar')); +check(fs.chmod, fs.chmodSync, new URL('file:///foo\u0000bar'), '0644'); +check(fs.chown, fs.chownSync, new URL('file:///foo\u0000bar'), 12, 34); +check(fs.link, fs.linkSync, new URL('file:///foo\u0000bar'), 'foobar'); +check(fs.link, fs.linkSync, 'foobar', new URL('file:///foo\u0000bar')); +check(fs.lstat, fs.lstatSync, new URL('file:///foo\u0000bar')); +check(fs.mkdir, fs.mkdirSync, new URL('file:///foo\u0000bar'), '0755'); +check(fs.open, fs.openSync, new URL('file:///foo\u0000bar'), 'r'); +check(fs.readFile, fs.readFileSync, new URL('file:///foo\u0000bar')); +check(fs.readdir, fs.readdirSync, new URL('file:///foo\u0000bar')); +check(fs.readlink, fs.readlinkSync, new URL('file:///foo\u0000bar')); +check(fs.realpath, fs.realpathSync, new URL('file:///foo\u0000bar')); +check(fs.rename, fs.renameSync, new URL('file:///foo\u0000bar'), 'foobar'); +check(fs.rename, fs.renameSync, 'foobar', new URL('file:///foo\u0000bar')); +check(fs.rmdir, fs.rmdirSync, new URL('file:///foo\u0000bar')); +check(fs.stat, fs.statSync, new URL('file:///foo\u0000bar')); +check(fs.symlink, fs.symlinkSync, new URL('file:///foo\u0000bar'), 'foobar'); +check(fs.symlink, fs.symlinkSync, 'foobar', new URL('file:///foo\u0000bar')); +check(fs.truncate, fs.truncateSync, new URL('file:///foo\u0000bar')); +check(fs.unlink, fs.unlinkSync, new URL('file:///foo\u0000bar')); +check(null, fs.unwatchFile, new URL('file:///foo\u0000bar'), common.fail); +check(fs.utimes, fs.utimesSync, new URL('file:///foo\u0000bar'), 0, 0); +check(null, fs.watch, new URL('file:///foo\u0000bar'), common.fail); +check(null, fs.watchFile, new URL('file:///foo\u0000bar'), common.fail); +check(fs.writeFile, fs.writeFileSync, new URL('file:///foo\u0000bar')); + +check(fs.access, fs.accessSync, new URL('file:///foo%00bar')); +check(fs.access, fs.accessSync, new URL('file:///foo%00bar'), fs.F_OK); +check(fs.appendFile, fs.appendFileSync, new URL('file:///foo%00bar')); +check(fs.chmod, fs.chmodSync, new URL('file:///foo%00bar'), '0644'); +check(fs.chown, fs.chownSync, new URL('file:///foo%00bar'), 12, 34); +check(fs.link, fs.linkSync, new URL('file:///foo%00bar'), 'foobar'); +check(fs.link, fs.linkSync, 'foobar', new URL('file:///foo%00bar')); +check(fs.lstat, fs.lstatSync, new URL('file:///foo%00bar')); +check(fs.mkdir, fs.mkdirSync, new URL('file:///foo%00bar'), '0755'); +check(fs.open, fs.openSync, new URL('file:///foo%00bar'), 'r'); +check(fs.readFile, fs.readFileSync, new URL('file:///foo%00bar')); +check(fs.readdir, fs.readdirSync, new URL('file:///foo%00bar')); +check(fs.readlink, fs.readlinkSync, new URL('file:///foo%00bar')); +check(fs.realpath, fs.realpathSync, new URL('file:///foo%00bar')); +check(fs.rename, fs.renameSync, new URL('file:///foo%00bar'), 'foobar'); +check(fs.rename, fs.renameSync, 'foobar', new URL('file:///foo%00bar')); +check(fs.rmdir, fs.rmdirSync, new URL('file:///foo%00bar')); +check(fs.stat, fs.statSync, new URL('file:///foo%00bar')); +check(fs.symlink, fs.symlinkSync, new URL('file:///foo%00bar'), 'foobar'); +check(fs.symlink, fs.symlinkSync, 'foobar', new URL('file:///foo%00bar')); +check(fs.truncate, fs.truncateSync, new URL('file:///foo%00bar')); +check(fs.unlink, fs.unlinkSync, new URL('file:///foo%00bar')); +check(null, fs.unwatchFile, new URL('file:///foo%00bar'), common.fail); +check(fs.utimes, fs.utimesSync, new URL('file:///foo%00bar'), 0, 0); +check(null, fs.watch, new URL('file:///foo%00bar'), common.fail); +check(null, fs.watchFile, new URL('file:///foo%00bar'), common.fail); +check(fs.writeFile, fs.writeFileSync, new URL('file:///foo%00bar')); + // an 'error' for exists means that it doesn't exist. // one of many reasons why this file is the absolute worst. fs.exists('foo\u0000bar', common.mustCall((exists) => { |