node:zlib supports callback, synchronous, and stream APIs.
| Family | Support |
|---|---|
| Deflate | Wrapped and raw compress/decompress functions, Sync variants, classes, and create* helpers |
| Gzip | Gzip, gunzip, unzip, Sync variants, classes, and create* helpers |
| Brotli | Compress/decompress functions, classes, and create* helpers |
| Zstandard | Compress/decompress functions, classes, and create* helpers |
| Utilities | crc32, constants, and codes |
const zlib = require("node:zlib");
const input = Buffer.from("hello, zlib!");
const gzip = zlib.gzipSync(input);
zlib.gunzipSync(gzip).toString(); // "hello, zlib!"
const zstd = zlib.zstdCompressSync(input);
zlib.zstdDecompressSync(zstd).toString(); // "hello, zlib!"Callback forms receive (error, result); synchronous forms return a
Buffer or throw.
node:zlib/iter
The Elide-specific iterator module supplies transform factories for gzip,
deflate, raw deflate, Brotli, and Zstandard. A factory returns an object with
.transform(asyncIterable); output arrives in batches of byte chunks.
import { gzip } from "node:zlib/iter";
async function* input() {
yield new TextEncoder().encode("hello");
}
for await (const batch of gzip().transform(input())) {
for (const chunk of batch) {
// compressed bytes
}
}Use Compression Streams with Web Streams.