Skip to content

Commit 5579ceb

Browse files
authored
Update stream-collector.ts
1 parent 3e30223 commit 5579ceb

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
let res = new Uint8Array(0);
2114
const reader = stream.getReader();
@@ -32,26 +25,3 @@ async function collectStream(stream: ReadableStream): Promise<Uint8Array> {
3225
}
3326
return res;
3427
}
35-
36-
function readToBase64(blob: Blob): Promise<string> {
37-
return new Promise((resolve, reject) => {
38-
const reader = new FileReader();
39-
reader.onloadend = () => {
40-
// reference: https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
41-
// response from readAsDataURL is always prepended with "data:*/*;base64,"
42-
if (reader.readyState !== 2) {
43-
return reject(new Error("Reader aborted too early"));
44-
}
45-
const result = (reader.result ?? "") as string;
46-
// Response can include only 'data:' for empty blob, return empty string in this case.
47-
// Otherwise, return the string after ','
48-
const commaIndex = result.indexOf(",");
49-
const dataOffset = commaIndex > -1 ? commaIndex + 1 : result.length;
50-
resolve(result.substring(dataOffset));
51-
};
52-
reader.onabort = () => reject(new Error("Read aborted"));
53-
reader.onerror = () => reject(reader.error);
54-
// reader.readAsArrayBuffer is not always available
55-
reader.readAsDataURL(blob);
56-
});
57-
}

0 commit comments

Comments
 (0)