fix: mkdir accepts encoding options

This commit is contained in:
bcheidemann 2021-05-23 13:20:05 +01:00 committed by David Humphrey
parent 75a2cc86cf
commit 594ab2621f
1 changed files with 44 additions and 6 deletions

View File

@ -1120,9 +1120,15 @@ function unlink_node(context, path, callback) {
find_node(context, parentPath, read_directory_data); find_node(context, parentPath, read_directory_data);
} }
function read_directory(context, path, callback) { function read_directory(context, path, options, callback) {
path = normalize(path); path = normalize(path);
if (typeof options === 'function') {
callback = options;
options = {};
}
options = validate_directory_options(options);
var directoryNode; var directoryNode;
var directoryData; var directoryData;
@ -1132,6 +1138,27 @@ function read_directory(context, path, callback) {
} else { } else {
directoryData = result; directoryData = result;
var files = Object.keys(directoryData); var files = Object.keys(directoryData);
if (options.encoding) {
var fileBuffers = files.map(function (file) {
return Buffer.from(file);
});
if (options.encoding === 'buffer') {
files = fileBuffers;
}
else {
files = fileBuffers.map(function (fileBuffer) {
return fileBuffer.toString(options.encoding);
});
}
}
if (options.withFileTypes) {
// TODO: map files to fs.Dirent
return callback(new Error('readdir does not support option withFileTypes yet'));
}
callback(null, files); callback(null, files);
} }
} }
@ -1150,6 +1177,17 @@ function read_directory(context, path, callback) {
find_node(context, path, read_directory_data); find_node(context, path, read_directory_data);
} }
function validate_directory_options(options, enc) {
if (!options) {
options = { encoding: enc };
} else if (typeof options === 'function') {
options = { encoding: enc };
} else if (typeof options === 'string') {
options = { encoding: options };
}
return options;
}
function make_symbolic_link(context, srcpath, dstpath, callback) { function make_symbolic_link(context, srcpath, dstpath, callback) {
dstpath = normalize(dstpath); dstpath = normalize(dstpath);
var name = basename(dstpath); var name = basename(dstpath);
@ -2163,8 +2201,8 @@ function lseek(context, fd, offset, whence, callback) {
} }
} }
function readdir(context, path, callback) { function readdir(context, path, options, callback) {
read_directory(context, path, callback); read_directory(context, path, options, callback);
} }
function toUnixTimestamp(time) { function toUnixTimestamp(time) {