Add CultCache inspector app

This commit is contained in:
Meta Vens 2026-05-16 11:06:33 +01:00
commit b460612680
20 changed files with 5965 additions and 10 deletions

235
inspector/src/main.ts Normal file
View file

@ -0,0 +1,235 @@
import { inspectCultCacheBytes, type CultCacheInspection, type InspectedCatalogEntry, type InspectedRecord } from "../../src/cult-cache-inspector";
import "./styles.css";
const app = document.querySelector<HTMLDivElement>("#app");
if (!app) {
throw new Error("Missing inspector app root.");
}
const appRoot = app;
type Selection = {
record: number;
catalog: number;
};
let inspection: CultCacheInspection | undefined;
let selection: Selection = { record: 0, catalog: 0 };
let errorMessage = "";
let dragging = false;
appRoot.addEventListener("dragover", (event) => {
event.preventDefault();
dragging = true;
render();
});
appRoot.addEventListener("dragleave", (event) => {
if (event.target === appRoot) {
dragging = false;
render();
}
});
appRoot.addEventListener("drop", (event) => {
event.preventDefault();
dragging = false;
const file = event.dataTransfer?.files.item(0);
if (file) {
void inspectFile(file);
}
});
render();
function render(): void {
appRoot.innerHTML = `
<main class="workspace ${dragging ? "is-dragging" : ""}">
<section class="sidebar">
<div class="brand">
<h1>CultCache Inspector</h1>
<p>Drop a <code>.cc</code> file and read the wire format without registering app schemas.</p>
</div>
<label class="dropzone">
<input id="file-input" type="file" accept=".cc,.msgpack,.mpack,application/octet-stream" />
<span class="drop-title">Drop CultCache file</span>
<span class="drop-copy">or choose one from disk</span>
</label>
${errorMessage ? `<div class="error">${escapeHtml(errorMessage)}</div>` : ""}
${inspection ? renderSummary(inspection) : renderEmptyState()}
</section>
<section class="content">
${inspection ? renderInspection(inspection, selection) : renderNoFile()}
</section>
</main>
`;
appRoot.querySelector<HTMLInputElement>("#file-input")?.addEventListener("change", (event) => {
const file = (event.currentTarget as HTMLInputElement).files?.item(0);
if (file) {
void inspectFile(file);
}
});
appRoot.querySelectorAll<HTMLButtonElement>("[data-record]").forEach((button) => {
button.addEventListener("click", () => {
selection = { ...selection, record: Number(button.dataset.record) };
render();
});
});
appRoot.querySelectorAll<HTMLButtonElement>("[data-catalog]").forEach((button) => {
button.addEventListener("click", () => {
selection = { ...selection, catalog: Number(button.dataset.catalog) };
render();
});
});
}
async function inspectFile(file: File): Promise<void> {
try {
const bytes = new Uint8Array(await file.arrayBuffer());
inspection = inspectCultCacheBytes(file.name, bytes, file.size);
selection = { record: 0, catalog: 0 };
errorMessage = "";
} catch (error) {
inspection = undefined;
errorMessage = error instanceof Error ? error.message : String(error);
}
render();
}
function renderSummary(model: CultCacheInspection): string {
return `
<div class="summary">
${metric("Format", model.format)}
${metric("Records", model.records.length.toString())}
${metric("Schemas", model.catalog.length.toString())}
${metric("Bytes", model.fileSizeBytes.toString())}
</div>
`;
}
function renderEmptyState(): string {
return `
<div class="summary">
${metric("Format", ".cc")}
${metric("Mode", "Read-only")}
${metric("Payload", "MessagePack")}
${metric("Runtime", "Vite")}
</div>
`;
}
function renderNoFile(): string {
return `
<div class="hero">
<h2>No file loaded</h2>
<p>Drag a CultCache <code>.cc</code> store into the window. The inspector shows the snapshot header, schema catalog, records, and decoded payload slot arrays.</p>
</div>
`;
}
function renderInspection(model: CultCacheInspection, current: Selection): string {
const record = model.records[current.record];
const catalogEntry = model.catalog[current.catalog];
return `
<div class="columns">
<section class="panel">
<header><h2>Records</h2><span>${model.records.length}</span></header>
<div class="list">
${model.records.map((entry, index) => renderRecordButton(entry, index, index === current.record)).join("") || `<div class="empty">No records</div>`}
</div>
</section>
<section class="panel detail">
<header><h2>Record Detail</h2><span>${escapeHtml(model.filePath)}</span></header>
${record ? renderRecordDetail(record) : `<div class="empty">No record selected</div>`}
</section>
</div>
<div class="columns bottom">
<section class="panel">
<header><h2>Schema Catalog</h2><span>${model.catalog.length}</span></header>
<div class="list">
${model.catalog.map((entry, index) => renderCatalogButton(entry, index, index === current.catalog)).join("") || `<div class="empty">No schema catalog</div>`}
</div>
</section>
<section class="panel detail">
<header><h2>Catalog Entry</h2><span>${catalogEntry ? escapeHtml(catalogEntry.schemaVersion) : ""}</span></header>
${catalogEntry ? renderCatalogDetail(catalogEntry) : `<div class="empty">No catalog entry selected</div>`}
</section>
</div>
`;
}
function renderRecordButton(record: InspectedRecord, index: number, selected: boolean): string {
return `
<button class="row" type="button" data-record="${index}" aria-selected="${selected}">
<strong>${escapeHtml(record.key)}</strong>
<span>${escapeHtml(record.schemaName)} · ${record.payloadBytes} bytes</span>
</button>
`;
}
function renderCatalogButton(entry: InspectedCatalogEntry, index: number, selected: boolean): string {
return `
<button class="row" type="button" data-catalog="${index}" aria-selected="${selected}">
<strong>${escapeHtml(entry.schemaName)}</strong>
<span>${escapeHtml(entry.schemaId)}</span>
</button>
`;
}
function renderRecordDetail(record: InspectedRecord): string {
return `
${facts([
["Key", record.key],
["Schema", record.schemaName],
["Schema ID", record.schemaId],
["Stored", record.storedAt],
["Payload", `${record.payloadBytes} bytes`],
])}
${record.payloadDecodeError ? `<div class="error">${escapeHtml(record.payloadDecodeError)}</div>` : ""}
<pre>${escapeHtml(JSON.stringify(record.payloadPreview, null, 2))}</pre>
`;
}
function renderCatalogDetail(entry: InspectedCatalogEntry): string {
return `
${facts([
["Schema", entry.schemaName],
["Version", entry.schemaVersion],
["Schema ID", entry.schemaId],
["Hash", entry.contentHash],
["Compatible", entry.compatibleSchemaIds.join(", ")],
])}
<pre>${escapeHtml(JSON.stringify({
members: entry.members,
canonicalSchemaJson: parseJson(entry.canonicalSchemaJson),
}, null, 2))}</pre>
`;
}
function facts(rows: Array<[string, string]>): string {
return `<dl>${rows.map(([name, value]) => `<dt>${escapeHtml(name)}</dt><dd>${escapeHtml(value)}</dd>`).join("")}</dl>`;
}
function metric(label: string, value: string): string {
return `<div class="metric"><span>${escapeHtml(label)}</span><strong>${escapeHtml(value)}</strong></div>`;
}
function parseJson(value: string): unknown {
try {
return JSON.parse(value);
} catch {
return value;
}
}
function escapeHtml(value: string): string {
return value
.replaceAll("&", "&amp;")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;")
.replaceAll('"', "&quot;")
.replaceAll("'", "&#39;");
}

