Skip to content

Commit

Permalink
fix: hopefully fix download issues for good
Browse files Browse the repository at this point in the history
  • Loading branch information
vinceau committed May 11, 2022
1 parent b641203 commit d6de258
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/utils/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit d6de258

Please sign in to comment.