From 6eb55bf6a19d47786257189b5207c7736253c4cf Mon Sep 17 00:00:00 2001 From: Viet Huynh Date: Sun, 19 Nov 2023 16:39:54 +0700 Subject: [PATCH] refactored downloadFiles to a more efficient approach --- lib/utils.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 79ad4ea..1c60b57 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -49,19 +49,16 @@ export const isComment = (value) => * @param {string[]} urls The URLs to the files to be downloaded. */ export const downloadFiles = async (filePath, urls) => { - const writeStream = createWriteStream(filePath, { flags: "a" }); const responses = await Promise.all(urls.map((url) => fetch(url))); for (const response of responses) { - const readable = ReadStream.from(response.body, { - autoDestroy: true, - }); + const writeStream = createWriteStream(filePath, { flags: "a" }); - readable.on("end", () => { - writeStream.write("\n"); - }); - - readable.pipe(writeStream, { end: false }); + ReadStream.from(response.body) + .on("end", () => { + writeStream.write("\n"); + }) + .pipe(writeStream); } };