Skip to content

fix: handle stream reading past file size in FsaNodeReadStream#1210

Merged
streamich merged 2 commits into
masterfrom
copilot/fix-2b466ca6-c20f-4dd5-ba6c-f247873f1bc2
Oct 6, 2025
Merged

fix: handle stream reading past file size in FsaNodeReadStream#1210
streamich merged 2 commits into
masterfrom
copilot/fix-2b466ca6-c20f-4dd5-ba6c-f247873f1bc2

Conversation

Copilot AI commented Oct 6, 2025

Copy link
Copy Markdown
Contributor

Issue

When using the FSA-to-Node adapter with createReadStream() and a range read where the start position is beyond the file end, the stream would throw an unhandled RangeError: Invalid typed array length instead of returning an empty buffer and closing gracefully.

Root Cause

The FsaNodeReadStream.__read__() method was attempting to create a Uint8Array with an offset (start) that exceeded the buffer's byte length:

const uint8 = new Uint8Array(buffer, start, end - start);

When start >= buffer.byteLength, this constructor call fails because you cannot create a typed array view with an offset beyond the buffer bounds.

Solution

Added a check to return an empty Uint8Array(0) when the start position is at or beyond the file size:

if (start >= buffer.byteLength) return new Uint8Array(0);

This matches the behavior of Node.js's native fs.createReadStream(), which returns an empty stream when reading past the file end.

Testing

Added two test cases:

  1. Reading with start: 100 on a 4-byte file - returns empty buffer
  2. Reading with start: 4 on a 4-byte file (exactly at end) - returns empty buffer

Both tests verify the stream closes properly without errors and produces an empty output.

All existing tests continue to pass (897 tests).

Original prompt

This section details on the original issue you should resolve

<issue_title>Bug: stream reading past file size</issue_title>
<issue_description>When FS(A) to Node adapter is used and .createReadStream() execute a range read, where range starts after the file end, it should return an empty buffer and close the stream. Now it throws an unhandled Uint8Array read range error.</issue_description>

<agent_instructions>Check that Fsa-to-Node and Node-to-Fsa adapters correctly read streaming range requests.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

Fixes #1209

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] Bug: stream reading past file size fix: handle stream reading past file size in FsaNodeReadStream Oct 6, 2025
Copilot AI requested a review from streamich October 6, 2025 12:05
@streamich streamich marked this pull request as ready for review October 6, 2025 20:37
@streamich streamich merged commit 83f887b into master Oct 6, 2025
25 checks passed
@streamich streamich deleted the copilot/fix-2b466ca6-c20f-4dd5-ba6c-f247873f1bc2 branch October 6, 2025 20:40
@github-actions

github-actions Bot commented Oct 6, 2025

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 4.49.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: stream reading past file size

2 participants