Skip to content

Commit 6ebb08a

Browse files
committed
fix: handle blobs as stream when FileReader.readAsArrayBuffer is available
1 parent d36fe07 commit 6ebb08a

File tree

5 files changed

+65
-15
lines changed

5 files changed

+65
-15
lines changed

package-lock.json

+38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@
3636
"querystring": "^0.2.0",
3737
"react-native-polyfill-globals": "^2.0.0",
3838
"react-native-test-runner": "^2.0.0",
39+
"react-native-url-polyfill": "^1.2.0",
3940
"standard-version": "^9.1.0",
41+
"text-encoding": "^0.7.0",
42+
"web-streams-polyfill": "^3.0.1",
4043
"zora": "^4.0.2"
4144
},
4245
"files": [

src/StreamBlobResponse.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
import BlobManager from "react-native/Libraries/Blob/BlobManager";
22
import Response from "./Response";
3-
// import { createBlobReader } from "./utils";
3+
import { createBlobReader } from "./utils";
44

55
class StreamBlobResponse {
66
constructor(blobData, stream, streamController, options) {
77
const blob = BlobManager.createFromOptions(blobData);
88
this._blobData = blobData;
99
this._blobResponse = new Response(blob, options);
10-
// this._streamResponse = new Response(stream, options);
1110

12-
// return createBlobReader(blob)
13-
// .readAsArrayBuffer()
14-
// .then((arrayBuffer) => {
15-
// this._arrayBufferResponse = new Response(arrayBuffer, options);
16-
// streamController.enqueue(new Uint8Array(arrayBuffer));
17-
18-
// return this;
19-
// });
11+
return createBlobReader(blob)
12+
.readAsArrayBuffer()
13+
.then((arrayBuffer) => {
14+
this._streamResponse = new Response(stream, options);
15+
this._arrayBufferResponse = new Response(arrayBuffer, options);
16+
streamController.enqueue(new Uint8Array(arrayBuffer));
17+
18+
return this;
19+
})
20+
.catch(() => {
21+
return this;
22+
});
2023
}
2124

2225
get bodyUsed() {

src/utils.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ function createBlobReader(blob) {
1010
});
1111

1212
return {
13-
readAsArrayBuffer: () => {
13+
readAsArrayBuffer: async () => {
1414
reader.readAsArrayBuffer(blob);
1515
return fileReaderReady;
1616
},
17-
readAsText: () => {
17+
readAsText: async () => {
1818
reader.readAsText(blob);
1919
return fileReaderReady;
2020
},

test/index.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import "react-native-polyfill-globals/auto";
21
import { Platform } from "react-native";
2+
import { polyfill as polyfillEncoding } from "react-native-polyfill-globals/src/encoding";
3+
import { polyfill as polyfillReadableStream } from "react-native-polyfill-globals/src/readable-stream";
4+
import { polyfill as polyfillURL } from "react-native-polyfill-globals/src/url";
35
import { test } from "zora";
46
import delay from "delay";
57
import { Headers, Request, Response, fetch } from "../";
68

9+
polyfillEncoding();
10+
polyfillReadableStream();
11+
polyfillURL();
12+
713
const BASE_URL = Platform.select({
814
android: "http://10.0.2.2:8082",
915
ios: "http://localhost:8082",
@@ -21,11 +27,11 @@ function createBlobReader(blob) {
2127
});
2228

2329
return {
24-
readAsArrayBuffer: () => {
30+
readAsArrayBuffer: async () => {
2531
reader.readAsArrayBuffer(blob);
2632
return fileReaderReady;
2733
},
28-
readAsText: () => {
34+
readAsText: async () => {
2935
reader.readAsText(blob);
3036
return fileReaderReady;
3137
},

0 commit comments

Comments
 (0)