Skip to content

Commit 9544428

Browse files
committed
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: #67846
1 parent 40c40fa commit 9544428

4 files changed

Lines changed: 814 additions & 145 deletions

File tree

‎doc/api/zlib.md‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2564,6 +2564,119 @@ added: v0.5.8
25642564

25652565
Creates and returns a new [`Unzip`][] object.
25662566

2567+
## Synchronous incremental codecs
2568+
2569+
<!-- YAML
2570+
added: v28.0.0
2571+
-->
2572+
2573+
The synchronous incremental codecs decompress (or compress) data incrementally
2574+
into buffers the caller provides. They are an alternative to the streaming
2575+
classes and the one-shot synchronous convenience methods:
2576+
2577+
* The streaming classes (`zlib.createGunzip()`, and so on) run every chunk
2578+
through the thread pool, allocate a new output buffer for every chunk, and add
2579+
the `Transform` overhead.
2580+
* The one-shot synchronous methods (`zlib.gunzipSync()`, and so on) need the
2581+
whole input in memory and return the whole output as one buffer.
2582+
2583+
The codecs let a caller own the output buffer, so it can be reused, pooled, or
2584+
aligned. There is no thread pool hop and no stream machinery, which makes them a
2585+
good fit for parsers that consume decompressed data right away (for example, a
2586+
tar parser).
2587+
2588+
These classes are available:
2589+
2590+
* [`zlib.DeflateSync`][], [`zlib.InflateSync`][]
2591+
* [`zlib.GzipSync`][], [`zlib.GunzipSync`][]
2592+
* [`zlib.DeflateRawSync`][], [`zlib.InflateRawSync`][]
2593+
* [`zlib.UnzipSync`][]
2594+
* [`zlib.BrotliCompressSync`][], [`zlib.BrotliDecompressSync`][]
2595+
* [`zlib.ZstdCompressSync`][], [`zlib.ZstdDecompressSync`][]
2596+
2597+
The `zlib.create*Sync()` functions create the corresponding codec, and each
2598+
codec exposes [`codec.process()`][], [`codec.end()`][], [`codec.reset()`][], and
2599+
[`codec.close()`][] methods.
2600+
2601+
### `zlib.createDeflateSync([options])`, `zlib.createInflateSync([options])`, and so on
2602+
2603+
<!-- YAML
2604+
added: v28.0.0
2605+
-->
2606+
2607+
* `options` {zlib options}
2608+
2609+
Creates and returns a new synchronous incremental codec for the algorithm the
2610+
factory is named after. The available factories are `createDeflateSync`,
2611+
`createInflateSync`, `createGzipSync`, `createGunzipSync`,
2612+
`createDeflateRawSync`, `createInflateRawSync`, `createUnzipSync`,
2613+
`createBrotliCompressSync`, `createBrotliDecompressSync`,
2614+
`createZstdCompressSync`, and `createZstdDecompressSync`.
2615+
2616+
### `codec.process(input, output[, options])`
2617+
2618+
<!-- YAML
2619+
added: v28.0.0
2620+
-->
2621+
2622+
* `input` {Buffer|TypedArray|DataView|ArrayBuffer|string} The data to process.
2623+
* `output` {Buffer|TypedArray|DataView|ArrayBuffer} A buffer to write into.
2624+
* `options` {Object}
2625+
* `flush` {integer} The flush flag. Defaults to the algorithm default
2626+
(`Z_NO_FLUSH`, `BROTLI_OPERATION_PROCESS`, or `ZSTD_e_continue`).
2627+
* `offset` {integer} The byte offset into `output` at which to start writing.
2628+
Default: `0`. This lets a caller append output into a shared, pooled buffer
2629+
across calls instead of handing in a fresh buffer each time.
2630+
* Returns: {Object} An object with `bytesRead`, `bytesWritten`, and `finished`
2631+
properties.
2632+
2633+
Processes `input`, writing as much output as fits in `output` (starting at
2634+
`options.offset`). The returned `bytesRead` is how many input bytes were
2635+
consumed and `bytesWritten` is how many bytes were written to `output`. When
2636+
`bytesWritten` equals `output.byteLength - offset`, the output buffer is full
2637+
and the caller should call `process()` again with the remaining input and a
2638+
fresh output buffer (or a new `offset`). `finished` is `true` once the end of
2639+
the compressed stream (or frame) has been reached.
2640+
2641+
### `codec.end(output)`
2642+
2643+
<!-- YAML
2644+
added: v28.0.0
2645+
-->
2646+
2647+
* `output` {Buffer|TypedArray|DataView|ArrayBuffer} A buffer to write into.
2648+
* `options` {Object}
2649+
* `offset` {integer} The byte offset into `output` at which to start writing.
2650+
Default: `0`.
2651+
* Returns: {Object} An object with `bytesWritten` and `finished` properties.
2652+
2653+
Performs the final flush (the finish flush) with no new input, writing any
2654+
remaining output into `output`. After `end()` returns with `finished` set to
2655+
`true`, the codec cannot process further input; calling `process()` again throws
2656+
an `ERR_STREAM_WRITE_AFTER_END` error. If a truncated stream is fed, `end()`
2657+
throws (for example, an `unexpected end of file` error).
2658+
2659+
### `codec.reset()`
2660+
2661+
<!-- YAML
2662+
added: v28.0.0
2663+
-->
2664+
2665+
* Returns: {Object} Returns the codec, so it can be reused for a new stream.
2666+
2667+
Resets the underlying stream so the codec can be reused. This avoids the cost of
2668+
creating a new codec for every archive.
2669+
2670+
### `codec.close()`
2671+
2672+
<!-- YAML
2673+
added: v28.0.0
2674+
-->
2675+
2676+
Closes the codec and frees its underlying native handle. After calling
2677+
`close()`, any further `process()` or `end()` call throws an
2678+
`ERR_STREAM_PREMATURE_CLOSE` error.
2679+
25672680
## `zlib.createZipArchive(entries[, options])`
25682681

