Compression API
The WHATWG CompressionStream / DecompressionStream interfaces, backed by a native miniz extension. CompressionStream compresses a stream of chunks; DecompressionStream reverses the operation.
Globals
| Global | Type | Description |
|---|---|---|
CompressionStream | class | Transform stream that compresses written chunks. |
DecompressionStream | class | Transform stream that decompresses written chunks. |
Both are standard Streams — they expose .readable and .writable and can be piped through a pipeline.
new CompressionStream(format)
Creates a compression transform. format defaults to 'gzip'.
| Format | Wire format |
|---|---|
'gzip' | gzip wrapper (10-byte header + DEFLATE + CRC32 + ISIZE) |
'deflate' | zlib wrapper (2-byte header + DEFLATE + Adler-32 trailer) |
'deflate-raw' | raw DEFLATE, no wrapper |
let cs = new CompressionStream('gzip');
let rs = new ReadableStream({ start(c) { c.enqueue(new TextEncoder().encode('hello world')); c.close(); } });
let chunks = [];
await rs
.pipeThrough(cs)
.pipeTo(new WritableStream({ write(c) { chunks.push(c); } }));
let gzipBytes = chunks[0]; // Uint8Array of gzip-compressed datanew DecompressionStream(format)
Creates a decompression transform. format defaults to 'gzip' and must match the wrapper used to produce the data ('gzip', 'deflate', or 'deflate-raw').
let ds = new DecompressionStream('gzip');
let rs = new ReadableStream({ start(c) { c.enqueue(gzipBytes); c.close(); } });
let out = [];
await rs
.pipeThrough(ds)
.pipeTo(new WritableStream({ write(c) { out.push(c); } }));
let text = new TextDecoder().decode(out[0]); // "hello world"An unsupported format name throws Error: CompressionStream: unsupported format: <fmt> (or DecompressionStream:).
Piping with HTTP
A typical use is compressing a fetch response body:
let res = await fetch('https://example.com/big.json');
let decompressed = res.body.pipeThrough(new DecompressionStream('gzip'));
let text = await new Response(decompressed).text();Build option
Compression requires QZ_WITH_COMPRESS=ON (default) at build time. When disabled, the classes exist but error the readable side with TypeError: Native compression extension not available. See Build Options.
Notes
- Input chunks should be
Uint8ArrayorArrayBuffer(other chunk types are coerced vianew Uint8Array(...)). - All-at-once processing: the full input is held in memory until the stream closes, so extremely large streams are not memory-bounded.
- Decompression of malformed, truncated, or empty DEFLATE input errors the readable side (with a bounded iteration cap) rather than producing partial output or hanging forever.