Skip to content

Commit 572dd76

Browse files
Revert "BlobManager: implement Blob from ArrayBuffer (#39276)" (#41170)
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
1 parent 37e509f commit 572dd76

File tree

4 files changed

+11
-23
lines changed

4 files changed

+11
-23
lines changed

packages/react-native/Libraries/Blob/Blob.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ class Blob {
5757
* Currently we only support creating Blobs from other Blobs.
5858
* Reference: https://developer.mozilla.org/en-US/docs/Web/API/Blob/Blob
5959
*/
60-
constructor(
61-
parts: Array<$ArrayBufferView | ArrayBuffer | Blob | string> = [],
62-
options?: BlobOptions,
63-
) {
60+
constructor(parts: Array<Blob | string> = [], options?: BlobOptions) {
6461
const BlobManager = require('./BlobManager');
6562
this.data = BlobManager.createFromParts(parts, options).data;
6663
}

packages/react-native/Libraries/Blob/BlobManager.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import type {BlobCollector, BlobData, BlobOptions} from './BlobTypes';
1212

1313
import NativeBlobModule from './NativeBlobModule';
14-
import {fromByteArray} from 'base64-js';
1514
import invariant from 'invariant';
1615

1716
const Blob = require('./Blob');
@@ -60,20 +59,19 @@ class BlobManager {
6059
* Create blob from existing array of blobs.
6160
*/
6261
static createFromParts(
63-
parts: Array<$ArrayBufferView | ArrayBuffer | Blob | string>,
62+
parts: Array<Blob | string>,
6463
options?: BlobOptions,
6564
): Blob {
6665
invariant(NativeBlobModule, 'NativeBlobModule is available.');
6766

6867
const blobId = uuidv4();
6968
const items = parts.map(part => {
7069
if (part instanceof ArrayBuffer || ArrayBuffer.isView(part)) {
71-
return {
72-
// $FlowFixMe[incompatible-cast]
73-
data: fromByteArray(new Uint8Array((part: ArrayBuffer))),
74-
type: 'string',
75-
};
76-
} else if (part instanceof Blob) {
70+
throw new Error(
71+
"Creating blobs from 'ArrayBuffer' and 'ArrayBufferView' are not supported",
72+
);
73+
}
74+
if (part instanceof Blob) {
7775
return {
7876
data: part.data,
7977
type: 'blob',

packages/react-native/Libraries/Blob/File.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class File extends Blob {
2323
* Constructor for JS consumers.
2424
*/
2525
constructor(
26-
parts: Array<$ArrayBufferView | ArrayBuffer | Blob | string>,
26+
parts: Array<Blob | string>,
2727
name: string,
2828
options?: BlobOptions,
2929
) {

packages/react-native/Libraries/Blob/__tests__/Blob-test.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ jest.setMock('../../BatchedBridge/NativeModules', {
1515
});
1616

1717
const Blob = require('../Blob');
18-
const {fromByteArray} = require('base64-js');
1918

2019
describe('Blob', function () {
2120
it('should create empty blob', () => {
@@ -27,7 +26,7 @@ describe('Blob', function () {
2726
expect(blob.type).toBe('');
2827
});
2928

30-
it('should create blob from ArrayBuffer, other blobs, strings', () => {
29+
it('should create blob from other blobs and strings', () => {
3130
const blobA = new Blob();
3231
const blobB = new Blob();
3332
const textA = 'i \u2665 dogs';
@@ -44,20 +43,14 @@ describe('Blob', function () {
4443
blobA.data.size = 34540;
4544
blobB.data.size = 65452;
4645

47-
const buffer = new ArrayBuffer(4);
48-
49-
const blob = new Blob([blobA, blobB, textA, textB, textC, buffer]);
46+
const blob = new Blob([blobA, blobB, textA, textB, textC]);
5047

5148
expect(blob.size).toBe(
5249
blobA.size +
5350
blobB.size +
5451
global.Buffer.byteLength(textA, 'UTF-8') +
5552
global.Buffer.byteLength(textB, 'UTF-8') +
56-
global.Buffer.byteLength(textC, 'UTF-8') +
57-
global.Buffer.byteLength(
58-
fromByteArray(new Uint8Array(buffer)),
59-
'UTF-8',
60-
),
53+
global.Buffer.byteLength(textC, 'UTF-8'),
6154
);
6255
expect(blob.type).toBe('');
6356
});

0 commit comments

Comments
 (0)