Tiny CBOR

Encode and decode a useful subset of CBOR into simple JavaScript structures. Focused on clarity and portability.

Install

npm i @levischuck/tiny-cbor
pnpm add @levischuck/tiny-cbor
deno add jsr:@levischuck/tiny-cbor
bunx jsr add @levischuck/tiny-cbor

Quick start

quick-start.tsts

Partial decoding

When CBOR is embedded in a larger byte stream, use decodePartialCBOR to start from an index.

partial.tsts

Tags

Wrap values with a CBOR tag without interpretation at decode-time.

tags.tsts

API

encodeCBOR

Encode a supported structure to a CBOR byte string.

decodeCBOR

Decode CBOR data from a binary stream. The entire data stream from [0, length) will be consumed. If you require a partial decoding, see decodePartialCBOR.

decodePartialCBOR

Like decodeCBOR, but the length of the data is unknown and there is likely more — possibly unrelated non-CBOR — data afterwards. Start decoding at a specific index and receive the value plus the number of bytes consumed.

CBORTag

A value which is wrapped with a CBOR Tag. Several tags are registered with defined meanings like 0 for a date string. These meanings are not interpreted when decoded or encoded. This class is an immutable record. If the tag number or value needs to change, construct a new tag.

CBORType

Supported types which are encodable and decodable with Tiny CBOR. Note that plain JavaScript objects are omitted. If you're looking to use JSON objects more easily, check out Tiny CBOR Schema.

number | bigint | string | Uint8Array | boolean | null | undefined | CBORType[] | CBORTag | Map<string | number, CBORType>

Source codeBack to Docs