Skip to content
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

BlobManager: implement Blob from ArrayBuffer #39276

Closed

Conversation

CodyJasonBennett
Copy link
Contributor

Summary:

Fixes a critical networking path used commonly in graphics code, among many other use-cases. It is commonplace to store images within a single binary such as a GLB (see KHR_binary_glTF). Base64 encoded glTF can be critically slow in older browsers with an increased size, so it's not a suitable fallback for those targeting multiple platforms. Furthermore, large base64 payloads can cause crash behavior on native when passed via JSI (e.g. expo-gl). For textures, this can be several megabytes of high precision data, and a uri from a blob is preferred rather than writing platform-specific workarounds via the filesystem.

A user-land patch I've employed in pmndrs/react-three-fiber, as well as existing user-land patches, is to test the Blob constructor with an empty ArrayBuffer. base64-js is used here since it's already installed with react-native for binary utils elsewhere. This is declared as a dependency in the linked library since this is an implementation detail, but will be de-duplicated at install time.

import { fromByteArray } from 'base64-js'

// Patch Blob for ArrayBuffer if unsupported
try {
  new Blob([new ArrayBuffer(4)])
} catch (_) {
  global.Blob = class extends Blob {
    constructor(parts, options) {
      super(
        parts?.map((part) => {
          if (part instanceof ArrayBuffer || ArrayBuffer.isView(part)) {
            part = fromByteArray(new Uint8Array(part))
          }

          return part
        }),
        options,
      )
    }
  }
}

Changelog:

[INTERNAL] [FIXED] - Implement Blob from ArrayBuffer

Test Plan:

Run the following at the project root to selectively test changes:

jest packages/react-native/Libraries/Blob

Note: base64 encoding will add an additional ~33% size increase to binary data as part of this PR. This is padded to 4 bytes so a byte length of 4 will encode as 8. Native ArrayBuffer support would not have this behavior and remove the overhead of base64 encoding back and forth. This is a growing behavior across the networking stack, such as in FileReader.

@facebook-github-bot
Copy link
Contributor

Hi @CodyJasonBennett!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Sep 4, 2023
@github-actions
Copy link

github-actions bot commented Sep 4, 2023

Warnings
⚠️ One hour and a half have passed and the E2E jobs haven't finished yet.
⚠️

packages/react-native/Libraries/Blob/BlobManager.js#L11 - packages/react-native/Libraries/Blob/BlobManager.js line 11 – Requires should be sorted alphabetically (lint/sort-imports)

Generated by 🚫 dangerJS against 6498dbe

@facebook-github-bot
Copy link
Contributor

@dmytrorykun has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@github-actions
Copy link

github-actions bot commented Sep 7, 2023

This pull request was successfully merged by @CodyJasonBennett in a228b0f.

When will my fix make it into a release? | Upcoming Releases

@github-actions github-actions bot added the Merged This PR has been merged. label Sep 7, 2023
@CodyJasonBennett CodyJasonBennett deleted the feat/blob-arraybuffer branch September 7, 2023 10:59
CodyJasonBennett added a commit to CodyJasonBennett/react-native that referenced this pull request Oct 24, 2023
facebook-github-bot pushed a commit that referenced this pull request Oct 31, 2023
Summary:
As per #41079, we're outputting ASCII encoded data URIs to `FileReader.readAsDataURL` due to lack of native `ArrayBuffer` support and unclear use of encoding to align with web. I'll revisit this at a later point with a better testing strategy once we have a good idea of how this should behave internally.

Aside from purely reverting #39276, I've kept the use of `ArrayBuffer.isView(part)` to the previous `part instanceof global.ArrayBufferView` since it is more correct.

## Changelog:

[INTERNAL] [REMOVED] - Revert Blob from ArrayBuffer

Pull Request resolved: #41170

Test Plan:
Run the following at the project root to selectively test changes:

`jest packages/react-native/Libraries/Blob`

Reviewed By: cipolleschi

Differential Revision: D50601036

Pulled By: dmytrorykun

fbshipit-source-id: 0ef5c960c253db255c2f8532ea1f44111093706c
Othinn pushed a commit to Othinn/react-native that referenced this pull request Jan 9, 2024
facebook#41170)

Summary:
As per facebook#41079, we're outputting ASCII encoded data URIs to `FileReader.readAsDataURL` due to lack of native `ArrayBuffer` support and unclear use of encoding to align with web. I'll revisit this at a later point with a better testing strategy once we have a good idea of how this should behave internally.

Aside from purely reverting facebook#39276, I've kept the use of `ArrayBuffer.isView(part)` to the previous `part instanceof global.ArrayBufferView` since it is more correct.

## Changelog:

[INTERNAL] [REMOVED] - Revert Blob from ArrayBuffer

Pull Request resolved: facebook#41170

Test Plan:
Run the following at the project root to selectively test changes:

`jest packages/react-native/Libraries/Blob`

Reviewed By: cipolleschi

Differential Revision: D50601036

Pulled By: dmytrorykun

fbshipit-source-id: 0ef5c960c253db255c2f8532ea1f44111093706c
@wcandillon
Copy link
Contributor

Thank you for this important patch @CodyJasonBennett. Is there a way to track in which RN version this has landed?

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. Merged This PR has been merged. 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.

3 participants