Skip to content

Commit

Permalink
fs: add FileHandle.prototype.readLines
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Apr 3, 2022
1 parent 1c69dfe commit 2098780
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
52 changes: 52 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,56 @@ If one or more `filehandle.read()` calls are made on a file handle and then a
position till the end of the file. It doesn't always read from the beginning
of the file.
#### `filehandle.readLines([options])`
<!-- YAML
added: REPLACEME
-->
* `options` {Object}
* `encoding` {string} **Default:** `null`
* `autoClose` {boolean} **Default:** `true`
* `emitClose` {boolean} **Default:** `true`
* `start` {integer}
* `end` {integer} **Default:** `Infinity`
* `highWaterMark` {integer} **Default:** `64 * 1024`
* Returns: [`readlinePromises.Interface`][]
Convenient alias to create a `readline` interface and stream over the file. See
[`filehandle.createReadStream()`][] for the options.
```mjs
import { open } from 'node:fs/promises';

let fh;
try {
fh = await open(new URL(import.meta.url));

for await (const line of fh.readLines()) {
console.log(line);
}
} finally {
await fh?.close();
}
```
```cjs
const { open } = require('node:fs/promises');

(async () => {
let fh;
try {
fh = await open(__filename);

for await (const line of fh.readLines()) {
console.log(line);
}
} finally {
await fh?.close();
}
})();
```
#### `filehandle.readv(buffers[, position])`
<!-- YAML
Expand Down Expand Up @@ -7497,6 +7547,7 @@ the file contents.
[`ReadDirectoryChangesW`]: https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-readdirectorychangesw
[`UV_THREADPOOL_SIZE`]: cli.md#uv_threadpool_sizesize
[`event ports`]: https://illumos.org/man/port_create
[`filehandle.createReadStream()`]: #filehandlecreatereadstreamoptions
[`filehandle.writeFile()`]: #filehandlewritefiledata-options
[`fs.access()`]: #fsaccesspath-mode-callback
[`fs.accessSync()`]: #fsaccesssyncpath-mode
Expand Down Expand Up @@ -7542,6 +7593,7 @@ the file contents.
[`fsPromises.utimes()`]: #fspromisesutimespath-atime-mtime
[`inotify(7)`]: https://man7.org/linux/man-pages/man7/inotify.7.html
[`kqueue(2)`]: https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
[`readlinePromises.Interface`]: readline.md#class-readlinepromisesinterface
[`util.promisify()`]: util.md#utilpromisifyoriginal
[bigints]: https://tc39.github.io/proposal-bigint
[caveats]: #caveats
Expand Down
8 changes: 8 additions & 0 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const kUnref = Symbol('kUnref');
const kLocked = Symbol('kLocked');

const { kUsePromises } = binding;
const { Interface } = require('internal/readline/interface');
const {
JSTransferable, kDeserialize, kTransfer, kTransferList
} = require('internal/worker/js_transferable');
Expand Down Expand Up @@ -175,6 +176,13 @@ class FileHandle extends EventEmitterMixin(JSTransferable) {
return fsCall(readFile, this, options);
}

readLines(options = undefined) {
return new Interface({
input: this.createReadStream(options),
crlfDelay: Infinity,
});
}

stat(options) {
return fsCall(fstat, this, options);
}
Expand Down

0 comments on commit 2098780

Please sign in to comment.