From 576831370f12fd5868585abcadf8bce0db312dda Mon Sep 17 00:00:00 2001 From: bcheidemann Date: Sat, 29 May 2021 12:18:29 +0100 Subject: [PATCH] fix: handle withFileTypes options in readdir --- src/dirent.js | 12 +++++++++ src/filesystem/implementation.js | 42 +++++++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 src/dirent.js diff --git a/src/dirent.js b/src/dirent.js new file mode 100644 index 0000000..76b3d49 --- /dev/null +++ b/src/dirent.js @@ -0,0 +1,12 @@ +'use strict'; + +const Stats = require('./stats.js'); + +function Dirent(path, fileNode, devName) { + this.constructor = Dirent; + Stats.call(this, path, fileNode, devName); +} + +Dirent.prototype = Stats.prototype; + +module.exports = Dirent; diff --git a/src/filesystem/implementation.js b/src/filesystem/implementation.js index 35382ef..fda0d8e 100644 --- a/src/filesystem/implementation.js +++ b/src/filesystem/implementation.js @@ -4,6 +4,7 @@ var dirname = Path.dirname; var basename = Path.basename; var isAbsolutePath = Path.isAbsolute; var shared = require('../shared.js'); +var async = require('../../lib/async.js'); var Constants = require('../constants.js'); var NODE_TYPE_FILE = Constants.NODE_TYPE_FILE; @@ -35,6 +36,7 @@ var openFiles = require('../open-files.js'); var OpenFileDescription = require('../open-file-description.js'); var SuperNode = require('../super-node.js'); var Node = require('../node.js'); +var Dirent = require('../dirent.js'); var Stats = require('../stats.js'); /** @@ -1155,11 +1157,30 @@ function read_directory(context, path, options, callback) { } if (options.withFileTypes) { - // TODO: map files to fs.Dirent - return callback(new Error('readdir does not support option withFileTypes yet')); - } + var dirEnts = []; - callback(null, files); + // eslint-disable-next-line no-inner-declarations + function to_dir_entry(file, callback) { + const filename = Buffer.from(file, options.encoding).toString(); + const filepath = Path.join(path, filename); + get_dir_entry(context, filepath, function(error, dirEnt) { + if (error) { + callback(error); + } + dirEnt.name = file; + dirEnts.push(dirEnt); + callback(); + }); + } + + async.eachSeries(files, to_dir_entry, function (error) { + callback(error, dirEnts); + }); + } + + else { + callback(null, files); + } } } @@ -1177,6 +1198,19 @@ function read_directory(context, path, options, callback) { find_node(context, path, read_directory_data); } +function get_dir_entry(context, path, callback) { + function check_result(error, result) { + if(error) { + callback(error); + } else { + var stats = new Dirent(path, result, context.name); + callback(null, stats); + } + } + + lstat_file(context, path, check_result); +} + function validate_directory_options(options, enc) { if (!options) { options = { encoding: enc };