summaryrefslogtreecommitdiff
path: root/lib/fs.js
diff options
context:
space:
mode:
authorBryan English <bryan@bryanenglish.com>2018-07-27 19:29:32 -0700
committerBryan English <bryan@bryanenglish.com>2018-08-13 19:18:45 -0700
commitc7944a7a7bd92a446ea81d774ccad31e0e7c8a3f (patch)
treec7b86a281f2af282e080b34965debcea28caf2b7 /lib/fs.js
parent78584b64d88c0567c46e3cf7bae42aa73927df40 (diff)
downloadandroid-node-v8-c7944a7a7bd92a446ea81d774ccad31e0e7c8a3f.tar.gz
android-node-v8-c7944a7a7bd92a446ea81d774ccad31e0e7c8a3f.tar.bz2
android-node-v8-c7944a7a7bd92a446ea81d774ccad31e0e7c8a3f.zip
fs: readdir optionally returning type information
readdir and readdirSync now have a "withFileTypes" option, which, when enabled, provides an array of DirectoryEntry objects, similar to Stats objects, which have the filename and the type information. Refs: https://github.com/nodejs/node/issues/15699 PR-URL: https://github.com/nodejs/node/pull/22020 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Diffstat (limited to 'lib/fs.js')
-rw-r--r--lib/fs.js23
1 files changed, 19 insertions, 4 deletions
diff --git a/lib/fs.js b/lib/fs.js
index bb8c17bbbb..a0e5f64476 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -58,6 +58,8 @@ const { getPathFromURL } = require('internal/url');
const internalUtil = require('internal/util');
const {
copyObject,
+ Dirent,
+ getDirents,
getOptions,
nullCheck,
preprocessSymlinkDestination,
@@ -773,8 +775,19 @@ function readdir(path, options, callback) {
validatePath(path);
const req = new FSReqCallback();
- req.oncomplete = callback;
- binding.readdir(pathModule.toNamespacedPath(path), options.encoding, req);
+ if (!options.withFileTypes) {
+ req.oncomplete = callback;
+ } else {
+ req.oncomplete = (err, result) => {
+ if (err) {
+ callback(err);
+ return;
+ }
+ getDirents(path, result, callback);
+ };
+ }
+ binding.readdir(pathModule.toNamespacedPath(path), options.encoding,
+ !!options.withFileTypes, req);
}
function readdirSync(path, options) {
@@ -783,9 +796,10 @@ function readdirSync(path, options) {
validatePath(path);
const ctx = { path };
const result = binding.readdir(pathModule.toNamespacedPath(path),
- options.encoding, undefined, ctx);
+ options.encoding, !!options.withFileTypes,
+ undefined, ctx);
handleErrorFromBinding(ctx);
- return result;
+ return options.withFileTypes ? getDirents(path, result) : result;
}
function fstat(fd, options, callback) {
@@ -1819,6 +1833,7 @@ module.exports = fs = {
writeFileSync,
write,
writeSync,
+ Dirent,
Stats,
get ReadStream() {