@glow-xyz/beet

中文文档
0.6.0发布于 2022/08/27Apache-2.0TypeScriptESM + CJS近 30 天下载 5737

Strict borsh compatible de/serializer

原始使用文档
文档目录9 节
  1. Features
  2. Fixed Size vs. Dynamic Types
  3. API
  4. Examples
  5. Single Fixed Struct Configuration
  6. Single Fixable Struct Configuration
  7. Nested Struct Configuration
  8. Using Beet Primitives Directly
  9. Using Beet Composites Directly

@glow-xyz/beet

Strict borsh compatible de/serializer.

Table of Contents generated with DocToc

Features

  • fully composable, i.e. coption(array(utf8String)) is handled correctly
  • structs can be nested and composed
  • pre-computes byteSize of any fixed size type, no matter how deeply nested or composed it is
  • converts non-fixed types to their fixed versions simply by providing a value or serialized data
  • fixed size and fixable structs expose identical serialize/deserialize API and perform conversions under the hood when needed
  • logs struct configs including byte sizes as well as de/serialization tasks for easy diagnostics
beet:debug struct GameStruct {
beet:debug   win: u8 1 B
beet:debug   totalWin: u16 2 B
beet:debug   whaleAccount: u128 16 B
beet:debug   losses: i32 4 B
beet:debug } 23 B +0ms
beet:trace serializing [GameStruct] _GameScore { win: 1, totalWin: 100, whaleAccount: <BN: fffffffffffffffffffffffffffffffb>, losses: -234 } to 23 bytes buffer +0ms
beet:trace deserializing [GameStruct] from 23 bytes buffer +2ms
beet:trace <Buffer 01 64 00 fb ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 16 ff ff ff> +0ms
beet:trace [
beet:trace     1, 100,   0, 251, 255, 255,
beet:trace   255, 255, 255, 255, 255, 255,
beet:trace   255, 255, 255, 255, 255, 255,
beet:trace   255,  22, 255, 255, 255
beet:trace ] +0ms

Fixed Size vs. Dynamic Types

Beet is optimized for fixed types as this allows logging detailed diagnostics about the structure of data that it is processing as well as avoiding Buffer resizes.

Only beets that have fixed in the name are of fixed size, all others are fixable types which expose toFixedFromData and toFixedFromValue methods to convert to a fixed beet from serialized data or a value respectively.

Beet provides the FixableBeetStruct to de/serialize args that have non-fixed size fields.

Thus beet implements the entire borsh spec, however if you want a library that processes dynamic types directly use one of the alternatives, i.e. borsh-js.

API

Please find the API docs here.

Examples

Single Fixed Struct Configuration

import { BeetStruct, i32, u16, u8 } from '@metaplex-foundation/beet'

class Result {
  constructor(
    readonly win: number,
    readonly totalWin: number,
    readonly losses: number
  ) {}

  static readonly struct = new BeetStruct<Result>(
    [
      ['win', u8],
      ['totalWin', u16],
      ['losses', i32],
    ],
    (args) => new Result(args.win!, args.totalWin!, args.losses!),
    'Results'
  )
}

Single Fixable Struct Configuration

import { FixableBeetStruct, i32, u16, u8, array } from '@metaplex-foundation/beet'

class Result {
  constructor(
    readonly win: number,
    readonly totalWin: number,
    readonly losses: number[]
  ) {}

  static readonly struct = new FixableBeetStruct<Result>(
    [
      ['win', u8],
      ['totalWin', u16],
      ['losses', array(i32)],
    ],
    (args) => new Result(args.win!, args.totalWin!, args.losses!),
    'Results'
  )
}

Nested Struct Configuration

NOTE: uses Result struct from the above example for the results field of Trader

import { BeetStruct, fixedSizeUtf8String } from '@metaplex-foundation/beet'
class Trader {
  constructor(
    readonly name: string,
    readonly results: Results,
    readonly age: number
  ) {}

  static readonly struct = new BeetStruct<Trader>(
    [
      ['name', fixedSizeUtf8String(4)],
      ['results', Results.struct],
      ['age', u8],
    ],
    (args) => new Trader(args.name!, args.results!, args.age!),
    'Trader'
  )
}
  
const trader = new Trader('bob ', new Results(20, 1200, -455), 22)
const [buf] = Trader.struct.serialize(trader)
const [deserializedTrader] = Trader.struct.deserialize(buf)

Using Beet Primitives Directly

Fixed Size

import { u8 } from '@metaplex-foundation/beet'
const n = 1
const buf = Buffer.alloc(u8.byteSize)
u8.write(buf, 0, n)
u8.read(buf, 0) // === 1

Dynamic Size

import { u8, array } from '@metaplex-foundation/beet'
const xs = [ 1, 2 ]
const beet = array(u8)
const fixedBeet = beet.toFixedFromValue(xs)
const buf = Buffer.alloc(fixedBeet.byteSize)
fixedBeet.write(buf, 0, xs)
fixedBeet.read(buf, 0) // === [ 1, 2 ]

Using Beet Composites Directly

NOTE: use Result struct from the above example to wrap in a Composite type

Option

NOTE: that the coption is a fixable beet since it has a different size for the Some vs. the None case.

const resultOption: Beet<COption<Result>> = coption(Result.struct)

const result = new Result(20, 1200, -455)
const fixedBeet = resultOption.toFixedFromValue(result)

const buf = Buffer.alloc(fixedBeet.byteSize)
fixedBeet.write(buf, 0, result)
beet.read(buf, 0) // same  as result

Array

Uniform Fixed Size
const resultArray: Beet<Array<Result>> = uniformFixedSizeArray(Result.struct, 3)
const results =[ new Result(20, 1200, -455), new Result(3, 999, 0), new Result(30, 100, -3) ]
const buf = Buffer.alloc(resultArray.byteSize)
beet.write(buf, 0, results)
beet.read(buf, 0) // same  as results
Dynamic Size
const resultArray: Beet<Array<Result>> = array(Result.struct)
const results =[ new Result(20, 1200, -455), new Result(3, 999, 0), new Result(30, 100, -3) ]
const fixedBeet = resultsArray.toFixedFromValue(results)

const buf = Buffer.alloc(fixedBeet.byteSize)
fixedBeet.write(buf, 0, results)
fixedBeet.read(buf, 0) // same  as results

版本与发布记录

@glow-xyz/beet 共发布 6 个版本,首次发布于 2022/07/04;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.6.0latest2022/08/274 年前
0.5.02022/07/294 年前
0.4.02022/07/294 年前
0.4.0-beta.12022/07/294 年前
0.4.0-beta.02022/07/294 年前
0.3.02022/07/044 年前

关于 @glow-xyz/beet 的常见问题

@glow-xyz/beet 是什么?

Strict borsh compatible de/serializer

如何安装 @glow-xyz/beet?

在项目目录运行 npm install @glow-xyz/beet,或使用 pnpm add / yarn add / bun add。

@glow-xyz/beet 支持 TypeScript 吗?

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

@glow-xyz/beet 支持 ESM 还是 CommonJS?

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

@glow-xyz/beet 可以商用吗?

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

@glow-xyz/beet 的下载量有多少?

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

@glow-xyz/beet 还在维护吗?

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