From ad97314418286b66a3c714b47559a7437b7cba85 Mon Sep 17 00:00:00 2001 From: Bryan English Date: Fri, 27 Jul 2018 19:29:32 -0700 Subject: [PATCH] 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 Reviewed-By: James M Snell Reviewed-By: Anna Henningsen Reviewed-By: Benjamin Gruenbaum Reviewed-By: Roman Reiss Reviewed-By: John-David Dalton --- doc/api/fs.md | 101 +++++++++++- lib/fs.js | 23 ++- lib/internal/fs/promises.js | 12 +- lib/internal/fs/utils.js | 117 ++++++++++++- src/node_constants.cc | 10 ++ src/node_file.cc | 217 ++++++++++++++++++++----- test/parallel/test-fs-readdir-types.js | 95 +++++++++++ tools/doc/type-parser.js | 1 + 8 files changed, 524 insertions(+), 52 deletions(-) create mode 100644 test/parallel/test-fs-readdir-types.js diff --git a/doc/api/fs.md b/doc/api/fs.md index 57aabee7035fe7..4c00885117e4ca 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -283,6 +283,92 @@ synchronous use libuv's threadpool, which can have surprising and negative performance implications for some applications. See the [`UV_THREADPOOL_SIZE`][] documentation for more information. +## Class: fs.Dirent + + +When [`fs.readdir()`][] or [`fs.readdirSync()`][] is called with the +`withFileTypes` option set to `true`, the resulting array is filled with +`fs.Dirent` objects, rather than strings or `Buffers`. + +### dirent.isBlockDevice() + + +* Returns: {boolean} + +Returns `true` if the `fs.Dirent` object describes a block device. + +### dirent.isCharacterDevice() + + +* Returns: {boolean} + +Returns `true` if the `fs.Dirent` object describes a character device. + +### dirent.isDirectory() + + +* Returns: {boolean} + +Returns `true` if the `fs.Dirent` object describes a file system +directory. + +### dirent.isFIFO() + + +* Returns: {boolean} + +Returns `true` if the `fs.Dirent` object describes a first-in-first-out +(FIFO) pipe. + +### dirent.isFile() + + +* Returns: {boolean} + +Returns `true` if the `fs.Dirent` object describes a regular file. + +### dirent.isSocket() + + +* Returns: {boolean} + +Returns `true` if the `fs.Dirent` object describes a socket. + +### dirent.isSymbolicLink() + + +* Returns: {boolean} + +Returns `true` if the `fs.Dirent` object describes a symbolic link. + + +### dirent.name + + +* {string|Buffer} + +The file name that this `fs.Dirent` object refers to. The type of this +value is determined by the `options.encoding` passed to [`fs.readdir()`][] or +[`fs.readdirSync()`][]. + ## Class: fs.FSWatcher