// Client side of the content-addressed store: file bytes (PDF originals, // renders, extracted text) cached in IndexedDB by sha-256, fetched from the // server on miss and pushed there when created locally. Blobs are immutable, // so a cache hit never needs revalidation. import { apiFetch, hasServerBase } from '../api'; import { endpoints } from '../endpoints'; import * as db from './db'; export async function hashBytes(bytes: ArrayBuffer | Uint8Array): Promise { // The view's own range, never its whole backing buffer: a subarray (what // fflate returns for stored zip entries) would otherwise digest the // surrounding archive and never match its hash. const source = bytes instanceof Uint8Array ? new Uint8Array(bytes) : bytes; const digest = await crypto.subtle.digest('SHA-256', source); return [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, '0')).join(''); } async function getLocal(hash: string): Promise { const stored = await db.getBlob(hash); if (!stored) return null; const copy = new Uint8Array(stored.bytes); return new Blob([copy.buffer as ArrayBuffer], { type: stored.mime }); } export async function putLocal(bytes: Blob, mime: string): Promise { const raw = new Uint8Array(await bytes.arrayBuffer()); const hash = await hashBytes(raw); await db.putBlob({ hash, bytes: raw, mime, size: raw.length }); return hash; } // Store bytes claimed to hash to `hash` (from a peer, a pack, or the server). // The content-addressed store trusts that hash equals content, so a caller that // did not produce the bytes itself must go through here: we recompute the digest // and refuse a mismatch, otherwise a poisoned pack or LAN peer could plant // bytes under a legitimate document's hash. Returns false when it does not match. export async function putVerified(hash: string, bytes: Uint8Array, mime: string): Promise { if ((await hashBytes(bytes)) !== hash) return false; await db.putBlob({ hash, bytes, mime, size: bytes.length }); return true; } // Local cache first, then the server. Returns null when neither has it // (offline and never seen). export async function getOrFetch(hash: string): Promise { const local = await getLocal(hash); if (local) return local; // A standalone shell with no configured server has no backend to ask; the // bytes arrive by sync instead. Skip the fetch so it does not error against // the webview origin (connection refused). if (!hasServerBase()) return null; try { const res = await apiFetch(endpoints.blob(hash)); if (!res.ok) return null; const mime = res.headers.get('content-type') ?? 'application/octet-stream'; const raw = new Uint8Array(await res.arrayBuffer()); if (!(await putVerified(hash, raw, mime))) return null; const copy = new Uint8Array(raw); return new Blob([copy.buffer as ArrayBuffer], { type: mime }); } catch { return null; } } // Best-effort upload of a locally-created blob (desktop-added files, packs). export async function pushToServer(hash: string): Promise { if (!hasServerBase()) return false; const stored = await db.getBlob(hash); if (!stored) return false; try { const copy = new Uint8Array(stored.bytes); const res = await apiFetch(endpoints.blob(hash), { method: 'PUT', headers: { 'Content-Type': stored.mime }, body: new Blob([copy.buffer as ArrayBuffer]), }); return res.ok; } catch { return false; } } export async function textOf(hash: string): Promise { const blob = await getOrFetch(hash); return blob ? blob.text() : null; }