import type { CID } from 'multiformats/cid' /** Minimal async block store interface — get/put/has. */ export interface BlockStore { get(cid: CID): Promise put(cid: CID, bytes: Uint8Array): Promise has(cid: CID): Promise } /** * A block store that reads locally first, with optional remote fallback. * On get: local hit → return; local miss + remote → fetch, write-back, return. * put/has always operate on local only. */ export class LocalFirstBlockStore implements BlockStore { constructor( private local: BlockStore, private remote?: Pick, ) {} async get(cid: CID): Promise { if (await this.local.has(cid)) return this.local.get(cid) if (!this.remote) throw new Error(`Block not found: ${cid}`) const bytes = await this.remote.get(cid) await this.local.put(cid, bytes) return bytes } put(cid: CID, bytes: Uint8Array): Promise { return this.local.put(cid, bytes) } has(cid: CID): Promise { return this.local.has(cid) } }