# @dfinity/cbor 中文文档

规范地址：https://npmcn.com/package/%40dfinity/cbor/

版本：0\.2\.3
npm 元数据更新时间：2026-10-05T08:12:57.196Z
中文译文更新时间：2026-11-12T05:20:21.550Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40dfinity/cbor
原文对照：https://npmcn.com/package/%40dfinity/cbor/?lang=original
项目主页：https://github.com/dfinity/cbor-js#readme
源码仓库：https://github.com/dfinity/cbor-js

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：94 kB，19 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2025-05-27T10:55:12.308Z
- 最新版本发布：2026-04-23T08:50:03.229Z
- 近 30 天下载量：189667（2026-10-12 至 2026-11-10，npm 官方下载统计）
- 已发布版本数：5（近 12 个月 1 个）

## 中文简介

纯JavaScript中"简明二进制对象"(CBOR)的小型执行.

## 安装

```sh
npm install @dfinity/cbor
```

也可以使用：`pnpm add @dfinity/cbor`、`yarn add @dfinity/cbor`、`bun add @dfinity/cbor`

## README

# CBOR 编码器/解码器

[![NPM Version](https://img.shields.io/npm/v/%40dfinity%2Fcbor)](https://www.npmjs.com/package/@dfinity/cbor) ![Libraries.io dependency status for latest release](https://img.shields.io/librariesio/release/npm/%40dfinity%2Fcbor)

[![Test](https://github.com/dfinity/cbor-js/actions/workflows/test.yml/badge.svg)](https://github.com/dfinity/cbor-js/actions/workflows/test.yml) [![Lint](https://github.com/dfinity/cbor-js/actions/workflows/lint.yml/badge.svg)](https://github.com/dfinity/cbor-js/actions/workflows/lint.yml)

纯JavaScript中"简明二进制对象"(CBOR)的小型执行.

> 注意: 此软件包并非100%兼容 [CBOR 规格](https://www.rfc-editor.org/rfc/rfc8949.html)见 [未执行](https://npmcn.com/package/%40dfinity/cbor/#readme-not-implemented) 详细情况.

## 安装

使用 `npm`数字 :

```bash
npm install @dfinity/cbor
```

使用 `pnpm`数字 :

```bash
pnpm add @dfinity/cbor
```

使用 `yarn`数字 :

```bash
yarn add @dfinity/cbor
```

## 使用量

简单 :

```ts
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`
```

改为替换/复制:

```ts
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 密码

## :工具箱:函数

-   [解码](https://npmcn.com/package/%40dfinity/cbor/#readme-gear-decode)
-   [编码](https://npmcn.com/package/%40dfinity/cbor/#readme-gear-encode)
-   [自动编码](https://npmcn.com/package/%40dfinity/cbor/#readme-gear-encodewithselfdescribedtag)

### : gear: 解码

将 CBOR 字节数组解码为值. 更多信息请参见 QQlink Reviver .

| 函数 | 类型 |
| --- | --- |
| `decode` | `<T extends unknown = any>(input: Uint8Array<ArrayBufferLike>, reviver?: Reviver<T> or undefined) => T` |

参数:

-   `input`: - CBOR字节数组来解码.
-   `reviver`: - 一个可用于操纵已解码值的函数.

实例:

简单

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

退役

```ts
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: 编码

将一个值编码成 CBOR 字节数组.

| 函数 | 类型 |
| --- | --- |
| `encode` | `<T = any>(value: CborValue<T>, replacer?: Replacer<T> or undefined) => Uint8Array<ArrayBufferLike>` |

参数:

-   `value`: - 编码值 .
-   `replacer`: - 一个在被编码之前可用于操纵输入的函数.

实例:

简单

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

替换器

```ts
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: 用自定义编码

将一个值编码为 CBOR 字节数组( 和 QQlink 编码 ) , 但预置自定义的 CBOR 标记( 55799).

| 函数 | 类型 |
| --- | --- |
| `encodeWithSelfDescribedTag` | `<T = any>(value: CborValue<T>, replacer?: Replacer<T> or undefined) => Uint8Array<ArrayBufferLike>` |

参数:

-   `value`: - 编码值 .
-   `replacer`: - 一个在被编码之前可用于操纵输入的函数.

实例:

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

## :扳手:常数

-   [(原始内容存档于2018-10-21) (中文(简体) ). CBOR SELF DESCRIBED TAG](https://npmcn.com/package/%40dfinity/cbor/#readme-gear-cbor_self_described_tag)
-   [COBOR STOP 代码](https://npmcn.com/package/%40dfinity/cbor/#readme-gear-cbor_stop_code)
-   [类型( V)](https://npmcn.com/package/%40dfinity/cbor/#readme-gear-token_value_max)
-   [单曲](https://npmcn.com/package/%40dfinity/cbor/#readme-gear-one_byte_max)
-   [双BYTES MAS](https://npmcn.com/package/%40dfinity/cbor/#readme-gear-two_bytes_max)
-   [四点半](https://npmcn.com/package/%40dfinity/cbor/#readme-gear-four_bytes_max)
-   [\[8\] (中文(简体) )](https://npmcn.com/package/%40dfinity/cbor/#readme-gear-eight_bytes_max)

### :齿轮: CBOR SELF DESCRIBED TAG

标签号码 `55799`,CBOR的自定义标签. 这个标签的头的序列化是 `0xd9d9f7`。 。 。 .

| 常数 | 类型 |
| --- | --- |
| `CBOR_SELF_DESCRIBED_TAG` | `55799` |

### : 齿轮: CBOR STOP CODE

| 常数 | 类型 |
| --- | --- |
| `CBOR_STOP_CODE` | `unique symbol` |

### : 齿轮: TOKEN VALUE MAX

| 常数 | 类型 |
| --- | --- |
| `TOKEN_VALUE_MAX` | `23` |

### :齿轮: one BYTE MAX

| 常数 | 类型 |
| --- | --- |
| `ONE_BYTE_MAX` | `255` |

### : 齿轮: 2 BYTES MAX

| 常数 | 类型 |
| --- | --- |
| `TWO_BYTES_MAX` | `65535` |

### : 齿轮: Four BYTES MAX

| 常数 | 类型 |
| --- | --- |
| `FOUR_BYTES_MAX` | `4294967295` |

### :齿轮: E8T BYTES MAX

可编码为 8 字节的最大值 : `18446744073709551615n`。 。 。 .

| 常数 | 类型 |
| --- | --- |
| `EIGHT_BYTES_MAX` | `bigint` |

## : factor: 解码错误

## : factor: 编码错误

## : nut 并 bolt: enum

-   [Cbor 简单字](https://npmcn.com/package/%40dfinity/cbor/#readme-gear-cborsimpletype)
-   [CborMajorType 软件](https://npmcn.com/package/%40dfinity/cbor/#readme-gear-cbormajortype)
-   [Cbor 最小类型](https://npmcn.com/package/%40dfinity/cbor/#readme-gear-cborminortype)

### :齿轮: CborSimpleType

| 财产 | 类型 | 说明 |
| --- | --- | --- |
| `False` | `0x14` |  |
| `True` | `0x15` |  |
| `Null` | `0x16` |  |
| `Undefined` | `0x17` |  |
| `Break` | `0x1f` |  |

### : 齿轮: Cbor MajorType

| 财产 | 类型 | 说明 |
| --- | --- | --- |
| `UnsignedInteger` | `0` |  |
| `NegativeInteger` | `1` |  |
| `ByteString` | `2` |  |
| `TextString` | `3` |  |
| `Array` | `4` |  |
| `Map` | `5` |  |
| `Tag` | `6` |  |
| `Simple` | `7` |  |

### : 齿轮: CborminorType

| 财产 | 类型 | 说明 |
| --- | --- | --- |
| `Value` | `23` |  |
| `OneByte` | `24` |  |
| `TwoBytes` | `25` |  |
| `FourBytes` | `26` |  |
| `EightBytes` | `27` |  |
| `Indefinite` | `31` |  |

## 未执行

-   自定义标签编码/解码 .
    -   自定义标记允许自定义类型的编码和解码.
    -   目前我们不使用这种自定义标记(尽管我们可能应该使用).
    -   因为我们不直接编码开发者提供数据(由Candidd编码),那么我们可以安全地说我们不需要这个特性.
-   文本/字节字符串的单位测试,长度不适宜于四个字节或更小的字节.
    -   文本字符串的"长度"可以用8字节编码,这意味着最大字符串长度是: `18,446,744,073,709,551,615`。测试覆盖一个字符串长度,编码最多可达4个字节,比这个更长,测试变得极其缓慢.
    -   4个字节中最多的是 `2,147,483,647` 表示 ~2gb 字符串的长度,但无论如何都无法装入单个的 IC 信件.
-   文本和字节字符串的定时长度编码
    -   要将字符串长度比前面提到的8字节限制长,可以将字符串以"不确定"长度来编码.
    -   与前一点类似,由于信息限制,这对IC来说是不切实际的.

## 捐款

看看这个 [捐款准则](https://npmcn.com/package/%40dfinity/cbor/.github/CONTRIBUTING.md)。 。 。 .

### 设置

-   安装 [小时](https://pnpm.io/)
-   安装 [承诺](https://commitizen-tools.github.io/commitizen/)
-   安装 [承诺前](https://pre-commit.com/)
-   安装依赖性 :
    
    ```bash
    pnpm install
    ```
    

### 正在测试

```bash
pnpm test
```

### 格式化

```bash
pnpm format
```

### 生成文件

我们用 [tsdoc- 标记下](https://github.com/peterpeterparker/tsdoc-markdown) 生成文档.

更新文件 `README.md` 文件, 运行 :

```bash
pnpm tsdoc
```

## 许可证

本项目根据以下规定获得许可证: [Apache 许可证 2.0](https://npmcn.com/package/%40dfinity/cbor/LICENSE)。 。 。 .

## 常见问题

### @dfinity/cbor 是什么？

纯JavaScript中"简明二进制对象"\(CBOR\)的小型执行\.

### 如何安装 @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 和提交记录判断。

