micro-compress

raw JSON →
1.0.0 verified Sat Apr 25 auth: no javascript maintenance

Middleware to add gzip/brotli compression to HTTP microservices built with the Micro framework (from ZEIT/Now). Version 1.0.0 is stable and wraps the popular compression npm package, passing all options through. It is designed specifically for Micro's async request handler pattern, not Express or other frameworks. The package is minimal (no dependencies beyond compression and micro) and follows the same promise-based flow as Micro itself. It is released under MIT license and has not seen updates since 2017, indicating maintenance status.

error Error: Cannot find module 'micro-compress'
cause Forgot to run npm install or installed in wrong directory.
fix
Run npm install micro-compress in the project root.
error TypeError: compress is not a function
cause Using named import { compress } instead of default require.
fix
Use const compress = require('micro-compress');
error TypeError: Cannot read property 'writeHead' of undefined
cause Passed handler as first argument without wrapping correctly or missing micro peer dependency.
fix
Ensure micro is installed: npm install micro. Then wrap: compress(handler) or compress(options, handler).
gotcha micro-compress only works with the Micro framework, not Express or other HTTP servers.
fix Use compression middleware designed for your framework (e.g., compression for Express).
gotcha No ESM support; must use CommonJS require().
fix Use require('micro-compress') instead of import. If you need ESM, transpile or use dynamic import.
gotcha Options object is the first argument; if omitted, default compression options are used.
fix Pass options as first argument: compress({level: 9}, handler). Do not pass handler as first argument.
gotcha micro-compress does not support brotli compression natively; relies on compression package which may not include brotli in older versions.
fix If brotli needed, ensure compression@>=1.7.0 is installed and pass { brotli: { enabled: true } }. Check compression docs.
npm install micro-compress
yarn add micro-compress
pnpm add micro-compress

Wraps a Micro request handler with compression middleware. The handler remains async, and the response is gzip-compressed if accepted.

const { send, json } = require('micro');
const compress = require('micro-compress');
const handler = async (req, res) => {
  const body = await json(req);
  send(res, 200, body);
};
module.exports = compress(handler);