Skip to content

Commit

Permalink
Patch react-native to support read ArrayBuffer from blob
Browse files Browse the repository at this point in the history
  • Loading branch information
hans00 committed Dec 24, 2023
1 parent 14c08ec commit f153bd1
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions patches/react-native+0.71.8.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
diff --git a/node_modules/react-native/Libraries/Blob/FileReader.js b/node_modules/react-native/Libraries/Blob/FileReader.js
index 57b3093..e1fef89 100644
--- a/node_modules/react-native/Libraries/Blob/FileReader.js
+++ b/node_modules/react-native/Libraries/Blob/FileReader.js
@@ -12,6 +12,8 @@ import type Blob from './Blob';

import NativeFileReaderModule from './NativeFileReaderModule';

+import { toByteArray } from 'base64-js';
+
const EventTarget = require('event-target-shim');

type ReadyState =
@@ -74,8 +76,35 @@ class FileReader extends (EventTarget(...READER_EVENTS): any) {
}
}

- readAsArrayBuffer(): any {
- throw new Error('FileReader.readAsArrayBuffer is not implemented');
+ readAsArrayBuffer(blob: ?Blob): any {
+ 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 {

0 comments on commit f153bd1

Please sign in to comment.