Files
Netcatty/electron/hostBrowser/downloadStore.cjs
T
陈大猫andCodex b6bff4df6b feat(browser): browse host services from terminal side panels (#3701)
* Codex worktree snapshot: startup-cleanup

Co-authored-by: Codex

* fix(browser): close malformed proxy connections without a reply

* feat(browser): embed host browsing in terminal side panels

* style(browser): blend compact browser chrome into terminal panels

* feat(browser): open terminal links internally with a system browser preference

* fix(browser): retain the first terminal link during panel mount replay

* fix(browser): release contexts when their split pane closes

* style(browser): remove the downloads panel shadow

* fix(browser): ignore late downloads after panel closure

* fix(browser): mount browser content on terminal auto-open

* fix(browser): preserve empty-vault recovery with bookmarks

* fix(browser): expose actions for supported terminal types

* fix(browser): retain open requests before the panel mounts

* fix(browser): align vault actions with supported hosts

* fix(browser): share download controls across host contexts

* fix(browser): keep vault browser requests on visible sessions

* fix(browser): release contexts after workspace detach

* fix(browser): enforce WebRTC routing on page contents

* fix(browser): skip unsupported sessions in open requests

* style(browser): encode glyphs in ASCII-safe source
2026-10-17 02:26:36 +08:00

82 lines
3.4 KiB
JavaScript

"use strict";
const fs = require("node:fs");
const path = require("node:path");
const MAX_DOWNLOADS = 500;
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);
}
const persist = () => {
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);
};
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; },
upsert(raw) {
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");
if (index >= 0) items[index] = next;
else items.unshift(next);
items = items.sort((a, b) => b.startedAt - a.startedAt).slice(0, MAX_DOWNLOADS);
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;
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; persist(); }
},
};
}
module.exports = { createDownloadStore };