# @cmdcode/buff 中文文档

规范地址：https://npmcn.com/package/%40cmdcode/buff/

版本：2\.2\.5
npm 元数据更新时间：2024-01-30T00:37:38.428Z
中文译文更新时间：2026-11-10T04:00:35.165Z
软件包许可：CC-BY-1\.0

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

原始 npm 页面：https://www.npmjs.com/package/%40cmdcode/buff
原文对照：https://npmcn.com/package/%40cmdcode/buff/?lang=original
源码仓库：https://github.com/cmdruid/buff

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

- 包体积（解压后）：462 kB，74 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：2 个
- 首次发布：2023-09-05T21:04:25.125Z
- 最新版本发布：2024-01-30T00:37:38.204Z
- 近 30 天下载量：11147（2026-10-10 至 2026-11-08，npm 官方下载统计）
- 已发布版本数：12（近 12 个月 0 个）

## 中文简介

一支瑞士军刀 字节操纵.

## 安装

```sh
npm install @cmdcode/buff
```

也可以使用：`pnpm add @cmdcode/buff`、`yarn add @cmdcode/buff`、`bun add @cmdcode/buff`

## README

# 婴儿

字节操控的刀子.

特征 :

-   易地在数据格式之间移动!
-   Base58, Base64, Bech32 等间编码/解码.
-   `Buff` 被确认为本地对象 `Uint8Array`。 。 。 .
-   预置、 附加、 排序、 分拆和加入多种格式的数组.
-   读取并作前缀瓦林.
-   将数据块转换成可消耗流 .
-   用途 `DataView.setUint8` 为超快表演.
-   支持所有事物的内向!

## 如何导入

这个库旨在支持在节点和浏览器环境中的经典和现代无害环境管理进口.

通过国家预防机制或纱线安装的实例:

```bash
npm install @cmdcode/buff || yarn add @cmdcode/buff
```

经典导入到节点项目 :

```ts
const { Buff, Bytes } = require('@cmdcode/buff')
```

现代导入到节点项目 :

```ts
import { Buff, Bytes } from '@cmdcode/buff'
```

经典导入到基于浏览器的项目 :

```html
<script src="https://unpkg.com/@cmdcode/buff/dist/browser.js"></script>
<script>
  const { Buff, Bytes } = window.buff
</script>
```

现代导入到基于浏览器的项目 :

```html
<script type="module">
  import { Buff, Bytes } from "https://unpkg.com/@cmdcode/buff/dist/module.mjs" 
</script>
```

## 如何使用

这个 `Buff` 类是基数的缩放 `Uint8Array` 课。 它提供了一个Uint8Array的同一种默认功能,可以作为Uint8Array的倒置替代. Typescript将Buff视为Uint8Array对象.

```ts
import { Buff, Bytes } from '@cmdcode/buff'

// Bytes covers value types that are convertable to Uint8Array.
type Bytes  = string | number | bigint | Uint8Array | Buff
// You can optionally specify the endianess of data.
type Endian = 'le' | 'be'

const bytes = new Buff (
  data    : Bytes | Bytes[] | ArrayBuffer,  
  size   ?: number, // Specify the size of the array (for padding)
  endian ?: Endian  // Specify the endianess of the array.
)

```

您可以从许多不同的类型和格式转换为 `Buff` 对象.

```ts
Buff
  .any     = (data : any, size ?: number)        => Buff,
  .raw     = (data : Uint8Array, size ?: number) => Buff,
  .str     = (data : string, size ?: number)     => Buff,
  .hex     = (data : string, size ?: number)     => Buff,
  .bin     = (data : string, size ?: number)     => Buff,
  .num     = (data : number, size ?: number)     => Buff,
  .big     = (data : bigint, size ?: number)     => Buff,
  .bytes   = (data : Bytes,  size ?: number)     => Buff,
  .json    = (data : T,  replacer ?: Replacer)   => Buff,
  .b58chk  = (data : string)                     => Buff,
  .base64  = (data : string)                     => Buff,
  .b64url  = (data : string)                     => Buff,

  .bech32  = (
    data        : string,  // Data to be encoded
    limit      ?: number,  // Enforce a character limit.
    chk_prefix ?: string   // Enforce a certain prefix.
  ) => Buff,

  .bech32m = (
    data        : string,  // Data to be encoded
    limit      ?: number,  // Enforce a character limit.
    chk_prefix ?: string   // Enforce a certain prefix.
  ) => Buff
}
```

与 `Buff`,您可以访问广泛的API,用于格式之间的转换.

