Skip to content

@insler/serde-cbor

The CBOR adapter package: cborSerde, a Serde<Uint8Array> backed by cbor2. Reach for it when CBOR’s compact, self-describing binary encoding (RFC 8949) is wanted on a binary wire.

Terminal window
bun add @insler/serde-cbor
import { cborSerde } from '@insler/serde-cbor';
const bytes = cborSerde.encode({ id: 'ord-1', quantity: 3, price: 19.5 });
// Uint8Array — CBOR-encoded
const order = cborSerde.decode(bytes);
// { id: 'ord-1', quantity: 3, price: 19.5 }

It honors the shared serde conventions: encode(undefined) produces empty bytes, decoding empty bytes returns undefined.

cborSerde is a standard Serde<Uint8Array> — swap it with jsonBytesSerde, msgpackSerde, or an Avro serde wherever a binary serde is accepted, such as the rpc subsystem’s NATS transport serde option.

Depends on @insler/serde (the interface) and cbor2 — it is transport-agnostic by design, coupled to nothing but the Serde<Uint8Array> seam.