UNPKG

@atproto/repo

Version:

atproto repo and MST implementation

79 lines 3.29 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.getRecords = exports.getFullRepo = void 0; const car_1 = require("../car"); const cid_set_1 = require("../cid-set"); const error_1 = require("../error"); const mst_1 = require("../mst"); const types_1 = require("../types"); const util = __importStar(require("../util")); // Full Repo // ------------- const getFullRepo = (storage, commitCid) => { return (0, car_1.writeCarStream)(commitCid, iterateFullRepo(storage, commitCid)); }; exports.getFullRepo = getFullRepo; async function* iterateFullRepo(storage, commitCid) { const commit = await storage.readObjAndBytes(commitCid, types_1.def.commit); yield { cid: commitCid, bytes: commit.bytes }; const mst = mst_1.MST.load(storage, commit.obj.data); for await (const block of mst.carBlockStream()) { yield block; } } // Narrow slices // ------------- const getRecords = (storage, commitCid, paths) => { return (0, car_1.writeCarStream)(commitCid, iterateRecordBlocks(storage, commitCid, paths)); }; exports.getRecords = getRecords; async function* iterateRecordBlocks(storage, commitCid, paths) { const commit = await storage.readObjAndBytes(commitCid, types_1.def.commit); yield { cid: commitCid, bytes: commit.bytes }; const mst = mst_1.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 cid_set_1.CidSet(cur)); }, new cid_set_1.CidSet()); const found = await storage.getBlocks(allCids.toList()); if (found.missing.length > 0) { throw new error_1.MissingBlocksError('writeRecordsToCarStream', found.missing); } for (const block of found.blocks.entries()) { yield block; } } //# sourceMappingURL=provider.js.map