Skip to content

Commit 52b1d06

Browse files
committed
Simplify readableStreamToBuffer.
1 parent 5651926 commit 52b1d06

File tree

2 files changed

+2
-25
lines changed

2 files changed

+2
-25
lines changed

src/test/fixtures/node-worker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function getWorkerScript(file: string): string {
2323
onmessage: function () {},
2424
DecompressionStream,
2525
CompressionStream,
26+
Response,
2627
};
2728
2829
vm.runInNewContext(scriptContent, sandbox, { filename: "${file}" });

src/utils/gz.worker.js

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,7 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
async function readableStreamToBuffer(stream) {
6-
const reader = stream.getReader();
7-
const chunks = [];
8-
9-
try {
10-
while (true) {
11-
const { done, value } = await reader.read();
12-
if (done) break;
13-
if (value) {
14-
chunks.push(value);
15-
}
16-
}
17-
} finally {
18-
reader.releaseLock();
19-
}
20-
21-
// Calculate total length and combine chunks
22-
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
23-
const result = new Uint8Array(totalLength);
24-
let offset = 0;
25-
for (const chunk of chunks) {
26-
result.set(chunk, offset);
27-
offset += chunk.length;
28-
}
29-
30-
return result;
6+
return new Uint8Array(await new Response(stream).arrayBuffer());
317
}
328

339
onmessage = async (e) => {

0 commit comments

Comments
 (0)