From 9389039c7ac0e9e3272a4c1bdbda36b464cd8f87 Mon Sep 17 00:00:00 2001 From: fyears Date: Sat, 27 Nov 2021 14:43:19 +0800 Subject: [PATCH] avoid using for await --- src/s3.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/s3.ts b/src/s3.ts index 87ecdb8..bf467c3 100644 --- a/src/s3.ts +++ b/src/s3.ts @@ -225,12 +225,12 @@ const getObjectBodyToArrayBuffer = async ( b: Readable | ReadableStream | Blob ) => { if (b instanceof Readable) { - const chunks: Uint8Array[] = []; - for await (let chunk of b) { - chunks.push(chunk); - } - const buf = Buffer.concat(chunks); - return bufferToArrayBuffer(buf); + return (await new Promise((resolve, reject) => { + const chunks: Uint8Array[] = []; + b.on("data", (chunk) => chunks.push(chunk)); + b.on("error", reject); + b.on("end", () => resolve(bufferToArrayBuffer(Buffer.concat(chunks)))); + })) as ArrayBuffer; } else if (b instanceof ReadableStream) { return await new Response(b, {}).arrayBuffer(); } else if (b instanceof Blob) {