"use strict"; const fs = require("node:fs"); const path = require("node:path"); const MAX_DOWNLOADS = 500; const PROGRESS_PERSIST_INTERVAL_MS = 1000; const STATUSES = new Set(["waiting", "progressing", "completed", "cancelled", "interrupted"]); function normalizeDownload(raw) { if (!raw || typeof raw !== "object" || typeof raw.id !== "string" || !raw.id || raw.id.length > 128 || typeof raw.hostId !== "string" || !raw.hostId || raw.hostId.length > 256) throw new Error("Invalid download"); return { id: raw.id, hostId: raw.hostId, filename: String(raw.filename || "download").slice(0, 255), url: String(raw.url || "").slice(0, 4096), savePath: typeof raw.savePath === "string" ? raw.savePath.slice(0, 4096) : "", status: STATUSES.has(raw.status) ? raw.status : "interrupted", received: Number.isSafeInteger(raw.received) && raw.received >= 0 ? raw.received : 0, total: Number.isSafeInteger(raw.total) && raw.total >= 0 ? raw.total : 0, startedAt: Number.isSafeInteger(raw.startedAt) && raw.startedAt > 0 ? raw.startedAt : Date.now(), endedAt: Number.isSafeInteger(raw.endedAt) && raw.endedAt > 0 ? raw.endedAt : null, }; } function createDownloadStore(app) { const file = path.join(app.getPath("userData"), "host-browser-downloads.json"); let items = []; try { const parsed = JSON.parse(fs.readFileSync(file, "utf8")); if (Array.isArray(parsed)) { items = parsed.slice(0, MAX_DOWNLOADS).flatMap((raw) => { try { const item = normalizeDownload(raw); if (item.status === "progressing" || item.status === "waiting") item.status = "interrupted"; return [item]; } catch { return []; } }); } } catch (error) { if (error?.code !== "ENOENT") console.warn("[HostBrowser] Could not read downloads:", error?.message); } let dirty = false; let progressTimer = null; const persist = () => { if (progressTimer !== null) clearTimeout(progressTimer); progressTimer = null; if (!dirty) return; fs.mkdirSync(path.dirname(file), { recursive: true }); const temporary = `${file}.${process.pid}.tmp`; fs.writeFileSync(temporary, JSON.stringify(items), { mode: 0o600 }); fs.renameSync(temporary, file); dirty = false; }; const scheduleProgressPersist = () => { if (progressTimer !== null) return; progressTimer = setTimeout(() => { progressTimer = null; try { persist(); } catch (error) { // Keep dirty state for the next update or shutdown, without a retry loop. console.warn("[HostBrowser] Could not save download progress:", error?.message); } }, PROGRESS_PERSIST_INTERVAL_MS); progressTimer.unref?.(); }; const isMissing = (item) => item.status === "completed" && (!item.savePath || !fs.existsSync(item.savePath)); return { list(hostId) { return items.filter((item) => item.hostId === hostId) .sort((a, b) => b.startedAt - a.startedAt) .map((item) => ({ ...item, missing: isMissing(item) })); }, get(hostId, id) { return items.find((item) => item.hostId === hostId && item.id === id) || null; }, flush: persist, upsert(raw, { deferProgress = false } = {}) { const next = normalizeDownload(raw); const index = items.findIndex((item) => item.id === next.id); if (index >= 0 && items[index].hostId !== next.hostId) throw new Error("Download belongs to another host"); const previous = items[index]; // Only byte progress is replaceable. New records, state transitions and // recovery metadata remain durable before returning to the caller. const replaceableProgress = deferProgress && previous?.status === "progressing" && next.status === "progressing" && ["filename", "url", "savePath", "total", "startedAt", "endedAt"].every((key) => previous[key] === next[key]); if (index >= 0) items[index] = next; else items.unshift(next); items = items.sort((a, b) => b.startedAt - a.startedAt).slice(0, MAX_DOWNLOADS); dirty = true; if (replaceableProgress) scheduleProgressPersist(); else persist(); return next; }, remove(hostId, id) { const next = items.filter((item) => item.hostId !== hostId || item.id !== id || ["waiting", "progressing"].includes(item.status)); if (next.length === items.length) return false; items = next; dirty = true; persist(); return true; }, clearFinished(hostId) { const next = items.filter((item) => item.hostId !== hostId || ["waiting", "progressing"].includes(item.status)); if (next.length !== items.length) { items = next; dirty = true; persist(); } }, }; } module.exports = { createDownloadStore };