From d6de2582223141a3d76efc19f6271cb253fcfe03 Mon Sep 17 00:00:00 2001 From: Vince Au Date: Wed, 11 May 2022 22:29:31 +1000 Subject: [PATCH] fix: hopefully fix download issues for good --- src/utils/download.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/utils/download.ts b/src/utils/download.ts index 673590ac3..f0fed28fe 100644 --- a/src/utils/download.ts +++ b/src/utils/download.ts @@ -45,15 +45,28 @@ export async function download(options: { if (contentLength) { totalBytes = parseInt(contentLength); } + const file = fs.createWriteStream(destinationFile, { flags: "wx" }); + file + .on("close", () => { + resolve(); + }) + .on("error", (err) => { + file.destroy(); + reject(err); + }); + res .on("data", (chunk) => { - file.write(chunk); - transferredBytes += chunk.length; - onProgress && onProgress({ transferredBytes, totalBytes }); + file.write(chunk, (err) => { + if (!err) { + transferredBytes += chunk.length; + onProgress && onProgress({ transferredBytes, totalBytes }); + } + }); }) .on("end", () => { - file.end(() => resolve()); + file.end(); }) .on("error", (err) => { file.destroy();