Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Lightweight, blazing-fast, and dead-simple brotli decompressor powered by WebAssembly.
@nick/brotli
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.).
Install
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
Usage
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`);
The decompress function is pre-initialized and ready for immediate use.
Overview
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 handling[^1].
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.
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.
[^1]: 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 a Uint8Array).
Prior Art
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.
Add Package
deno add jsr:@nick/brotli
Import symbol
import * as brotli from "@nick/brotli";
Import directly with a jsr specifier
import * as brotli from "jsr:@nick/brotli";
Add Package
pnpm i jsr:@nick/brotli
pnpm dlx jsr add @nick/brotli
Import symbol
import * as brotli from "@nick/brotli";
Add Package
yarn add jsr:@nick/brotli
yarn dlx jsr add @nick/brotli
Import symbol
import * as brotli from "@nick/brotli";
Add Package
vlt install jsr:@nick/brotli
Import symbol
import * as brotli from "@nick/brotli";
Add Package
npx jsr add @nick/brotli
Import symbol
import * as brotli from "@nick/brotli";
Add Package
bunx jsr add @nick/brotli
Import symbol
import * as brotli from "@nick/brotli";