# @bokuweb/zstd-wasm 中文文档

规范地址：https://npmcn.com/package/%40bokuweb/zstd-wasm/

版本：0\.0\.27
npm 元数据更新时间：2025-02-17T01:41:30.812Z
中文译文更新时间：2026-11-08T10:17:33.357Z
软件包许可：MIT

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

原始 npm 页面：https://www.npmjs.com/package/%40bokuweb/zstd-wasm
原文对照：https://npmcn.com/package/%40bokuweb/zstd-wasm/?lang=original
项目主页：https://github.com/bokuweb/zstd-wasm#readme
源码仓库：https://github.com/bokuweb/zstd-wasm

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

- 包体积（解压后）：904 kB，83 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2021-07-09T15:36:56.747Z
- 最新版本发布：2025-02-17T01:41:30.619Z
- 近 30 天下载量：737826（2026-10-09 至 2026-11-07，npm 官方下载统计）
- 已发布版本数：32（近 12 个月 0 个）

## 中文简介

![Build Status](https://github.com/bokuweb/zstd-wasm/workflows/Continuous%20Integration/badge.svg) [<img src='https://img.shields.io/npm/v/@bokuweb/zstd-wasm.svg' alt="建设状态"](https://www.npmjs.com/package/@bokuweb/zstd-wasm)

## 安装

```sh
npm install @bokuweb/zstd-wasm
```

也可以使用：`pnpm add @bokuweb/zstd-wasm`、`yarn add @bokuweb/zstd-wasm`、`bun add @bokuweb/zstd-wasm`

## README

# {\\fn黑体\\fs22\\bord1\\shad0\\3ahbe\\4ah00\\fscx67\\fscy66\\2chffffff\\3ch808080}你这混蛋

![Build Status](https://github.com/bokuweb/zstd-wasm/workflows/Continuous%20Integration/badge.svg) [![Build Status](https://img.shields.io/npm/v/@bokuweb/zstd-wasm.svg)](https://www.npmjs.com/package/@bokuweb/zstd-wasm) [![](https://img.shields.io/npm/dm/@bokuweb/zstd-wasm.svg)](https://www.npmjs.com/package/@bokuweb/zstd-wasm)

Zstandard用于浏览器,Node.js和Deno. 现在只支持简单的API.

## 安装

```
npm install @bokuweb/zstd-wasm
```

## 实例

### 节点.js

播放场 : [https://runkit.com/bokuweb/60f1172a58aae5001aea2418 (中文(简体) )](https://runkit.com/bokuweb/60f1172a58aae5001aea2418)

```js
const { init, compress, decompress } = require('@bokuweb/zstd-wasm');

(async () => {
  await init();
  const compressed = compress(Buffer.from('Hello zstd!!'), 10);
  const res = decompress(compressed);
  Buffer.from(res).toString(); // Hello zstd!!
})();
```

### 德诺

另见:, [https://github.com/bokuweb/zstd-wasm/tree/master/examples/deno 页面存档备份,存于互联网档案馆](https://github.com/bokuweb/zstd-wasm/tree/master/examples/deno)

```ts
import { init, decompress, compress } from 'https://deno.land/x/zstd_wasm/deno/zstd.ts';

await init();
const encoder = new TextEncoder();
const buffer = encoder.encode('Hello World');
const compressed = compress(buffer, 10);
const decompressed = decompress(compressed);
const decoder = new TextDecoder();
console.log(decoder.decode(decompressed));
```

### 没有捆绑器

```js
import { init, compress, decompress } from '@bokuweb/zstd-wasm';

(async () => {
  await init('./zstd.wasm'); // Please deploy `node_modules/@bokuweb/zstd-wasm/lib/wasm/zstd.wasm` to your hosting server.
  const compressed = compress(Buffer.from('Hello zstd!!'), 10);
  const res = decompress(compressed);
  Buffer.from(res).toString(); // Hello zstd!!
})();
```

* * *

### 与 `vite`

请在配置后设置 `vite.config.js`。 。 。 .

```js
import { defineConfig } from 'vite';

export default defineConfig({
  optimizeDeps: {
    exclude: ['@bokuweb/zstd-wasm'],
    esbuildOptions: {
      target: 'es2020',
    },
  },
});
```

* * *

### 有网络包4

我们需要使用 `file-loader` 与网络包装4. 因为网络包装与Emscripten创建的原子弹模块效果不佳. 看到没, [https://github.com/webpack/webpack/issues/7352 互联网档案馆的存檔,存档日期2013-12-02](https://github.com/webpack/webpack/issues/7352)

```js
import { init, compress, decompress } from '@bokuweb/zstd-wasm';

(async () => {
  await init();
  const compressed = compress(Buffer.from('Hello zstd!!'), 10);
  const res = decompress(compressed);
  Buffer.from(res).toString(); // Hello zstd!!
})();
```

在这种情况下,请安装 `file-loader` 一个人.

-   网络包装. config.js

```js
{
  // ...snipped
  module: {
    rules: [
      {
        test: /zstd\.wasm$/,
        type: 'javascript/auto',
        loader: 'file-loader', // Please use file loader
      },
    ],
  },
}
```

* * *

### 带有网络包装5

我们需要使用 `Asset Modules` 和网络包装5. 因为网络包装与Emscripten创建的原子弹模块效果不佳. 看到没, [https://github.com/webpack/webpack/issues/7352 互联网档案馆的存檔,存档日期2013-12-02](https://github.com/webpack/webpack/issues/7352)

```js
import { init, compress, decompress } from '@bokuweb/zstd-wasm';

(async () => {
  await init();
  const compressed = compress(Buffer.from('Hello zstd!!'), 10);
  const res = decompress(compressed);
  Buffer.from(res).toString(); // Hello zstd!!
})();
```

-   网络包装. config.js

```js
{
  // ...snipped
  module: {
    rules: [
      {
        test: /zstd\.wasm/,
        type: 'asset/resource',
      },
    ],
  },
  // ...snipped
}
```

## 使用词典

1.  创建词典

`zstd --train FullPathToTrainingSet/* -o dictionaryName`

2.  用字典压缩

```typescript
const dict = readFileSync('./dict');
const compressed = compressUsingDict(createCCtx(), buffer, dict, 10);
```

3.  用字典解压

```typescript
const dict = readFileSync('./dict');
const decompressed = decompressUsingDict(createDCtx(), buffer, dict);
```

另见 [实例](https://npmcn.com/package/%40bokuweb/zstd-wasm/test/compress_using_dict.test.ts)

## API 密码

### async init( path: string): 保证

初始化模块 . 请指定路径 `zstd.wasm` 浏览器上没有bunders.

### 压缩( 缓冲: Uint8Array, 压缩级? 数): Uint8Array

-   缓冲:要压缩的数据.
-   压缩级别:(可选)压缩级别,默认值为3

**示例**

```typescript
const compressed = compress(buffer, 10);
```

### 解压( 缓冲: Uint8Array): Uint8Array

-   缓冲:要解压的数据.

**示例**

```typescript
const decompressed = decompress(buffer);
```

### 压缩 UsingDict( cctx: 数字, 缓冲器: Uint8Array, dict: Uint8Array, 压缩级? 数字): Uint8Array

-   cctx:用于压缩上下文的指针. 请创建 cctx 与 `createCCtx`。 。 。 .
-   缓冲:要压缩的数据.
-   dict:字典数据.
-   压缩级别:(可选)压缩级别,默认值为3

**示例**

```typescript
const dict = readFileSync('./dict');
const compressed = compressUsingDict(createCCtx(), buffer, dict, 10);
```

### 创建CCtx (): 编号

-   创建用于压缩上下文的指针.

### 已解压UsingDict(dctx:数字;dict:Uint8Array):Uint8Array

-   dctx:用来解压上下文的指针. 请创建 cctx 与 `createDCtx`。 。 。 .
-   缓冲:要解压的数据.
-   dict:字典数据.

**示例**

```typescript
const dict = readFileSync('./dict');
const decompressed = decompressUsingDict(createDCtx(), buffer, dict);
```

### 创建 DCtx (): 编号

-   创建解压上下文的指针.

## 许可证

TypeScript胶由麻省理工学院许可提供,zstd由Facebook根据BSD 3-Clause许可提供.

## 常见问题

### @bokuweb/zstd-wasm 是什么？

\<img src='https://img\.shields\.io/npm/v/@bokuweb/zstd-wasm\.svg' alt="建设状态"

### 如何安装 @bokuweb/zstd-wasm？

在项目目录运行 npm install @bokuweb/zstd-wasm，或使用 pnpm add / yarn add / bun add。

### @bokuweb/zstd-wasm 支持 TypeScript 吗？

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

### @bokuweb/zstd-wasm 支持 ESM 还是 CommonJS？

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

### @bokuweb/zstd-wasm 可以商用吗？

@bokuweb/zstd-wasm 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @bokuweb/zstd-wasm 的下载量有多少？

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

### @bokuweb/zstd-wasm 还在维护吗？

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

