Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fs: remove options.encoding from Dir.read*()
This is unlikely to be necessary in any case, and causes much
unwarrented complexity when implementing further
optimizations.

Refs: #29893 (comment)

PR-URL: #29908
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
Fishrock123 committed Oct 9, 2019
1 parent 0ff4a55 commit 7f22aaf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 27 deletions.
25 changes: 6 additions & 19 deletions doc/api/fs.md
Expand Up @@ -351,13 +351,11 @@ added: REPLACEME
Synchronously close the directory's underlying resource handle. Synchronously close the directory's underlying resource handle.
Subsequent reads will result in errors. Subsequent reads will result in errors.


### dir.read([options]) ### dir.read()
<!-- YAML <!-- YAML
added: REPLACEME added: REPLACEME
--> -->


* `options` {Object}
* `encoding` {string|null} **Default:** `'utf8'`
* Returns: {Promise} containing {fs.Dirent} * Returns: {Promise} containing {fs.Dirent}


Asynchronously read the next directory entry via readdir(3) as an Asynchronously read the next directory entry via readdir(3) as an
Expand All @@ -369,13 +367,11 @@ is completed.
_Directory entries returned by this function are in no particular order as _Directory entries returned by this function are in no particular order as
provided by the operating system's underlying directory mechanisms._ provided by the operating system's underlying directory mechanisms._


### dir.read([options, ]callback) ### dir.read(callback)
<!-- YAML <!-- YAML
added: REPLACEME added: REPLACEME
--> -->


* `options` {Object}
* `encoding` {string|null} **Default:** `'utf8'`
* `callback` {Function} * `callback` {Function}
* `err` {Error} * `err` {Error}
* `dirent` {fs.Dirent} * `dirent` {fs.Dirent}
Expand All @@ -385,25 +381,19 @@ Asynchronously read the next directory entry via readdir(3) as an


The `callback` will be called with a [Dirent][] after the read is completed. The `callback` will be called with a [Dirent][] after the read is completed.


The `encoding` option sets the encoding of the `name` in the `dirent`.

_Directory entries returned by this function are in no particular order as _Directory entries returned by this function are in no particular order as
provided by the operating system's underlying directory mechanisms._ provided by the operating system's underlying directory mechanisms._


### dir.readSync([options]) ### dir.readSync()
<!-- YAML <!-- YAML
added: REPLACEME added: REPLACEME
--> -->


* `options` {Object}
* `encoding` {string|null} **Default:** `'utf8'`
* Returns: {fs.Dirent} * Returns: {fs.Dirent}


Synchronously read the next directory entry via readdir(3) as an Synchronously read the next directory entry via readdir(3) as an
[`fs.Dirent`][]. [`fs.Dirent`][].


The `encoding` option sets the encoding of the `name` in the `dirent`.

_Directory entries returned by this function are in no particular order as _Directory entries returned by this function are in no particular order as
provided by the operating system's underlying directory mechanisms._ provided by the operating system's underlying directory mechanisms._


Expand Down Expand Up @@ -2658,8 +2648,7 @@ Creates an [`fs.Dir`][], which contains all further functions for reading from
and cleaning up the directory. and cleaning up the directory.


The `encoding` option sets the encoding for the `path` while opening the The `encoding` option sets the encoding for the `path` while opening the
directory and subsequent read operations (unless otherwise overriden during directory and subsequent read operations.
reads from the directory).


## fs.opendirSync(path[, options]) ## fs.opendirSync(path[, options])
<!-- YAML <!-- YAML
Expand All @@ -2677,8 +2666,7 @@ Creates an [`fs.Dir`][], which contains all further functions for reading from
and cleaning up the directory. and cleaning up the directory.


The `encoding` option sets the encoding for the `path` while opening the The `encoding` option sets the encoding for the `path` while opening the
directory and subsequent read operations (unless otherwise overriden during directory and subsequent read operations.
reads from the directory).


## fs.read(fd, buffer, offset, length, position, callback) ## fs.read(fd, buffer, offset, length, position, callback)
<!-- YAML <!-- YAML
Expand Down Expand Up @@ -4835,8 +4823,7 @@ Creates an [`fs.Dir`][], which contains all further functions for reading from
and cleaning up the directory. and cleaning up the directory.


The `encoding` option sets the encoding for the `path` while opening the The `encoding` option sets the encoding for the `path` while opening the
directory and subsequent read operations (unless otherwise overriden during directory and subsequent read operations.
reads from the directory).


Example using async interation: Example using async interation:


Expand Down
12 changes: 4 additions & 8 deletions lib/internal/fs/dir.js
Expand Up @@ -48,18 +48,16 @@ class Dir {
return this[kDirPath]; return this[kDirPath];
} }


read(options, callback) { read(callback) {
if (this[kDirClosed] === true) { if (this[kDirClosed] === true) {
throw new ERR_DIR_CLOSED(); throw new ERR_DIR_CLOSED();
} }


callback = typeof options === 'function' ? options : callback;
if (callback === undefined) { if (callback === undefined) {
return this[kDirReadPromisified](options); return this[kDirReadPromisified]();
} else if (typeof callback !== 'function') { } else if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK(callback); throw new ERR_INVALID_CALLBACK(callback);
} }
options = getOptions(options, this[kDirOptions]);


const req = new FSReqCallback(); const req = new FSReqCallback();
req.oncomplete = (err, result) => { req.oncomplete = (err, result) => {
Expand All @@ -70,7 +68,7 @@ class Dir {
}; };


this[kDirHandle].read( this[kDirHandle].read(
options.encoding, this[kDirOptions].encoding,
req req
); );
} }
Expand All @@ -80,11 +78,9 @@ class Dir {
throw new ERR_DIR_CLOSED(); throw new ERR_DIR_CLOSED();
} }


options = getOptions(options, this[kDirOptions]);

const ctx = { path: this[kDirPath] }; const ctx = { path: this[kDirPath] };
const result = this[kDirHandle].read( const result = this[kDirHandle].read(
options.encoding, this[kDirOptions].encoding,
undefined, undefined,
ctx ctx
); );
Expand Down

0 comments on commit 7f22aaf

Please sign in to comment.