@foxglove/wasm-lz4

中文文档
1.0.2发布于 2023/02/01MITTypeScriptCommonJS零依赖近 30 天下载 23万

https://github.com/lz4/lz4 compiled to WebAssembly. For now only decompression is supported. PRs welcome!

原始使用文档
文档目录8 节
  1. API
  2. Using the module in a browser
  3. Usage with Webpack
  4. Asynchronous loading & compiling
  5. Developing locally
  6. License
  7. Releasing
  8. Stay in touch

@foxglove/wasm-lz4

https://github.com/lz4/lz4 compiled to WebAssembly. For now only decompression is supported. PRs welcome!

API

wasm-lz4 exports a single function:

export default (buffer: Buffer, destinationByteSize: number) => Buffer;

Here is an example of using the module:

import fs from "fs";
import decompress from "wasm-lz4";

const compressedBytes = fs.readFileSync("compressed.lz4");

// currently you need to know the exact expected size of the output buffer
// so the wasm runtime can allocate enough bytes to decompress into
// TODO: this should not be necessary...it's exposed in the lz4 C code
const outputBytes = compressedBytes * 10;
const decompressedBytes = decompress(compressedBytes, outputBytes);

Using the module in a browser

Emscripten compiled WebAssembly modules are built in 2 parts: a .js side and a .wasm side. In the browser the .js side needs to download the .wasm side from the server so it can compile it. There is more information available in the emscripten documentation.

Usage with Webpack

We require the wasm-lz4.wasm module in our locateFile definition, so that module bundling with dynamic paths is possible. So if you are using Webpack, you can add the following entry:

...
rules : [
  ...
  {
    test: /\.wasm$/,
    type: "javascript/auto",
    use: {
      loader: "file-loader",
      options: {
        name: "[name]-[hash].[ext]",
      },
    },
  },
  ...
]
...

The javascript/auto type setting tells Webpack to bypass its default importing logic, and just import the file as-is. Hopefully this hack will go away with improved WebAssembly support in webpack.

Asynchronous loading & compiling

After the .wasm binary is loaded (via fetch in the browser or require in node) it must be compiled by the WebAssembly runtime. If you call decompress before it is finished compiling it will throw an error indicating it isn't ready yet. To wait for the module to be loaded you can do something like the following:

import decompress from 'wasm-lz4'

async function doWork() {
  await decompress.isLoaded;
  decompress(buffer, outputByteLength);
}

Developing locally

  1. Run npm install to install dependencies.
  2. Run npm run build to invoke emcc inside a Docker container and compile the code in wasm-lz4.c as well as the required lz4 source files. The output will be in dist/ on the host machine.
  3. Run npm test to run the tests.

License

@foxglove/wasm-lz4 is licensed under MIT License.

Releasing

  1. Run yarn version --[major|minor|patch] to bump version
  2. Run git push && git push --tags to push new tag
  3. GitHub Actions will take care of the rest

Stay in touch

Join our Slack channel to ask questions, share feedback, and stay up to date on what our team is working on.

版本与发布记录

@foxglove/wasm-lz4 共发布 1 个版本,首次发布于 2023/02/01;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.2latest2023/02/013 年前

关于 @foxglove/wasm-lz4 的常见问题

@foxglove/wasm-lz4 是什么?

https://github.com/lz4/lz4 compiled to WebAssembly. For now only decompression is supported. PRs welcome!

如何安装 @foxglove/wasm-lz4?

在项目目录运行 npm install @foxglove/wasm-lz4,或使用 pnpm add / yarn add / bun add。

@foxglove/wasm-lz4 支持 TypeScript 吗?

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

@foxglove/wasm-lz4 支持 ESM 还是 CommonJS?

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

@foxglove/wasm-lz4 可以商用吗?

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

@foxglove/wasm-lz4 的下载量有多少?

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

@foxglove/wasm-lz4 还在维护吗?

最新版本 1.0.2 发布于 2023/02/01(3 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/02/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。