mirror of
https://github.com/nodejs/node.git
synced 2025-05-09 17:22:44 +00:00

This makes several changes: 1. Allow path/filename to be passed in as a Buffer on fs methods 2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink, fs.readlinkSync and fs.watch. 3. Documentation updates For 1... it's now possible to do: ```js fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { }); ``` For 2... ```js fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { }); fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { }); ``` encoding can also be passed as a string ```js fs.readdir('/fs/foo/bar', 'hex', (err,list) => { }); ``` The default encoding is set to UTF8 so this addresses the discrepency that existed previously between fs.readdir and fs.watch handling filenames differently. Fixes: https://github.com/nodejs/node/issues/2088 Refs: https://github.com/nodejs/node/issues/3519 PR-URL: https://github.com/nodejs/node/pull/5616 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
53 lines
1.0 KiB
JavaScript
53 lines
1.0 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const assert = require('assert');
|
|
|
|
if (common.isFreeBSD) {
|
|
console.log('1..0 # Skipped: Test currently not working on FreeBSD');
|
|
return;
|
|
}
|
|
|
|
const fn = '新建文夹件.txt';
|
|
const a = path.join(common.tmpDir, fn);
|
|
|
|
const watcher1 = fs.watch(
|
|
common.tmpDir,
|
|
{encoding: 'hex'},
|
|
(event, filename) => {
|
|
if (filename)
|
|
assert.equal(filename, 'e696b0e5bbbae69687e5a4b9e4bbb62e747874');
|
|
watcher1.close();
|
|
}
|
|
);
|
|
|
|
const watcher2 = fs.watch(
|
|
common.tmpDir,
|
|
(event, filename) => {
|
|
if (filename)
|
|
assert.equal(filename, fn);
|
|
watcher2.close();
|
|
}
|
|
);
|
|
|
|
const watcher3 = fs.watch(
|
|
common.tmpDir,
|
|
{encoding: 'buffer'},
|
|
(event, filename) => {
|
|
if (filename) {
|
|
assert(filename instanceof Buffer);
|
|
assert.equal(filename.toString('utf8'), fn);
|
|
}
|
|
watcher3.close();
|
|
}
|
|
);
|
|
|
|
const fd = fs.openSync(a, 'w+');
|
|
fs.closeSync(fd);
|
|
|
|
process.on('exit', () => {
|
|
fs.unlink(a);
|
|
});
|