diff --git a/doc/api/zlib.md b/doc/api/zlib.md index 926d2ad76fa..37d94c70b70 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])`