Iterable Compression
History
The node:zlib/iter module provides compression and decompression transforms
for use with the node:stream/iter iterable streams API.
This module is available only when the --experimental-stream-iter CLI flag
is enabled.
Each algorithm has both an async variant (stateful async generator, for use
with pull() and pipeTo()) and a sync variant (stateful sync
generator, for use with pullSync() and pipeToSync()).
The async transforms run compression on the libuv threadpool, overlapping I/O with JavaScript execution. The sync transforms run compression directly on the main thread.
Note: The defaults for these transforms are tuned for streaming throughput, and differ from the defaults in
node:zlib. In particular, gzip/deflate default to level 4 (not 6) and memLevel 9 (not 8), and Brotli defaults to quality 6 (not 11). These choices match common HTTP server configurations and provide significantly faster compression with only a small reduction in compression ratio. All defaults can be overridden via options.
import { from, pull, bytes, text } from 'node:stream/iter';
import { compressGzip, decompressGzip } from 'node:zlib/iter';
// Async round-trip
const compressed = await bytes(pull(from('hello'), compressGzip()));
const original = await text(pull(from(compressed), decompressGzip()));
console.log(original); // 'hello'compressBrotli(options?): voidcompressBrotliSync(options?): Object<Object><number>65536
(64 KB).<Object>zlib.constants
entries. The most important compressor parameters are:BROTLI_MODE_GENERIC
(default),
BROTLI_MODE_TEXT
, or
BROTLI_MODE_FONT
.BROTLI_MIN_QUALITY
to
BROTLI_MAX_QUALITY
.
Default:
6
(not
BROTLI_DEFAULT_QUALITY
which is 11). Quality 6 is appropriate for streaming; quality 11 is
intended for offline/build-time compression.0
(unknown).20
(1 MB).
The Brotli library default is 22 (4 MB); the reduced default saves
memory without significant compression impact for streaming workloads.<TypedArray>
|
<DataView><Object>Create a Brotli compression transform. Output is compatible with
zlib.brotliDecompress() and decompressBrotli()/decompressBrotliSync().
compressDeflate(options?): voidcompressDeflateSync(options?): Object<Object><number>65536
(64 KB).<number>0
-
9
).
Default:
4
.<number>Z_DEFAULT_WINDOWBITS
(15).<number>9
.<number>Z_DEFAULT_STRATEGY
.<TypedArray>
|
<DataView><Object>Create a deflate compression transform. Output is compatible with
zlib.inflate() and decompressDeflate()/decompressDeflateSync().
compressGzip(options?): voidcompressGzipSync(options?): Object<Object><number>65536
(64 KB).<number>0
-
9
).
Default:
4
.<number>Z_DEFAULT_WINDOWBITS
(15).<number>9
.<number>Z_DEFAULT_STRATEGY
.<TypedArray>
|
<DataView><Object>Create a gzip compression transform. Output is compatible with zlib.gunzip()
and decompressGzip()/decompressGzipSync().
compressZstd(options?): voidcompressZstdSync(options?): Object<Object><number>65536
(64 KB).<Object>zlib.constants
entries. The most important compressor parameters are:ZSTD_CLEVEL_DEFAULT
(3).0
.ZSTD_fast
,
ZSTD_dfast
,
ZSTD_greedy
,
ZSTD_lazy
,
ZSTD_lazy2
,
ZSTD_btlazy2
,
ZSTD_btopt
,
ZSTD_btultra
,
ZSTD_btultra2
.
See the
Zstd compressor options
in the zlib documentation for the
full list.<number><TypedArray>
|
<DataView><Object>Create a Zstandard compression transform. Output is compatible with
zlib.zstdDecompress() and decompressZstd()/decompressZstdSync().
decompressBrotli(options?): voiddecompressBrotliSync(options?): Object<Object><number>65536
(64 KB).<Object>zlib.constants
entries. Available decompressor parameters:<TypedArray>
|
<DataView><Object>Create a Brotli decompression transform.
decompressDeflate(options?): voiddecompressDeflateSync(options?): Object<Object><number>65536
(64 KB).<number>Z_DEFAULT_WINDOWBITS
(15).<TypedArray>
|
<DataView><Object>Create a deflate decompression transform.
decompressGzip(options?): voiddecompressGzipSync(options?): Object<Object><number>65536
(64 KB).<number>Z_DEFAULT_WINDOWBITS
(15).<TypedArray>
|
<DataView><Object>Create a gzip decompression transform.
decompressZstd(options?): voiddecompressZstdSync(options?): Object<Object><number>65536
(64 KB).<Object>zlib.constants
entries. Available decompressor parameters:<TypedArray>
|
<DataView><Object>Create a Zstandard decompression transform.