Skip to content

Commit 8940ae2

Browse files
committed
fs: add FileHandle.prototype.readLines
1 parent 68fb0bf commit 8940ae2

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

doc/api/fs.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,46 @@ If one or more `filehandle.read()` calls are made on a file handle and then a
488488
position till the end of the file. It doesn't always read from the beginning
489489
of the file.
490490
491+
#### `filehandle.readLines([options])`
492+
493+
<!-- YAML
494+
added: REPLACEME
495+
-->
496+
497+
* `options` {Object}
498+
* `encoding` {string} **Default:** `null`
499+
* `autoClose` {boolean} **Default:** `true`
500+
* `emitClose` {boolean} **Default:** `true`
501+
* `start` {integer}
502+
* `end` {integer} **Default:** `Infinity`
503+
* `highWaterMark` {integer} **Default:** `64 * 1024`
504+
* Returns: [`readlinePromises.Interface`][]
505+
506+
Convenient alias to create a `readline` interface and stream over the file. See
507+
[`filehandle.createReadStream()`][] for the options.
508+
509+
```mjs
510+
import { open } from 'node:fs/promises';
511+
512+
const file = await open('./some/file/to/read');
513+
514+
for await (const line of file.readLines()) {
515+
console.log(line);
516+
}
517+
```
518+
519+
```cjs
520+
const { open } = require('node:fs/promises');
521+
522+
(async () => {
523+
const file = await open('./some/file/to/read');
524+
525+
for await (const line of file.readLines()) {
526+
console.log(line);
527+
}
528+
})();
529+
```
530+
491531
#### `filehandle.readv(buffers[, position])`
492532
493533
<!-- YAML
@@ -7493,6 +7533,7 @@ the file contents.
74937533
[`ReadDirectoryChangesW`]: https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-readdirectorychangesw
74947534
[`UV_THREADPOOL_SIZE`]: cli.md#uv_threadpool_sizesize
74957535
[`event ports`]: https://illumos.org/man/port_create
7536+
[`filehandle.createReadStream()`]: #filehandlecreatereadstreamoptions
74967537
[`filehandle.createWriteStream()`]: #filehandlecreatewritestreamoptions
74977538
[`filehandle.writeFile()`]: #filehandlewritefiledata-options
74987539
[`fs.access()`]: #fsaccesspath-mode-callback
@@ -7539,6 +7580,7 @@ the file contents.
75397580
[`fsPromises.utimes()`]: #fspromisesutimespath-atime-mtime
75407581
[`inotify(7)`]: https://man7.org/linux/man-pages/man7/inotify.7.html
75417582
[`kqueue(2)`]: https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
7583+
[`readlinePromises.Interface`]: readline.md#class-readlinepromisesinterface
75427584
[`util.promisify()`]: util.md#utilpromisifyoriginal
75437585
[bigints]: https://tc39.github.io/proposal-bigint
75447586
[caveats]: #caveats

lib/internal/fs/promises.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ const kUnref = Symbol('kUnref');
9696
const kLocked = Symbol('kLocked');
9797

9898
const { kUsePromises } = binding;
99+
const { Interface } = require('internal/readline/interface');
99100
const {
100101
JSTransferable, kDeserialize, kTransfer, kTransferList
101102
} = require('internal/worker/js_transferable');
@@ -175,6 +176,13 @@ class FileHandle extends EventEmitterMixin(JSTransferable) {
175176
return fsCall(readFile, this, options);
176177
}
177178

179+
readLines(options = undefined) {
180+
return new Interface({
181+
input: this.createReadStream(options),
182+
crlfDelay: Infinity,
183+
});
184+
}
185+
178186
stat(options) {
179187
return fsCall(fstat, this, options);
180188
}

test/parallel/test-bootstrap-modules.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ const expectedModules = new Set([
111111
'NativeModule internal/process/warning',
112112
'NativeModule internal/promise_hooks',
113113
'NativeModule internal/querystring',
114+
'NativeModule internal/readline/callbacks',
115+
'NativeModule internal/readline/interface',
116+
'NativeModule internal/readline/utils',
114117
'NativeModule internal/socketaddress',
115118
'NativeModule internal/source_map/source_map_cache',
116119
'NativeModule internal/stream_base_commons',
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import '../common/index.mjs';
2+
import tmpdir from '../common/tmpdir.js';
3+
4+
import assert from 'node:assert';
5+
import { open, writeFile } from 'node:fs/promises';
6+
import path from 'node:path';
7+
8+
tmpdir.refresh();
9+
10+
const filePath = path.join(tmpdir.path, 'file.txt');
11+
12+
await writeFile(filePath, '1\n\n2\n');
13+
14+
let file;
15+
try {
16+
file = await open(filePath);
17+
18+
let i = 0;
19+
for await (const line of file.readLines()) {
20+
switch (i++) {
21+
case 0:
22+
assert.strictEqual(line, '1');
23+
break;
24+
25+
case 1:
26+
assert.strictEqual(line, '');
27+
break;
28+
29+
case 2:
30+
assert.strictEqual(line, '2');
31+
break;
32+
33+
default:
34+
assert.fail();
35+
break;
36+
}
37+
}
38+
} finally {
39+
await file?.close();
40+
}

0 commit comments

Comments
 (0)