Binary delivery safeguards
Prerequisites: previous curriculum lessons on Node streams, HTTP, buffers and asynchronous errors. Local lab baseline: Node.js 22.16.0, Linux, ESM. Use a disposable directory. Static examples are not ByteMind interactive execution. Each example is independent; save the named files together and run node main.mjs. The author harness imposes a five-second process deadline.
How it works
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.
Reject an expanded payload over a tiny budget
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);}Expected output:
32limit rejectedThe fixture expands to just 32 bytes. The cap applies to decoded output, not merely compressed input.
Limit streamed bytes and discard a partial temporary file
import {Readable,Transform} from 'node:stream';import {pipeline} from 'node:stream/promises';import {mkdtemp,rm} from 'node:fs/promises';import {createWriteStream} from 'node:fs';import {tmpdir} from 'node:os';import {join} from 'node:path';const dir=await mkdtemp(join(tmpdir(),'bytemind-'));let size=0;try { const limiter=new Transform({transform(chunk,enc,cb){size+=chunk.length;if(size>5)return cb(new Error('size limit'));cb(null,chunk);}}); try {await pipeline(Readable.from([Buffer.from('abc'),Buffer.from('def')]),limiter,createWriteStream(join(dir,'payload'),{flags:'wx'}));} catch(error){console.log(error.message);}}finally{await rm(dir,{recursive:true,force:true});console.log('temporary directory removed');}Expected output:
size limittemporary directory removedThe trusted generated directory is removed even when the transform rejects the second chunk.
Common mistakes
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.
Practice
Change the allowed expanded size in the gzip example to match exactly the original length. Verify that equality is accepted and a one-byte-smaller limit fails.
Further reading
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Binary delivery safeguards — knowledge check
Apply the lesson to a fresh scenario and explain the resource or protocol boundary.
Which two sizes need separate limits for compressed uploads?
Why use server-generated storage identifiers?
What should happen after a pipeline rejects oversized input?