{"version":3,"sources":["/home/runner/work/kora/kora/packages/store/dist/chunk-TJMEA7QZ.cjs","../src/blob/content-addressed-blob-store.ts"],"names":[],"mappings":"AAAA;ACAA,oCAA4E;AAMrE,IAAM,mBAAA,EAAN,MAAA,QAAiC,MAAM;AAAA,EAC7C,WAAA,CACU,YAAA,EACA,UAAA,EACR;AACD,IAAA,KAAA,CAAM,CAAA,gDAAA,EAAmD,YAAY,CAAA,WAAA,EAAc,UAAU,CAAA,CAAA;AAHpF,IAAA;AACA,IAAA;AAGG,IAAA;AACb,EAAA;AALU,EAAA;AACA,EAAA;AAKX;AAkDkE;AAC9C,iBAAA;AACkC,kBAAA;AAE0B,EAAA;AAC/B,IAAA;AAChB,IAAA;AAEgB,MAAA;AAC9B,MAAA;AACe,MAAA;AAChC,IAAA;AACO,IAAA;AACR,EAAA;AAEoD,EAAA;AACjB,IAAA;AACR,IAAA;AAClB,MAAA;AACR,IAAA;AACwC,IAAA;AACf,IAAA;AACqB,MAAA;AAC9C,IAAA;AAE4C,IAAA;AAC9B,IAAA;AACP,IAAA;AACR,EAAA;AAE0C,EAAA;AACf,IAAA;AAC3B,EAAA;AAE6C,EAAA;AACf,IAAA;AAC9B,EAAA;AAE8B,EAAA;AACX,IAAA;AACnB,EAAA;AAEgC,EAAA;AACH,IAAA;AAC7B,EAAA;AACD;AD3DkG;AACA;AACA;AACA;AACA","file":"/home/runner/work/kora/kora/packages/store/dist/chunk-TJMEA7QZ.cjs","sourcesContent":[null,"import { type BlobRef, type BlobRefMetadata, createBlobRef, hashBlob } from '@korajs/core'\n\n/**\n * Thrown when a blob read fails its integrity check: the bytes stored under a\n * hash do not actually hash to that value (corruption or tampering).\n */\nexport class BlobIntegrityError extends Error {\n\tconstructor(\n\t\treadonly expectedHash: string,\n\t\treadonly actualHash: string,\n\t) {\n\t\tsuper(`Blob integrity check failed: content under hash ${expectedHash} hashes to ${actualHash}`)\n\t\tthis.name = 'BlobIntegrityError'\n\t}\n}\n\n/**\n * A content-addressed store for the bytes behind `blob` fields.\n *\n * Blobs are keyed by the SHA-256 hash of their content, so identical content is\n * stored exactly once (deduplication) and a peer never needs to transfer bytes\n * it already holds. Reads are integrity-checked: the returned bytes are verified\n * to hash to the requested key.\n */\nexport interface ContentAddressedBlobStore {\n\t/**\n\t * Whether stored blobs survive a reload of the page or restart of the process (OPFS and\n\t * filesystem stores: true; the in-memory store: false). Offline readiness reads it: a pin\n\t * that keeps files is not `ready` on a store that forgets them (`storage-non-durable`).\n\t * Optional so a custom store written before this field existed still compiles; a store that\n\t * does not say (or cannot vouch for its bytes) is treated as not durable, so declare\n\t * `readonly durable = true` on a store whose bytes survive a restart.\n\t */\n\treadonly durable?: boolean\n\t/**\n\t * Store bytes and return their content-addressed reference. Storing content\n\t * that already exists is a no-op that returns the same reference (dedup).\n\t */\n\tput(bytes: Uint8Array, metadata?: BlobRefMetadata): Promise<BlobRef>\n\t/**\n\t * Retrieve the bytes for a hash, or null if absent. The returned bytes are\n\t * verified to hash to the requested value; a mismatch throws\n\t * {@link BlobIntegrityError} rather than returning corrupt data.\n\t */\n\tget(hash: string): Promise<Uint8Array | null>\n\t/** Whether the store holds content for the given hash. */\n\thas(hash: string): Promise<boolean>\n\t/** Remove the content for a hash. Returns whether anything was removed. */\n\tdelete(hash: string): Promise<boolean>\n\t/** Number of distinct blobs held. */\n\tsize(): Promise<number>\n\t/**\n\t * List the hashes of every blob currently held. Used by garbage collection to\n\t * find content no live reference points to. Order is unspecified.\n\t */\n\tlist(): Promise<string[]>\n}\n\n/**\n * In-memory {@link ContentAddressedBlobStore}. The reference backend used for\n * tests and for environments without a persistent blob backend yet. Persistent\n * backends (OPFS client-side, filesystem/S3 server-side) implement the same\n * interface.\n */\nexport class MemoryBlobStore implements ContentAddressedBlobStore {\n\treadonly durable = false\n\tprivate readonly blobs = new Map<string, Uint8Array>()\n\n\tasync put(bytes: Uint8Array, metadata: BlobRefMetadata = {}): Promise<BlobRef> {\n\t\tconst ref = await createBlobRef(bytes, metadata)\n\t\tif (!this.blobs.has(ref.hash)) {\n\t\t\t// Copy so later mutation of the caller's buffer cannot corrupt the store.\n\t\t\tconst stored = new Uint8Array(bytes.byteLength)\n\t\t\tstored.set(bytes)\n\t\t\tthis.blobs.set(ref.hash, stored)\n\t\t}\n\t\treturn ref\n\t}\n\n\tasync get(hash: string): Promise<Uint8Array | null> {\n\t\tconst stored = this.blobs.get(hash)\n\t\tif (stored === undefined) {\n\t\t\treturn null\n\t\t}\n\t\tconst actualHash = await hashBlob(stored)\n\t\tif (actualHash !== hash) {\n\t\t\tthrow new BlobIntegrityError(hash, actualHash)\n\t\t}\n\t\t// Return a copy so callers cannot mutate the stored bytes.\n\t\tconst out = new Uint8Array(stored.byteLength)\n\t\tout.set(stored)\n\t\treturn out\n\t}\n\n\tasync has(hash: string): Promise<boolean> {\n\t\treturn this.blobs.has(hash)\n\t}\n\n\tasync delete(hash: string): Promise<boolean> {\n\t\treturn this.blobs.delete(hash)\n\t}\n\n\tasync size(): Promise<number> {\n\t\treturn this.blobs.size\n\t}\n\n\tasync list(): Promise<string[]> {\n\t\treturn [...this.blobs.keys()]\n\t}\n}\n"]}