@bokuweb/zstd-wasm

中文文档
0.0.27发布于 2025/02/17MITTypeScriptCommonJS零依赖近 30 天下载 73.8万
原始使用文档
文档目录18 节
  1. installation
  2. examples
  3. Node.js
  4. Deno
  5. without bundlers
  6. with vite
  7. with webpack4
  8. with webpack5
  9. Using dictionary
  10. API
  11. async init(path?: string): Promise
  12. compress(buffer: Uint8Array, compressionLevel?: number): Ui…
  13. decompress(buffer: Uint8Array): Uint8Array
  14. compressUsingDict(cctx: number, buffer: Uint8Array, dict: U…
  15. createCCtx(): number
  16. decompressUsingDict(dctx: number, dict: Uint8Array): Uint8A…
  17. createDCtx(): number
  18. License

zstd-wasm

Build Status Build Status

Zstandard for browser, Node.js and Deno. For now support only simple API.

installation

npm install @bokuweb/zstd-wasm

examples

Node.js

Playground: 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!!
})();

Deno

See also, 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));

without bundlers

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!!
})();

with vite

Please set following config in vite.config.js.

import { defineConfig } from 'vite';

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

with webpack4

We need to use file-loader with webpack4. This is because, webpack doesn’t work well with wasm modules created with Emscripten. See, https://github.com/webpack/webpack/issues/7352

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!!
})();

In this case, please install file-loader by yourself.

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

with webpack5

We need to use Asset Modules with webpack5. This is because, webpack doesn’t work well with wasm modules created with Emscripten. See, https://github.com/webpack/webpack/issues/7352

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!!
})();
  • webpack.config.js
{
  // ...snipped
  module: {
    rules: [
      {
        test: /zstd\.wasm/,
        type: 'asset/resource',
      },
    ],
  },
  // ...snipped
}

Using dictionary

  1. Create the dictionary

zstd --train FullPathToTrainingSet/* -o dictionaryName

  1. Compress with dictionary
const dict = readFileSync('./dict');
const compressed = compressUsingDict(createCCtx(), buffer, dict, 10);
  1. Decompress with dictionary
const dict = readFileSync('./dict');
const decompressed = decompressUsingDict(createDCtx(), buffer, dict);

See also example

API

async init(path?: string): Promise

Initialize module. Please specify path to zstd.wasm without bunders on browser.

compress(buffer: Uint8Array, compressionLevel?: number): Uint8Array

  • buffer: data to compress.
  • compressionLevel: (optional) compression level, default value is 3

Example:

const compressed = compress(buffer, 10);

decompress(buffer: Uint8Array): Uint8Array

  • buffer: data to decompress.

Example:

const decompressed = decompress(buffer);

compressUsingDict(cctx: number, buffer: Uint8Array, dict: Uint8Array, compressionLevel?: number): Uint8Array

  • cctx: a pointer to compress context. please create cctx with createCCtx.
  • buffer: data to compress.
  • dict: dictionary data.
  • compressionLevel: (optional) compression level, default value is 3

Example:

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

createCCtx(): number

  • create a pointer to compress context.

decompressUsingDict(dctx: number, dict: Uint8Array): Uint8Array

  • dctx: a pointer to decompress context. please create cctx with createDCtx.
  • buffer: data to decompress.
  • dict: dictionary data.

Example:

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

createDCtx(): number

  • create a pointer to decompress context.

License

TypeScript glue is provided under the MIT License, and the zstd is provided by Facebook under the BSD 3-Clause License.

版本与发布记录

@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="Build Status"

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