From 7f2ccd423da3f73438e099ac7cf508f74fda1b4a Mon Sep 17 00:00:00 2001 From: James M Snell Date: Fri, 9 Jul 2021 13:22:36 -0700 Subject: [PATCH] fs: add FileHandle.prototype.readableWebStream() Adds an experimental `readableWebStream()` method to `FileHandle` that returns a web `ReadableStream` Signed-off-by: James M Snell --- doc/api/fs.md | 38 +++++++++++++++ lib/internal/fs/promises.js | 22 +++++++++ .../test-filehandle-readablestream.js | 48 +++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 test/parallel/test-filehandle-readablestream.js diff --git a/doc/api/fs.md b/doc/api/fs.md index ab5d7f08e1082f..77194e9a64cc7d 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -302,6 +302,44 @@ Reads data from the file and stores that in the given buffer. If the file is not modified concurrently, the end-of-file is reached when the number of bytes read is zero. +#### `filehandle.readableWebStream()` + + +> Stability: 1 - Experimental + +* Returns: {ReadableStream} + +Returns a `ReadableStream` that may be used to read the files data. + +An error will be thrown if this method is called more than once or is called +after the `FileHandle` is closed or closing. + +```mjs +import { + open, +} from 'node:fs/promises'; + +const file = await open('./some/file/to/read'); + +for await (const chunk of file.readableWebStream()) + console.log(chunk); +``` + +```cjs +const { + open, +} = require('fs/promises'); + +(async () => { + const file = await open('./some/file/to/read'); + + for await (const chunk of file.readableWebStream()) + console.log(chunk); +})(); +``` + #### `filehandle.readFile(options)`