debrotli
v0.1.0
Published
Blazing fast brotli decompressor powered by WebAssembly.
Maintainers
Readme
@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/brotlinpx jsr add @nick/brotlibunx jsr add @nick/brotlipnpm dlx jsr add @nick/brotliyarn dlx jsr add @nick/brotliUsage
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
decompressfunction 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
BufferAPI, 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-supportedUint8Arraytype.
[^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.
