HTTP Stream Decompression

2.1.0 · maintenance · verified Tue Apr 21

Inflation is a focused utility that automatically decompresses HTTP streams using Node.js's built-in zlib module. It supports common compression algorithms like gzip, deflate, and brotli. Currently at version 2.1.0, the package demonstrates a stable, low-maintenance release cadence, with its last publish two years ago. It's widely adopted, with over 1.5 million dependents, indicating its reliability for its specific function. Its key differentiator is its simplicity and direct integration with Node.js streams for handling `Content-Encoding` headers, aiming to simplify the process of consuming compressed HTTP request bodies or responses without needing to manually detect the compression type. While robust for its core purpose, users should be aware of specific Node.js version requirements, especially for Brotli support.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart sets up a basic HTTP server that listens for incoming requests. It uses `inflation` to automatically decompress the request body based on the `Content-Encoding` header, then reads the raw content and logs it to the console. This demonstrates how to integrate `inflation` into an HTTP request processing pipeline for handling compressed data. Note that `raw-body` is used here for convenience in reading the stream, but is not a dependency of `inflation` itself.

const inflate = require('inflation');
const raw = require('raw-body');
const http = require('http');

// Minimal HTTP server demonstrating stream decompression
http.createServer(function (req, res) {
  // Assume 'raw-body' is installed for this example: npm i raw-body
  // For simplicity, we are not handling response ending or error cases robustly.
  // In a real application, proper error handling and response management are crucial.
  raw(inflate(req), 'utf-8', function (err, string) {
    if (err) {
      console.error('Error reading inflated body:', err);
      res.statusCode = 500;
      res.end('Error processing request body');
      return;
    }
    console.log('Received inflated body:', string);
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end(`Processed body length: ${string.length}`);
  });
}).listen(3000, () => {
  console.log('Server listening on http://localhost:3000');
  console.log('Send a compressed POST request to test (e.g., with Content-Encoding: gzip)');
});

view raw JSON →