UNPKG

@atproto/repo

Version:

atproto repo and MST implementation

132 lines 3.95 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.BlockMap = void 0; const cid_1 = require("multiformats/cid"); const uint8arrays = __importStar(require("uint8arrays")); const common_1 = require("@atproto/common"); const lexicon_1 = require("@atproto/lexicon"); class BlockMap { constructor() { Object.defineProperty(this, "map", { enumerable: true, configurable: true, writable: true, value: new Map() }); } async add(value) { const block = await (0, common_1.dataToCborBlock)((0, lexicon_1.lexToIpld)(value)); this.set(block.cid, block.bytes); return block.cid; } set(cid, bytes) { this.map.set(cid.toString(), bytes); return this; } get(cid) { return this.map.get(cid.toString()); } delete(cid) { this.map.delete(cid.toString()); return this; } getMany(cids) { const missing = []; const blocks = new BlockMap(); for (const cid of cids) { const got = this.map.get(cid.toString()); if (got) { blocks.set(cid, got); } else { missing.push(cid); } } return { blocks, missing }; } has(cid) { return this.map.has(cid.toString()); } clear() { this.map.clear(); } forEach(cb) { this.map.forEach((val, key) => cb(val, cid_1.CID.parse(key))); } entries() { const entries = []; this.forEach((bytes, cid) => { entries.push({ cid, bytes }); }); return entries; } cids() { return this.entries().map((e) => e.cid); } addMap(toAdd) { toAdd.forEach((bytes, cid) => { this.set(cid, bytes); }); return this; } get size() { return this.map.size; } get byteSize() { let size = 0; this.forEach((bytes) => { size += bytes.length; }); return size; } equals(other) { if (this.size !== other.size) { return false; } for (const entry of this.entries()) { const otherBytes = other.get(entry.cid); if (!otherBytes) return false; if (!uint8arrays.equals(entry.bytes, otherBytes)) { return false; } } return true; } } exports.BlockMap = BlockMap; exports.default = BlockMap; //# sourceMappingURL=block-map.js.map