mirror of
https://github.com/birbwatcher/wayback-machine-downloader.git
synced 2026-01-29 01:40:41 +00:00
fix: add defensive stream handling for snapshot responses
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Wayback Machine Downloader 0.3.0 by WhitelightSEO
|
||||
* Wayback Machine Downloader 0.3.1 by WhitelightSEO
|
||||
* Run: node index.js
|
||||
*/
|
||||
|
||||
|
||||
@@ -179,9 +179,32 @@ class WaybackMachineDownloader {
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
const ws = fs.createWriteStream(filePath);
|
||||
Readable.fromWeb(res.body).pipe(ws);
|
||||
ws.on("finish", resolve);
|
||||
ws.on("error", reject);
|
||||
const rs = Readable.fromWeb(res.body);
|
||||
|
||||
let settled = false;
|
||||
const cleanupPartialFile = async () => {
|
||||
try {
|
||||
await fs.promises.rm(filePath, { force: true });
|
||||
} catch {}
|
||||
};
|
||||
|
||||
const handleStreamError = (err) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
rs.destroy();
|
||||
ws.destroy();
|
||||
cleanupPartialFile().finally(() => reject(err));
|
||||
};
|
||||
|
||||
rs.on("error", handleStreamError);
|
||||
ws.on("error", handleStreamError);
|
||||
ws.on("finish", () => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
resolve();
|
||||
});
|
||||
|
||||
rs.pipe(ws);
|
||||
});
|
||||
|
||||
const contentType = res.headers.get("content-type") || "";
|
||||
|
||||
Reference in New Issue
Block a user