Skip to content

Implement opendir API #663

@remcohaszing

Description

@remcohaszing

Implement all variants of the NodeJS opendir method are missing on the fs export.

Documentation from Node.js:


fs.opendir(path[, options], callback)#

History   |   -- | --   |     |     |     |  

Asynchronously open a directory. See the POSIX opendir(3) documentation for more details.

Creates an <fs.Dir>, which contains all further functions for reading from and cleaning up the directory.

The encoding option sets the encoding for the path while opening the directory and subsequent read operations.

fs.opendir(path[, options], callback)[#](https://nodejs.org/api/fs.html#fsopendirpath-options-callback) History path [](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) | [](https://nodejs.org/api/buffer.html#class-buffer) | [](https://nodejs.org/api/url.html#the-whatwg-url-api) options [](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) encoding [](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) | [](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type) Default: 'utf8' bufferSize [](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) Number of directory entries that are buffered internally when reading from the directory. Higher values lead to better performance but higher memory usage. Default: 32 recursive [](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) Default: false callback [](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) err [](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) dir [](https://nodejs.org/api/fs.html#class-fsdir) Asynchronously open a directory. See the POSIX [opendir(3)](http://man7.org/linux/man-pages/man3/opendir.3.html) documentation for more details.

Creates an <fs.Dir>, which contains all further functions for reading from and cleaning up the directory.

The encoding option sets the encoding for the path while opening the directory and subsequent read operations.


fs.opendirSync(path[, options])#

History   |   -- | --   |     |     |  
  • path <string> | <Buffer> | <URL>
  • options <Object>
    • encoding <string> | <null> Default: 'utf8'
    • bufferSize <number> Number of directory entries that are buffered internally when reading from the directory. Higher values lead to better performance but higher memory usage. Default: 32
    • recursive <boolean> Default: false
  • Returns: <fs.Dir>

Synchronously open a directory. See opendir(3).

Creates an <fs.Dir>, which contains all further functions for reading from and cleaning up the directory.

The encoding option sets the encoding for the path while opening the directory and subsequent read operations.

fs.opendirSync(path[, options])[#](https://nodejs.org/api/fs.html#fsopendirsyncpath-options) History path [](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) | [](https://nodejs.org/api/buffer.html#class-buffer) | [](https://nodejs.org/api/url.html#the-whatwg-url-api) options [](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) encoding [](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) | [](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type) Default: 'utf8' bufferSize [](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) Number of directory entries that are buffered internally when reading from the directory. Higher values lead to better performance but higher memory usage. Default: 32 recursive [](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) Default: false Returns: [](https://nodejs.org/api/fs.html#class-fsdir) Synchronously open a directory. See [opendir(3)](http://man7.org/linux/man-pages/man3/opendir.3.html).

Creates an <fs.Dir>, which contains all further functions for reading from and cleaning up the directory.

The encoding option sets the encoding for the path while opening the directory and subsequent read operations.


fsPromises.opendir(path[, options])#

History   |   -- | --   |     |     |  
  • path <string> | <Buffer> | <URL>
  • options <Object>
    • encoding <string> | <null> Default: 'utf8'
    • bufferSize <number> Number of directory entries that are buffered internally when reading from the directory. Higher values lead to better performance but higher memory usage. Default: 32
    • recursive <boolean> Resolved Dir will be an <AsyncIterable> containing all sub files and directories. Default: false
  • Returns: <Promise> Fulfills with an <fs.Dir>.

Asynchronously open a directory for iterative scanning. See the POSIX opendir(3) documentation for more detail.

Creates an <fs.Dir>, which contains all further functions for reading from and cleaning up the directory.

The encoding option sets the encoding for the path while opening the directory and subsequent read operations.

Example using async iteration:

import { opendir } from 'node:fs/promises';

try {
const dir = await opendir('./');
for await (const dirent of dir)
console.log(dirent.name);
} catch (err) {
console.error(err);
}
copy

When using the async iterator, the <fs.Dir> object will be automatically closed after the iterator exits.

fsPromises.opendir(path[, options])#
History
path | |
options
encoding
| Default: 'utf8'
bufferSize Number of directory entries that are buffered internally when reading from the directory. Higher values lead to better performance but higher memory usage. Default: 32
recursive Resolved Dir will be an containing all sub files and directories. Default: false
Returns: Fulfills with an <fs.Dir>.
Asynchronously open a directory for iterative scanning. See the POSIX opendir(3) documentation for more detail.

Creates an <fs.Dir>, which contains all further functions for reading from and cleaning up the directory.

The encoding option sets the encoding for the path while opening the directory and subsequent read operations.

Example using async iteration:

import { opendir } from 'node:fs/promises';

try {
const dir = await opendir('./');
for await (const dirent of dir)
console.log(dirent.name);
} catch (err) {
console.error(err);
} copy
When using the async iterator, the <fs.Dir> object will be automatically closed after the iterator exits.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions