Bounded streaming and processing
Keeps file processing within explicit memory, disk, time and concurrency limits while allowing cancellation and safe cleanup.
streaming-resource-limits
See how it helpsWhen is it useful?
Use it when uploads, archives or media conversions can exhaust a worker despite appearing small at input.
What does it help you do?
- Streams large files with backpressure instead of full buffering.
- Limits decompressed size, execution time and worker concurrency.
- Isolates subprocess arguments and cleans owned scratch files.
Try asking this
Process an oversized archive safely when extraction exceeds its resource budget. Use a small archive that expands beyond the configured limit. Stop extraction with a clear failure and remove only the operation's owned temporary files.
Detailed documentation
Read the skill’s original instructions, examples and checks in their authored language.
Show less
Streaming & Resource Limits
Loading a 500 MB upload or video into an in-memory Buffer / byte[] (fs.readFile, res.arrayBuffer(), unbounded multipart buffering) crashes or starves the server as soon as three users upload concurrently. Handle file I/O and media processing with bounded streams, explicit backpressure and strict resource limits.
1. End-to-End Streaming & Backpressure
- Never buffer whole files in memory when streaming is possible:
- Pipe request/storage streams directly using backpressure-aware primitives (
stream.pipelinein Node.js, WebReadableStream.pipeTo(), Goio.Copy, Python chunked generators, PHP stream resources). - Compute cryptographic hashes (
SHA-256/MD5) and inspect initial magic bytes incrementally on the stream via transform streams rather than reading the file twice or buffering it into RAM.
- Pipe request/storage streams directly using backpressure-aware primitives (
- Direct-to-Storage Bypass:
- For files larger than a few megabytes when cloud object storage is available, prefer direct client-to-object-storage uploads (presigned URLs / multipart) so application server memory, CPU and event loops are not tied up proxying gigabytes of binary data.
- Range Requests (
HTTP 206 Partial Content):- Support
Accept-Ranges: bytesandRange: bytes=start-endon large downloads, audio/video playback and PDF previews so clients can seek and resume downloads without fetching the entire file.
- Support
2. Bounding Processing Memory, CPU, Time & Output Size
Media decoders (libvips/sharp, ImageMagick, ffmpeg, Poppler/pdfium, archive extractors) process complex binary formats that can trigger extreme memory spikes or CPU hangs:
- Pixel / Dimension Limits (Decompression Bomb Defense):
- A 400 KB PNG can have dimensions of
60000 x 60000pixels (3.6 gigapixels), which expands to 14.4 GB of uncompressed RGBA memory when decoded! - Inspect image dimensions from the header first and set hard pixel limits (e.g.,
limitInputPixelsinsharp/maxResolutionin decoders) before decoding.
- A 400 KB PNG can have dimensions of
- Execution Timeouts & Abort Signals:
- Wrap every external CLI (
ffmpeg,ffprobe,clamscan) and worker step in a hard timeout (AbortSignal.timeout(ms)/ process kill timer) that terminates hung child processes cleanly.
- Wrap every external CLI (
- Bounded Concurrency & Worker Isolation:
- Run CPU- and memory-intensive media transformations in background job queues with bounded worker concurrency (
concurrency: 2..Nmatched to container RAM/CPU), never synchronously inside the HTTP request thread.
- Run CPU- and memory-intensive media transformations in background job queues with bounded worker concurrency (
- Safe Subprocess Invocation:
- Spawn external tools (
ffmpeg,ffprobe,gs) using argument arrays (execFile/spawn(bin, args, { shell: false })) — never interpolate filenames or metadata into a shell string! - When passing paths to
ffmpegor CLI tools, prefix local relative paths (./input.mp4or absolute paths) so a filename starting with-orhttp://is never interpreted as a CLI flag or remote protocol.
- Spawn external tools (
3. Ephemeral Scratch Disk Hygiene
- When a tool requires a local file on disk (such as
ffmpegMP4 muxing or virus scanning), write to an isolated temporary directory (mkdtemp), enforce a maximum disk write quota, and guarantee deletion in atry / finallyblock even when the job throws or times out.
Included files 1
SKILL.md
