@exodus/borc

中文文档
2.1.1发布于 2024/06/14MITCommonJS近 30 天下载 7.8万

Encode and parse data in the Concise Binary Object Representation (CBOR) data format (RFC7049).

原始使用文档
文档目录11 节
  1. About
  2. Installation
  3. Benchmarks
  4. Example
  5. API
  6. Supported types
  7. Customizations
  8. Encode Custom Types
  9. Encode Custom Tags
  10. Decode Custom Tags
  11. License

borc

Coverage Status Dependency Status Travis CI Circle CI

Assimilate all your JavaScript objects into the Concise Binary Object Representation (CBOR) data format (RFC7049) as fast as possible.

About

This library is a fork of the awesome node-cbor. It borrows a lot of the interface, but drops all streaming and async processing in favor of a minimal syn api and being as fast as possible.

Installation

$ npm install --save borc

Benchmarks

TODO

Example

const cbor = require('borc')
const assert = require('assert')

const encoded = cbor.encode(true) // returns <Buffer f5>
const decoded = cbor.decodeFirst(encoded)
// decoded is the unpacked object
assert.ok(decoded === true)

// Use integers as keys
var m = new Map()
m.set(1, 2)
encoded = cbor.encode(m) // <Buffer a1 01 02>

API

See https://dignifiedquire.github.io/borc for details

The sync encoding and decoding are exported as a leveldb encoding, as cbor.leveldb.

Supported types

The following types are supported for encoding:

  • boolean
  • number (including -0, NaN, and ±Infinity)
  • string
  • Array, Set (encoded as Array)
  • Object (including null), Map
  • undefined
  • Buffer
  • Date,
  • RegExp
  • url.URL
  • bignumber

Decoding supports the above types, including the following CBOR tag numbers:

TagGenerated Type
0Date
1Date
2bignumber
3bignumber
4bignumber
5bignumber
32url.URL
35RegExp

Customizations

Borc supports custom tags as well as custom input types.

Encode Custom Types

class MyType {
  constructor (val) {
    this.val = val
  }

  // Gets called when encoding this object
  // gen - instance of the encoder
  // obj - the object being encoded
  //
  // should return true on success and false otherwise
  encodeCBOR (gen) {
    return gen.pushAny('mytype:' + this.val)
  }
}

cbor.encode([new MyType('hello')])

Encode Custom Tags

cbor.encode([new cbor.Tagged(42, 'hello')])

Decode Custom Tags

const decoder = new cbor.Decoder({
  tags: {
    42: (val) => val + ' world'
  }
})

License

MIT

版本与发布记录

@exodus/borc 共发布 7 个版本,首次发布于 2019/07/08;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.1.1latest2024/06/142 年前
2.1.0-exodus42023/03/303 年前
2.1.0-exodus32023/03/303 年前
3.0.0-exodus.02021/06/215 年前
2.1.0-exodus22019/07/177 年前
2.1.0-exodus12019/07/177 年前

关于 @exodus/borc 的常见问题

@exodus/borc 是什么?

Encode and parse data in the Concise Binary Object Representation (CBOR) data format (RFC7049).

如何安装 @exodus/borc?

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

@exodus/borc 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/exodus__borc 是否存在。

@exodus/borc 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@exodus/borc 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=4。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@exodus/borc 可以商用吗?

@exodus/borc 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@exodus/borc 的下载量有多少?

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

@exodus/borc 还在维护吗?

最新版本 2.1.1 发布于 2024/06/14(2 年前);至今共发布 7 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/07/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。