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-06 11:03:37 -0800 |
commit | 9549329158ecde9653892161cc1890ac8c4e83c8 (patch) | |
tree | e66bc39fd63705741d3d76c570500c9779e53b5a /test/parallel/test-fs-null-bytes.js | |
parent | 3fffebbde3221323d45f72a22a936120fe43de58 (diff) | |
download | android-node-v8-9549329158ecde9653892161cc1890ac8c4e83c8.tar.gz android-node-v8-9549329158ecde9653892161cc1890ac8c4e83c8.tar.bz2 android-node-v8-9549329158ecde9653892161cc1890ac8c4e83c8.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 | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/test/parallel/test-fs-null-bytes.js b/test/parallel/test-fs-null-bytes.js index fa29e64c9b..6bfa825d2f 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,65 @@ check(null, fs.watch, 'foo\u0000bar', common.fail); check(null, fs.watchFile, 'foo\u0000bar', common.fail); check(fs.writeFile, fs.writeFileSync, 'foo\u0000bar'); +const fileUrl = new URL('file:///C:/foo\u0000bar'); +const fileUrl2 = new URL('file:///C:/foo%00bar'); + +check(fs.access, fs.accessSync, fileUrl); +check(fs.access, fs.accessSync, fileUrl, fs.F_OK); +check(fs.appendFile, fs.appendFileSync, fileUrl); +check(fs.chmod, fs.chmodSync, fileUrl, '0644'); +check(fs.chown, fs.chownSync, fileUrl, 12, 34); +check(fs.link, fs.linkSync, fileUrl, 'foobar'); +check(fs.link, fs.linkSync, 'foobar', fileUrl); +check(fs.lstat, fs.lstatSync, fileUrl); +check(fs.mkdir, fs.mkdirSync, fileUrl, '0755'); +check(fs.open, fs.openSync, fileUrl, 'r'); +check(fs.readFile, fs.readFileSync, fileUrl); +check(fs.readdir, fs.readdirSync, fileUrl); +check(fs.readlink, fs.readlinkSync, fileUrl); +check(fs.realpath, fs.realpathSync, fileUrl); +check(fs.rename, fs.renameSync, fileUrl, 'foobar'); +check(fs.rename, fs.renameSync, 'foobar', fileUrl); +check(fs.rmdir, fs.rmdirSync, fileUrl); +check(fs.stat, fs.statSync, fileUrl); +check(fs.symlink, fs.symlinkSync, fileUrl, 'foobar'); +check(fs.symlink, fs.symlinkSync, 'foobar', fileUrl); +check(fs.truncate, fs.truncateSync, fileUrl); +check(fs.unlink, fs.unlinkSync, fileUrl); +check(null, fs.unwatchFile, fileUrl, common.fail); +check(fs.utimes, fs.utimesSync, fileUrl, 0, 0); +check(null, fs.watch, fileUrl, common.fail); +check(null, fs.watchFile, fileUrl, common.fail); +check(fs.writeFile, fs.writeFileSync, fileUrl); + +check(fs.access, fs.accessSync, fileUrl2); +check(fs.access, fs.accessSync, fileUrl2, fs.F_OK); +check(fs.appendFile, fs.appendFileSync, fileUrl2); +check(fs.chmod, fs.chmodSync, fileUrl2, '0644'); +check(fs.chown, fs.chownSync, fileUrl2, 12, 34); +check(fs.link, fs.linkSync, fileUrl2, 'foobar'); +check(fs.link, fs.linkSync, 'foobar', fileUrl2); +check(fs.lstat, fs.lstatSync, fileUrl2); +check(fs.mkdir, fs.mkdirSync, fileUrl2, '0755'); +check(fs.open, fs.openSync, fileUrl2, 'r'); +check(fs.readFile, fs.readFileSync, fileUrl2); +check(fs.readdir, fs.readdirSync, fileUrl2); +check(fs.readlink, fs.readlinkSync, fileUrl2); +check(fs.realpath, fs.realpathSync, fileUrl2); +check(fs.rename, fs.renameSync, fileUrl2, 'foobar'); +check(fs.rename, fs.renameSync, 'foobar', fileUrl2); +check(fs.rmdir, fs.rmdirSync, fileUrl2); +check(fs.stat, fs.statSync, fileUrl2); +check(fs.symlink, fs.symlinkSync, fileUrl2, 'foobar'); +check(fs.symlink, fs.symlinkSync, 'foobar', fileUrl2); +check(fs.truncate, fs.truncateSync, fileUrl2); +check(fs.unlink, fs.unlinkSync, fileUrl2); +check(null, fs.unwatchFile, fileUrl2, common.fail); +check(fs.utimes, fs.utimesSync, fileUrl2, 0, 0); +check(null, fs.watch, fileUrl2, common.fail); +check(null, fs.watchFile, fileUrl2, common.fail); +check(fs.writeFile, fs.writeFileSync, fileUrl2); + // 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) => { |