diff --git a/lib/fs.js b/lib/fs.js index 53dcffad0b8cdb..8edded9ee06b5d 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1370,7 +1370,7 @@ function mkdirSync(path, options) { } /* - * An recurssive algorithm for reading the entire contents of the `basePath` directory. + * An recursive algorithm for reading the entire contents of the `basePath` directory. * This function does not validate `basePath` as a directory. It is passed directly to * `binding.readdir`. * @param {string} basePath @@ -1429,31 +1429,24 @@ function readdirRecursive(basePath, options, callback) { read(context.pathsQueue[i++]); } -function processReaddirResult({ result, currentPath, context }) { - // Calling `readdir` with `withFileTypes=true`, the result is an array of arrays. - // The first array is the names, and the second array is the types. - // They are guaranteed to be the same length; hence, setting `length` to the length - // of the first array within the result. - if (context.withFileTypes) { - handleDirents({ result, currentPath, context }); - } else { - handleFilePaths({ result, currentPath, context }); - } -} +// Calling `readdir` with `withFileTypes=true`, the result is an array of arrays. +// The first array is the names, and the second array is the types. +// They are guaranteed to be the same length; hence, setting `length` to the length +// of the first array within the result. +const processReaddirResult = (context) => (context.withFileTypes ? handleDirents(context) : handleFilePaths(context)); function handleDirents({ result, currentPath, context }) { const { 0: names, 1: types } = result; - const length = names.length; + const { length } = names; for (let i = 0; i < length; i++) { // Avoid excluding symlinks, as they are not directories. // Refs: https://github.com/nodejs/node/issues/52663 const fullPath = pathModule.join(currentPath, names[i]); - const stat = binding.internalModuleStat(binding, fullPath); const dirent = getDirent(currentPath, names[i], types[i]); ArrayPrototypePush(context.readdirResults, dirent); - if (dirent.isDirectory() || stat === 1) { + if (dirent.isDirectory() || binding.internalModuleStat(binding, fullPath) === 1) { ArrayPrototypePush(context.pathsQueue, fullPath); } }