Suite of AT Protocol TypeScript libraries built on web standards
21
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 45 lines 1.1 kB view raw
1import { decodeAll } from "@atp/lex/cbor"; 2import type { Cid } from "@atp/lex/data"; 3 4export type CborPrimitive = 5 | string 6 | number 7 | bigint 8 | boolean 9 | null 10 | undefined; 11export type CborArray = CborValue[]; 12export type CborObject = { [key: string]: CborValue }; 13export type CborValue = 14 | CborPrimitive 15 | CborArray 16 | CborObject 17 | Cid 18 | Uint8Array; 19 20export function cborDecodeMulti<T extends CborValue = CborValue>( 21 encoded: Uint8Array, 22): T[] { 23 if (encoded.byteLength === 0) { 24 return []; 25 } 26 return Array.from(decodeAll(encoded)) as T[]; 27} 28 29export function cborDecodeSingle<T extends CborValue = CborValue>( 30 encoded: Uint8Array, 31): T { 32 const results = cborDecodeMulti<T>(encoded); 33 if (results.length !== 1) { 34 throw new Error(`Expected single value, got ${results.length} values`); 35 } 36 return results[0]; 37} 38 39export function cborDecodeMultiAsObjects(encoded: Uint8Array): CborObject[] { 40 return cborDecodeMulti<CborObject>(encoded); 41} 42 43export function cborDecodeMultiAsArrays(encoded: Uint8Array): CborArray[] { 44 return cborDecodeMulti<CborArray>(encoded); 45}