Skip to content

zlib: add synchronous incremental codecs with caller-provided buffers - #67882

Draft
mcollina wants to merge 1 commit into
nodejs:mainfrom
mcollina:zlib-incremental-sync
Draft

mcollina wants to merge 1 commit into
nodejs:mainfrom
mcollina:zlib-incremental-sync

Conversation

@mcollina

Copy link
Copy Markdown
Member

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. The classes 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) used by both the streaming classes and
the new synchronous codecs, and 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: #67846

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. zlib Issues and PRs related to the zlib module and its compression dependencies. labels Nov 30, 2026
@mcollina
mcollina force-pushed the zlib-incremental-sync branch from 7a4db97 to 9544428 Compare November 30, 2026 07:52
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: nodejs#67846
Signed-off-by: Matteo Collina <matteo.collina@gmail.com>
@mcollina
mcollina force-pushed the zlib-incremental-sync branch from 9544428 to f82a487 Compare November 30, 2026 08:34
Comment thread src/node_zlib.cc
// no pending output left to emit.
stream_finished_ =
BrotliEncoderIsFinished(state_.get()) &&
!BrotliEncoderHasMoreOutput(state_.get());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is written from the worker thread but read from the JS thread without any synchronization, I assume this would need to be at least std::atomic<bool> then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. zlib Issues and PRs related to the zlib module and its compression dependencies.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants