npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@glow-xyz/beet

v0.6.0

Published

Strict borsh compatible de/serializer

Downloads

2,020

Readme

@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