Skip to content

Commit 1303244

Browse files
Build Uint8Array from ReadableStream without experimental Response constructor
1 parent dae6d08 commit 1303244

File tree

1 file changed

+16
-5
lines changed
  • packages/stream-collector-browser/src

1 file changed

+16
-5
lines changed
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
import { StreamCollector } from "@aws-sdk/types";
22

3-
export const streamCollector: StreamCollector = (
4-
stream: ReadableStream
3+
export const streamCollector: StreamCollector = async (
4+
stream: ReadableStream<Uint8Array>
55
): Promise<Uint8Array> => {
6-
return new Response(stream)
7-
.arrayBuffer()
8-
.then(arrayBuffer => new Uint8Array(arrayBuffer));
6+
let res = new Uint8Array(0);
7+
const reader = stream.getReader();
8+
let isDone = false;
9+
while(!isDone) {
10+
const { done, value } = await reader.read();
11+
if(value) {
12+
const prior = res;
13+
res = new Uint8Array(prior.length + value.length);
14+
res.set(prior);
15+
res.set(value, prior.length);
16+
}
17+
isDone = done;
18+
}
19+
return res;
920
};

0 commit comments

Comments
 (0)