zlib.gzip() and streaming compression
Compression represents bytes in a smaller form when the data has useful redundancy. Node provides gzip, deflate and Brotli through `node:zlib`. The receiver must use the matching decoding format, and decompression must reproduce the original bytes. Gzip and deflate are distinct formats in HTTP content coding; Brotli is another coding. Compressed bytes are not text and should remain Buffers or streams. For tiny known inputs synchronous functions are useful for demonstration, but they block the current JavaScript thread. For larger tasks choose asynchronous or streaming APIs and cap concurrency, input and output sizes. Use `Accept-Encoding` to choose an acceptable representation, `Content-Encoding` to describe the actual coding and `Vary: Accept-Encoding` when response selection depends on it. Do not blindly choose an encoding the client has disabled with q=0. Compression may be counterproductive for small or already-compressed data. Do not convert compressed bytes to UTF-8 before decoding. Do not assume the compressed representation is always smaller or that compression encrypts data. Avoid compressing secrets together with attacker-influenced text without a dedicated security review. Sources: https://nodejs.org/download/release/v22.16.0/docs/api/zlib.html.
Formal Syntax & Structure
zlib.gzip(buffer[, options], callback); zlib.createGzip([options])Working Example
import { gzipSync,gunzipSync } from 'node:zlib';const original=Buffer.from('Café stock: 3','utf8');const packed=gzipSync(original);const restored=gunzipSync(packed,{maxOutputLength:128});console.log(restored.equals(original),restored.toString('utf8'));