UNPKG

@atcute/car

Version:

lightweight DASL CAR and atproto repository decoder for AT Protocol.

62 lines 2 kB
import * as CBOR from '@atcute/cbor'; import * as CID from '@atcute/cid'; const isCidLink = (value) => { if (value instanceof CID.CidLinkWrapper) { return true; } if (value === null || typeof value !== 'object') { return false; } return '$link' in value && typeof value.$link === 'string'; }; const isBytes = (value) => { if (value instanceof CBOR.BytesWrapper) { return true; } if (value === null || typeof value !== 'object') { return false; } return '$bytes' in value && typeof value.$bytes === 'string'; }; /** * checks if a value is a valid commit object * @param value the value to check * @returns true if the value is a valid commit object, false otherwise */ export const isCommit = (value) => { if (value === null || typeof value !== 'object') { return false; } const obj = value; return (obj.version === 3 && typeof obj.did === 'string' && isCidLink(obj.data) && typeof obj.rev === 'string' && (obj.prev === null || isCidLink(obj.prev)) && isBytes(obj.sig)); }; /** * checks if a value is a valid mst tree entry object * @param value the value to check * @returns true if the value is a valid mst tree entry object, false otherwise */ export const isTreeEntry = (value) => { if (value === null || typeof value !== 'object') { return false; } const obj = value; return (typeof obj.p === 'number' && isBytes(obj.k) && isCidLink(obj.v) && (obj.t === null || isCidLink(obj.t))); }; /** * checks if a value is a valid mst node object * @param value the value to check * @returns true if the value is a valid mst node object, false otherwise */ export const isMstNode = (value) => { if (value === null || typeof value !== 'object') { return false; } const obj = value; return (obj.l === null || isCidLink(obj.l)) && Array.isArray(obj.e) && obj.e.every(isTreeEntry); }; //# sourceMappingURL=mst.js.map