UNPKG

@atproto/common-web

Version:

Shared web-platform-friendly code for atproto libraries

129 lines 4.42 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.ipldEquals = exports.ipldToJson = exports.jsonToIpld = void 0; const cid_1 = require("multiformats/cid"); const ui8 = __importStar(require("uint8arrays")); // @NOTE avoiding use of check.is() here only because it makes // these implementations slow, and they often live in hot paths. const jsonToIpld = (val) => { // walk arrays if (Array.isArray(val)) { return val.map((item) => (0, exports.jsonToIpld)(item)); } // objects if (val && typeof val === 'object') { // check for dag json values if (typeof val['$link'] === 'string' && Object.keys(val).length === 1) { return cid_1.CID.parse(val['$link']); } if (typeof val['$bytes'] === 'string' && Object.keys(val).length === 1) { return ui8.fromString(val['$bytes'], 'base64'); } // walk plain objects const toReturn = {}; for (const key of Object.keys(val)) { toReturn[key] = (0, exports.jsonToIpld)(val[key]); } return toReturn; } // pass through return val; }; exports.jsonToIpld = jsonToIpld; const ipldToJson = (val) => { // walk arrays if (Array.isArray(val)) { return val.map((item) => (0, exports.ipldToJson)(item)); } // objects if (val && typeof val === 'object') { // convert bytes if (val instanceof Uint8Array) { return { $bytes: ui8.toString(val, 'base64'), }; } // convert cids if (cid_1.CID.asCID(val)) { return { $link: val.toString(), }; } // walk plain objects const toReturn = {}; for (const key of Object.keys(val)) { toReturn[key] = (0, exports.ipldToJson)(val[key]); } return toReturn; } // pass through return val; }; exports.ipldToJson = ipldToJson; const ipldEquals = (a, b) => { // walk arrays if (Array.isArray(a) && Array.isArray(b)) { if (a.length !== b.length) return false; for (let i = 0; i < a.length; i++) { if (!(0, exports.ipldEquals)(a[i], b[i])) return false; } return true; } // objects if (a && b && typeof a === 'object' && typeof b === 'object') { // check bytes if (a instanceof Uint8Array && b instanceof Uint8Array) { return ui8.equals(a, b); } // check cids if (cid_1.CID.asCID(a) && cid_1.CID.asCID(b)) { return cid_1.CID.asCID(a)?.equals(cid_1.CID.asCID(b)); } // walk plain objects if (Object.keys(a).length !== Object.keys(b).length) return false; for (const key of Object.keys(a)) { if (!(0, exports.ipldEquals)(a[key], b[key])) return false; } return true; } return a === b; }; exports.ipldEquals = ipldEquals; //# sourceMappingURL=ipld.js.map