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 🙏

© 2026 – Pkg Stats / Ryan Hefner

streaming-bz2-decompress

v3.0.1

Published

Decompress a bzip2 stream in the browser - no Node.js required.

Readme

Streaming BZ2 Decompress

Overview

Decompress a bzip2 stream in the browser - no Node.js required.

Table of Contents

Installation

npm i streaming-bz2-decompress

Usage

The library exposes a single function decompressStream which takes care of the decompression process. It receives the callbacks onDecompressed and onError in order to pass decompressed data back to the consumer. It returns the actions dataFinished, addData and cancel in order to receive input from the consumer.

Use addData to pass compressed data. The current implementation does best-effort streaming by decompressing complete bzip2 blocks in Web Workers while more input is still arriving. Once finished passing compressed data, use dataFinished to flush the final block and completion state. The final onDecompressed call has done = true.

addData and dataFinished return promises. Await them when chunk ordering or completion ordering matters.

API

decompressStream

The only function exposed by this library is decompressStream.

export type Awaitable<T> = T | Promise<T>;

export interface DecompressStreamCallbacks {
  onDecompressed: (id: number, data: Uint8Array, done: boolean) => Awaitable<void>;
  onError: (id: number, e: string) => Awaitable<void>;
  // Internal compressed input scan/dispatch window. Default: 500 KB.
  streamingChunkSize?: number;
  // Maximum parallel workers. Pass 0 to disable worker streaming.
  workerCount?: number;
}

export interface DecompressStreamActions {
  dataFinished: () => Promise<void>;
  addData: (data: Uint8Array) => Promise<void>;
  cancel: () => void;
}

declare function decompressStream(callbacks: DecompressStreamCallbacks): DecompressStreamActions;

Example

Here is a basic example to demonstrate the usage:

let totalReceived = 0;

const decompressionActions = decompressStream({
  onDecompressed: async (id, data, done) => {
    totalReceived += data.byteLength;
    console.log('Received data for task ' + id + ': ' + data.byteLength);

    if (done) {
      console.log('Task ' + id + ' done. Received: ' + totalReceived);
    }
  },
  onError: (id, e) => {
    console.error('Error for task ' + id + ': ' + e);
  }
});

const response = await fetch('http://path/to/file.bz2');

if (!response.ok || !response.body) {
  throw Error(`Failed to fetch with status: ${response.statusText || 'unknown'}`);
}

const reader = response.body.getReader();

while (true) {
  const readRes = await reader.read();
  const { done, value } = readRes;

  if (done) {
    await decompressionActions.dataFinished();
    break;
  }

  if (!value) {
    throw Error('No value');
  }

  await decompressionActions.addData(value);
}

// Received data for task 0: 898440
// Received data for task 0: 898818
// Task 0 done. Received: 52846954

Migrating

From v2 to v3

Version 3 keeps the v2 async API, but changes the decompression engine. Browser builds now use WebAssembly plus generated Blob workers for the fast path. Most consumers do not need code changes if they already await addData() and dataFinished().

Check these runtime requirements:

  • WebAssembly must be available.
  • Blob workers must be allowed by CSP. If they are not allowed, pass workerCount: 0 to disable worker streaming.
  • For compressed local .bz2 files, stream reads and use chunks around 500 KB to 4 MB.
  • If UI responsiveness is more important than maximum throughput, cap workerCount.

From v1 to v2

Version 2 is intentionally breaking. The v1 actions were typed as synchronous void methods. In v2, addData() and dataFinished() return promises so stream completion can wait for downstream consumers.

Before:

actions.addData(chunk);
actions.dataFinished();

After:

await actions.addData(chunk);
await actions.dataFinished();

Synchronous callbacks still work, but callbacks can now return a promise when the next stream stage needs backpressure:

const actions = decompressStream({
  onDecompressed: async (_id, data, done) => {
    await consumeChunk(data, done);
  },
  onError: (_id, error) => {
    console.error(error);
  }
});

Bundlers

The browser worker is packaged as generated inline source and created with a Blob URL. The published package does not rely on Vite-only ?worker imports, so it can be Vite/Rolldown dependency-optimized or prebundled.

Node.js uses dist/bzip2Worker.js for worker threads. Browser builds should not emit a separate bzip2Worker-*.js asset; browser workers use the generated inline source instead.

The WASM loader is also packaged as generated browser-safe code, so browser bundlers should not warn about upstream Node fs or path imports.

Local files

For local .bz2 files read from fast storage, stream the file and await addData() for each chunk. Keep compressed input chunks around 500 KB to 4 MB. The library internally scans and dispatches work in 500 KB windows, and applies bounded yielding so worker output callbacks can run before dataFinished() even when input is produced quickly.

If the producer reads the whole file into memory before calling addData(), peak memory includes that caller-owned buffer, the library compressed buffer, worker input/output transfers, and decompressed chunks waiting for the consumer. For lowest peak memory, use Blob.stream(), File.stream(), or another streaming reader instead of arrayBuffer() on the full file.

Caveats

  • WebAssembly must be available in the browser runtime.
  • Blob workers must be allowed for parallel browser streaming. Pass workerCount: 0 to disable worker streaming when Blob workers are blocked.
  • Complete bzip2 blocks are dispatched internally in 500 KB compressed input windows by default. Consumers can pass larger chunks; tune streamingChunkSize only after measuring.
  • workerCount can cap parallel bzip2 workers. Pass 0 to disable worker streaming and use full-file fallback.
  • There's no CRC validation for the entire file at the end - only the per-block ones. There's no proper BZ2 format eof detection. That's why dataFinished is required.

Contributing

  • Currently, there are no tests for this repository. PRs are welcome to add tests.

Credits

This library is based on the following repositories:

License

This project is licensed under the MIT License.