Skip to content

Commit 9f3ddeb

Browse files
richardddkuhe
authored andcommitted
Update stream-collector.ts
1 parent 536fb7f commit 9f3ddeb

File tree

1 file changed

+1
-31
lines changed

1 file changed

+1
-31
lines changed
Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
import { StreamCollector } from "@smithy/types";
2-
import { fromBase64 } from "@smithy/util-base64";
32

43
//reference: https://snack.expo.io/r1JCSWRGU
54
export const streamCollector: StreamCollector = (stream: Blob | ReadableStream): Promise<Uint8Array> => {
65
if (typeof Blob === "function" && stream instanceof Blob) {
7-
return collectBlob(stream);
6+
return new Uint8Array(await stream.arrayBuffer());
87
}
98

109
return collectStream(stream as ReadableStream);
1110
};
1211

13-
async function collectBlob(blob: Blob): Promise<Uint8Array> {
14-
const base64 = await readToBase64(blob);
15-
const arrayBuffer = fromBase64(base64);
16-
return new Uint8Array(arrayBuffer);
17-
}
18-
1912
async function collectStream(stream: ReadableStream): Promise<Uint8Array> {
2013
const chunks = [];
2114
const reader = stream.getReader();
@@ -40,26 +33,3 @@ async function collectStream(stream: ReadableStream): Promise<Uint8Array> {
4033

4134
return collected;
4235
}
43-
44-
function readToBase64(blob: Blob): Promise<string> {
45-
return new Promise((resolve, reject) => {
46-
const reader = new FileReader();
47-
reader.onloadend = () => {
48-
// reference: https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
49-
// response from readAsDataURL is always prepended with "data:*/*;base64,"
50-
if (reader.readyState !== 2) {
51-
return reject(new Error("Reader aborted too early"));
52-
}
53-
const result = (reader.result ?? "") as string;
54-
// Response can include only 'data:' for empty blob, return empty string in this case.
55-
// Otherwise, return the string after ','
56-
const commaIndex = result.indexOf(",");
57-
const dataOffset = commaIndex > -1 ? commaIndex + 1 : result.length;
58-
resolve(result.substring(dataOffset));
59-
};
60-
reader.onabort = () => reject(new Error("Read aborted"));
61-
reader.onerror = () => reject(reader.error);
62-
// reader.readAsArrayBuffer is not always available
63-
reader.readAsDataURL(blob);
64-
});
65-
}

0 commit comments

Comments
 (0)