import { randomUUID, createHash } from 'node:crypto' import { Buffer } from 'node:buffer' import { fromBase64 } from './pds-client.js' export function makeSnapshotId() { return `${Date.now().toString(36)}-${randomUUID()}` } export function chunkRkey(snapshotId, index) { return `${snapshotId}-${String(index).padStart(6, '0')}` } export async function saveSnapshot({ pds, repoId, refs, stream }) { const snapshotId = makeSnapshotId() const createdAt = new Date().toISOString() const digest = createHash('sha256').update(stream).digest('hex') const blob = await pds.uploadBlob(stream, { contentType: 'application/octet-stream' }) const manifest = { $type: `${pds.collectionPrefix}.snapshot`, repoId, snapshotId, createdAt, streamSha256: digest, blob, refs, } await pds.createRecord(pds.snapshotCollection, manifest, snapshotId) return manifest } function extractBlobCid(blob) { if (!blob || typeof blob !== 'object') return null const { ref, cid } = blob if (typeof cid === 'string') return cid if (typeof ref === 'string') return ref if (ref && typeof ref === 'object') { if (typeof ref.$link === 'string') return ref.$link if (typeof ref.cid === 'string') return ref.cid } return null } function pickLatestSnapshot(records, repoId) { const snapshots = records .map((record) => record.value) .filter((value) => value && value.repoId === repoId) snapshots.sort((a, b) => { const timeDiff = String(b.createdAt).localeCompare(String(a.createdAt)) if (timeDiff !== 0) return timeDiff return String(b.snapshotId).localeCompare(String(a.snapshotId)) }) return snapshots[0] ?? null } export async function loadLatestSnapshotManifest({ pds, repoId }) { const snapshotRecords = await pds.listAllRecords(pds.snapshotCollection) return pickLatestSnapshot(snapshotRecords, repoId) } export async function loadLatestSnapshot({ pds, repoId }) { const manifest = await loadLatestSnapshotManifest({ pds, repoId }) if (!manifest) return null let stream const blobCid = extractBlobCid(manifest.blob) if (blobCid) { stream = await pds.getBlob(blobCid) } else { const chunkRecords = await pds.listAllRecords(pds.chunkCollection) const chunks = chunkRecords .map((record) => record.value) .filter((value) => value && value.repoId === repoId && value.snapshotId === manifest.snapshotId) .sort((a, b) => a.index - b.index) if (chunks.length !== manifest.chunkCount) { throw new Error( `snapshot ${manifest.snapshotId} is incomplete: expected ${manifest.chunkCount} chunks, found ${chunks.length}`, ) } stream = Buffer.concat(chunks.map((chunk) => fromBase64(chunk.data))) } const digest = createHash('sha256').update(stream).digest('hex') if (digest !== manifest.streamSha256) { throw new Error( `snapshot ${manifest.snapshotId} digest mismatch: expected ${manifest.streamSha256}, got ${digest}`, ) } return { manifest, stream } } export async function listLatestRefs({ pds, repoId }) { const manifest = await loadLatestSnapshotManifest({ pds, repoId }) if (!manifest) return [] return (manifest.refs ?? []).map((ref) => ({ refname: ref.target, oid: ref.oid })) }