brotli-wasm-2
v0.0.3
Published
JavaScript bindings for Brotli compression compiled to WebAssembly
Maintainers
Readme
brotli-wasm-2
Documentation is available at this URL: https://npmx.dev/package-docs/brotli-wasm-2.
Installation
npm install brotli-wasm-2 # npm
yarn add brotli-wasm-2 # yarn
pnpm add brotli-wasm-2 # pnpmUsage
Node.js or Browser
import { brotliCompress, brotliDecompress } from "brotli-wasm-2";
brotliCompress(new TextEncoder().encode("Hello world!"));
brotliCompress(new TextEncoder().encode("Hello world!"), {
quality: 2,
mode: "text",
});
brotliDecompress(brotliCompress(new TextEncoder().encode("Hello world!")));This requires Node.js v24.0.5 or above (or Node.js v24.0.0 with flag --js-source-phase-imports) or Deno version v2.6 or higher. While Bun does not support source phase imports, it should work still since it supports fetching file URLs. In Webpack, make sure experiments.sourceImport is set to true. This is also supported on Cloudflare's workerd and Rollup.
WebAssembly ESM Integration
WebAssembly ESM integration is a a Phase 3 proposal that allows importing WebAsssembly files directly as ES modules. It is supported as of Node.js v24.0.5 (v24.0.0 with flag --experimental-wasm-modules) and Deno 2.1. It is supported in Webpack or Rspack by enabling experiments.asyncWebAssembly, in Rolldown via rolldown-plugin-wasm, and in Vite natively since v8.1.0. It can also be polyfilled via es-module-shims.
This may be preferred in some cases over the package entrypoint since V8 can cache WebAssembly code instantiated via streaming.
import { brotliCompress, brotliDecompress } from "brotli-wasm-2/brotli.wasm";
brotliCompress(new TextEncoder().encode("Hello world!"));
brotliCompress(new TextEncoder().encode("Hello world!"), {
quality: 2,
mode: "text",
});
brotliDecompress(brotliCompress(new TextEncoder().encode("Hello world!")));Manual instantiation fallback
The previous syntaxes should work on most runtimes and some bundlers (e.g. Rollup); however, if you want to instantiate it manually and use the exports directly from the WebAssembly instance, you can do so like this:
Vite
import init from "brotli-wasm-2/brotli.wasm?init";
import { getImports } from "brotli-wasm-2/imports";
const instance = await init(getImports());
const { brotliCompress, brotliDecompress } =
instance.exports as typeof import("brotli-wasm-2");Remote URL
import { getImports } from "brotli-wasm-2/imports";
const instance = await WebAssembly.instantiateStreaming(
fetch("https://esm.sh/brotli-wasm-2@%5E0.0.1/brotli.wasm"),
getImports(),
);
const { brotliCompress, brotliDecompress } =
instance.exports as typeof import("brotli-wasm-2");Unwasm, Vercel Edge
import mod from "brotli-wasm-2/brotli.wasm?module";
import { getImports } from "brotli-wasm-2/imports";
const instance = WebAssembly.instantiate(mod, getImports());
const { brotliCompress } = instance.exports as typeof import("brotli-wasm-2");License
This project is licensed under the MIT license. See the LICENSE file for details.
