-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Patch react-native to support read ArrayBuffer from blob
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |