Skip to content

Commit 11d1c23

Browse files
aduh95danielleadams
authored andcommitted
fs: add FileHandle.prototype.readLines
PR-URL: #42590 Reviewed-By: LiviaMedeiros <livia@cirno.name>
1 parent 842bc64 commit 11d1c23

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

doc/api/fs.md

+41
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,46 @@ If one or more `filehandle.read()` calls are made on a file handle and then a
515515
position till the end of the file. It doesn't always read from the beginning
516516
of the file.
517517
518+
#### `filehandle.readLines([options])`
519+
520+
<!-- YAML
521+
added: REPLACEME
522+
-->
523+
524+
* `options` {Object}
525+
* `encoding` {string} **Default:** `null`
526+
* `autoClose` {boolean} **Default:** `true`
527+
* `emitClose` {boolean} **Default:** `true`
528+
* `start` {integer}
529+
* `end` {integer} **Default:** `Infinity`
530+
* `highWaterMark` {integer} **Default:** `64 * 1024`
531+
* Returns: {readline.InterfaceConstructor}
532+
533+
Convenience method to create a `readline` interface and stream over the file.
534+
See [`filehandle.createReadStream()`][] for the options.
535+
536+
```mjs
537+
import { open } from 'node:fs/promises';
538+
539+
const file = await open('./some/file/to/read');
540+
541+
for await (const line of file.readLines()) {
542+
console.log(line);
543+
}
544+
```
545+
546+
```cjs
547+
const { open } = require('node:fs/promises');
548+
549+
(async () => {
550+
const file = await open('./some/file/to/read');
551+
552+
for await (const line of file.readLines()) {
553+
console.log(line);
554+
}
555+
})();
556+
```
557+
518558
#### `filehandle.readv(buffers[, position])`
519559
520560
<!-- YAML
@@ -7635,6 +7675,7 @@ the file contents.
76357675
[`ReadDirectoryChangesW`]: https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-readdirectorychangesw
76367676
[`UV_THREADPOOL_SIZE`]: cli.md#uv_threadpool_sizesize
76377677
[`event ports`]: https://illumos.org/man/port_create
7678+
[`filehandle.createReadStream()`]: #filehandlecreatereadstreamoptions
76387679
[`filehandle.createWriteStream()`]: #filehandlecreatewritestreamoptions
76397680
[`filehandle.writeFile()`]: #filehandlewritefiledata-options
76407681
[`fs.access()`]: #fsaccesspath-mode-callback

lib/internal/fs/promises.js

+8
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ const kUnref = Symbol('kUnref');
102102
const kLocked = Symbol('kLocked');
103103

104104
const { kUsePromises } = binding;
105+
const { Interface } = require('internal/readline/interface');
105106
const {
106107
JSTransferable, kDeserialize, kTransfer, kTransferList
107108
} = require('internal/worker/js_transferable');
@@ -181,6 +182,13 @@ class FileHandle extends EventEmitterMixin(JSTransferable) {
181182
return fsCall(readFile, this, options);
182183
}
183184

185+
readLines(options = undefined) {
186+
return new Interface({
187+
input: this.createReadStream(options),
188+
crlfDelay: Infinity,
189+
});
190+
}
191+
184192
stat(options) {
185193
return fsCall(fstat, this, options);
186194
}

test/parallel/test-bootstrap-modules.js

+3
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ const expectedModules = new Set([
114114
'NativeModule internal/process/warning',
115115
'NativeModule internal/promise_hooks',
116116
'NativeModule internal/querystring',
117+
'NativeModule internal/readline/callbacks',
118+
'NativeModule internal/readline/interface',
119+
'NativeModule internal/readline/utils',
117120
'NativeModule internal/socketaddress',
118121
'NativeModule internal/source_map/source_map_cache',
119122
'NativeModule internal/stream_base_commons',
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)