Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fetch-http-handler): add polyfill to collect Blob in react-native #1483

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/metal-emus-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/fetch-http-handler": patch
---

Add polyfill to collect Blob in react-native environments
35 changes: 34 additions & 1 deletion packages/fetch-http-handler/src/stream-collector.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { StreamCollector } from "@smithy/types";
import { fromBase64 } from "@smithy/util-base64";

export const streamCollector: StreamCollector = async (stream: Blob | ReadableStream): Promise<Uint8Array> => {
if ((typeof Blob === "function" && stream instanceof Blob) || stream.constructor?.name === "Blob") {
return new Uint8Array(await (stream as Blob).arrayBuffer());
if (Blob.prototype.arrayBuffer !== undefined) {
return new Uint8Array(await (stream as Blob).arrayBuffer());
}
return collectBlob(stream as Blob);
}

return collectStream(stream as ReadableStream);
};

async function collectBlob(blob: Blob): Promise<Uint8Array> {
const base64 = await readToBase64(blob);
const arrayBuffer = fromBase64(base64);
return new Uint8Array(arrayBuffer);
}

async function collectStream(stream: ReadableStream): Promise<Uint8Array> {
const chunks = [];
const reader = stream.getReader();
@@ -32,3 +42,26 @@ async function collectStream(stream: ReadableStream): Promise<Uint8Array> {

return collected;
}

function readToBase64(blob: Blob): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => {
// reference: https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
// response from readAsDataURL is always prepended with "data:*/*;base64,"
if (reader.readyState !== 2) {
return reject(new Error("Reader aborted too early"));
}
const result = (reader.result ?? "") as string;
// Response can include only 'data:' for empty blob, return empty string in this case.
// Otherwise, return the string after ','
const commaIndex = result.indexOf(",");
const dataOffset = commaIndex > -1 ? commaIndex + 1 : result.length;
resolve(result.substring(dataOffset));
};
reader.onabort = () => reject(new Error("Read aborted"));
reader.onerror = () => reject(reader.error);
// reader.readAsArrayBuffer is not always available
reader.readAsDataURL(blob);
});
}
kuhe marked this conversation as resolved.
Show resolved Hide resolved