API Reference

cbor-x

Fast CBOR encoder and decoder for Node.js supporting structured cloning, records, and binary-friendly payloads.

Official Documentation ↗
CompatibilityThis documentation covers version v1. Check the official docs for the latest API surface.

Installation

npmnpm install cbor-x
pnpmpnpm add cbor-x

Methods

Overview

cbor-x is a fast CBOR encoder and decoder for Node.js with structured cloning, records, and binary-friendly payloads. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.

Core APIs

  • encode — Encodes a value to Compact Binary Object Representation bytes.
  • decode — Decodes CBOR bytes into a JavaScript value, honoring configured extensions.
  • new Encoder — Creates a reusable encoder that can share records and register custom types.

Example

const { encode, decode } = require('cbor-x');

const bytes = encode({ ok: true, n: 42 });
const value = decode(bytes);
console.log(value.n);

Notes

CBOR is not JSON, so peers need a compatible decoder. Encoder settings such as records must match on both ends.

Verification

Encode and decode a value with nested arrays and confirm the round-tripped value equals the original.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/kriszyp/cbor-x@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:48:40.244Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

FAQ

Is CBOR compatible with JSON?
CBOR is a binary format with its own type set; it can carry equivalent data but needs a CBOR decoder rather than JSON.parse.
When is CBOR preferable to MessagePack?
CBOR offers a richer type model and is an IETF standard, which helps when interoperating with systems that already speak CBOR.
How do records reduce size?
Record mode assigns numeric ids to shared object shapes so keys are not repeated, shrinking repeated structs substantially.