Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixup! fs: add stream utilities to FileHandle
  • Loading branch information
aduh95 committed Sep 8, 2021
commit 158823acb1f5e1d9cc557705a4568905a198059a
14 changes: 0 additions & 14 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,15 +377,12 @@ added: REPLACEME
-->

* `options` {Object}
* `flags` {string} See [support of file system `flags`][]. **Default:**
`'r'`.
* `encoding` {string} **Default:** `null`
* `autoClose` {boolean} **Default:** `true`
* `emitClose` {boolean} **Default:** `true`
* `start` {integer}
* `end` {integer} **Default:** `Infinity`
* `highWaterMark` {integer} **Default:** `64 * 1024`
* `fs` {Object|null} **Default:** `null`
* Returns: {fs.ReadStream} See [Readable Stream][].

Unlike the 16 kb default `highWaterMark` for a readable stream, the stream
Expand All @@ -408,10 +405,6 @@ By default, the stream will emit a `'close'` event after it has been
destroyed, like most `Readable` streams. Set the `emitClose` option to
`false` to change this behavior.

By providing the `fs` option, it is possible to override the corresponding `fs`
implementations for `open`, `read`, and `close`. When providing the `fs` option,
overrides for `open`, `read`, and `close` are required.

```mjs
import { open } from 'fs/promises';

Expand Down Expand Up @@ -666,7 +659,6 @@ added: REPLACEME
* `autoClose` {boolean} **Default:** `true`
* `emitClose` {boolean} **Default:** `true`
* `start` {integer}
* `fs` {Object|null} **Default:** `null`
* Returns: {fs.WriteStream} See [Writable Stream][].

`options` may also include a `start` option to allow writing data at some
Expand All @@ -685,12 +677,6 @@ By default, the stream will emit a `'close'` event after it has been
destroyed, like most `Writable` streams. Set the `emitClose` option to
`false` to change this behavior.

By providing the `fs` option it is possible to override the corresponding `fs`
implementations for `open`, `write`, `writev` and `close`. Overriding `write()`
without `writev()` can reduce performance as some optimizations (`_writev()`)
will be disabled. When providing the `fs` option, overrides for `open`,
`close`, and at least one of `write` and `writev` are required.

#### `filehandle.writev(buffers[, position])`
<!-- YAML
added: v12.9.0
Expand Down
17 changes: 10 additions & 7 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ const {
const {
readableStreamCancel,
} = require('internal/webstreams/readablestream');
const { ReadStream, WriteStream } = require('internal/fs/streams');

const getDirectoryEntriesPromise = promisify(getDirents);
const validateRmOptionsPromise = promisify(validateRmOptions);
Expand All @@ -116,6 +115,12 @@ function lazyLoadCpPromises() {
return cpPromises ??= require('internal/fs/cp/cp').cpFn;
}

// Lazy loaded to avoid circular dependency.
let fsStreams;
function lazyFsStreams() {
return fsStreams ??= require('internal/fs/streams');
}

class FileHandle extends EventEmitterMixin(JSTransferable) {
/**
* @param {InternalFSBinding.FileHandle | undefined} filehandle
Expand Down Expand Up @@ -257,36 +262,34 @@ class FileHandle extends EventEmitterMixin(JSTransferable) {
* @typedef {import('./streams').ReadStream
* } NodeJSReadStream
* @param {{
* flags?: string;
* encoding?: string;
* autoClose?: boolean;
* emitClose?: boolean;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove autoClose & emitClose from here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain why?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are legacy. The only reason we have still have them is to avoid breakage. Since this is a new API they are not necessary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s a new way for exposing the same old API, removing it would be harder than keeping it – also I don’t think they’re documented as legacy anywhere, so that’s probably best to leave that discussion for another PR.

* start: number;
* end?: number;
* highWaterMark?: number;
* fs?: Object | null;
* }} [options]
* @returns {NodeJSReadStream}
*/
Comment thread
aduh95 marked this conversation as resolved.
readStream(options = undefined) {
return new ReadStream(undefined, { ...options, fd: this[kFd] });
const { ReadStream } = lazyFsStreams();
return new ReadStream(undefined, { ...options, fd: this });
}

/**
* @typedef {import('./streams').WriteStream
* } NodeJSWriteStream
* @param {{
* flags?: string;
* encoding?: string;
* autoClose?: boolean;
* emitClose?: boolean;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove autoClose & emitClose from here?

* start: number;
* fs?: Object | null;
* }} [options]
* @returns {NodeJSWriteStream}
*/
Comment thread
aduh95 marked this conversation as resolved.
writeStream(options = undefined) {
return new WriteStream(undefined, { ...options, fd: this[kFd] });
const { WriteStream } = lazyFsStreams();
return new WriteStream(undefined, { ...options, fd: this });
}

[kTransfer]() {
Expand Down