compression
semver
>=1.7.0 <2.0.0postconditions7functions2last verified2026-04-16coverage score100%Postconditions — what we check
- compression · filter-throwserrorWhenThe filter option function throws an error when called during a requestThrows
Error propagated to Express error handler via next(err)Required handlingIf providing a custom filter function, it MUST NOT throw — errors inside filter are not caught internally and will crash the request pipeline. Wrap custom filter logic in try-catch and return true/false safely.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[1] - compression · downstream-errorerrorWhenAn error occurs in the response stream during compressionThrows
Error emitted on the response streamRequired handlingCaller MUST attach an Express error handler (app.use with 4-argument function) to catch stream errors propagated by the middleware.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[1] - compression · below-thresholdinfoWhenResponse body size is below the configured threshold (default: 1kb)ReturnsResponse is sent uncompressed; no compression is appliedRequired handlingNo action required — use the returned value as needed.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[1]
- compression · not-compressibleinfoWhenContent-Type is not compressible (per compressible module) and no custom filter is setReturnsResponse is passed through uncompressedRequired handlingNo action required — use the returned value as needed.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[1]
- compression · compression-appliedinfoWhenResponse is compressible, above threshold, and client supports compressionReturnsResponse body is compressed (gzip or deflate) and Content-Encoding header is setRequired handlingNo action required — use the returned value as needed.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[1]
- compression · zlib-stream-unhandled-errorerrorWhenThe internal zlib compression stream emits an 'error' event (e.g., memory allocation failure, invalid zlib parameters, or a rare compression fault)Throws
Uncaught exception — the zlib stream error is emitted with no .on('error') listener attached by compression middleware, causing the Node.js process to throw an uncaught exception (or unhandledRejection) and potentially crash.Required handlingCallers using compression() middleware SHOULD attach an Express error handler (app.use with 4-argument signature) and/or a global process.on('uncaughtException') handler. The middleware does not protect against internal zlib stream errors. For production use, a process supervisor (pm2, systemd) must restart the process after such a crash.costhighin prodimmediate exceptionusers seeservice unavailablevisibilityvisible - compression · sse-missing-flusherrorWhenServer-Sent Events (SSE) or any streaming response uses compression() middleware and writes data with res.write() but does NOT call res.flush() after each writeReturnsCompressed data is buffered in the zlib stream and NOT immediately delivered to the client. SSE clients receive no events (or very delayed batched events) until the zlib buffer fills.Required handlingAny SSE or streaming endpoint using compression() MUST call res.flush() after each res.write() call to force zlib to flush its buffer and deliver the compressed chunk immediately. Alternative: set Cache-Control: no-transform to bypass compression for SSE endpoints.costmediumin proddelayed failureusers seedegraded performancevisibilitysilentSources[1]
Sources
Every postcondition cites at least one of these. Numbered to match the footnotes above.
Need a different package?
Request a profile