function escape(text) {
return text.replace(/[&<>"]/g, (char) => {
if (char === "&")
return "&";
if (char === "<")
return "<";
if (char === ">")
return ">";
return """;
});
}
function bytes(value) {
if (value < 1024)
return `${value} B`;
if (value < 1024 * 1024)
return `${(value / 1024).toFixed(1)} KB`;
return `${(value / 1024 / 1024).toFixed(2)} MB`;
}
function time(at) {
return new Date(at).toLocaleTimeString("zh-CN", { hour12: false });
}
function sourceClass(source) {
if (source === "main")
return "ok";
if (source === "backup")
return "warn";
if (source === "seed")
return "warn";
return "bad";
}
export function mountApp(root) {
root.innerHTML = `
JSON 原子写
—
—
记录0
体积0
写入0 ms
读取0 ms
备份0
临时文件0
SQLite · node:sqlite
—
—
记录0
体积0
写入0 ms
读取0 ms
日志—
选型基准
单次运行,仅作量级参考
怎么选
先看数据形态,再看性能
- 设置 / 少量文档(<1,000 条):JSON 原子写,简单、可读、易备份。
- 结构化查询 / 频繁写入:
node:sqlite + WAL,单写者、事务。
- better-sqlite3:只在需要扩展或目标 Node 缺少
node:sqlite 时考虑,需处理 ABI。
`;
const versions = root.querySelector('[data-testid="versions"]');
const jsonSource = root.querySelector('[data-testid="json-source"]');
const jsonPath = root.querySelector('[data-testid="json-path"]');
const jsonCount = root.querySelector('[data-testid="json-count"]');
const jsonSize = root.querySelector('[data-testid="json-size"]');
const jsonWrite = root.querySelector('[data-testid="json-write"]');
const jsonRead = root.querySelector('[data-testid="json-read"]');
const jsonBackup = root.querySelector('[data-testid="json-backup"]');
const jsonTmp = root.querySelector('[data-testid="json-tmp"]');
const jsonError = root.querySelector('[data-testid="json-error"]');
const jsonNotes = root.querySelector('[data-testid="json-notes"]');
const sqliteAvailable = root.querySelector('[data-testid="sqlite-available"]');
const sqlitePath = root.querySelector('[data-testid="sqlite-path"]');
const sqliteCount = root.querySelector('[data-testid="sqlite-count"]');
const sqliteSize = root.querySelector('[data-testid="sqlite-size"]');
const sqliteWrite = root.querySelector('[data-testid="sqlite-write"]');
const sqliteRead = root.querySelector('[data-testid="sqlite-read"]');
const sqliteJournal = root.querySelector('[data-testid="sqlite-journal"]');
const sqliteError = root.querySelector('[data-testid="sqlite-error"]');
const sqliteNotes = root.querySelector('[data-testid="sqlite-notes"]');
const pathsEl = root.querySelector('[data-testid="paths"]');
const dbAsar = root.querySelector('[data-testid="db-asar"]');
const benchTable = root.querySelector('[data-testid="bench-table"]');
const eventsEl = root.querySelector('[data-testid="events"]');
const status = root.querySelector('[data-testid="status"]');
let state = null;
function renderNotes(element, notes) {
if (!element)
return;
element.innerHTML =
notes.length === 0
? `暂无记录`
: notes
.map((note) => `
${escape(note.title)}
${escape(note.tags.join(" · "))} · ${time(Date.parse(note.updatedAt))}
`)
.join("");
}
function renderJson() {
if (!state)
return;
const info = state.json;
if (jsonSource) {
jsonSource.textContent = info.source;
jsonSource.className = `tag ${sourceClass(info.source)}`;
}
if (jsonPath)
jsonPath.textContent = "userData/json-store/store.json";
if (jsonCount)
jsonCount.textContent = String(info.count);
if (jsonSize)
jsonSize.textContent = bytes(info.size);
if (jsonWrite)
jsonWrite.textContent = `${info.writeMs} ms`;
if (jsonRead)
jsonRead.textContent = `${info.readMs} ms`;
if (jsonBackup)
jsonBackup.textContent = bytes(info.backupBytes);
if (jsonTmp)
jsonTmp.textContent = String(info.tmpFiles);
if (jsonError)
jsonError.textContent = info.error ? `恢复原因:${info.error}` : "";
renderNotes(jsonNotes, info.notes);
}
function renderSqlite() {
if (!state)
return;
const info = state.sqlite;
if (sqliteAvailable) {
sqliteAvailable.textContent = info.available ? "内置可用" : "不可用";
sqliteAvailable.className = `tag ${info.available ? "ok" : "bad"}`;
}
if (sqlitePath)
sqlitePath.textContent = "userData/hub.sqlite";
if (sqliteCount)
sqliteCount.textContent = String(info.count);
if (sqliteSize)
sqliteSize.textContent = bytes(info.size);
if (sqliteWrite)
sqliteWrite.textContent = `${info.writeMs} ms`;
if (sqliteRead)
sqliteRead.textContent = `${info.readMs} ms`;
if (sqliteJournal)
sqliteJournal.textContent = info.journalMode || "—";
if (sqliteError)
sqliteError.textContent = info.error || "";
renderNotes(sqliteNotes, info.notes);
}
function renderPaths() {
if (!state || !pathsEl)
return;
const p = state.paths;
const rows = [
["userData", "userData/(可写)"],
["JSON", "userData/json-store/store.json"],
["SQLite", "userData/hub.sqlite"],
["appPath", "app/(只读,不可写)"],
["resources", "resources/(只读)"],
];
pathsEl.innerHTML = rows
.map(([key, value]) => `
${key}${escape(value)}`)
.join("");
if (dbAsar) {
dbAsar.textContent = p.dbInAsar
? "数据库在 ASAR 内(错误)"
: "数据库不在 ASAR";
dbAsar.className = `tag ${p.dbInAsar ? "bad" : "ok"}`;
}
}
function renderBench() {
if (!benchTable || !state)
return;
const bench = state.bench;
if (!bench) {
benchTable.innerHTML = `尚未运行基准。点击 1,000 / 5,000 / 10,000 条。
`;
return;
}
benchTable.innerHTML = `
| 指标 | JSON | SQLite |
| 写入 | ${bench.json.writeMs} ms | ${bench.sqlite.writeMs} ms |
| 读取 | ${bench.json.readMs} ms | ${bench.sqlite.readMs} ms |
| 体积 | ${bytes(bench.json.bytes)} | ${bytes(bench.sqlite.bytes)} |
| 日志 | — | ${bench.sqlite.journalMode} |
${bench.count.toLocaleString()} 条 · JSON 写入 ${bench.json.writeMs}ms / SQLite 写入 ${bench.sqlite.writeMs}ms
`;
}
function renderEvents() {
if (!eventsEl || !state)
return;
eventsEl.innerHTML =
state.events.length === 0
? `暂无活动`
: state.events
.slice(0, 8)
.map((row) => `
${time(row.at)}
${escape(row.kind)}
${escape(row.detail)}
`)
.join("");
}
function render(next) {
state = next;
if (versions) {
versions.textContent = `Electron ${next.electronVersion} · Node ${next.nodeVersion}`;
}
renderJson();
renderSqlite();
renderPaths();
renderBench();
renderEvents();
}
void window.api.store.snapshot().then(render);
const unsubscribe = window.api.store.onEvent(() => {
void window.api.store.snapshot().then(render);
});
const add = async () => {
render(await window.api.store.addNote("来自界面的持久化记录:比较 JSON 原子写与 SQLite 事务。"));
};
root
.querySelector('[data-testid="json-add"]')
?.addEventListener("click", () => void add());
root
.querySelector('[data-testid="sqlite-add"]')
?.addEventListener("click", () => void add());
root
.querySelector('[data-testid="json-reload"]')
?.addEventListener("click", async () => {
render(await window.api.store.reload());
});
root
.querySelector('[data-testid="sqlite-reload"]')
?.addEventListener("click", async () => {
render(await window.api.store.reload());
});
root
.querySelector('[data-testid="json-corrupt"]')
?.addEventListener("click", async () => {
render(await window.api.store.corrupt());
if (status)
status.textContent = "已模拟损坏并从 .bak 恢复";
});
root
.querySelector('[data-testid="refresh"]')
?.addEventListener("click", async () => {
render(await window.api.store.refresh());
});
for (const count of [1000, 5000, 10000]) {
root
.querySelector(`[data-testid="bench-${count}"]`)
?.addEventListener("click", async () => {
render(await window.api.store.bench(count));
if (status)
status.textContent = `已完成 ${count.toLocaleString()} 条基准`;
});
}
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
window.__prepareShots = async (stage) => {
document.body.dataset.shot = stage;
if (stage === "json") {
render(await window.api.store.addNote("截图记录:JSON 原子写把临时文件 rename 到 store.json,旧文件保留为 .bak。"));
return;
}
if (stage === "sqlite") {
render(await window.api.store.addNote("截图记录:SQLite 使用 WAL 与事务,单写者顺序写入。"));
return;
}
render(await window.api.store.bench(5000));
await sleep(200);
};
window.__smoke = async () => {
const api = window.api.store;
const first = await api.snapshot();
const afterAdd = await api.addNote("smoke 测试:新增一条持久化记录。");
await sleep(80);
const reloaded = await api.reload();
const corrupt = await api.corrupt();
const recovered = await api.addNote("smoke 测试:恢复后继续写入。");
const bench = await api.bench(1000);
const final = await api.refresh();
unsubscribe();
const benchValue = bench.bench;
if (!benchValue)
throw new Error("BENCH_MISSING");
return {
jsonCount: afterAdd.json.count,
sqliteCount: afterAdd.sqlite.count,
sqliteAvailable: first.sqlite.available,
journalMode: first.sqlite.journalMode,
source: reloaded.json.source,
backupSource: corrupt.json.source,
recoveredSource: recovered.json.source,
jsonWriteMs: afterAdd.json.writeMs,
jsonReadMs: afterAdd.json.readMs,
sqliteWriteMs: afterAdd.sqlite.writeMs,
sqliteReadMs: afterAdd.sqlite.readMs,
jsonBytes: afterAdd.json.size,
sqliteBytes: afterAdd.sqlite.size,
tmpFiles: afterAdd.json.tmpFiles,
dbInAsar: final.paths.dbInAsar,
benchJsonMs: benchValue.json.writeMs,
benchSqliteMs: benchValue.sqlite.writeMs,
benchBytes: benchValue.sqlite.bytes,
events: final.events.length,
};
};
}
//# sourceMappingURL=App.js.map