@@ -1760,11 +1760,261 @@ added: |
1760 | 1760 |
|
1761 | 1761 | Decompress a chunk of data with [`ZstdDecompress`][]. |
1762 | 1762 |
|
| 1763 | +## Iterable Compression |
| 1764 | + |
| 1765 | +<!-- YAML |
| 1766 | +added: v25.9.0 |
| 1767 | +--> |
| 1768 | + |
| 1769 | +> Stability: 1 - Experimental |
| 1770 | +
|
| 1771 | +The `node:zlib/iter` module provides compression and decompression transforms |
| 1772 | +for use with the [`node:stream/iter`][] iterable streams API. |
| 1773 | + |
| 1774 | +This module is available only when the `--experimental-stream-iter` CLI flag |
| 1775 | +is enabled. |
| 1776 | + |
| 1777 | +Each algorithm has both an async variant (stateful async generator, for use |
| 1778 | +with [`pull()`][] and [`pipeTo()`][]) and a sync variant (stateful sync |
| 1779 | +generator, for use with `pullSync()` and `pipeToSync()`). |
| 1780 | + |
| 1781 | +The async transforms run compression on the libuv threadpool, overlapping |
| 1782 | +I/O with JavaScript execution. The sync transforms run compression directly |
| 1783 | +on the main thread. |
| 1784 | + |
| 1785 | +> Note: The defaults for these transforms are tuned for streaming throughput, |
| 1786 | +> and differ from the defaults in `node:zlib`. In particular, gzip/deflate |
| 1787 | +> default to level 4 (not 6) and memLevel 9 (not 8), and Brotli defaults to |
| 1788 | +> quality 6 (not 11). These choices match common HTTP server configurations |
| 1789 | +> and provide significantly faster compression with only a small reduction in |
| 1790 | +> compression ratio. All defaults can be overridden via options. |
| 1791 | +
|
| 1792 | +```mjs |
| 1793 | +import { from, pull, bytes, text } from 'node:stream/iter'; |
| 1794 | +import { compressGzip, decompressGzip } from 'node:zlib/iter'; |
| 1795 | + |
| 1796 | +// Async round-trip |
| 1797 | +const compressed = await bytes(pull(from('hello'), compressGzip())); |
| 1798 | +const original = await text(pull(from(compressed), decompressGzip())); |
| 1799 | +console.log(original); // 'hello' |
| 1800 | +``` |
| 1801 | + |
| 1802 | +```cjs |
| 1803 | +const { from, pull, bytes, text } = require('node:stream/iter'); |
| 1804 | +const { compressGzip, decompressGzip } = require('node:zlib/iter'); |
| 1805 | + |
| 1806 | +async function run() { |
| 1807 | + const compressed = await bytes(pull(from('hello'), compressGzip())); |
| 1808 | + const original = await text(pull(from(compressed), decompressGzip())); |
| 1809 | + console.log(original); // 'hello' |
| 1810 | +} |
| 1811 | + |
| 1812 | +run().catch(console.error); |
| 1813 | +``` |
| 1814 | + |
| 1815 | +```mjs |
| 1816 | +import { fromSync, pullSync, textSync } from 'node:stream/iter'; |
| 1817 | +import { compressGzipSync, decompressGzipSync } from 'node:zlib/iter'; |
| 1818 | + |
| 1819 | +// Sync round-trip |
| 1820 | +const compressed = pullSync(fromSync('hello'), compressGzipSync()); |
| 1821 | +const original = textSync(pullSync(compressed, decompressGzipSync())); |
| 1822 | +console.log(original); // 'hello' |
| 1823 | +``` |
| 1824 | + |
| 1825 | +```cjs |
| 1826 | +const { fromSync, pullSync, textSync } = require('node:stream/iter'); |
| 1827 | +const { compressGzipSync, decompressGzipSync } = require('node:zlib/iter'); |
| 1828 | + |
| 1829 | +const compressed = pullSync(fromSync('hello'), compressGzipSync()); |
| 1830 | +const original = textSync(pullSync(compressed, decompressGzipSync())); |
| 1831 | +console.log(original); // 'hello' |
| 1832 | +``` |
| 1833 | + |
| 1834 | +### `compressBrotli([options])` |
| 1835 | + |
| 1836 | +### `compressBrotliSync([options])` |
| 1837 | + |
| 1838 | +<!-- YAML |
| 1839 | +added: v25.9.0 |
| 1840 | +--> |
| 1841 | + |
| 1842 | +* `options` {Object} |
| 1843 | + * `chunkSize` {number} Output buffer size. **Default:** `65536` (64 KB). |
| 1844 | + * `params` {Object} Key-value object where keys and values are |
| 1845 | + `zlib.constants` entries. The most important compressor parameters are: |
| 1846 | + * `BROTLI_PARAM_MODE` -- `BROTLI_MODE_GENERIC` (default), |
| 1847 | + `BROTLI_MODE_TEXT`, or `BROTLI_MODE_FONT`. |
| 1848 | + * `BROTLI_PARAM_QUALITY` -- ranges from `BROTLI_MIN_QUALITY` to |
| 1849 | + `BROTLI_MAX_QUALITY`. **Default:** `6` (not `BROTLI_DEFAULT_QUALITY` |
| 1850 | + which is 11). Quality 6 is appropriate for streaming; quality 11 is |
| 1851 | + intended for offline/build-time compression. |
| 1852 | + * `BROTLI_PARAM_SIZE_HINT` -- expected input size. **Default:** `0` |
| 1853 | + (unknown). |
| 1854 | + * `BROTLI_PARAM_LGWIN` -- window size (log2). **Default:** `20` (1 MB). |
| 1855 | + The Brotli library default is 22 (4 MB); the reduced default saves |
| 1856 | + memory without significant compression impact for streaming workloads. |
| 1857 | + * `BROTLI_PARAM_LGBLOCK` -- input block size (log2). |
| 1858 | + See the [Brotli compressor options][] in the zlib documentation for the |
| 1859 | + full list. |
| 1860 | + * `dictionary` {Buffer|TypedArray|DataView} |
| 1861 | +* Returns: {Object} A stateful transform. |
| 1862 | + |
| 1863 | +Create a Brotli compression transform. Output is compatible with |
| 1864 | +`zlib.brotliDecompress()` and `decompressBrotli()`/`decompressBrotliSync()`. |
| 1865 | + |
| 1866 | +### `compressDeflate([options])` |
| 1867 | + |
| 1868 | +### `compressDeflateSync([options])` |
| 1869 | + |
| 1870 | +<!-- YAML |
| 1871 | +added: v25.9.0 |
| 1872 | +--> |
| 1873 | + |
| 1874 | +* `options` {Object} |
| 1875 | + * `chunkSize` {number} Output buffer size. **Default:** `65536` (64 KB). |
| 1876 | + * `level` {number} Compression level (`0`-`9`). **Default:** `4`. |
| 1877 | + * `windowBits` {number} **Default:** `Z_DEFAULT_WINDOWBITS` (15). |
| 1878 | + * `memLevel` {number} **Default:** `9`. |
| 1879 | + * `strategy` {number} **Default:** `Z_DEFAULT_STRATEGY`. |
| 1880 | + * `dictionary` {Buffer|TypedArray|DataView} |
| 1881 | +* Returns: {Object} A stateful transform. |
| 1882 | + |
| 1883 | +Create a deflate compression transform. Output is compatible with |
| 1884 | +`zlib.inflate()` and `decompressDeflate()`/`decompressDeflateSync()`. |
| 1885 | + |
| 1886 | +### `compressGzip([options])` |
| 1887 | + |
| 1888 | +### `compressGzipSync([options])` |
| 1889 | + |
| 1890 | +<!-- YAML |
| 1891 | +added: v25.9.0 |
| 1892 | +--> |
| 1893 | + |
| 1894 | +* `options` {Object} |
| 1895 | + * `chunkSize` {number} Output buffer size. **Default:** `65536` (64 KB). |
| 1896 | + * `level` {number} Compression level (`0`-`9`). **Default:** `4`. |
| 1897 | + * `windowBits` {number} **Default:** `Z_DEFAULT_WINDOWBITS` (15). |
| 1898 | + * `memLevel` {number} **Default:** `9`. |
| 1899 | + * `strategy` {number} **Default:** `Z_DEFAULT_STRATEGY`. |
| 1900 | + * `dictionary` {Buffer|TypedArray|DataView} |
| 1901 | +* Returns: {Object} A stateful transform. |
| 1902 | + |
| 1903 | +Create a gzip compression transform. Output is compatible with `zlib.gunzip()` |
| 1904 | +and `decompressGzip()`/`decompressGzipSync()`. |
| 1905 | + |
| 1906 | +### `compressZstd([options])` |
| 1907 | + |
| 1908 | +### `compressZstdSync([options])` |
| 1909 | + |
| 1910 | +<!-- YAML |
| 1911 | +added: v25.9.0 |
| 1912 | +--> |
| 1913 | + |
| 1914 | +* `options` {Object} |
| 1915 | + * `chunkSize` {number} Output buffer size. **Default:** `65536` (64 KB). |
| 1916 | + * `params` {Object} Key-value object where keys and values are |
| 1917 | + `zlib.constants` entries. The most important compressor parameters are: |
| 1918 | + * `ZSTD_c_compressionLevel` -- **Default:** `ZSTD_CLEVEL_DEFAULT` (3). |
| 1919 | + * `ZSTD_c_checksumFlag` -- generate a checksum. **Default:** `0`. |
| 1920 | + * `ZSTD_c_strategy` -- compression strategy. Values include |
| 1921 | + `ZSTD_fast`, `ZSTD_dfast`, `ZSTD_greedy`, `ZSTD_lazy`, |
| 1922 | + `ZSTD_lazy2`, `ZSTD_btlazy2`, `ZSTD_btopt`, `ZSTD_btultra`, |
| 1923 | + `ZSTD_btultra2`. |
| 1924 | + See the [Zstd compressor options][] in the zlib documentation for the |
| 1925 | + full list. |
| 1926 | + * `pledgedSrcSize` {number} Expected uncompressed size (optional hint). |
| 1927 | + * `dictionary` {Buffer|TypedArray|DataView} |
| 1928 | +* Returns: {Object} A stateful transform. |
| 1929 | + |
| 1930 | +Create a Zstandard compression transform. Output is compatible with |
| 1931 | +`zlib.zstdDecompress()` and `decompressZstd()`/`decompressZstdSync()`. |
| 1932 | + |
| 1933 | +### `decompressBrotli([options])` |
| 1934 | + |
| 1935 | +### `decompressBrotliSync([options])` |
| 1936 | + |
| 1937 | +<!-- YAML |
| 1938 | +added: v25.9.0 |
| 1939 | +--> |
| 1940 | + |
| 1941 | +* `options` {Object} |
| 1942 | + * `chunkSize` {number} Output buffer size. **Default:** `65536` (64 KB). |
| 1943 | + * `params` {Object} Key-value object where keys and values are |
| 1944 | + `zlib.constants` entries. Available decompressor parameters: |
| 1945 | + * `BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION` -- boolean |
| 1946 | + flag affecting internal memory allocation. |
| 1947 | + * `BROTLI_DECODER_PARAM_LARGE_WINDOW` -- boolean flag enabling "Large |
| 1948 | + Window Brotli" mode (not compatible with [RFC 7932][]). |
| 1949 | + See the [Brotli decompressor options][] in the zlib documentation for |
| 1950 | + details. |
| 1951 | + * `dictionary` {Buffer|TypedArray|DataView} |
| 1952 | +* Returns: {Object} A stateful transform. |
| 1953 | + |
| 1954 | +Create a Brotli decompression transform. |
| 1955 | + |
| 1956 | +### `decompressDeflate([options])` |
| 1957 | + |
| 1958 | +### `decompressDeflateSync([options])` |
| 1959 | + |
| 1960 | +<!-- YAML |
| 1961 | +added: v25.9.0 |
| 1962 | +--> |
| 1963 | + |
| 1964 | +* `options` {Object} |
| 1965 | + * `chunkSize` {number} Output buffer size. **Default:** `65536` (64 KB). |
| 1966 | + * `windowBits` {number} **Default:** `Z_DEFAULT_WINDOWBITS` (15). |
| 1967 | + * `dictionary` {Buffer|TypedArray|DataView} |
| 1968 | +* Returns: {Object} A stateful transform. |
| 1969 | + |
| 1970 | +Create a deflate decompression transform. |
| 1971 | + |
| 1972 | +### `decompressGzip([options])` |
| 1973 | + |
| 1974 | +### `decompressGzipSync([options])` |
| 1975 | + |
| 1976 | +<!-- YAML |
| 1977 | +added: v25.9.0 |
| 1978 | +--> |
| 1979 | + |
| 1980 | +* `options` {Object} |
| 1981 | + * `chunkSize` {number} Output buffer size. **Default:** `65536` (64 KB). |
| 1982 | + * `windowBits` {number} **Default:** `Z_DEFAULT_WINDOWBITS` (15). |
| 1983 | + * `dictionary` {Buffer|TypedArray|DataView} |
| 1984 | +* Returns: {Object} A stateful transform. |
| 1985 | + |
| 1986 | +Create a gzip decompression transform. |
| 1987 | + |
| 1988 | +### `decompressZstd([options])` |
| 1989 | + |
| 1990 | +### `decompressZstdSync([options])` |
| 1991 | + |
| 1992 | +<!-- YAML |
| 1993 | +added: v25.9.0 |
| 1994 | +--> |
| 1995 | + |
| 1996 | +* `options` {Object} |
| 1997 | + * `chunkSize` {number} Output buffer size. **Default:** `65536` (64 KB). |
| 1998 | + * `params` {Object} Key-value object where keys and values are |
| 1999 | + `zlib.constants` entries. Available decompressor parameters: |
| 2000 | + * `ZSTD_d_windowLogMax` -- maximum window size (log2) the decompressor |
| 2001 | + will allocate. Limits memory usage against malicious input. |
| 2002 | + See the [Zstd decompressor options][] in the zlib documentation for |
| 2003 | + details. |
| 2004 | + * `dictionary` {Buffer|TypedArray|DataView} |
| 2005 | +* Returns: {Object} A stateful transform. |
| 2006 | + |
| 2007 | +Create a Zstandard decompression transform. |
| 2008 | + |
| 2009 | +[Brotli compressor options]: #compressor-options |
| 2010 | +[Brotli decompressor options]: #decompressor-options |
1763 | 2011 | [Brotli parameters]: #brotli-constants |
1764 | 2012 | [Cyclic redundancy check]: https://en.wikipedia.org/wiki/Cyclic_redundancy_check |
1765 | 2013 | [Memory usage tuning]: #memory-usage-tuning |
1766 | | -[RFC 7932]: https://www.rfc-editor.org/rfc/rfc7932.txt |
| 2014 | +[RFC 7932]: https://www.rfc-editor.org/rfc/rfc7932.html |
1767 | 2015 | [Streams API]: stream.md |
| 2016 | +[Zstd compressor options]: #compressor-options |
| 2017 | +[Zstd decompressor options]: #decompressor-options |
1768 | 2018 | [Zstd parameters]: #zstd-constants |
1769 | 2019 | [`.flush()`]: #zlibflushkind-callback |
1770 | 2020 | [`Accept-Encoding`]: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3 |
@@ -1783,6 +2033,9 @@ Decompress a chunk of data with [`ZstdDecompress`][]. |
1783 | 2033 | [`ZstdDecompress`]: #class-zlibzstddecompress |
1784 | 2034 | [`buffer.kMaxLength`]: buffer.md#bufferkmaxlength |
1785 | 2035 | [`deflateInit2` and `inflateInit2`]: https://zlib.net/manual.html#Advanced |
| 2036 | +[`node:stream/iter`]: stream_iter.md |
| 2037 | +[`pipeTo()`]: stream_iter.md#pipetosource-transforms-writer-options |
| 2038 | +[`pull()`]: stream_iter.md#pullsource-transforms-options |
1786 | 2039 | [`stream.Transform`]: stream.md#class-streamtransform |
1787 | 2040 | [convenience methods]: #convenience-methods |
1788 | 2041 | [zlib documentation]: https://zlib.net/manual.html#Constants |
|