CBOR 编码器/解码器
纯JavaScript中"简明二进制对象"(CBOR)的小型执行.
安装
使用 npm数字 :
npm install @dfinity/cbor
使用 pnpm数字 :
pnpm add @dfinity/cbor
使用 yarn数字 :
yarn add @dfinity/cbor
使用量
简单 :
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`
改为替换/复制:
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 密码
:工具箱:函数
: gear: 解码
将 CBOR 字节数组解码为值. 更多信息请参见 QQlink Reviver .
| 函数 | 类型 |
|---|---|
decode | <T extends unknown = any>(input: Uint8Array<ArrayBufferLike>, reviver?: Reviver<T> or undefined) => T |
参数:
input: - CBOR字节数组来解码.reviver: - 一个可用于操纵已解码值的函数.
实例:
简单
const value = true;
const encoded = encode(value); // returns `Uint8Array [245]` (which is "F5" in hex)
const decoded = decode(encoded); // returns `true`
退役
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: - 一个在被编码之前可用于操纵输入的函数.
实例:
简单
const value = true;
const encoded = encode(value); // returns `Uint8Array [245]` (which is "F5" in hex)
替换器
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: - 一个在被编码之前可用于操纵输入的函数.
实例:
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
- COBOR STOP 代码
- 类型( V)
- 单曲
- 双BYTES MAS
- 四点半
- [8] (中文(简体) )
:齿轮: 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
:齿轮: 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字节编码,这意味着最大字符串长度是:
- 文本和字节字符串的定时长度编码
- 要将字符串长度比前面提到的8字节限制长,可以将字符串以"不确定"长度来编码.
- 与前一点类似,由于信息限制,这对IC来说是不切实际的.
捐款
看看这个 捐款准则。 。 。 .
设置
正在测试
pnpm test
格式化
pnpm format
生成文件
我们用 tsdoc- 标记下 生成文档.
更新文件 README.md 文件, 运行 :
pnpm tsdoc
许可证
本项目根据以下规定获得许可证: Apache 许可证 2.0。 。 。 .