mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-16 20:11:08 +00:00
* 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
92 lines
3.7 KiB
JavaScript
92 lines
3.7 KiB
JavaScript
"use strict";
|
|
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
const { randomUUID } = require("node:crypto");
|
|
const { validImageDataUrl } = require("./siteIconStore.cjs");
|
|
|
|
const MAX_BOOKMARKS = 1000;
|
|
|
|
function normalizeBookmark(raw, previous = null) {
|
|
if (!raw || typeof raw !== "object") throw new Error("Invalid bookmark");
|
|
const url = new URL(String(raw.url || ""));
|
|
if (!["http:", "https:"].includes(url.protocol) || !url.hostname || url.username || url.password) {
|
|
throw new Error("Bookmarks require an HTTP or HTTPS address without credentials");
|
|
}
|
|
const scope = raw.scope === "all-hosts" ? "all-hosts" : "host";
|
|
const hostId = scope === "host" ? String(raw.hostId || "").slice(0, 256) : null;
|
|
if (scope === "host" && !hostId) throw new Error("Host bookmark is missing its host");
|
|
const title = String(raw.title || url.hostname).trim().slice(0, 120);
|
|
const icon = String(raw.icon || title[0] || "\u25c7").trim().slice(0, 4);
|
|
const color = /^#[0-9a-f]{6}$/i.test(raw.color) ? raw.color : "#247969";
|
|
const now = Date.now();
|
|
return {
|
|
id: previous?.id || (typeof raw.id === "string" && raw.id.trim() && raw.id.length <= 128 ? raw.id : randomUUID()),
|
|
title: title || url.hostname,
|
|
url: url.href,
|
|
scope,
|
|
hostId,
|
|
icon: icon || "\u25c7",
|
|
iconMode: raw.iconMode === "custom" ? "custom" : "auto",
|
|
iconDataUrl: raw.iconDataUrl === null ? null
|
|
: validImageDataUrl(raw.iconDataUrl) ? raw.iconDataUrl
|
|
: previous?.iconDataUrl || null,
|
|
color,
|
|
order: Number.isSafeInteger(raw.order) ? raw.order : (previous?.order ?? 0),
|
|
createdAt: previous?.createdAt || (Number.isSafeInteger(raw.createdAt) && raw.createdAt > 0 ? raw.createdAt : now),
|
|
updatedAt: previous ? now : (Number.isSafeInteger(raw.updatedAt) && raw.updatedAt > 0 ? raw.updatedAt : now),
|
|
};
|
|
}
|
|
|
|
function createBookmarkStore(app, onChange = () => {}) {
|
|
const file = path.join(app.getPath("userData"), "host-browser-bookmarks.json");
|
|
let items = [];
|
|
try {
|
|
const parsed = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
if (Array.isArray(parsed) && parsed.length <= MAX_BOOKMARKS) {
|
|
items = parsed.flatMap((item) => {
|
|
try { return [normalizeBookmark(item)]; } catch { return []; }
|
|
});
|
|
}
|
|
} catch (error) {
|
|
if (error?.code !== "ENOENT") console.warn("[HostBrowser] Could not read bookmarks:", 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);
|
|
onChange(items);
|
|
};
|
|
return {
|
|
list: () => items.map((item) => ({ ...item })),
|
|
upsert(input) {
|
|
const index = items.findIndex((item) => item.id === input?.id);
|
|
if (index < 0 && items.length >= MAX_BOOKMARKS) throw new Error("Bookmark limit reached");
|
|
const next = normalizeBookmark(input, index >= 0 ? items[index] : null);
|
|
if (index >= 0) items[index] = next;
|
|
else items.push(next);
|
|
persist();
|
|
return { ...next };
|
|
},
|
|
remove(id) {
|
|
const next = items.filter((item) => item.id !== id);
|
|
if (next.length !== items.length) { items = next; persist(); }
|
|
},
|
|
apply(input) {
|
|
if (!Array.isArray(input) || input.length > MAX_BOOKMARKS) throw new Error("Invalid bookmark list");
|
|
const ids = new Set();
|
|
const next = input.map((item) => {
|
|
const normalized = normalizeBookmark(item);
|
|
if (ids.has(normalized.id)) throw new Error("Duplicate bookmark ID");
|
|
ids.add(normalized.id);
|
|
return normalized;
|
|
});
|
|
items = next;
|
|
persist();
|
|
},
|
|
};
|
|
}
|
|
|
|
module.exports = { createBookmarkStore, normalizeBookmark };
|