Skip to content

Commit

Permalink
fix: handle blobs as stream when FileReader.readAsArrayBuffer is avai…
Browse files Browse the repository at this point in the history
…lable
  • Loading branch information
acostalima committed Jan 25, 2021
1 parent d36fe07 commit 6ebb08a
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 15 deletions.
38 changes: 38 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
"querystring": "^0.2.0",
"react-native-polyfill-globals": "^2.0.0",
"react-native-test-runner": "^2.0.0",
"react-native-url-polyfill": "^1.2.0",
"standard-version": "^9.1.0",
"text-encoding": "^0.7.0",
"web-streams-polyfill": "^3.0.1",
"zora": "^4.0.2"
},
"files": [
Expand Down
23 changes: 13 additions & 10 deletions src/StreamBlobResponse.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import BlobManager from "react-native/Libraries/Blob/BlobManager";
import Response from "./Response";
// import { createBlobReader } from "./utils";
import { createBlobReader } from "./utils";

class StreamBlobResponse {
constructor(blobData, stream, streamController, options) {
const blob = BlobManager.createFromOptions(blobData);
this._blobData = blobData;
this._blobResponse = new Response(blob, options);
// this._streamResponse = new Response(stream, options);

// return createBlobReader(blob)
// .readAsArrayBuffer()
// .then((arrayBuffer) => {
// this._arrayBufferResponse = new Response(arrayBuffer, options);
// streamController.enqueue(new Uint8Array(arrayBuffer));

// return this;
// });
return createBlobReader(blob)
.readAsArrayBuffer()
.then((arrayBuffer) => {
this._streamResponse = new Response(stream, options);
this._arrayBufferResponse = new Response(arrayBuffer, options);
streamController.enqueue(new Uint8Array(arrayBuffer));

return this;
})
.catch(() => {
return this;
});
}

get bodyUsed() {
Expand Down
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ function createBlobReader(blob) {
});

return {
readAsArrayBuffer: () => {
readAsArrayBuffer: async () => {
reader.readAsArrayBuffer(blob);
return fileReaderReady;
},
readAsText: () => {
readAsText: async () => {
reader.readAsText(blob);
return fileReaderReady;
},
Expand Down
12 changes: 9 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import "react-native-polyfill-globals/auto";
import { Platform } from "react-native";
import { polyfill as polyfillEncoding } from "react-native-polyfill-globals/src/encoding";
import { polyfill as polyfillReadableStream } from "react-native-polyfill-globals/src/readable-stream";
import { polyfill as polyfillURL } from "react-native-polyfill-globals/src/url";
import { test } from "zora";
import delay from "delay";
import { Headers, Request, Response, fetch } from "../";

polyfillEncoding();
polyfillReadableStream();
polyfillURL();

const BASE_URL = Platform.select({
android: "http://10.0.2.2:8082",
ios: "http://localhost:8082",
Expand All @@ -21,11 +27,11 @@ function createBlobReader(blob) {
});

return {
readAsArrayBuffer: () => {
readAsArrayBuffer: async () => {
reader.readAsArrayBuffer(blob);
return fileReaderReady;
},
readAsText: () => {
readAsText: async () => {
reader.readAsText(blob);
return fileReaderReady;
},
Expand Down

0 comments on commit 6ebb08a

Please sign in to comment.