@dfinity/cbor

中文文档
0.2.3发布于 2026/04/23Apache-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 19万

A small implementation of Concise Binary Object Representation (CBOR) in pure JavaScript.

原始使用文档
文档目录28 节
  1. Installation
  2. Usage
  3. API
  4. :toolbox: Functions
  5. :gear: decode
  6. :gear: encode
  7. :gear: encodeWithSelfDescribedTag
  8. :wrench: Constants
  9. :gear: CBOR_SELF_DESCRIBED_TAG
  10. :gear: CBOR_STOP_CODE
  11. :gear: TOKEN_VALUE_MAX
  12. :gear: ONE_BYTE_MAX
  13. :gear: TWO_BYTES_MAX
  14. :gear: FOUR_BYTES_MAX
  15. :gear: EIGHT_BYTES_MAX
  16. :factory: DecodingError
  17. :factory: EncodingError
  18. :nut_and_bolt: Enum
  19. :gear: CborSimpleType
  20. :gear: CborMajorType
  21. :gear: CborMinorType
  22. Not implemented
  23. Contributing
  24. Setup
  25. Running tests
  26. Formatting
  27. Generating documentation
  28. License

CBOR encoder/decoder

NPM Version Libraries.io dependency status for latest release

Test Lint

A small implementation of Concise Binary Object Representation (CBOR) in pure JavaScript.

Note: this package is not 100% compatible with the CBOR specification. See the Not implemented section for more details.

Installation

Using npm:

npm install @dfinity/cbor

Using pnpm:

pnpm add @dfinity/cbor

Using yarn:

yarn add @dfinity/cbor

Usage

Simple:

import { encode, decode } from '@dfinity/cbor';

const value = true;
const encoded = encode(value); // returns `Uint8Array [245]` (which is "F5" in hex)
const decoded = decode(encoded); // returns `true`

With replacer/reviver:

import { encode, decode, type Replacer, type Reviver } from '@dfinity/cbor';

const value = { a: 1, b: 2 };

// Encoding with replacer
const replacer: Replacer = val => (typeof val === 'number' ? val * 2 : val);
const result = encode(value, replacer);
decode(result); // { a: 2, b: 4 }

// Decoding with reviver
const bytes = encode(value);
const reviver: Reviver = val => (typeof val === 'number' ? val * 2 : val);
decode(bytes, reviver); // { a: 2, b: 4 }

API

:toolbox: Functions

:gear: decode

Decodes a CBOR byte array into a value. See {@link Reviver} for more information.

FunctionType
decode<T extends unknown = any>(input: Uint8Array<ArrayBufferLike>, reviver?: Reviver<T> or undefined) => T

Parameters:

  • input: - The CBOR byte array to decode.
  • reviver: - A function that can be used to manipulate the decoded value.

Examples:

Simple

const value = true;
const encoded = encode(value); // returns `Uint8Array [245]` (which is "F5" in hex)
const decoded = decode(encoded); // returns `true`

Reviver

const bytes = ...; // Uint8Array corresponding to the CBOR encoding of `{ a: 1, b: 2 }`
const reviver: Reviver = val => (typeof val === 'number' ? val * 2 : val);
decode(bytes, reviver); // returns `{ a: 2, b: 4 }`

:gear: encode

Encodes a value into a CBOR byte array.

FunctionType
encode<T = any>(value: CborValue<T>, replacer?: Replacer<T> or undefined) => Uint8Array<ArrayBufferLike>

Parameters:

  • value: - The value to encode.
  • replacer: - A function that can be used to manipulate the input before it is encoded.

Examples:

Simple

const value = true;
const encoded = encode(value); // returns `Uint8Array [245]` (which is "F5" in hex)

Replacer

const replacer: Replacer = val => (typeof val === 'number' ? val * 2 : val);
encode({ a: 1, b: 2 }, replacer); // returns the Uint8Array corresponding to the CBOR encoding of `{ a: 2, b: 4 }`

:gear: encodeWithSelfDescribedTag

Encodes a value into a CBOR byte array (same as {@link encode}), but prepends the self-described CBOR tag (55799).

FunctionType
encodeWithSelfDescribedTag<T = any>(value: CborValue<T>, replacer?: Replacer<T> or undefined) => Uint8Array<ArrayBufferLike>

