UNPKG

@atproto/repo

Version:

atproto repo and MST implementation

41 lines 1.68 kB
import { writeCarStream } from '../car.js'; import { CidSet } from '../cid-set.js'; import { MissingBlocksError } from '../error.js'; import { MST } from '../mst/index.js'; import { def } from '../types.js'; import * as util from '../util.js'; // Full Repo // ------------- export const getFullRepo = (storage, commitCid) => { return writeCarStream(commitCid, iterateFullRepo(storage, commitCid)); }; async function* iterateFullRepo(storage, commitCid) { const commit = await storage.readObjAndBytes(commitCid, def.commit); yield { cid: commitCid, bytes: commit.bytes }; const mst = MST.load(storage, commit.obj.data); for await (const block of mst.carBlockStream()) { yield block; } } // Narrow slices // ------------- export const getRecords = (storage, commitCid, paths) => { return writeCarStream(commitCid, iterateRecordBlocks(storage, commitCid, paths)); }; async function* iterateRecordBlocks(storage, commitCid, paths) { const commit = await storage.readObjAndBytes(commitCid, def.commit); yield { cid: commitCid, bytes: commit.bytes }; const mst = MST.load(storage, commit.obj.data); const cidsForPaths = await Promise.all(paths.map((p) => mst.cidsForPath(util.formatDataKey(p.collection, p.rkey)))); const allCids = cidsForPaths.reduce((acc, cur) => { return acc.addSet(new CidSet(cur)); }, new CidSet()); const found = await storage.getBlocks(allCids.toList()); if (found.missing.length > 0) { throw new MissingBlocksError('writeRecordsToCarStream', found.missing); } for (const block of found.blocks.entries()) { yield block; } } //# sourceMappingURL=provider.js.map