# @geckos\.io/typed-array-buffer-schema 中文文档

规范地址：https://npmcn.com/package/%40geckos.io/typed-array-buffer-schema/

版本：1\.2\.1
npm 元数据更新时间：2022-08-16T11:22:27.562Z
中文译文更新时间：2026-11-16T01:32:55.369Z
软件包许可：BSD-3-Clause

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40geckos.io/typed-array-buffer-schema
原文对照：https://npmcn.com/package/%40geckos.io/typed-array-buffer-schema/?lang=original
项目主页：http://geckos.io/
源码仓库：https://github.com/geckosio/typed-array-buffer-schema

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：279 kB，43 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：2 个
- 首次发布：2020-06-15T14:09:35.755Z
- 最新版本发布：2022-08-16T11:22:27.487Z
- 近 30 天下载量：4077（2026-10-16 至 2026-11-14，npm 官方下载统计）
- 已发布版本数：11（近 12 个月 0 个）

## 中文简介

基于缓冲转换器的 Schema 对象

## 安装

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

也可以使用：`pnpm add @geckos.io/typed-array-buffer-schema`、`yarn add @geckos.io/typed-array-buffer-schema`、`bun add @geckos.io/typed-array-buffer-schema`

## README

![logo](https://npmcn.com/package/%40geckos.io/typed-array-buffer-schema/readme/logo.png)

# 已键入阵列缓冲Schema

## 基于缓冲转换器的 Schema 对象

#### 使用简单的Schema将您的 JavaScript 对象易地转换/压缩为二进制数据 .

[![NPM version](https://img.shields.io/npm/v/@geckos.io/typed-array-buffer-schema.svg?style=flat-square)](https://www.npmjs.com/package/@geckos.io/typed-array-buffer-schema) [![Github Workflow](https://img.shields.io/github/workflow/status/geckosio/typed-array-buffer-schema/CI/master?label=github%20build&logo=github&style=flat-square)](https://github.com/geckosio/typed-array-buffer-schema/actions?query=workflow%3ACI) [![GitHub last commit](https://img.shields.io/github/last-commit/geckosio/typed-array-buffer-schema?style=flat-square)](https://github.com/geckosio/typed-array-buffer-schema/commits/master) [![Downloads](https://img.shields.io/npm/dm/@geckos.io/typed-array-buffer-schema.svg?style=flat-square)](https://www.npmjs.com/package/@geckos.io/typed-array-buffer-schema) [![Codecov](https://img.shields.io/codecov/c/github/geckosio/typed-array-buffer-schema?logo=codecov&style=flat-square)](https://codecov.io/gh/geckosio/typed-array-buffer-schema) [![build with TypeScript](https://img.shields.io/badge/built%20with-TypeScript-blue?style=flat-square)](https://www.typescriptlang.org/)

* * *

## 导言

看看YouTube上的简短介绍视频!

[https://youtu.be/TBd1miOrLPQ (中文(简体) )](https://youtu.be/TBd1miOrLPQ)

## 安装

从 npm 安装 .

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

或使用捆绑版.

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

## 快照内插

您可以轻松地将这个库与快照国际化库合并 [@geckos](https://www.npmjs.com/package/@geckos.io/snapshot-interpolation)。 。 。 .

## 使用量

#### 型号.js

```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)
```

#### 服务器.js

```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)
```

#### 客户端.js

```js
import { mainModel } from './model'

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

## Schema 标识

每个Schema都有独特的ID. 要从 Schema, Model 或 缓冲中获取 Schema ID, 请使用下列帮助函数 :

```ts
// 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)
```

## 数据视图

所有支持数据列表

```js
// 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 是什么？

基于缓冲转换器的 Schema 对象

### 如何安装 @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 和提交记录判断。

