From 11d1c23fa03bfcb351caae715715bc30e01cdb59 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sun, 9 Oct 2022 19:16:40 -0500 Subject: [PATCH] fs: add `FileHandle.prototype.readLines` PR-URL: https://github.com/nodejs/node/pull/42590 Reviewed-By: LiviaMedeiros --- doc/api/fs.md | 41 +++++++++++++++++++ lib/internal/fs/promises.js | 8 ++++ test/parallel/test-bootstrap-modules.js | 3 ++ ...test-fs-promises-file-handle-readLines.mjs | 40 ++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 test/parallel/test-fs-promises-file-handle-readLines.mjs diff --git a/doc/api/fs.md b/doc/api/fs.md index b9d1f018450b6d..3e5ff52986163a 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -515,6 +515,46 @@ 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])` + + + +* `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: {readline.InterfaceConstructor} + +Convenience method to create a `readline` interface and stream over the file. +See [`filehandle.createReadStream()`][] for the options. + +```mjs +import { open } from 'node:fs/promises'; + +const file = await open('./some/file/to/read'); + +for await (const line of file.readLines()) { + console.log(line); +} +``` + +```cjs +const { open } = require('node:fs/promises'); + +(async () => { + const file = await open('./some/file/to/read'); + + for await (const line of file.readLines()) { + console.log(line); + } +})(); +``` + #### `filehandle.readv(buffers[, position])`