mirror of
https://github.com/Nezumi-2711/cloudflare-gateway-pihole-scripts.git
synced 2026-09-22 13:38:37 +00:00
fix node-fetch vs. fetch compatibility
This commit is contained in:
+10
-11
@@ -1,8 +1,7 @@
|
|||||||
import { once } from "events";
|
import { once } from "events";
|
||||||
import { createReadStream, createWriteStream } from "fs";
|
import { ReadStream, createReadStream, createWriteStream } from "fs";
|
||||||
import { basename } from "path";
|
import { basename } from "path";
|
||||||
import { createInterface } from "readline";
|
import { createInterface } from "readline";
|
||||||
import { WritableStream } from "stream/web";
|
|
||||||
|
|
||||||
if (!globalThis.fetch) {
|
if (!globalThis.fetch) {
|
||||||
globalThis.fetch = (await import("node-fetch")).default;
|
globalThis.fetch = (await import("node-fetch")).default;
|
||||||
@@ -58,19 +57,19 @@ export const isComment = (value) =>
|
|||||||
*/
|
*/
|
||||||
export const downloadFiles = async (filePath, urls) => {
|
export const downloadFiles = async (filePath, urls) => {
|
||||||
const writeStream = createWriteStream(filePath, { flags: "a" });
|
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)));
|
const responses = await Promise.all(urls.map((url) => fetch(url)));
|
||||||
|
|
||||||
for (const response of responses) {
|
for (const response of responses) {
|
||||||
await response.body?.pipeTo(writableStream, { preventClose: true });
|
const readable = ReadStream.from(response.body, {
|
||||||
writeStream.write("\n");
|
autoDestroy: true,
|
||||||
}
|
});
|
||||||
|
|
||||||
await writableStream.close();
|
readable.on("end", () => {
|
||||||
|
writeStream.write("\n");
|
||||||
|
});
|
||||||
|
|
||||||
|
readable.pipe(writeStream, { end: false });
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user