Skip to main content
Home

Built and signed on GitHub Actions

Lightweight, blazing-fast, and dead-simple brotli decompressor powered by WebAssembly.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
8 months ago (0.1.0)

@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`);
Important

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.

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.

[^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

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.


MIT © Nicholas Berlette. All rights reserved.

GitHubIssuesJSRNPMbrocha


JSR JSR NPM

Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@nick/brotli

Import symbol

import * as brotli from "@nick/brotli";
or

Import directly with a jsr specifier

import * as brotli from "jsr:@nick/brotli";

Add Package

pnpm i jsr:@nick/brotli
or (using pnpm 10.8 or older)
pnpm dlx jsr add @nick/brotli

Import symbol

import * as brotli from "@nick/brotli";

Add Package

yarn add jsr:@nick/brotli
or (using Yarn 4.8 or older)
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";