From f82a487980dabc062b9d383f9a5a5bb39cb3ebdf Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Mon, 30 Nov 2026 07:51:12 +0000 Subject: [PATCH] zlib: add synchronous incremental codecs with caller-provided buffers Add a public, incremental, synchronous API to node:zlib that lets callers decompress (or compress) data into buffers they provide, without the stream machinery or the thread pool. The new *Sync classes (DeflateSync, GzipSync, GunzipSync, BrotliCompressSync, ZstdCompressSync, and the rest) expose process(input, output[, options]), end(output[, options]), reset(), and close(). process() returns { bytesRead, bytesWritten, finished }, end() performs the finish flush, and reset() reuses the codec for a new stream. They cover the zlib, brotli, and zstd algorithms. process() and end() accept an options.offset so callers can append output into a shared, pooled buffer instead of handing in a fresh buffer on every call. Handle creation is factored into shared helpers (createZlibHandle, createBrotliHandle, createZstdHandle) so the streaming classes and the new synchronous codecs share the same option parsing. Stream/frame completion is exposed from the native side via a new getStreamFinished binding, so the finished flag is accurate even when the end of a decompressed stream is reached mid-process. Refs: https://github.com/nodejs/node/issues/67846 Signed-off-by: Matteo Collina --- doc/api/zlib.md | 130 +++++ lib/zlib.js | 575 +++++++++++++++----- src/node_zlib.cc | 38 +- test/parallel/test-zlib-incremental-sync.js | 216 ++++++++ 4 files changed, 814 insertions(+), 145 deletions(-) create mode 100644 test/parallel/test-zlib-incremental-sync.js diff --git a/doc/api/zlib.md b/doc/api/zlib.md index 926d2ad76faa..37d94c70b701 100644 --- a/doc/api/zlib.md +++ b/doc/api/zlib.md @@ -2564,6 +2564,119 @@ added: v0.5.8 Creates and returns a new [`Unzip`][] object. +## Synchronous incremental codecs + + + +The synchronous incremental codecs decompress (or compress) data incrementally +into buffers the caller provides. They are an alternative to the streaming +classes and the one-shot synchronous convenience methods: + +* The streaming classes (`zlib.createGunzip()`, and so on) run every chunk + through the thread pool, allocate a new output buffer for every chunk, and add + the `Transform` overhead. +* The one-shot synchronous methods (`zlib.gunzipSync()`, and so on) need the + whole input in memory and return the whole output as one buffer. + +The codecs let a caller own the output buffer, so it can be reused, pooled, or +aligned. There is no thread pool hop and no stream machinery, which makes them a +good fit for parsers that consume decompressed data right away (for example, a +tar parser). + +These classes are available: + +* [`zlib.DeflateSync`][], [`zlib.InflateSync`][] +* [`zlib.GzipSync`][], [`zlib.GunzipSync`][] +* [`zlib.DeflateRawSync`][], [`zlib.InflateRawSync`][] +* [`zlib.UnzipSync`][] +* [`zlib.BrotliCompressSync`][], [`zlib.BrotliDecompressSync`][] +* [`zlib.ZstdCompressSync`][], [`zlib.ZstdDecompressSync`][] + +The `zlib.create*Sync()` functions create the corresponding codec, and each +codec exposes [`codec.process()`][], [`codec.end()`][], [`codec.reset()`][], and +[`codec.close()`][] methods. + +### `zlib.createDeflateSync([options])`, `zlib.createInflateSync([options])`, and so on + + + +* `options` {zlib options} + +Creates and returns a new synchronous incremental codec for the algorithm the +factory is named after. The available factories are `createDeflateSync`, +`createInflateSync`, `createGzipSync`, `createGunzipSync`, +`createDeflateRawSync`, `createInflateRawSync`, `createUnzipSync`, +`createBrotliCompressSync`, `createBrotliDecompressSync`, +`createZstdCompressSync`, and `createZstdDecompressSync`. + +### `codec.process(input, output[, options])` + + + +* `input` {Buffer|TypedArray|DataView|ArrayBuffer|string} The data to process. +* `output` {Buffer|TypedArray|DataView|ArrayBuffer} A buffer to write into. +* `options` {Object} + * `flush` {integer} The flush flag. Defaults to the algorithm default + (`Z_NO_FLUSH`, `BROTLI_OPERATION_PROCESS`, or `ZSTD_e_continue`). + * `offset` {integer} The byte offset into `output` at which to start writing. + Default: `0`. This lets a caller append output into a shared, pooled buffer + across calls instead of handing in a fresh buffer each time. +* Returns: {Object} An object with `bytesRead`, `bytesWritten`, and `finished` + properties. + +Processes `input`, writing as much output as fits in `output` (starting at +`options.offset`). The returned `bytesRead` is how many input bytes were +consumed and `bytesWritten` is how many bytes were written to `output`. When +`bytesWritten` equals `output.byteLength - offset`, the output buffer is full +and the caller should call `process()` again with the remaining input and a +fresh output buffer (or a new `offset`). `finished` is `true` once the end of +the compressed stream (or frame) has been reached. + +### `codec.end(output)` + + + +* `output` {Buffer|TypedArray|DataView|ArrayBuffer} A buffer to write into. +* `options` {Object} + * `offset` {integer} The byte offset into `output` at which to start writing. + Default: `0`. +* Returns: {Object} An object with `bytesWritten` and `finished` properties. + +Performs the final flush (the finish flush) with no new input, writing any +remaining output into `output`. After `end()` returns with `finished` set to +`true`, the codec cannot process further input; calling `process()` again throws +an `ERR_STREAM_WRITE_AFTER_END` error. If a truncated stream is fed, `end()` +throws (for example, an `unexpected end of file` error). + +### `codec.reset()` + + + +* Returns: {Object} Returns the codec, so it can be reused for a new stream. + +Resets the underlying stream so the codec can be reused. This avoids the cost of +creating a new codec for every archive. + +### `codec.close()` + + + +Closes the codec and frees its underlying native handle. After calling +`close()`, any further `process()` or `end()` call throws an +`ERR_STREAM_PREMATURE_CLOSE` error. + ## `zlib.createZipArchive(entries[, options])`