Skip to content

fs: fix for FileHandle.readableWebStream #58842

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ class FileHandle extends EventEmitter {

async pull(controller) {
const view = controller.byobRequest.view;
const { bytesRead } = await readFn(view, view.byteOffset, view.byteLength);
const { bytesRead } = await readFn(view, 0, view.byteLength);

if (bytesRead === 0) {
controller.close();
Expand Down
31 changes: 31 additions & 0 deletions test/parallel/test-filehandle-readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,37 @@ const check = readFileSync(__filename, { encoding: 'utf8' });
await file.close();
})().then(common.mustCall());

// Make sure 'byob' reader works with views into
// different parts of a single ArrayBuffer
(async () => {
const file = await open(__filename);
const dec = new TextDecoder();
const readable = file.readableWebStream();
const reader = readable.getReader({ mode: 'byob' });
const size = (await file.stat()).size;

let buff = new ArrayBuffer(size);
let offset = 0;
let result;
do {
result = await reader.read(new DataView(buff, offset, Math.min(100, buff.byteLength - offset)));
if (result.value !== undefined) {
buff = result.value.buffer;
offset += result.value.byteLength;
assert.ok(result.value.byteLength <= 100);
}
} while (!result.done && (offset < buff.byteLength));
const data = dec.decode(new Uint8Array(buff));

assert.strictEqual(check, data);

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

await file.close();
})().then(common.mustCall());

// Make sure a warning is logged if a non-'bytes' type is passed.
(async () => {
const file = await open(__filename);
Expand Down
Loading