npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tiny-brotli-dec-wasm

v1.0.1

Published

A tiny Brotli streaming decoder.

Downloads

7

Readme

tiny-brotli-dec-wasm

A tiny Brotli streaming decoder in 70.6 KiB of gzipped files with no runtime dependencies.

Quick start

Install the library

npm install tiny-brotli-dec-wasm

Use the decoder to decompress Romeo and Juliet

import BrotliDecompressStream, { Result } from "tiny-brotli-dec-wasm"

// Do the initial asynchronous initialization.
// After the async initialization the decoder can be created synchronously.
await BrotliDecompressStream.init();
const brotli = BrotliDecompressStream.create();

// Fetch the data
const url = "http://github.com/jprendes/tiny-brotli-dec-wasm/raw/main/demo/romeo_juliet.txt.br";
const response = await fetch(url);

// Collect output as text
const text = [];
const decoder = new TextDecoder();

for await (let chunk of response.body) {
    while (true) {
        // Set max output buffer size as 1024
        const output = brotli.dec(chunk, 1024);
        const result = brotli.result();
        
        // Remove from the buffer all the input that has already been consumed
        chunk = chunk.subarray(brotli.lastInputOffset());

        // Do something with the output
        text.push(decoder.decode(output, { stream: true }));

        if (result === Result.NeedsMoreInput) {
            // The whole `chunk` has been consumed and should be empty.
            // Wait for a new chunk.
            break;
        } else if (result === Result.NeedsMoreOutput) {
            // The `chunk` may or may nor be entirely consumed, but the
            // decoder can still produce more output.
            // Keep decoding.
            continue;
        } if (result === Result.Success) {
            // We are done!
            break;
        } else if (resul === Result.Error) {
            // Something went wrong
            throw new Error("Error decompressing file");
        }
    }
}

// Release the decoder
brotli.free();

// Print the decompressed output
console.log(text.join(""));

Alternatives

This library was inspired and borrows ideas from

  • brotli-dec-wasm: Similar library using Brotli's Rust binding and wasm-pack. Provides a function for one-shot decompressing. Generates larger binary files.
  • cedric-h/brotli: Proof of concept building the Brotli compressor with a minimal C runtime. Provides no decompression and no JS library.

Development

Requirements

To build this project you will need:

  • clang >=15
  • cmake >=3.22

Optionally, you will also need:

  • zopfli or gzip to compress the output files. Using zopfli results in smaller files.
  • node >=18, or deno >=1.31, to run the demo.

In all cases, things might also work with earlier versions, but it's not tested.

Build

The build is driven by cmake:

cmake -DCMAKE_TOOLCHAIN_FILE=wasm-toolchain.cmake -G Ninja -S . -B build
cmake --build build -- tiny-brotli-dec-wasm

To compress the output files run

zopfli --gzip build/tiny-brotli-dec-wasm.*
# gzip --keep -9 build/tiny-brotli-dec-wasm.*

Run demo

The demo decompresses Romeo and Juliet 1000 times, and prints the run time per iteration. Passing the -p or --print argument does an extra run and prints the result to stdout.

To run the demo run

node demo/demo.mjs -p
# deno run --allow-read demo/demo.mjs -p