Parameters:

  • value: - The value to encode.
  • replacer: - A function that can be used to manipulate the input before it is encoded.

Examples:

const value = true;
const encoded = encodeWithSelfDescribedTag(value); // returns the Uint8Array [217, 217, 247, 245] (which is "D9D9F7F5" in hex)

:wrench: Constants

:gear: CBOR_SELF_DESCRIBED_TAG

The tag number 55799, the self-described tag for CBOR. The serialization of this tag's head is 0xd9d9f7.

ConstantType
CBOR_SELF_DESCRIBED_TAG55799

:gear: CBOR_STOP_CODE

ConstantType
CBOR_STOP_CODEunique symbol

:gear: TOKEN_VALUE_MAX

ConstantType
TOKEN_VALUE_MAX23

:gear: ONE_BYTE_MAX

ConstantType
ONE_BYTE_MAX255

:gear: TWO_BYTES_MAX

ConstantType
TWO_BYTES_MAX65535

:gear: FOUR_BYTES_MAX

ConstantType
FOUR_BYTES_MAX4294967295

:gear: EIGHT_BYTES_MAX

The maximum value that can be encoded in 8 bytes: 18446744073709551615n.

ConstantType
EIGHT_BYTES_MAXbigint

:factory: DecodingError

:factory: EncodingError

:nut_and_bolt: Enum

:gear: CborSimpleType

PropertyTypeDescription
False0x14
True0x15
Null0x16
Undefined0x17
Break0x1f

:gear: CborMajorType

PropertyTypeDescription
UnsignedInteger0
NegativeInteger1
ByteString2
TextString3
Array4
Map5
Tag6
Simple7

:gear: CborMinorType

PropertyTypeDescription
Value23
OneByte24
TwoBytes25
FourBytes26
EightBytes27
Indefinite31

Not implemented

  • Custom tag encoding/decoding.
    • Custom tags allow for encoding and decoding of custom types.
    • We currently don't use this custom tags (although we probably should).
    • Since we don't directly encode developer provided data (that's encoded by Candid) then we can safely say we don't need the feature.
  • Unit tests for text/byte strings with a length that does not fit in four bytes or less.
    • The "length" of the text string can be encoded with up to 8 bytes, which means the largest possible string length is 18,446,744,073,709,551,615. The tests cover a string length that's encoded up to four 4 bytes, longer than this and the tests became extremely slow.
    • The largest number in 4 bytes is 2,147,483,647 which would represent the length of an ~2gb string, which is not possible to fit into a single IC message anyway.
  • Indeterminite length encoding for text and byte strings
    • To encode a string length longer than the previously mentioned 8 byte limit, a string can be encoded with an "indeterminate" length.
    • Similar to the previous point, this would be impractical for the IC due to message limits.

Contributing

Check out the contribution guidelines.

Setup

Running tests

pnpm test

Formatting

pnpm format

Generating documentation

We use tsdoc-markdown to generate the documentation.

To update the documentation in the README.md file, run:

pnpm tsdoc

License

This project is licensed under the Apache License 2.0.

版本与发布记录

@dfinity/cbor 共发布 5 个版本,首次发布于 2025/05/27;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.3latest2026/04/236 个月前
0.2.22025/06/171.4 年前
0.2.12025/05/281.5 年前
0.2.02025/05/281.5 年前
0.1.02025/05/271.5 年前

关于 @dfinity/cbor 的常见问题

@dfinity/cbor 是什么?

A small implementation of Concise Binary Object Representation (CBOR) in pure JavaScript.

如何安装 @dfinity/cbor?

在项目目录运行 npm install @dfinity/cbor,或使用 pnpm add / yarn add / bun add。

@dfinity/cbor 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dfinity/cbor 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@dfinity/cbor 可以商用吗?

@dfinity/cbor 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@dfinity/cbor 的下载量有多少?

根据 npm 官方下载统计,@dfinity/cbor 在 2026/10/12 – 2026/11/10 这 30 天内被下载 189,667 次,日均约 6,322 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dfinity/cbor 还在维护吗?

最新版本 0.2.3 发布于 2026/04/23(6 个月前);至今共发布 5 个版本,其中过去 12 个月发布了 1 个;首次发布于 2025/05/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。