Something went wrong. Try again.
Synapse is a study workspace. Organize courses read and annotate the material, train with generated exercises and watch your progress forked from denishovart.com/synapse
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788// 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<string> { // 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<Blob | null> { 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<string> { 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<boolean> { 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<Blob | null> { 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<boolean> { 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<string | null> { const blob = await getOrFetch(hash); return blob ? blob.text() : null;}