25692682
<!-- YAML
@@ -3611,6 +3724,23 @@ Create a Zstandard decompression transform.
36113724
[`zlib.ZipEntry.createSync()`]: #static-method-zlibzipentrycreatesyncfilename-data-options
36123725
[`zlib.ZipFile.open()`]: #static-method-zlibzipfileopenfilename-options
36133726
[`zlib.crc32()`]: #zlibcrc32data-value
3727+
[`zlib.createDeflateSync`]: #synchronous-incremental-codecs
3728+
[`zlib.createGunzipSync`]: #synchronous-incremental-codecs
3729+
[`zlib.BrotliCompressSync`]: #synchronous-incremental-codecs
3730+
[`zlib.BrotliDecompressSync`]: #synchronous-incremental-codecs
3731+
[`zlib.DeflateRawSync`]: #synchronous-incremental-codecs
3732+
[`zlib.DeflateSync`]: #synchronous-incremental-codecs
3733+
[`zlib.GunzipSync`]: #synchronous-incremental-codecs
3734+
[`zlib.GzipSync`]: #synchronous-incremental-codecs
3735+
[`zlib.InflateRawSync`]: #synchronous-incremental-codecs
3736+
[`zlib.InflateSync`]: #synchronous-incremental-codecs
3737+
[`zlib.UnzipSync`]: #synchronous-incremental-codecs
3738+
[`zlib.ZstdCompressSync`]: #synchronous-incremental-codecs
3739+
[`zlib.ZstdDecompressSync`]: #synchronous-incremental-codecs
3740+
[`codec.process()`]: #codecprocessinput-output-options
3741+
[`codec.end()`]: #codecendoutput
3742+
[`codec.reset()`]: #codecreset
3743+
[`codec.close()`]: #codecclose
36143744
[`zlib.createZipArchive()`]: #zlibcreateziparchiveentries-options
36153745
[`zlib.createZipArchiveSync()`]: #zlibcreateziparchivesyncentries-options
36163746
[`zlib.getMaxZipContentSize()`]: #zlibgetmaxzipcontentsize

0 commit comments

Comments
 (0)