Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions doc/api/zlib.md
Original file line number Diff line number Diff line change
Expand Up @@ -2564,6 +2564,119 @@ added: v0.5.8

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

## Synchronous incremental codecs

<!-- YAML
added: v28.0.0
-->

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

<!-- YAML
added: v28.0.0
-->

* `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])`

<!-- YAML
added: v28.0.0
-->

* `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)`

<!-- YAML
added: v28.0.0
-->

* `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()`

<!-- YAML
added: v28.0.0
-->

* 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()`

<!-- YAML
added: v28.0.0
-->

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])`

<!-- YAML
Expand Down Expand Up @@ -3611,6 +3724,23 @@ Create a Zstandard decompression transform.
[`zlib.ZipEntry.createSync()`]: #static-method-zlibzipentrycreatesyncfilename-data-options
[`zlib.ZipFile.open()`]: #static-method-zlibzipfileopenfilename-options
[`zlib.crc32()`]: #zlibcrc32data-value
[`zlib.createDeflateSync`]: #synchronous-incremental-codecs
[`zlib.createGunzipSync`]: #synchronous-incremental-codecs
[`zlib.BrotliCompressSync`]: #synchronous-incremental-codecs
[`zlib.BrotliDecompressSync`]: #synchronous-incremental-codecs
[`zlib.DeflateRawSync`]: #synchronous-incremental-codecs
[`zlib.DeflateSync`]: #synchronous-incremental-codecs
[`zlib.GunzipSync`]: #synchronous-incremental-codecs
[`zlib.GzipSync`]: #synchronous-incremental-codecs
[`zlib.InflateRawSync`]: #synchronous-incremental-codecs
[`zlib.InflateSync`]: #synchronous-incremental-codecs
[`zlib.UnzipSync`]: #synchronous-incremental-codecs
[`zlib.ZstdCompressSync`]: #synchronous-incremental-codecs
[`zlib.ZstdDecompressSync`]: #synchronous-incremental-codecs
[`codec.process()`]: #codecprocessinput-output-options
[`codec.end()`]: #codecendoutput
[`codec.reset()`]: #codecreset
[`codec.close()`]: #codecclose
[`zlib.createZipArchive()`]: #zlibcreateziparchiveentries-options
[`zlib.createZipArchiveSync()`]: #zlibcreateziparchivesyncentries-options
[`zlib.getMaxZipContentSize()`]: #zlibgetmaxzipcontentsize
Expand Down
Loading