Skip to content

cbor ​

ts
import {
encode
,
decode
} from 'mikro/cbor'

Encode and decode CBOR, a compact binary format similar to JSON but smaller and faster to parse. Used internally by the kv storage module.

encode(value) ​

ts
encode(value: unknown): Result<Uint8Array, CborError>

Encode a JavaScript value to CBOR bytes.

Supported types: number, string, boolean, null, undefined, Uint8Array, arrays, and plain objects with string keys. Functions, symbols, classes, and circular references are not supported.

ts
const 
bytes
=
encode
({
temp
: 22.5,
humidity
: 65}).
orPanic
('encode failed')
// bytes is a Uint8Array

decode(data) ​

ts
decode(data: Uint8Array): Result<unknown, CborError>

Decode CBOR bytes back to a JavaScript value.

ts
const 
bytes
=
encode
(42).
orPanic
('encode')
const
value
=
decode
(
bytes
).
orPanic
('decode')
// value is 42

CborError ​

VariantWhen
EncodeFailedValue contains a type that can't be CBOR-encoded
DecodeFailedInput bytes are not valid CBOR

Mikro.js is built with AI assistance, code and docs alike. Read the AI disclosure.