Standard library / zlib · on the one-page reference
zlib
Experimental — new, manually tested rather than conformance-suite covered.
A practical subset of
Node's
zlib module: one-shot gzip and raw-deflate compress/
decompress. Static functions, not Node's zlib.createGzip()
transform-stream style — there's no stream/Buffer object
in the language yet, and one-shot whole-buffer compression covers the
common case (config blobs, already-buffered HTTP bodies, small payloads).
Buffer-less v1 shape: Lumen's string is
[]const u8 — raw bytes, not validated UTF-8 (the same
property crypto.randomBytes/sha256 already rely
on) — so compressed binary output flows through string
directly rather than a dedicated byte-buffer type, which doesn't exist
yet.
zlib.gzipSync(data)
string → string — compresses data's
bytes into a real gzip container (RFC 1952: 10-byte header, deflate
body, CRC32 + size footer). The result is genuine gzip framing, not a
private format — verified by writing it to a .gz file
and decoding it with the system gunzip.
const input = "hello hello hello hello hello";
const gz = zlib.gzipSync(input);
console.log(gz.length < input.length); // true
zlib.gunzipSync(data)
string → string — inverse of gzipSync.
Deviation from Node: on any decode error (bad header,
truncated stream, checksum mismatch, or plain non-gzip garbage) returns
"" rather than throwing — the same fallback-don't-crash
shape every other fallible builtin here uses. A real gzip stream never
decodes to "" in-band, so this is unambiguous as an error
sentinel.
const input = "round trip me";
const back = zlib.gunzipSync(zlib.gzipSync(input));
console.log(back == input); // true
console.log(zlib.gunzipSync("not gzip data") == ""); // true
zlib.deflateSync(data)
string → string — raw deflate body only (RFC 1951),
no gzip/zlib header or footer. 18 bytes smaller than
gzipSync's output on the same input (the gzip container's
10-byte header + 8-byte footer).
const input = "hello hello hello hello hello";
console.log(zlib.deflateSync(input).length < zlib.gzipSync(input).length); // true
zlib.inflateSync(data)
string → string — inverse of
deflateSync; same ""-on-error fallback as
gunzipSync.
const input = "round trip me";
console.log(zlib.inflateSync(zlib.deflateSync(input)) == input); // true