From acc66157a9c5834fe20c017cfe3e37cb0280a995 Mon Sep 17 00:00:00 2001 From: Tim <57221600+TiltedToast@users.noreply.github.com> Date: Fri, 1 Mar 2024 02:07:34 +0100 Subject: [PATCH] incrementally write to file in downloadURL --- src/helpers/utils.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/helpers/utils.ts b/src/helpers/utils.ts index 27a111f..d978d39 100644 --- a/src/helpers/utils.ts +++ b/src/helpers/utils.ts @@ -391,10 +391,21 @@ export async function downloadURL(url: string, saveLocation: string) { return `Failed to download <${url}>\n${response.status} ${response.statusText}`; } - const buffer = await response.arrayBuffer().catch(console.error); + if (!response.body) return "Failed to extract contents from response"; - if (!buffer) return "Failed to extract contents from response"; - await Bun.write(absSaveLocation, buffer); + try { + const writer = Bun.file(absSaveLocation).writer(); + + for await (const chunk of response.body) { + writer.write(chunk as Uint8Array); + } + + await writer.flush(); + await writer.end(); + } catch (err) { + console.error(err); + return `Failed to write to file`; + } } /**