UNPKG

@atproto/repo

Version:

atproto repo and MST implementation

149 lines 4.37 kB
"use strict"; /* eslint-disable import/no-deprecated */ 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(entries) { Object.defineProperty(this, "map", { enumerable: true, configurable: true, writable: true, value: new Map() }); if (entries) { for (const [cid, bytes] of entries) { this.set(cid, bytes); } } } 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) { for (const [cid, bytes] of this) cb(bytes, cid); } entries() { return Array.from(this, toEntry); } cids() { return Array.from(this.keys()); } addMap(toAdd) { for (const [cid, bytes] of toAdd) this.set(cid, bytes); return this; } get size() { return this.map.size; } get byteSize() { let size = 0; for (const bytes of this.values()) size += bytes.length; return size; } equals(other) { if (this.size !== other.size) { return false; } for (const [cid, bytes] of this) { const otherBytes = other.get(cid); if (!otherBytes) return false; if (!uint8arrays.equals(bytes, otherBytes)) { return false; } } return true; } *keys() { for (const key of this.map.keys()) { yield cid_1.CID.parse(key); } } *values() { yield* this.map.values(); } *[Symbol.iterator]() { for (const [key, value] of this.map) { yield [cid_1.CID.parse(key), value]; } } } exports.BlockMap = BlockMap; function toEntry([cid, bytes]) { return { cid, bytes }; } exports.default = BlockMap; //# sourceMappingURL=block-map.js.map