minizlib is a high-performance JavaScript library designed for stream-based compression and decompression using zlib, gzip, Brotli, and Zstd algorithms. It leverages Node.js's native zlib bindings for optimal speed, providing a synchronous streaming API built on top of the 'minipass' stream implementation. The current stable version is 3.1.0, actively maintained with a focus on efficiency. A key differentiator is its synchronous operation on the main thread, which minimizes overhead and offers immediate processing, making it suitable for CPU-bound tasks in scenarios where consistent asynchronous behavior of Node's core `stream.Transform` might introduce undesirable latency. Unlike Node.js's built-in `zlib` module, minizlib exclusively provides stream interfaces and does not offer convenience methods for buffer-to-buffer compression/decompression, requiring users to compose streams for such tasks. It was developed to meet the demanding requirements of projects like 'node-tar' and 'minipass-fetch'.
npm install minizlibVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to decompress a Brotli-compressed data stream using `minizlib`. It sets up a simulated readable stream for compressed input and a writable stream for decompressed output, then pipes data through `BrotliDecompress`.
For very large or numerous compression tasks where event loop blocking is a concern, consider offloading operations to worker threads or using Node.js's built-in `zlib` streams which are designed to operate asynchronously in the background via libuv.
To achieve buffer-to-buffer operations, you must pipe data through a `minipass` stream. For example: `new Deflate().end(buffer).read()` to compress a buffer, or `new Inflate().pipe(new MyWritableStream())` to decompress.
Ensure your Node.js version meets the minimum requirements for Brotli (v10+) or Zstd (v22.15+) if you intend to use `BrotliCompress`/`BrotliDecompress` or `ZstdCompress`/`ZstdDecompress` classes. Otherwise, stick to zlib/gzip methods.
When creating a `Gzip` stream, pass `{ portable: true }` in the options object: `new Gzip({ portable: true })`.Ensure you are using named imports: `import { BrotliDecompress } from 'minizlib';` or `const { BrotliDecompress } = require('minizlib');`.Ensure that the data being piped into or written to `minizlib` streams (or any Minipass stream) is always a Buffer, Uint8Array, or string type. Transform non-compliant data upstream if necessary.
Add the appropriate import or require statement: `import { Deflate } from 'minizlib';` or `const { Deflate } = require('minizlib');`.