Skip to content

Commit

Permalink
feat(Blob): ArrayBuffer => Blob & FileReader#readAsArrayBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyJasonBennett committed Jun 15, 2022
1 parent c78baba commit facd9bb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
5 changes: 2 additions & 3 deletions Libraries/Blob/BlobManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const BlobRegistry = require('./BlobRegistry');
import type {BlobData, BlobOptions, BlobCollector} from './BlobTypes';
import NativeBlobModule from './NativeBlobModule';
import invariant from 'invariant';
import {getBlobForArrayBuffer} from 'react-native-blob-jsi-helper';

/*eslint-disable no-bitwise */
/*eslint-disable eqeqeq */
Expand Down Expand Up @@ -69,9 +70,7 @@ class BlobManager {
part instanceof ArrayBuffer ||
(global.ArrayBufferView && part instanceof global.ArrayBufferView)
) {
throw new Error(
"Creating blobs from 'ArrayBuffer' and 'ArrayBufferView' are not supported",
);
return getBlobForArrayBuffer(part);
}
if (part instanceof Blob) {
return {
Expand Down
28 changes: 27 additions & 1 deletion Libraries/Blob/FileReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

const Blob = require('./Blob');
const EventTarget = require('event-target-shim');
const {toByteArray} = require('base64-js');

import NativeFileReaderModule from './NativeFileReaderModule';

Expand Down Expand Up @@ -74,7 +75,32 @@ class FileReader extends (EventTarget(...READER_EVENTS): any) {
}

readAsArrayBuffer() {
throw new Error('FileReader.readAsArrayBuffer is not implemented');
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(
(dataUrl: string) => {
if (this._aborted) {
return;
}
const base64 = dataUrl.split(',')[1];
const buffer = toByteArray(base64);
this._result = buffer;
this._setReadyState(DONE);
},
error => {
if (this._aborted) {
return;
}
this._error = error;
this._setReadyState(DONE);
},
);
}

readAsDataURL(blob: ?Blob) {
Expand Down

0 comments on commit facd9bb

Please sign in to comment.