ported list download code to JS

This commit is contained in:
Viet Huynh
2023-09-17 16:48:34 +07:00
parent 083335278c
commit 245cc2e8cf
7 changed files with 42 additions and 22 deletions
+28 -1
View File
@@ -1,7 +1,12 @@
import { once } from "events";
import { createReadStream } from "fs";
import { createReadStream, createWriteStream } from "fs";
import { basename } from "path";
import { createInterface } from "readline";
import { WritableStream } from "stream/web";
if (!globalThis.fetch) {
globalThis.fetch = (await import("node-fetch")).default;
}
/**
* Sleeps for a specified amount of time.
@@ -46,6 +51,28 @@ export const isComment = (value) =>
value.startsWith("/*") ||
value.startsWith("*/");
/**
* Downloads files and concatenates them into one file.
* @param {string} filePath The path to the file being written to.
* @param {string[]} urls The URLs to the files to be downloaded.
*/
export const downloadFiles = async (filePath, urls) => {
const writeStream = createWriteStream(filePath, { flags: "a" });
const writableStream = new WritableStream({
write(chunk) {
writeStream.write(chunk);
},
});
const responses = await Promise.all(urls.map((url) => fetch(url)));
for (const response of responses) {
await response.body?.pipeTo(writableStream, { preventClose: true });
writeStream.write("\n");
}
await writableStream.close();
};
/**
* @callback onLine
* @param {string} line The current line.