@@ -844,7 +844,10 @@ ignored by the decompression classes. |
844 | 844 | empty dictionary by default) |
845 | 845 | * `info` {boolean} (If `true`, returns an object with `buffer` and `engine`.) |
846 | 846 | * `maxOutputLength` {integer} Limits output size when using |
847 | | - [convenience methods][]. **Default:** [`buffer.kMaxLength`][] |
| 847 | + [convenience methods][]. On streaming instances created via |
| 848 | + `zlib.create*Decompress()`, this option bounds per-chunk internal buffer |
| 849 | + growth rather than cumulative stream output. **Default:** |
| 850 | + [`buffer.kMaxLength`][] |
848 | 851 | * `rejectGarbageAfterEnd` {boolean} If `true`, decompression fails when |
849 | 852 | trailing input is detected after the end of the compressed stream. This |
850 | 853 | includes unreadable bytes and, when decompressing gzip, additional gzip |
@@ -879,7 +882,10 @@ Each Brotli-based class takes an `options` object. All options are optional. |
879 | 882 | * `chunkSize` {integer} **Default:** `16 * 1024` |
880 | 883 | * `params` {Object} Key-value object containing indexed [Brotli parameters][]. |
881 | 884 | * `maxOutputLength` {integer} Limits output size when using |
882 | | - [convenience methods][]. **Default:** [`buffer.kMaxLength`][] |
| 885 | + [convenience methods][]. On streaming instances created via |
| 886 | + `zlib.create*Decompress()`, this option bounds per-chunk internal buffer |
| 887 | + growth rather than cumulative stream output. **Default:** |
| 888 | + [`buffer.kMaxLength`][] |
883 | 889 | * `info` {boolean} If `true`, returns an object with `buffer` and `engine`. **Default:** `false` |
884 | 890 | * `rejectGarbageAfterEnd` {boolean} If `true`, decompression fails when |
885 | 891 | input remains after the first complete compressed stream. **Default:** `false` |
@@ -2294,7 +2300,10 @@ Each Zstd-based class takes an `options` object. All options are optional. |
2294 | 2300 | must be a non-negative safe integer and must match the input size when |
2295 | 2301 | compression finishes. Only applicable to Zstd compressors. |
2296 | 2302 | * `maxOutputLength` {integer} Limits output size when using |
2297 | | - [convenience methods][]. **Default:** [`buffer.kMaxLength`][] |
| 2303 | + [convenience methods][]. On streaming instances created via |
| 2304 | + `zlib.create*Decompress()`, this option bounds per-chunk internal buffer |
| 2305 | + growth rather than cumulative stream output. **Default:** |
| 2306 | + [`buffer.kMaxLength`][] |
2298 | 2307 | * `info` {boolean} If `true`, returns an object with `buffer` and `engine`. **Default:** `false` |
2299 | 2308 | * `dictionary` {Buffer|TypedArray|DataView|ArrayBuffer} Optional dictionary used |
2300 | 2309 | to improve compression efficiency when compressing or decompressing data that |
@@ -2811,6 +2820,75 @@ with `callback(error, result)`. |
2811 | 2820 | Every method has a `*Sync` counterpart, which accept the same arguments, but |
2812 | 2821 | without a callback. |
2813 | 2822 |
|
| 2823 | +Unlike [convenience methods][], streaming decompressors (`zlib.createGunzip()`, |
| 2824 | +`zlib.createBrotliDecompress()`, `zlib.createZstdDecompress()`, and related |
| 2825 | +classes) do not enforce a cumulative output limit via `maxOutputLength`. To cap |
| 2826 | +total decoded bytes across a stream, count chunk lengths inside a |
| 2827 | +[`pipeline()`][] [`stream.Transform`][] stage: |
| 2828 | + |
| 2829 | +```mjs |
| 2830 | +import { Transform } from 'node:stream'; |
| 2831 | +import { pipeline } from 'node:stream/promises'; |
| 2832 | +import { createGunzip } from 'node:zlib'; |
| 2833 | + |
| 2834 | +function createDecodedByteLimit(maxTotalBytes) { |
| 2835 | + let total = 0; |
| 2836 | + return new Transform({ |
| 2837 | + transform(chunk, _encoding, callback) { |
| 2838 | + total += chunk.length; |
| 2839 | + if (total > maxTotalBytes) { |
| 2840 | + const err = new RangeError( |
| 2841 | + `Decoded output exceeded ${maxTotalBytes} bytes`, |
| 2842 | + ); |
| 2843 | + callback(err); |
| 2844 | + return; |
| 2845 | + } |
| 2846 | + callback(null, chunk); |
| 2847 | + }, |
| 2848 | + }); |
| 2849 | +} |
| 2850 | + |
| 2851 | +await pipeline( |
| 2852 | + source, |
| 2853 | + createGunzip(), |
| 2854 | + createDecodedByteLimit(64 * 1024), |
| 2855 | + destination, |
| 2856 | +); |
| 2857 | +``` |
| 2858 | + |
| 2859 | +```cjs |
| 2860 | +const { Transform } = require('node:stream'); |
| 2861 | +const { pipeline } = require('node:stream/promises'); |
| 2862 | +const { createGunzip } = require('node:zlib'); |
| 2863 | + |
| 2864 | +function createDecodedByteLimit(maxTotalBytes) { |
| 2865 | + let total = 0; |
| 2866 | + return new Transform({ |
| 2867 | + transform(chunk, _encoding, callback) { |
| 2868 | + total += chunk.length; |
| 2869 | + if (total > maxTotalBytes) { |
| 2870 | + const err = new RangeError( |
| 2871 | + `Decoded output exceeded ${maxTotalBytes} bytes`, |
| 2872 | + ); |
| 2873 | + callback(err); |
| 2874 | + return; |
| 2875 | + } |
| 2876 | + callback(null, chunk); |
| 2877 | + }, |
| 2878 | + }); |
| 2879 | +} |
| 2880 | + |
| 2881 | +pipeline( |
| 2882 | + source, |
| 2883 | + createGunzip(), |
| 2884 | + createDecodedByteLimit(64 * 1024), |
| 2885 | + destination, |
| 2886 | +).catch((err) => { |
| 2887 | + console.error('An error occurred:', err); |
| 2888 | + process.exitCode = 1; |
| 2889 | +}); |
| 2890 | +``` |
| 2891 | + |
2814 | 2892 | ### `zlib.brotliCompress(buffer[, options], callback)` |
2815 | 2893 |
|
2816 | 2894 | <!-- YAML |
|