@geckos.io/typed-array-buffer-schema

中文文档
1.2.1发布于 2022/08/16BSD-3-ClauseTypeScriptCommonJS近 30 天下载 4077

A Schema based Object to Buffer converter

原始使用文档
文档目录7 节
  1. A Schema based Object to Buffer converter
  2. Introduction
  3. Install
  4. Snapshot Interpolation
  5. Usage
  6. Schema ID
  7. DataViews
logo

Typed Array Buffer Schema

A Schema based Object to Buffer converter

Easily convert/compress your JavaScript Objects to Binary Data using a simple to use Schema.

NPM version Github Workflow GitHub last commit Downloads Codecov build with TypeScript


Introduction

Checkout this short introduction video on YouTube!

https://youtu.be/TBd1miOrLPQ

Install

Install from npm.

npm install @geckos.io/typed-array-buffer-schema

Or use the bundled version.

<script src="https://unpkg.com/@geckos.io/[email protected]/bundle/typed-array-buffer-schema.js"></script>
<script>
  const { BufferSchema, Model }  = Schema
  const { uint8, int16, uint16, int64, string8, ...more } Schema
</script>

Snapshot Interpolation

You can easily combine this library with the Snapshot Interpolation library @geckos.io/snapshot-interpolation.

Usage

model.js

import { BufferSchema, Model } from '@geckos.io/typed-array-buffer-schema'
import { uint8, int16, uint16, int64, string8 } from '@geckos.io/typed-array-buffer-schema'

const playerSchema = BufferSchema.schema('player', {
  id: uint8,
  // The length parameter controls the length of a String8 or String16
  name: { type: string8, length: 6 },
  // The digits parameter controls where the decimal point is placed
  // Therefore, it divides the maximum and minimum values by 10^n
  x: { type: int16, digits: 2 },
  y: { type: int16, digits: 2 }
})

const towerSchema = BufferSchema.schema('tower', {
  id: uint8,
  health: uint8,
  team: uint8
})

const mainSchema = BufferSchema.schema('main', {
  time: int64,
  tick: uint16,
  players: [playerSchema],
  towers: [towerSchema]
})

export const mainModel = new Model(mainSchema)

// if you get the error "RangeError: Offset is outside the bounds of the DataView", increase the max. bufferSize.
// default is 8 (8KB).
export const mainModel = new Model(mainSchema, 8)

server.js

import { mainModel } from './model'

const gameState = {
  time: new Date().getTime(),
  tick: 32580,
  players: [
    { id: 0, name: 'Mistin', x: -14.43, y: 47.78 },
    { id: 1, name: 'Coobim', x: 21.85, y: -78.48 }
  ],
  towers: [
    { id: 0, health: 100, team: 0 },
    { id: 1, health: 89, team: 0 },
    { id: 2, health: 45, team: 1 }
  ]
}

const buffer = mainModel.toBuffer(gameState)

// toBuffer() shrunk the byte size from 241 to only 56
// that is -77% compression!
console.log(JSON.stringify(gameState).length) // 241
console.log(buffer.byteLength) // 56

// send the buffer to the client (using geckos.io or any other library)
sendMessage(buffer)

client.js

import { mainModel } from './model'

onMessage(buffer => {
  // access your game state
  const gameState = mainModel.fromBuffer(buffer)
})

Schema ID

Each Schema has an unique ID. To get the Schema ID from the Schema, Model or Buffer, use the helper functions listed below:

// get the schema id
const schemaId = BufferSchema.getIdFromSchema(schema)

// get the id of the top level schema (added via new Schema())
const modelId = BufferSchema.getIdFromModel(model)

// get the id of the top level schema
const bufferId = BufferSchema.getIdFromBuffer(buffer)

DataViews

A list of all supported dataViews

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays

/** -128 to 127 (1 byte) */
export const int8 = { _type: 'Int8Array', _bytes: 1 }
/** 0 to 255 (1 byte) */
export const uint8 = { _type: 'Uint8Array', _bytes: 1 }

/** -32768 to 32767 (2 bytes) */
export const int16 = { _type: 'Int16Array', _bytes: 2 }
/** 0 to 65535 (2 bytes) */
export const uint16 = { _type: 'Uint16Array', _bytes: 2 }

/** -2147483648 to 2147483647 (4 bytes) */
export const int32 = { _type: 'Int32Array', _bytes: 4 }
/** 0 to 4294967295 (4 bytes) */
export const uint32 = { _type: 'Uint32Array', _bytes: 4 }

/** -2^63 to 2^63-1 (8 bytes) */
export const int64 = { _type: 'BigInt64Array', _bytes: 8 }
/** 0 to 2^64-1 (8 bytes) */
export const uint64 = { _type: 'BigUint64Array', _bytes: 8 }

/** 1.2×10-38 to 3.4×1038 (7 significant digits e.g., 1.123456) (4 bytes) */
export const float32 = { _type: 'Float32Array', _bytes: 4 }

/** 5.0×10-324 to 1.8×10308 (16 significant digits e.g., 1.123...15) (8 bytes) */
export const float64 = { _type: 'Float64Array', _bytes: 8 }

/** 1 byte per character */
export const string8 = { _type: 'String8', _bytes: 1 }
/** 2 bytes per character */
export const string16 = { _type: 'String16', _bytes: 2 }

/** An array of 7 booleans */
export const bool8 = { _type: 'BitArray8', _bytes: 1 }
/** An array of 15 booleans */
export const bool16 = { _type: 'BitArray16', _bytes: 2 }

版本与发布记录

@geckos.io/typed-array-buffer-schema 共发布 11 个版本,首次发布于 2020/06/15;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.1latest2022/08/164 年前
1.2.02022/04/044 年前
1.1.02021/11/304 年前
1.0.32021/11/294 年前
1.0.22020/09/106 年前
1.0.12020/09/106 年前

关于 @geckos.io/typed-array-buffer-schema 的常见问题

@geckos.io/typed-array-buffer-schema 是什么?

A Schema based Object to Buffer converter

如何安装 @geckos.io/typed-array-buffer-schema?

在项目目录运行 npm install @geckos.io/typed-array-buffer-schema,或使用 pnpm add / yarn add / bun add。

@geckos.io/typed-array-buffer-schema 支持 TypeScript 吗?

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

@geckos.io/typed-array-buffer-schema 支持 ESM 还是 CommonJS?

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

@geckos.io/typed-array-buffer-schema 可以商用吗?

@geckos.io/typed-array-buffer-schema 使用 BSD-3-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。另外不得用原作者名义为衍生产品背书。以上为概括,不构成法律意见,请以许可原文为准。

@geckos.io/typed-array-buffer-schema 的下载量有多少?

根据 npm 官方下载统计,@geckos.io/typed-array-buffer-schema 在 2026/10/16 – 2026/11/14 这 30 天内被下载 4,077 次,日均约 136 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@geckos.io/typed-array-buffer-schema 还在维护吗?

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