lightweight DASL dCBOR42 (deterministic CBOR with tag 42) codec library for AT Protocol.
the specific profile being implemented is IPLD DAG-CBOR, with some additional notes to keep in mind:
undefined
types are still forbidden, except for when they are in amap
type, where fields will be omitted instead, which makes it easier to construct objects to then pass to the encoder.byte
andlink
types are represented by atproto's lex-json interfaces, but because these involve string codec and parsing, they are done lazily byBytesWrapper
andCidLinkWrapper
instances.- use
fromBytes
andfromCidLink
to convert them to Uint8Array or CID interface respectively, without hitting the string conversion path. - use
toBytes
andtoCidLink
for the other direction.
- use
- integers can't exceed JavaScript's safe integer range, no bigint conversions will occur as they will be thrown instead if encountered.
import { encode } from '@atcute/cbor';
const record = {
$type: 'app.bsky.feed.post',
createdAt: '2024-08-18T03:18:24.000Z',
langs: ['en'],
text: 'hello world!',
};
const cbor = encode(record);
// ^? Uint8Array(90) [ ... ]
Implementation based on the excellent microcbor
library.