Skip to content

Commit 0ffcd35

Browse files
sboettchaduh95
authored andcommitted
doc: clarify cumulative output limits for zlib streams
Clarify in `doc/api/zlib.md` that `maxOutputLength` bounds single-call convenience methods (`zlib.gunzip()`, `zlib.brotliDecompress()`, `zlib.zstdDecompress()`, and `*Sync` variants) rather than cumulative decoded output across `create*Decompress()` streams, and add a `stream/promises` `pipeline()` example enforcing a cumulative byte limit. Signed-off-by: Sophia Boettcher <93076268+sboettch@users.noreply.github.com> PR-URL: #67373 Refs: https://github.com/nodejs/node/blob/main/doc/api/zlib.md Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
1 parent 4ce81ec commit 0ffcd35

1 file changed

Lines changed: 81 additions & 3 deletions

File tree

‎doc/api/zlib.md‎

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,10 @@ ignored by the decompression classes.
844844
empty dictionary by default)
845845
* `info` {boolean} (If `true`, returns an object with `buffer` and `engine`.)
846846
* `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`][]
848851
* `rejectGarbageAfterEnd` {boolean} If `true`, decompression fails when
849852
trailing input is detected after the end of the compressed stream. This
850853
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.
879882
* `chunkSize` {integer} **Default:** `16 * 1024`
880883
* `params` {Object} Key-value object containing indexed [Brotli parameters][].
881884
* `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`][]
883889
* `info` {boolean} If `true`, returns an object with `buffer` and `engine`. **Default:** `false`
884890
* `rejectGarbageAfterEnd` {boolean} If `true`, decompression fails when
885891
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.
22942300
must be a non-negative safe integer and must match the input size when
22952301
compression finishes. Only applicable to Zstd compressors.
22962302
* `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`][]
22982307
* `info` {boolean} If `true`, returns an object with `buffer` and `engine`. **Default:** `false`
22992308
* `dictionary` {Buffer|TypedArray|DataView|ArrayBuffer} Optional dictionary used
23002309
to improve compression efficiency when compressing or decompressing data that
@@ -2811,6 +2820,75 @@ with `callback(error, result)`.
28112820
Every method has a `*Sync` counterpart, which accept the same arguments, but
28122821
without a callback.
28132822

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+
28142892
### `zlib.brotliCompress(buffer[, options], callback)`
28152893

28162894
<!-- YAML

0 commit comments

Comments
 (0)