Compression and decompression
Prerequisites: previous curriculum lessons on Node streams, HTTP, buffers and asynchronous errors. Local lab baseline: Node.js 22.16.0, Linux, ESM. Use a disposable directory. Static examples are not ByteMind interactive execution. Each example is independent; save the named files together and run node main.mjs. The author harness imposes a five-second process deadline.
How it works
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.
Round-trip a small UTF-8 buffer with gzip
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'));Expected output:
true Café stock: 3Byte equality verifies decoding; exact compressed size is intentionally not an assertion.
Pipe bytes through Brotli compression and decoding
import { Readable,Writable } from 'node:stream';import { pipeline } from 'node:stream/promises';import { createBrotliCompress,createBrotliDecompress } from 'node:zlib';const chunks=[];await pipeline(Readable.from([Buffer.from('one '),Buffer.from('two')]),createBrotliCompress(),createBrotliDecompress(),new Writable({write(chunk,enc,cb){chunks.push(chunk);cb();}}));console.log(Buffer.concat(chunks).toString());Expected output:
one twoThe transforms preserve content and pipeline manages completion and error propagation. Input is fixed and tiny; an untrusted stream requires a separate expanded-byte limiter.
Common mistakes
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.
Practice
Round-trip a short JSON document containing an accented character. Compare decoded bytes with the original instead of requiring a particular compressed size.
Further reading
Official documentation: zlib. Internal reference candidate: node-compression-and-decompression-lookup (route not verified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Compression and decompression — knowledge check
Apply the lesson to a fresh scenario and explain the resource or protocol boundary.
Which value should determine a round-trip test?
What does Content-Encoding: gzip describe?
Why avoid synchronous compression in a busy request handler?