What is minizlib?
The minizlib npm package is a minimal implementation of zlib bindings for Node.js, providing compression and decompression functionalities in a lightweight and efficient manner. It is designed to be faster and less memory-intensive than the native Node.js zlib module for certain use cases.
What are minizlib's main functionalities?
Compression
This feature allows you to compress data using the Deflate algorithm. The code sample demonstrates how to compress a simple string.
const { Deflate } = require('minizlib');
const input = Buffer.from('Hello World');
const deflate = new Deflate();
deflate.push(input, true); // true indicates this is the last chunk
if (deflate.err) { throw new Error(deflate.msg); }
const output = deflate.result;
Decompression
This feature enables you to decompress data that was compressed using the Deflate algorithm. The code sample shows how to decompress data to its original form.
const { Inflate } = require('minizlib');
const input = Buffer.from([/* Compressed data here */]);
const inflate = new Inflate();
inflate.push(input, true); // true indicates this is the last chunk
if (inflate.err) { throw new Error(inflate.msg); }
const output = inflate.result;
Other packages similar to minizlib
pako
Pako is a high-speed zlib port to pure JavaScript that works in the browser and Node.js. It offers similar compression and decompression functionalities as minizlib but with a broader scope, including support for gzip, deflate, and inflate algorithms. Pako is often chosen for its performance and compatibility with both server and client-side applications.
node-zlib-backport
node-zlib-backport provides a backport of newer Node.js zlib features to older versions. While it offers similar compression and decompression capabilities, its primary use case is to enable applications running on older Node.js versions to utilize newer zlib functionalities. It's more about compatibility rather than offering a minimalistic approach like minizlib.
minizlib
A fast zlib stream built on minipass and
Node.js's zlib binding.
This module was created to serve the needs of
node-tar and
minipass-fetch.
Brotli is supported in versions of node with a Brotli binding.
How does this differ from the streams in require('zlib')
?
First, there are no convenience methods to compress or decompress a
buffer. If you want those, use the built-in zlib
module. This is
only streams. That being said, Minipass streams to make it fairly easy to
use as one-liners: new zlib.Deflate().end(data).read()
will return the
deflate compressed result.
This module compresses and decompresses the data as fast as you feed
it in. It is synchronous, and runs on the main process thread. Zlib
and Brotli operations can be high CPU, but they're very fast, and doing it
this way means much less bookkeeping and artificial deferral.
Node's built in zlib streams are built on top of stream.Transform
.
They do the maximally safe thing with respect to consistent
asynchrony, buffering, and backpressure.
See Minipass for more on the differences between
Node.js core streams and Minipass streams, and the convenience methods
provided by that class.
Classes
- Deflate
- Inflate
- Gzip
- Gunzip
- DeflateRaw
- InflateRaw
- Unzip
- BrotliCompress (Node v10 and higher)
- BrotliDecompress (Node v10 and higher)
USAGE
const zlib = require('minizlib')
const input = sourceOfCompressedData()
const decode = new zlib.BrotliDecompress()
const output = whereToWriteTheDecodedData()
input.pipe(decode).pipe(output)
REPRODUCIBLE BUILDS
To create reproducible gzip compressed files across different operating
systems, set portable: true
in the options. This causes minizlib to set
the OS
indicator in byte 9 of the extended gzip header to 0xFF
for
'unknown'.