Skip to content

Commit 7dfa2b5

Browse files
Fishrock123juanarbol
authored andcommitted
fs: introduce opendir() and fs.Dir
This adds long-requested methods for asynchronously interacting and iterating through directory entries by using `uv_fs_opendir`, `uv_fs_readdir`, and `uv_fs_closedir`. `fs.opendir()` and friends return an `fs.Dir`, which contains methods for doing reads and cleanup. `fs.Dir` also has the async iterator symbol exposed. The `read()` method and friends only return `fs.Dirent`s for this API. Having a entry type or doing a `stat` call is deemed to be necessary in the majority of cases, so just returning dirents seems like the logical choice for a new api. Reading when there are no more entries returns `null` instead of a dirent. However the async iterator hides that (and does automatic cleanup). The code lives in separate files from the rest of fs, this is done partially to prevent over-pollution of those (already very large) files, but also in the case of js allows loading into `fsPromises`. Due to async_hooks, this introduces a new handle type of `DIRHANDLE`. This PR does not attempt to make complete optimization of this feature. Notable future improvements include: - Moving promise work into C++ land like FileHandle. - Possibly adding `readv()` to do multi-entry directory reads. - Aliasing `fs.readdir` to `fs.scandir` and doing a deprecation. Refs: nodejs/node-v0.x-archive#388 Refs: nodejs/node#583 Refs: libuv/libuv#2057 PR-URL: nodejs/node#29349 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: David Carlier <devnexen@gmail.com>
1 parent e474777 commit 7dfa2b5

1 file changed

Lines changed: 25 additions & 29 deletions

File tree

lib/internal/fs/promises.js

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -512,33 +512,29 @@ async function readFile(path, options) {
512512
}
513513

514514
module.exports = {
515-
exports: {
516-
access,
517-
copyFile,
518-
open,
519-
opendir: promisify(opendir),
520-
rename,
521-
truncate,
522-
rmdir,
523-
mkdir,
524-
readdir,
525-
readlink,
526-
symlink,
527-
lstat,
528-
stat,
529-
link,
530-
unlink,
531-
chmod,
532-
lchmod,
533-
lchown,
534-
chown,
535-
utimes,
536-
realpath,
537-
mkdtemp,
538-
writeFile,
539-
appendFile,
540-
readFile,
541-
},
542-
543-
FileHandle
515+
access,
516+
copyFile,
517+
open,
518+
opendir: promisify(opendir),
519+
rename,
520+
truncate,
521+
rmdir,
522+
mkdir,
523+
readdir,
524+
readlink,
525+
symlink,
526+
lstat,
527+
stat,
528+
link,
529+
unlink,
530+
chmod,
531+
lchmod,
532+
lchown,
533+
chown,
534+
utimes,
535+
realpath,
536+
mkdtemp,
537+
writeFile,
538+
appendFile,
539+
readFile
544540
};

0 commit comments

Comments
 (0)