From 594ab2621f75f984d806b79bf760240e6c70dbce Mon Sep 17 00:00:00 2001 From: bcheidemann Date: Sun, 23 May 2021 13:20:05 +0100 Subject: [PATCH] fix: mkdir accepts encoding options --- src/filesystem/implementation.js | 50 ++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/src/filesystem/implementation.js b/src/filesystem/implementation.js index f58542f..35382ef 100644 --- a/src/filesystem/implementation.js +++ b/src/filesystem/implementation.js @@ -1120,26 +1120,53 @@ function unlink_node(context, path, callback) { find_node(context, parentPath, read_directory_data); } -function read_directory(context, path, callback) { +function read_directory(context, path, options, callback) { path = normalize(path); + if (typeof options === 'function') { + callback = options; + options = {}; + } + options = validate_directory_options(options); + var directoryNode; var directoryData; function handle_directory_data(error, result) { - if(error) { + if (error) { callback(error); } else { directoryData = result; 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); } } function read_directory_data(error, result) { - if(error) { + if (error) { callback(error); - } else if(result.type !== NODE_TYPE_DIRECTORY) { + } else if (result.type !== NODE_TYPE_DIRECTORY) { callback(new Errors.ENOTDIR(null, path)); } else { directoryNode = result; @@ -1150,6 +1177,17 @@ function read_directory(context, path, callback) { 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) { dstpath = normalize(dstpath); var name = basename(dstpath); @@ -2163,8 +2201,8 @@ function lseek(context, fd, offset, whence, callback) { } } -function readdir(context, path, callback) { - read_directory(context, path, callback); +function readdir(context, path, options, callback) { + read_directory(context, path, options, callback); } function toUnixTimestamp(time) {