265
inspector/src/styles.css Normal file
View file

@ -0,0 +1,265 @@
:root {
color-scheme: light;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
color: #1b2421;
background: #f4f2ea;
font-synthesis: none;
text-rendering: optimizeLegibility;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-width: 760px;
min-height: 100vh;
}
button,
input {
font: inherit;
}
code {
font-family: "Cascadia Code", "SFMono-Regular", Consolas, monospace;
font-size: 0.92em;
}
.workspace {
display: grid;
grid-template-columns: 310px minmax(0, 1fr);
min-height: 100vh;
}
.sidebar {
border-right: 1px solid #cfd7d0;
background: #fffdf7;
padding: 22px;
}
.brand h1 {
margin: 0;
font-size: 25px;
line-height: 1.05;
}
.brand p {
margin: 10px 0 18px;
color: #61716b;
font-size: 13px;
line-height: 1.45;
}
.dropzone {
display: grid;
gap: 6px;
place-items: center;
min-height: 132px;
border: 2px dashed #8ba59e;
border-radius: 8px;
background: #eef7f4;
color: #123f3b;
cursor: pointer;
text-align: center;
}
.is-dragging .dropzone {
border-color: #0f766e;
background: #dff4ef;
}
.dropzone input {
position: absolute;
inline-size: 1px;
block-size: 1px;
opacity: 0;
pointer-events: none;
}
.drop-title {
font-weight: 800;
}
.drop-copy {
color: #61716b;
font-size: 13px;
}
.summary {
display: grid;
gap: 10px;
margin-top: 16px;
}
.metric {
border: 1px solid #d6ddd7;
border-radius: 8px;
background: #ffffff;
padding: 11px;
}
.metric span {
display: block;
color: #61716b;
font-size: 12px;
}
.metric strong {
display: block;
margin-top: 4px;
overflow-wrap: anywhere;
}
.content {
padding: 18px;
overflow: auto;
}
.hero {
display: grid;
align-content: center;
min-height: calc(100vh - 36px);
max-width: 620px;
}
.hero h2 {
margin: 0;
font-size: 42px;
line-height: 1;
}
.hero p {
margin: 14px 0 0;
color: #61716b;
line-height: 1.55;
}
.columns {
display: grid;
grid-template-columns: minmax(260px, 0.82fr) minmax(420px, 1.18fr);
gap: 14px;
}
.bottom {
margin-top: 14px;
}
.panel {
min-width: 0;
border: 1px solid #cfd7d0;
border-radius: 8px;
background: #ffffff;
overflow: hidden;
}
.panel header {
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
gap: 12px;
align-items: center;
border-bottom: 1px solid #dce2dd;
padding: 11px 12px;
}
.panel h2 {
margin: 0;
font-size: 15px;
}
.panel header span {
overflow: hidden;
color: #61716b;
font-size: 12px;
text-overflow: ellipsis;
white-space: nowrap;
}
.list {
display: grid;
gap: 8px;
max-height: 430px;
overflow: auto;
padding: 10px;
}
.row {
width: 100%;
border: 1px solid #d6ddd7;
border-radius: 6px;
background: #fbfcfa;
color: inherit;
padding: 10px;
text-align: left;
cursor: pointer;
}
.row[aria-selected="true"] {
border-color: #0f766e;
box-shadow: inset 3px 0 0 #0f766e;
}
.row strong,
.row span {
display: block;
overflow-wrap: anywhere;
}
.row span {
margin-top: 4px;
color: #61716b;
font-size: 12px;
}
.detail {
min-height: 360px;
}
.detail > dl,
.detail > pre,
.detail > .error {
margin: 12px;
}
dl {
display: grid;
grid-template-columns: 120px minmax(0, 1fr);
gap: 8px 12px;
}
dt {
color: #61716b;
font-size: 12px;
}
dd {
margin: 0;
overflow-wrap: anywhere;
}
pre {
max-height: 440px;
overflow: auto;
border-radius: 6px;
background: #15201e;
color: #f1fff9;
padding: 12px;
font-family: "Cascadia Code", "SFMono-Regular", Consolas, monospace;
font-size: 12px;
line-height: 1.45;
}
.error {
border: 1px solid #e0ada6;
border-radius: 6px;
background: #fff3f0;
color: #9c332f;
padding: 10px;
overflow-wrap: anywhere;
}
.empty {
padding: 18px;
color: #61716b;
}