UNPKG

@atproto/repo

Version:

atproto repo and MST implementation

62 lines 2.23 kB
import { MissingBlocksError } from './error.js'; import log from './logger.js'; import { MST } from './mst/index.js'; import * as parse from './parse.js'; import { def } from './types.js'; import * as util from './util.js'; export class ReadableRepo { constructor(params) { this.storage = params.storage; this.data = params.data; this.commit = params.commit; this.cid = params.cid; } static async load(storage, commitCid) { const commit = await storage.readObj(commitCid, def.versionedCommit); const data = await MST.load(storage, commit.data); log.info({ did: commit.did }, 'loaded repo for'); return new ReadableRepo({ storage, data, commit: util.ensureV3Commit(commit), cid: commitCid, }); } get did() { return this.commit.did; } get version() { return this.commit.version; } async *walkRecords(from) { for await (const leaf of this.data.walkLeavesFrom(from ?? '')) { const { collection, rkey } = util.parseDataKey(leaf.key); const record = await this.storage.readRecord(leaf.value); yield { collection, rkey, cid: leaf.value, record }; } } async getRecord(collection, rkey) { const dataKey = collection + '/' + rkey; const cid = await this.data.get(dataKey); if (!cid) return null; return this.storage.readObj(cid, def.unknown); } async getContents() { const entries = await this.data.list(); const cids = entries.map((e) => e.value); const { blocks, missing } = await this.storage.getBlocks(cids); if (missing.length > 0) { throw new MissingBlocksError('getContents record', missing); } const contents = {}; for (const entry of entries) { const { collection, rkey } = util.parseDataKey(entry.key); contents[collection] ??= {}; const parsed = await parse.getAndParseRecord(blocks, entry.value); contents[collection][rkey] = parsed.record; } return contents; } } //# sourceMappingURL=readable-repo.js.map