-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
fs: add stream utilities to FileHandle
#40009
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
Changes from 1 commit
51c212d
158823a
c5308bc
ffd161b
f4f2d85
ab59886
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
FileHandle
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -106,6 +106,7 @@ const { | |
| const { | ||
| readableStreamCancel, | ||
| } = require('internal/webstreams/readablestream'); | ||
| const { ReadStream, WriteStream } = require('internal/fs/streams'); | ||
|
|
||
| const getDirectoryEntriesPromise = promisify(getDirents); | ||
| const validateRmOptionsPromise = promisify(validateRmOptions); | ||
|
|
@@ -252,6 +253,42 @@ class FileHandle extends EventEmitterMixin(JSTransferable) { | |
| return readable; | ||
| } | ||
|
|
||
| /** | ||
| * @typedef {import('./streams').ReadStream | ||
| * } NodeJSReadStream | ||
| * @param {{ | ||
| * flags?: string; | ||
| * encoding?: string; | ||
| * autoClose?: boolean; | ||
| * emitClose?: boolean; | ||
|
Member
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?
Contributor
Author
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. Could you explain why?
Member
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. 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.
Contributor
Author
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. 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} | ||
| */ | ||
|
aduh95 marked this conversation as resolved.
|
||
| readStream(options = undefined) { | ||
| return new ReadStream(undefined, { ...options, fd: this[kFd] }); | ||
|
addaleax marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /** | ||
| * @typedef {import('./streams').WriteStream | ||
| * } NodeJSWriteStream | ||
| * @param {{ | ||
| * flags?: string; | ||
| * encoding?: string; | ||
| * autoClose?: boolean; | ||
| * emitClose?: boolean; | ||
|
Member
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; | ||
| * fs?: Object | null; | ||
| * }} [options] | ||
| * @returns {NodeJSWriteStream} | ||
| */ | ||
|
aduh95 marked this conversation as resolved.
|
||
| writeStream(options = undefined) { | ||
| return new WriteStream(undefined, { ...options, fd: this[kFd] }); | ||
| } | ||
|
|
||
| [kTransfer]() { | ||
| if (this[kClosePromise] || this[kRefs] > 1) { | ||
| throw lazyDOMException('Cannot transfer FileHandle while in use', | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| '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.
|
||
| const path = require('path'); | ||
| const tmpdir = require('../common/tmpdir'); | ||
| const assert = require('assert'); | ||
| const { finished } = require('stream/promises'); | ||
| const { Blob } = require('buffer'); | ||
| 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.writeStream(); | ||
| stream.end(buffer); | ||
| await finished(stream); | ||
|
|
||
| const readFileData = fs.readFileSync(filePathForHandle); | ||
|
aduh95 marked this conversation as resolved.
|
||
| assert.deepStrictEqual(buffer, readFileData); | ||
| } | ||
|
|
||
| async function validateRead() { | ||
| const filePathForHandle = path.resolve(tmpDir, 'tmp-read.txt'); | ||
| const buffer = Buffer.from('Hello world'.repeat(100), 'utf8'); | ||
|
|
||
| fs.writeFileSync(filePathForHandle, buffer); | ||
|
aduh95 marked this conversation as resolved.
Outdated
|
||
|
|
||
| const fileHandle = await open(filePathForHandle); | ||
|
|
||
| const chunks = []; | ||
| for await (const chunk of fileHandle.readStream()) { | ||
| chunks.push(chunk); | ||
| } | ||
|
|
||
| const arrayBuffer = await new Blob(chunks).arrayBuffer(); | ||
| assert.deepStrictEqual(Buffer.from(arrayBuffer), buffer); | ||
| } | ||
|
|
||
| Promise.all([ | ||
| validateWrite(), | ||
| validateRead(), | ||
| ]).then(common.mustCall()); | ||
Uh oh!
There was an error while loading. Please reload this page.