Registry / serialization / brotli-wasm

brotli-wasm

JSON →
library3.0.1jsnpmunverified

brotli-wasm is a JavaScript/TypeScript library providing Brotli compression and decompression capabilities across both Node.js (v18 and above) and browser environments, powered by WebAssembly. It wraps the well-regarded Rust Brotli crate (currently v5 as of `brotli-wasm` v3.x), compiling it to WASM for broad compatibility. The current stable version is 3.0.1, with major version updates occurring when significant breaking changes are introduced, such as API overhauls or minimum engine version bumps. A key differentiator is its consistent, battle-tested API that abstracts away the complexities of WASM loading and environment-specific implementations, allowing developers to use a single codebase for compression needs in diverse JavaScript runtimes. It offers both a simple `compress`/`decompress` API for buffers and advanced `CompressStream`/`DecompressStream` classes for streaming data, making it suitable for various use cases from file processing to network communication. It explicitly states no runtime dependencies.

npm install brotli-wasm
INSTALL
IMPORT
SIG · BROTLI-WASM
B
brotli-wasm
serializationjavascriptv3.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default (brotliPromise)
import brotliPromise from 'brotli-wasm';
const brotli = require('brotli-wasm');
The default export is a Promise in browser environments, which must be awaited before use. In Node.js, it can often be used synchronously but awaiting is recommended for consistent cross-environment code.
compress, decompress
const { compress, decompress } = await brotliPromise;
import { compress } from 'brotli-wasm';
The `compress` and `decompress` methods are properties of the resolved module after the `brotliPromise` has settled, not direct named exports from the package.
CompressStream, DecompressStream
import { CompressStream, DecompressStream } from 'brotli-wasm';
const { CompressStream } = brotliPromise;
These classes are named exports available directly from the module for advanced streaming operations. They can be imported directly in ESM contexts.

Demonstrates asynchronous loading and basic Brotli compression/decompression in a browser-compatible environment using `TextEncoder` and `TextDecoder`.

import brotliPromise from 'brotli-wasm'; const runExample = async () => { const brotli = await brotliPromise; // Import is async in browsers due to wasm requirements! const textEncoder = new TextEncoder(); const textDecoder = new TextDecoder(); const input = 'some input data for brotli compression and decompression demonstration. This string is long enough to show a realistic use case.'; const uncompressedData = textEncoder.encode(input); const compressedData = brotli.compress(uncompressedData); const decompressedData = brotli.decompress(compressedData); console.log('Original size:', uncompressedData.byteLength, 'bytes'); console.log('Compressed size:', compressedData.byteLength, 'bytes'); console.log('Decompressed data:', textDecoder.decode(decompressedData)); }; runExample();
Debug
Known issues
breakingVersion 3.0.0 updated the minimum supported Node.js engine to v18. Older Node.js versions are no longer supported.
fix
Upgrade your Node.js environment to v18.0.0 or higher.
affects: >=3.0.0
breakingVersion 3.0.0 updated the underlying Rust Brotli crate from v3.3 to v5. While no API changes are expected, subtle behavioral differences in compression or decompression might occur (YMMV).
fix
Thoroughly test existing Brotli compressed data and new compression results after upgrading to v3.0.0 to ensure compatibility and expected behavior.
affects: >=3.0.0
breakingIn version 2.0.0, the Streams API (`CompressStream`, `DecompressStream`) underwent significant changes. Methods like `result()` and `last_input_offset()` were removed. The `compress()` and `decompress()` methods now return a result object containing `buf`, `code`, and `in` properties, instead of just a buffer.
fix
Refactor code using `CompressStream` and `DecompressStream` to retrieve output data and status from the result object returned by `compress()`/`decompress()` methods, rather than querying stream state directly.
affects: >=2.0.0 <3.0.0
gotchaWhen importing `brotli-wasm` in a browser environment or using a bundler, the default export is a Promise that must be awaited before the `compress` and `decompress` functions become available.
fix
Always use `await brotliPromise` or `await import('brotli-wasm').then(m => m.default)` to ensure the WebAssembly module is loaded and initialized before attempting to use its functions.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: brotli.compress is not a function
Attempting to call `compress` or `decompress` on the `brotli-wasm` default export (which is a Promise in some environments) before it has resolved.
fix
Ensure the default export is awaited: `const brotli = await brotliPromise;` or `const brotli = await import('brotli-wasm').then(m => m.default);`
ReferenceError: Buffer is not defined
Using `Buffer.from()` or other Node.js `Buffer` APIs directly in a browser environment without a polyfill.
fix
In browser environments, use `TextEncoder` and `TextDecoder` for converting strings to/from `Uint8Array`, or include a `Buffer` polyfill like `browserify-zlib` if `Buffer` is strictly required.
require() of ES Module ... is not supported.
Trying to use CommonJS `require()` syntax to import `brotli-wasm` in an environment configured for pure ES Modules, or expecting synchronous `require` behavior in a browser context where only async ESM is supported for WASM.
fix
Use ES Module `import brotliPromise from 'brotli-wasm';` and ensure your bundler (Webpack, Vite, Rollup) or Node.js environment is configured to handle ES Modules and WebAssembly correctly.
Upgrade
Version history
3.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
21 hits · last 30 days
node
16
OpenAI (training)
1
Resources
brotli-wasm — npm install brotli-wasm · libregistry