@bokuweb/zstd-wasm

中文文档
0.0.27发布于 2025/02/17MITTypeScriptCommonJS零依赖近 30 天下载 73.8万
中文使用文档
文档目录18 节
  1. 安装
  2. 实例
  3. 节点.js
  4. 德诺
  5. 没有捆绑器
  6. 与 vite
  7. 有网络包4
  8. 带有网络包装5
  9. 使用词典
  10. API 密码
  11. async init( path: string): 保证
  12. 压缩( 缓冲: Uint8Array, 压缩级? 数): Uint8Array
  13. 解压( 缓冲: Uint8Array): Uint8Array
  14. 压缩 UsingDict( cctx: 数字, 缓冲器: Uint8Array, dict: Uint8Array, …
  15. 创建CCtx (): 编号
  16. 已解压UsingDict(dctx:数字;dict:Uint8Array):Uint8Array
  17. 创建 DCtx (): 编号
  18. 许可证

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

Build Status Build Status

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

安装

npm install @bokuweb/zstd-wasm

实例

节点.js

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

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 页面存档备份,存于互联网档案馆

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

没有捆绑器

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。 。 。 .

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

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
{
  // ...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

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
{
  // ...snipped
  module: {
    rules: [
      {
        test: /zstd\.wasm/,
        type: 'asset/resource',
      },
    ],
  },
  // ...snipped
}

使用词典

  1. 创建词典

zstd --train FullPathToTrainingSet/* -o dictionaryName

  1. 用字典压缩
const dict = readFileSync('./dict');
const compressed = compressUsingDict(createCCtx(), buffer, dict, 10);
  1. 用字典解压
const dict = readFileSync('./dict');
const decompressed = decompressUsingDict(createDCtx(), buffer, dict);

另见 实例

API 密码

async init( path: string): 保证

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

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

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

示例

const compressed = compress(buffer, 10);

解压( 缓冲: Uint8Array): Uint8Array

  • 缓冲:要解压的数据.

示例

const decompressed = decompress(buffer);

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

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

示例

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

创建CCtx (): 编号

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

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

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

示例

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

创建 DCtx (): 编号

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

许可证

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

版本与发布记录

@bokuweb/zstd-wasm 共发布 32 个版本,首次发布于 2021/07/09;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.27latest2025/02/171.7 年前
0.0.262025/02/041.8 年前
0.0.252025/02/041.8 年前
0.0.242025/02/041.8 年前
0.0.232025/02/041.8 年前
0.0.222024/12/131.9 年前

关于 @bokuweb/zstd-wasm 的常见问题

@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 和提交记录判断。