Skip to content

Bug in fileHandle.readableWebStream implementation #58817

@pdaehne

Description

@pdaehne

Version

v24.2.0

Platform

Darwin mac.igd.fraunhofer.de 24.5.0 Darwin Kernel Version 24.5.0: Tue Apr 22 19:54:25 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6020 arm64

Subsystem

No response

What steps will reproduce the bug?

Start the attached script.

bug.zip

#!/usr/bin/env node

import * as fsPromises from 'node:fs/promises';

const scriptURL = new URL(import.meta.url);
const fileHandle = await fsPromises.open(scriptURL);
const readableStream = fileHandle.readableWebStream();
const reader = readableStream.getReader({ mode: "byob" });
const arrayBuffer = new ArrayBuffer(3);
const view = new Uint8Array(arrayBuffer, 2, 1);
const result = await reader.read(view);

How often does it reproduce? Is there a required condition?

Always.

What is the expected behavior? Why is that the expected behavior?

I do not expect to get an exception.

What do you see instead?

I do get the following exception:

node:internal/fs/promises:689
  validateOffsetLengthRead(offset, length, buffer.byteLength);
  ^

RangeError [ERR_OUT_OF_RANGE]: The value of "length" is out of range. It must be <= -1. Received 1
    at read (node:internal/fs/promises:689:3)
    at fsCall (node:internal/fs/promises:467:18)
    at FileHandle.read (node:internal/fs/promises:199:12)
    at Object.pull (node:internal/fs/promises:322:37)
    at invokePromiseCallback (node:internal/webstreams/util:172:10)
    at Object.<anonymous> (node:internal/webstreams/util:177:23)
    at readableByteStreamControllerCallPullIfNeeded (node:internal/webstreams/readablestream:3145:24)
    at node:internal/webstreams/readablestream:3295:7 {
  code: 'ERR_OUT_OF_RANGE'
}

Node.js v24.2.0

Additional information

There seems to be a bug in line 321 of "node/lib/internal/fs/promises.js". The line currently looks like this:

const { bytesRead } = await readFn(view, view.byteOffset, view.byteLength);

It probably should look like this:

const { bytesRead } = await readFn(view, 0, view.byteLength);

You want to read to offset 0 of the Uint8Array "view", not to the byteOffset of its underlying ArrayBuffer.

I'm not able to test this fix (I cannot build node.js myself).

Metadata

Metadata

Assignees

No one assigned

    Labels

    fsIssues and PRs related to the fs subsystem / file system.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions