-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfiles.ts
40 lines (33 loc) · 1002 Bytes
/
files.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
* Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
*/
/**
* Consumes a stream and returns a concatenated array buffer. Useful in
* situations where we need to read the whole file because it forms part of a
* larger payload containing other fields, and we can't modify the underlying
* request structure.
*/
export async function readableStreamToArrayBuffer(
readable: ReadableStream<Uint8Array>,
): Promise<ArrayBuffer> {
const reader = readable.getReader();
const chunks: Uint8Array[] = [];
let totalLength = 0;
let done = false;
while (!done) {
const { value, done: doneReading } = await reader.read();
if (doneReading) {
done = true;
} else {
chunks.push(value);
totalLength += value.length;
}
}
const concatenatedChunks = new Uint8Array(totalLength);
let offset = 0;
for (const chunk of chunks) {
concatenatedChunks.set(chunk, offset);
offset += chunk.length;
}
return concatenatedChunks.buffer;
}