Lightweight brotli decompressor, powered by WebAssembly.
Supported by Deno, Bun, Node, modern browsers, and more.
This module provides a blazing-fast brotli decompressor, written in Rust and compiled to WebAssembly. It's compatibile with virtually any runtime supportive of WebAssembly, including Deno, Bun, Node, Cloudflare Workers, and all recent releases of modern browsers (Chrome, Firefox, Safari, Edge, etc.).
deno add jsr:@nick/brotli
npx jsr add @nick/brotli
bunx jsr add @nick/brotli
pnpm dlx jsr add @nick/brotli
yarn dlx jsr add @nick/brotli
import { decompress } from "@nick/brotli";
const res = await fetch("file:///compressed.txt.br");
const small = await res.bytes();
const large = decompress(small); // <- synchronous
console.log(`Decompressed ${small.length}B -> ${large.length}B`);
Important
The decompress
function is pre-initialized and ready for immediate use.
Under the hood on the Rust side of the codebase, this is a thin wrapper of the third-party brotli-decompressor crate, adding WebAssembly bindings and some graceful error handling1.
On the JavaScript side of the codebase, it adds support for several additional
input types, allowing you to decompress data from a string, ArrayBuffer
, or
any ArrayBufferView
object, including DataView
and all typed array types.
This results in a very similar signature to that of brotliDecompressSync
from the native node:zlib
module, giving you the ability to use this module as
an almost-drop-in replacement for it in most situations.
Note
This tool does not use the Node Buffer
API, which is from a time when the
JavaScript ecosystem didn't have a standardized way to represent binary data.
Instead we use its superclass - the universally-supported Uint8Array
type.
Tip
Shameless Plug: If you're looking for a non-WebAssembly Brotli decompressor, I've also published brocha, a 100% JS decompressor that weighs in at under 200KB. While it's not quite as fast as WASM, it benchmarks extremely well in comparison to other JS-based solutions.
Footnotes
-
The error handling is a bit more graceful than the original crate, but it might not be what you're looking for: in the case of erroneous input data (or data that is not compressed with Brotli), the
decompress
function will simply return the same input data (but always as aUint8Array
). ↩