Registry / http-networking / http-compression

http-compression

JSON →
library1.1.3jsnpmunverified

http-compression is a lightweight Node.js library designed to add Gzip and Brotli compression capabilities to HTTP servers. Currently at stable version 1.1.3, it primarily functions as Express-style middleware but can also be integrated directly with Node.js's `http.createServer` primitives. The project maintains a steady, albeit infrequent, release cadence, with recent updates focused on dependency maintenance and minor feature enhancements. Key differentiators include its extremely small footprint (< 1kB) with no external dependencies, automatic detection of the best compression encoding (Gzip or Brotli) based on the `Accept-Encoding` header, and configurable compression levels, response size thresholds, and MIME type filtering. It requires Node.js version 18 or greater.

http-networkingweb-framework
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

The package exports its main factory function as the default export. This function is called with options to create the middleware.

import compression from 'http-compression';

For CommonJS environments, the factory function is directly exported via `module.exports`.

const compression = require('http-compression');

Demonstrates how to integrate `http-compression` with a native Node.js HTTP server, applying custom compression settings for responses larger than a specified threshold.

import compression from 'http-compression'; import { createServer } from 'http'; const compressMiddleware = compression({ threshold: 500, // Compress responses larger than 500 bytes level: { brotli: 5, gzip: 5 }, // Custom compression levels mimes: /text|json/i // Only compress text and JSON }); const server = createServer((req, res) => { compressMiddleware(req, res, () => { // Simulate a large text response res.setHeader('Content-Type', 'text/plain'); res.end('hello world!'.repeat(100)); // Make sure content is > threshold }); }); server.listen(3000, () => { console.log('> Server listening at http://localhost:3000'); console.log('> Try fetching a large text response and observe Content-Encoding header.'); });
Debug
Known footguns
breakingThe `level` option for compression configuration was changed from accepting a single number to requiring an object containing `brotli` and `gzip` level properties.
gotchaThe package explicitly requires Node.js version 18 or higher. Running on older Node.js versions will result in an error or unexpected behavior.
gotchaResponses below the configured `threshold` (defaulting to 1024 bytes) will not be compressed. This is to avoid diminishing returns for very small payloads.
gotchaCompression is only applied to response `Content-Type` headers that match the `mimes` regular expression (default: `/text|javascript|\/json|xml/i`). Binary content is typically excluded.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
0 hits · last 30 days

No traffic data recorded yet.

Resources