Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 46 additions & 46 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,52 +302,6 @@ 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()`
<!-- YAML
added: REPLACEME
-->

> 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);

await file.close();
```

```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);

await file.close();
})();
```

While the `ReadableStream` will read the file to completion, it will not
close the `FileHandle` automatically. User code must still call the
`fileHandle.close()` method.

#### `filehandle.readFile(options)`
<!-- YAML
added: v10.0.0
Expand Down Expand Up @@ -404,6 +358,52 @@ changes:
{fs.Stats} object should be `bigint`. **Default:** `false`.
* Returns: {Promise} Fulfills with an {fs.Stats} for the file.

#### `filehandle.stream()`
<!-- YAML
added: REPLACEME
-->

> 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.stream())
console.log(chunk);

await file.close();
```

```cjs
const {
open,
} = require('fs/promises');

(async () => {
const file = await open('./some/file/to/read');

for await (const chunk of file.stream())
console.log(chunk);

await file.close();
})();
```

While the `ReadableStream` will read the file to completion, it will not
close the `FileHandle` automatically. User code must still call the
`fileHandle.close()` method.

#### `filehandle.sync()`
<!-- YAML
added: v10.0.0
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class FileHandle extends EventEmitterMixin(JSTransferable) {
* } ReadableStream
* @returns {ReadableStream}
*/
readableWebStream() {
stream() {
if (this[kFd] === -1)
throw new ERR_INVALID_STATE('The FileHandle is closed');
if (this[kClosePromise])
Expand Down
14 changes: 7 additions & 7 deletions test/parallel/test-filehandle-readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ const check = readFileSync(__filename, { encoding: 'utf8' });
const dec = new TextDecoder();
const file = await open(__filename);
let data = '';
for await (const chunk of file.readableWebStream())
for await (const chunk of file.stream())
data += dec.decode(chunk);

assert.strictEqual(check, data);

assert.throws(() => file.readableWebStream(), {
assert.throws(() => file.stream(), {
code: 'ERR_INVALID_STATE',
});

Expand All @@ -36,7 +36,7 @@ const check = readFileSync(__filename, { encoding: 'utf8' });
const file = await open(__filename);
await file.close();

assert.throws(() => file.readableWebStream(), {
assert.throws(() => file.stream(), {
code: 'ERR_INVALID_STATE',
});
})().then(common.mustCall());
Expand All @@ -47,7 +47,7 @@ const check = readFileSync(__filename, { encoding: 'utf8' });
const file = await open(__filename);
file.close();

assert.throws(() => file.readableWebStream(), {
assert.throws(() => file.stream(), {
code: 'ERR_INVALID_STATE',
});
})().then(common.mustCall());
Expand All @@ -56,7 +56,7 @@ const check = readFileSync(__filename, { encoding: 'utf8' });
// FileHandle is closed.
(async () => {
const file = await open(__filename);
const readable = file.readableWebStream();
const readable = file.stream();
const reader = readable.getReader();
file.close();
await reader.closed;
Expand All @@ -66,7 +66,7 @@ const check = readFileSync(__filename, { encoding: 'utf8' });
// FileHandle is closed.
(async () => {
const file = await open(__filename);
const readable = file.readableWebStream();
const readable = file.stream();
file.close();
const reader = readable.getReader();
await reader.closed;
Expand All @@ -76,7 +76,7 @@ const check = readFileSync(__filename, { encoding: 'utf8' });
// when a ReadableStream has been acquired for it.
(async () => {
const file = await open(__filename);
file.readableWebStream();
file.stream();
const mc = new MessageChannel();
mc.port1.onmessage = common.mustNotCall();
assert.throws(() => mc.port2.postMessage(file, [file]), {
Expand Down