Binary data

zlib maxOutputLength and byte-counting transforms

Binary delivery must constrain resource use and preserve access boundaries. An upload or compressed payload can be small on arrival but require substantially more memory, storage or processing when decoded. Enforce independent limits on received and expanded bytes. Count actual bytes read rather than trusting Content-Length or a MIME declaration. Set request deadlines and admission limits, and stop processing once a bound is exceeded. A synchronous decompression helper can use maxOutputLength for a small in-memory fixture; a streaming decoder needs an explicit counting transform after decompression. For uploads, create server-owned storage identifiers and keep data outside public serving paths until reviewed. Use maintained multipart parsers with configured part, field and file limits rather than writing your own general parser. File extensions and MIME headers are hints, not content validation. Use temporary storage with guaranteed cleanup and quota enforcement; never execute uploaded files. Do not limit only compressed input or trust client-supplied filenames. A streaming pipeline without an expanded-size bound still risks excessive output. Avoid extracting archives from unknown sources in beginner labs. Sources: https://nodejs.org/download/release/v22.16.0/docs/api/zlib.html, https://nodejs.org/download/release/v22.16.0/docs/api/stream.html.

Formal Syntax & Structure

nodejs syntaxnodejs
gunzipSync(buffer, { maxOutputLength }); new Transform({ transform })

Working Example

Example Snippetnodejs
import {gzipSync,gunzipSync} from 'node:zlib';const packed=gzipSync(Buffer.from('A'.repeat(32)));console.log(gunzipSync(packed,{maxOutputLength:32}).length);try {gunzipSync(packed,{maxOutputLength:16});console.log('unexpected');}catch(error){console.log(error.code==='ERR_BUFFER_TOO_LARGE'?'limit rejected':error.code);}
Node.js Reference