-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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
Implement FileReader.readAsArrayBuffer #30769
Implement FileReader.readAsArrayBuffer #30769
Conversation
Base commit: d2e8e7d |
Any chance we can get some feedback with this ? |
Would it be possible to get any progress on this? The lack of implementation for The inability to use And from what I can tell the CI failure is unrelated to any change in this PR. |
@acostalima, lets get this test fixed so we can get a PR review and merge this in! |
@glenvollmer the CI error does not seem to be related to the changes I've made 🤔 |
@acostalima, any idea what it could be? let me know if there is anything I can do to help out with getting this fixed |
@glenvollmer I believe that a dependency failed to download according to the log: https://app.circleci.com/pipelines/github/facebook/react-native/7824/workflows/4633f47d-fb4a-45e4-b986-fb329475ee6b/jobs/185596/parallel-runs/0/steps/0-114:
Maybe re-running the job might fix the issue...? |
@acostalima, did a little digging. I think the url or the sha1 in this file may be broken |
3a51a6e
to
497846d
Compare
@glenvollmer I've just rebased my branch with |
It works as well, can we merge it? |
The changes from this PR (thanks @acostalima !) to |
The relevant script has switched to ESM. The new patch (tested in 0.71.3) is: --- a/Libraries/Blob/FileReader.js
+++ b/Libraries/Blob/FileReader.js
@@ -11,6 +11,7 @@
import type Blob from './Blob';
import NativeFileReaderModule from './NativeFileReaderModule';
+import { toByteArray } from 'base64-js';
const EventTarget = require('event-target-shim');
@@ -74,8 +75,35 @@ class FileReader extends (EventTarget(...READER_EVENTS): any) {
}
}
- readAsArrayBuffer(): any {
- throw new Error('FileReader.readAsArrayBuffer is not implemented');
+ readAsArrayBuffer(blob: ?Blob): void {
+ this._aborted = false;
+
+ if (blob == null) {
+ throw new TypeError(
+ "Failed to execute 'readAsArrayBuffer' on 'FileReader': parameter 1 is not of type 'Blob'",
+ );
+ }
+
+ NativeFileReaderModule.readAsDataURL(blob.data).then(
+ (text: string) => {
+ if (this._aborted) {
+ return;
+ }
+
+ const base64 = text.split(',')[1];
+ const typedArray = toByteArray(base64);
+
+ this._result = typedArray.buffer;
+ this._setReadyState(DONE);
+ },
+ error => {
+ if (this._aborted) {
+ return;
+ }
+ this._error = error;
+ this._setReadyState(DONE);
+ },
+ );
}
readAsDataURL(blob: ?Blob): void {
|
Summary: Fixes a number of issues with third party libraries that use `Blob#arrayBuffer` or `FileReader#readAsArrayBuffer`, including #30769 #34402 #20091 #21209 ## Changelog [INTERNAL] [FIXED] - Implemented FileReader#readAsArrayBuffer Pull Request resolved: #36332 Test Plan: Added a test which fails against current release but passes after code changes. Reviewed By: christophpurrer Differential Revision: D43907171 Pulled By: javache fbshipit-source-id: 73d622ec569a282b6394732b9a0dc687b447fb62
When this PR gets the attention of the RN team, I'll push the fix. Thanks @SheetJSDev! |
At first, thanks @acostalima for this contribution. I'm trying to use this PR locally, however, I'm getting an empty buffer on this line this._result = typedArray.buffer;
console.log('buffer: ', this._result); // outputs: buffer [] I checked the |
Summary: Fixes a number of issues with third party libraries that use `Blob#arrayBuffer` or `FileReader#readAsArrayBuffer`, including facebook#30769 facebook#34402 facebook#20091 facebook#21209 ## Changelog [INTERNAL] [FIXED] - Implemented FileReader#readAsArrayBuffer Pull Request resolved: facebook#36332 Test Plan: Added a test which fails against current release but passes after code changes. Reviewed By: christophpurrer Differential Revision: D43907171 Pulled By: javache fbshipit-source-id: 73d622ec569a282b6394732b9a0dc687b447fb62
I had to change the line
to
to get this to work. |
This PR is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days. |
This PR was closed because it has been stalled for 7 days with no activity. |
Summary
The proposed implementation is not an efficient solution as is requires to encode the raw binary of a
Blob
into base64 natively and then decode the base64 into anArrayBuffer
in JS land. We have to do this for the time being because it's not yet possible to share/manipulate native memory or object references with JS. However, it works as a workaround for the short term.Related: #21209
Changelog
[JavaScript] [Added] - Implement
FileReader.readAsArrayBuffer
Test Plan
Run the following command at the root of React Native's directory: