Skip to content

feat: read blob bytes directly in FileReader.readAsArrayBuffer - #58031

Open
paradowstack wants to merge 4 commits into
react:mainfrom
paradowstack:feat/filereader-read-as-array-buffer
Open

feat: read blob bytes directly in FileReader.readAsArrayBuffer#58031
paradowstack wants to merge 4 commits into
react:mainfrom
paradowstack:feat/filereader-read-as-array-buffer

Conversation

@paradowstack

@paradowstack paradowstack commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Rebase Note:

This branch is stacked on #57937 (Promise<ArrayBuffer> for Java/ObjC TurboModules) and #58029 (feat: support Array as a TurboModule method parameter) and #58030 (feat: accept ArrayBuffer and ArrayBufferView parts in Blob and File) and must be rebased onto main once that lands. For now review only the last commit.

Summary:

FileReader.readAsArrayBuffer() was implemented on top of readAsDataURL: native base64-encoded the blob into a data URL, JS split the string and decoded it back to bytes with base64-js. That is a base64 encode, a JS decode, and two full copies of the payload for every read.

Native now returns the bytes directly. BlobModule.resolveBuffer / -[RCTBlobManager resolveBuffer:offset:size:] copy the requested range into an ArrayBuffer / RCTArrayBuffer, and readAsArrayBuffer resolves the Promise with it — zero-copy from there to JS. The single copy is required rather than incidental: the buffer is handed to JS zero-copy, and Blobs are immutable per the W3C File API, so JS must not be able to write through to blob storage.

Rebase Note:

This branch is stacked on #57937 (Promise<ArrayBuffer> for Java/ObjC TurboModules) and must be rebased onto main once that lands.

Changelog:

[GENERAL] [ADDED] - FileReader.readAsArrayBuffer() reads blob bytes directly instead of decoding a base64 data URL

Test Plan:

  • FileReader-test covers a successful read, TypeError on a null blob, aborting a pending read, the stale-read guard when a newer read supersedes an in-flight one, and a NotReadableError DOMException on rejection.
  • FileReaderModuleTest covers reading a whole blob, an offset/size range, an unknown blob id, a missing blob id, and that the resolved buffer does not alias blob storage.
  • BlobModuleTest / RCTBlobManagerTests cover resolveBuffer for whole and ranged reads, an unknown id, and that the returned buffer is an independent copy with a full 0..capacity window.
  • RNTester gains a Blob example that builds a blob per BlobPart variant, reads each back through readAsArrayBuffer, and compares the bytes.
  • Android and C++ API snapshots updated.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 20, 2026
@paradowstack paradowstack changed the title Feat/filereader read as array buffer feat: read blob bytes directly in FileReader.readAsArrayBuffer Aug 20, 2026
@paradowstack
paradowstack force-pushed the feat/filereader-read-as-array-buffer branch from 7ec3142 to 3e4bdb9 Compare August 20, 2026 13:29
@github-actions

Copy link
Copy Markdown

Warning

JavaScript API change detected

This PR commits an update to ReactNativeApi.d.ts, indicating a change to React Native's public JavaScript API.

  • Please include a clear changelog message.
  • This change will be subject to additional review.

This change was flagged as: POTENTIALLY_BREAKING

@paradowstack
paradowstack force-pushed the feat/filereader-read-as-array-buffer branch 3 times, most recently from ad5ade0 to 01ab62a Compare August 20, 2026 14:07
@paradowstack
paradowstack marked this pull request as ready for review August 20, 2026 14:08
@paradowstack
paradowstack force-pushed the feat/filereader-read-as-array-buffer branch from 01ab62a to 93b9c18 Compare August 20, 2026 14:38
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Aug 20, 2026
@paradowstack
paradowstack force-pushed the feat/filereader-read-as-array-buffer branch from 93b9c18 to 146222c Compare August 21, 2026 06:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Callstack Partner: Callstack Partner Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant