Files
Netcatty/electron/hostBrowser/siteIconStore.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

81 lines
3.2 KiB
JavaScript

"use strict";
const fs = require("node:fs");
const path = require("node:path");
const MAX_ICONS = 1000;
const MAX_ICON_BYTES = 64 * 1024;
const IMAGE_MIMES = new Set(["image/png", "image/jpeg", "image/webp", "image/gif", "image/x-icon", "image/vnd.microsoft.icon"]);
function iconOrigin(url) {
try {
const parsed = new URL(url);
return parsed.protocol === "http:" || parsed.protocol === "https:" ? parsed.origin : null;
} catch { return null; }
}
function validImageDataUrl(value) {
if (typeof value !== "string" || value.length > MAX_ICON_BYTES * 1.4) return false;
const match = /^data:([^;,]+);base64,([A-Za-z0-9+/]+={0,2})$/.exec(value);
if (!match || !IMAGE_MIMES.has(match[1])) return false;
return Buffer.byteLength(match[2], "base64") <= MAX_ICON_BYTES;
}
function normalizeIcon(raw) {
if (!raw || typeof raw !== "object") throw new Error("Invalid website icon");
const hostId = typeof raw.hostId === "string" ? raw.hostId.slice(0, 256) : "";
const origin = iconOrigin(raw.origin);
if (!hostId || !origin) throw new Error("Invalid website icon target");
const mode = raw.mode === "custom" ? "custom" : "auto";
const dataUrl = validImageDataUrl(raw.dataUrl) ? raw.dataUrl : null;
const glyph = typeof raw.glyph === "string" ? raw.glyph.trim().slice(0, 4) : "";
const color = /^#[0-9a-f]{6}$/i.test(raw.color) ? raw.color : "#247969";
if (!dataUrl && !glyph) throw new Error("Website icon is empty");
return { hostId, origin, mode, dataUrl, glyph, color };
}
function createSiteIconStore(app) {
const file = path.join(app.getPath("userData"), "host-browser-site-icons.json");
const icons = new Map();
try {
const parsed = JSON.parse(fs.readFileSync(file, "utf8"));
if (Array.isArray(parsed)) {
for (const raw of parsed.slice(0, MAX_ICONS)) {
try { const icon = normalizeIcon(raw); icons.set(`${icon.hostId}\n${icon.origin}`, icon); } catch {}
}
}
} catch (error) {
if (error?.code !== "ENOENT") console.warn("[HostBrowser] Could not read website icons:", error?.message);
}
const persist = () => {
fs.mkdirSync(path.dirname(file), { recursive: true });
const temporary = `${file}.${process.pid}.tmp`;
fs.writeFileSync(temporary, JSON.stringify([...icons.values()]), { mode: 0o600 });
fs.renameSync(temporary, file);
};
const keyFor = (hostId, url) => {
const origin = iconOrigin(url);
return origin && typeof hostId === "string" ? `${hostId}\n${origin}` : null;
};
return {
get(hostId, url) { const key = keyFor(hostId, url); return key ? icons.get(key) || null : null; },
set(hostId, url, input) {
const key = keyFor(hostId, url);
if (!key) throw new Error("Invalid website address");
const previous = icons.get(key);
if (input.mode === "auto" && previous?.mode === "custom") return previous;
if (!previous && icons.size >= MAX_ICONS) throw new Error("Website icon limit reached");
const next = normalizeIcon({ ...input, hostId, origin: iconOrigin(url) });
icons.set(key, next);
persist();
return next;
},
reset(hostId, url) {
const key = keyFor(hostId, url);
if (key && icons.delete(key)) persist();
},
};
}
module.exports = { createSiteIconStore, iconOrigin, validImageDataUrl };