```ts
const bytes = new Buff(data)

/* Quicky convert into many formats using getters. */

bytes
  .arr     => number[]    // Convert to a number array.
  .num     => number      // Convert to a Number.
  .big     => bigint      // Convert to a BigInt.
  .str     => string      // Convert to a UTF8 string.
  .hex     => string      // Convert to a hex string.
  .raw     => Uint8Array  // Convert to a pure Uint8Array.
  .bin     => string      // Convert to a binary string.
  .b58chk  => string      // Convert to base58 with checksum.
  .base64  => string      // Convert to base64 string.
  .b64url  => string      // Convert to base64url string.
  .digest  => Buff        // Convert to a sha256 digest.
  .id      => string      // Convert to a digest (hex string).
  .stream  => Stream      // Convert to a Stream object.

/* There are a few export methods that support extra params. */

bytes
  .toNum     : (endian ?: Endian)                  => number
  .toBig     : (endian ?: Endian)                  => bigint
  .toBin     : ()                                  => string
  .toHash    : ()                                  => Buff
  .toJson    : (reviver ?: Reviver)                => T
  .toBech32  : (prefix : string, limit ?: number)  => string
  .toBech32m : (prefix : string, limit ?: number)  => string
```

除了格式转换之外,还可以执行许多其他方便的任务.

```ts
Buff = {
  // Standard UTF-8 string-to-bytes encoding.
  encode : (str : string) => Uint8Array,
  // Standard UTF-8 bytes-to-string decoding.
  decode : (bytes : Uint8Array) => string,
  // Same as Uint8Array.from(), but returns a Buff object.
  from (data : Uint8Array | number[]) => Buff
  // Same as Uint8Array.of(), but returns a Buff object.
  of (...data : number[]) => Buff,
  // Join together multiple arrays of bytes.
  join : (array : Bytes[]) => Buff,
  // Sort multiple arrays of bytes in lexicographic order.
  sort (arr : Bytes[], size ?: number) => Buff[],
  // Return a buffer object with random data (uses webcrypto).
  random (size : number) => Buff,
  // Converts a number into a 'varint' for byte streams.
  varInt : (num : number, endian ?: Endian) => Buff
}

const bytes = new Buff(data)

bytes
  // Append data to your ubber object
  .append (data : Bytes) => Buff
  // Prepend data to your buffer object.
  .prepend (data : Bytes) => Buff
  // Encode the size of your buffer as a varint and prepend it.
  .prefixSize (endian ?: Endian) => Buff
  // Same as Uint8Array.reverse(), but returns a Buff object.
  .reverse () => Buff
  // Identical to Uint8Array.set() method.
  .set (array : ArrayLike<number>, offset ?: number) => void
  // Same as Uint8Array.slice(), but returns a Buff object.
  .slice (start ?: number, end ?: number) => Buff
  // Same as Uint8Array.subarray(), but returns a Buff object.
  .subarray (begin ?: number, end ?: number) => Buff
  // Same as Uint8Array.set(), but allows Bytes as input.
  .write (data : Bytes, offset ?: number) => void
```

这个 `Stream` 工具将取出一连串的数据,并允许您用字节表示.

```ts
import { Stream } from '@cmdcode/buff'

// Convert data into a stream object.
const stream = new Stream(data)

// You can convert a buff object into a stream.
const stream = new Buff(data).stream

stream
  // Reads x number of bytes, does not consume the stream.
  .peek(size: number) => Buff
  // Reads x number of bytes, consumes the stream.
  .read(size: number) => bytes
  // Reads the next bytes(s) as a varint, returns the number value.
  .readSize (endian ?: Endian) => number
```

## 依赖关系

这个库使用最小的依赖.

**@ scure/ base (英语)**  
用于执行多种格式的编码和解码.  
[https://github.com/paulmiller/scure-base (英语)](https://github.com/paulmillr/scure-base)

**@noble/hashes (英语)** 用于创建散列 `sha256`。 。 。 .  
[https://github.com/paulmiller/noble-hashes 互联网档案馆的存檔,存档日期2013-12-22](https://github.com/paulmillr/noble-hashes)

特别感谢保罗·米勒的出色工作.

## 错误/ 问题

请随意在议题页面上张贴任何问题或错误报告!

## 开发/测试

这个项目使用 `eslint` 财务报告和审定财务报表 `typescript` 促进发展, `tape` 用于单位测试,以及 `rollup` 为捆绑释放.

```bash
yarn test    || npm run test
yarn release || npm run release
```

## 捐款

欢迎所有的贡献!

## 许可证

随便你用这个密码! 没有保证!

## 常见问题

### @cmdcode/buff 是什么？

一支瑞士军刀 字节操纵\.

### 如何安装 @cmdcode/buff？

在项目目录运行 npm install @cmdcode/buff，或使用 pnpm add / yarn add / bun add。

### @cmdcode/buff 支持 TypeScript 吗？

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

### @cmdcode/buff 支持 ESM 还是 CommonJS？

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

### @cmdcode/buff 可以商用吗？

@cmdcode/buff 使用 CC-BY-1\.0 许可。声明的许可为 CC-BY-1\.0，请阅读许可原文确认使用条件。以上为概括，不构成法律意见，请以许可原文为准。

### @cmdcode/buff 的下载量有多少？

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

### @cmdcode/buff 还在维护吗？

最新版本 2\.2\.5 发布于 2024/01/30（2 年前）；至今共发布 12 个版本，其中过去 12 个月发布了 0 个；首次发布于 2023/09/06。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

