-
Notifications
You must be signed in to change notification settings - Fork 30.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fs: add stream utilities to FileHandle
#40009
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
51c212d
fs: add stream utilities to `FileHandle`
aduh95 158823a
fixup! fs: add stream utilities to `FileHandle`
aduh95 c5308bc
fixup! fs: add stream utilities to `FileHandle`
aduh95 ffd161b
fixup! fs: add stream utilities to FileHandle
aduh95 f4f2d85
fs: simplify `create(Read|Write)Stream` doc
aduh95 ab59886
fixup! fs: add stream utilities to `FileHandle`
aduh95 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,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 | ||
|
@@ -252,6 +258,40 @@ class FileHandle extends EventEmitterMixin(JSTransferable) { | |
return readable; | ||
} | ||
|
||
/** | ||
* @typedef {import('./streams').ReadStream | ||
* } ReadStream | ||
* @param {{ | ||
* encoding?: string; | ||
* autoClose?: boolean; | ||
* emitClose?: boolean; | ||
* start: number; | ||
* end?: number; | ||
* highWaterMark?: number; | ||
* }} [options] | ||
* @returns {ReadStream} | ||
*/ | ||
createReadStream(options = undefined) { | ||
const { ReadStream } = lazyFsStreams(); | ||
return new ReadStream(undefined, { ...options, fd: this }); | ||
} | ||
|
||
/** | ||
* @typedef {import('./streams').WriteStream | ||
* } WriteStream | ||
* @param {{ | ||
* encoding?: string; | ||
* autoClose?: boolean; | ||
* emitClose?: boolean; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we remove autoClose & emitClose from here? |
||
* start: number; | ||
* }} [options] | ||
* @returns {WriteStream} | ||
*/ | ||
createWriteStream(options = undefined) { | ||
const { WriteStream } = lazyFsStreams(); | ||
return new WriteStream(undefined, { ...options, fd: this }); | ||
} | ||
|
||
[kTransfer]() { | ||
if (this[kClosePromise] || this[kRefs] > 1) { | ||
throw lazyDOMException('Cannot transfer FileHandle while in use', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
// The following tests validate base functionality for the fs.promises | ||
// FileHandle.write method. | ||
|
||
const fs = require('fs'); | ||
const { open } = fs.promises; | ||
aduh95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const path = require('path'); | ||
const tmpdir = require('../common/tmpdir'); | ||
const assert = require('assert'); | ||
const { finished } = require('stream/promises'); | ||
const { buffer } = require('stream/consumers'); | ||
const tmpDir = tmpdir.path; | ||
|
||
tmpdir.refresh(); | ||
|
||
async function validateWrite() { | ||
const filePathForHandle = path.resolve(tmpDir, 'tmp-write.txt'); | ||
const fileHandle = await open(filePathForHandle, 'w'); | ||
const buffer = Buffer.from('Hello world'.repeat(100), 'utf8'); | ||
|
||
const stream = fileHandle.createWriteStream(); | ||
stream.end(buffer); | ||
await finished(stream); | ||
|
||
const readFileData = fs.readFileSync(filePathForHandle); | ||
aduh95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert.deepStrictEqual(buffer, readFileData); | ||
} | ||
|
||
async function validateRead() { | ||
const filePathForHandle = path.resolve(tmpDir, 'tmp-read.txt'); | ||
const buf = Buffer.from('Hello world'.repeat(100), 'utf8'); | ||
|
||
fs.writeFileSync(filePathForHandle, buf); | ||
|
||
const fileHandle = await open(filePathForHandle); | ||
assert.deepStrictEqual( | ||
await buffer(fileHandle.createReadStream()), | ||
buf | ||
); | ||
} | ||
|
||
Promise.all([ | ||
validateWrite(), | ||
validateRead(), | ||
]).then(common.mustCall()); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain why?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.