cbor-x
Fast CBOR encoder and decoder for Node.js supporting structured cloning, records, and binary-friendly payloads.
Official Documentation ↗v1. Check the official docs for the latest API surface.Installation
npm install cbor-xpnpm add cbor-xMethods
No methods match this filter.
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.
- The maxIncompleteBufferSize option limits how many bytes it keeps for one incomplete value (default 64 MB, 0x4000000); past that, the stream emits a “Maximum incomplete buffer size exceeded” error. (https://cdn.jsdelivr.net/gh/kriszyp/cbor-x@latest/README.md)
- If the input ends inside a value, the stream emits an “Unexpected end of CBOR data” error with incomplete: true. (https://cdn.jsdelivr.net/gh/kriszyp/cbor-x@latest/README.md)
- An undocumented custom handlesRead decoder that consumes multiple sibling items cannot be framed on fragmented input; it throws an error naming the tag. (https://cdn.jsdelivr.net/gh/kriszyp/cbor-x@latest/README.md)
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.
Report a problem with this API docOpens a pre-filled issue on GitHub.