HTTP Response Decompression Utility
Decompress-response is a focused utility package designed to automatically decompress HTTP `IncomingMessage` streams based on their `Content-Encoding` header. It supports common compression algorithms including `gzip`, `deflate`, `brotli`, and `zstd` (added in v10.0.0). Currently at stable version 10.0.0, released in late 2023, the package follows a release cadence tied to Node.js LTS updates, with major versions often introducing increased Node.js engine requirements. A key differentiator is its straightforward, single-function API that transforms an `http.IncomingMessage` stream into a decompressed stream, making it easy to integrate. It is notably used internally by popular HTTP client libraries like `got` and operates as a pure ECMAScript Module (ESM) package since version 8, requiring modern JavaScript module syntax for usage.
Common errors
-
ERR_REQUIRE_ESM: Must use import to load ES Module: .../node_modules/decompress-response/index.js
cause Attempting to `require()` `decompress-response` in a CommonJS context after upgrading to version 8.0.0 or newer, which is a pure ESM package.fixConvert your consuming file or project to use ECMAScript Modules (ESM) syntax (`import decompressResponse from 'decompress-response';`) and ensure your `package.json` includes `"type": "module"` or your file uses a `.mjs` extension. -
TypeError: decompressResponse is not a function
cause Incorrect import statement (e.g., `import { decompressResponse } from 'decompress-response';` instead of default import) or a TypeScript configuration mismatch, especially when transitioning between versions.fixEnsure you are using the correct default import: `import decompressResponse from 'decompress-response';`. For older TypeScript versions (5.x-7.x), check if the `import decompressResponse = require('decompress-response');` syntax is required by your specific setup. -
Error: stream.pipeline is not a function
cause Using an older Node.js version (pre-Node.js 10) with `decompress-response` versions that rely on `stream.pipeline` (v5.0.0 and later).fixUpgrade your Node.js runtime to version 10 or newer. For `decompress-response` v10.x, Node.js 20 or newer is required.
Warnings
- breaking Version 10.0.0 requires Node.js 20 or newer. Users on older Node.js versions must remain on `decompress-response` v9.x or earlier.
- breaking Version 9.0.0 requires Node.js 18 or newer. This continues the trend of aligning major releases with Node.js LTS versions.
- breaking Version 8.0.0 converted the package to pure ECMAScript Module (ESM). CommonJS `require()` statements are no longer supported, leading to `ERR_REQUIRE_ESM` errors.
- breaking Version 8.0.0 increased the minimum Node.js requirement to 12.20.0.
- breaking Version 7.0.0 removed the `content-encoding` header from the response. While primarily a TypeScript type change, if your application relied on inspecting this header after `decompressResponse` was applied, it would break.
- breaking Version 5.0.0 introduced a breaking change for TypeScript users, requiring a change from `import decompressResponse from 'decompress-response';` to `import decompressResponse = require('decompress-response');` due to a CommonJS-only export for the TypeScript definition. This was subsequently reverted by the pure ESM transition in v8.0.0.
- gotcha In version 9.0.0, a fix was implemented to prevent `decompress-response` from incorrectly modifying the original response object. Prior versions might have unintended side effects if the original response was used elsewhere after being passed to the function.
Install
-
npm install decompress-response -
yarn add decompress-response -
pnpm add decompress-response
Imports
- decompressResponse
const decompressResponse = require('decompress-response');import decompressResponse from 'decompress-response';
- http
import http from 'node:http';
- zlib
import zlib from 'node:zlib';
Quickstart
import http from 'node:http';
import zlib from 'node:zlib';
import decompressResponse from 'decompress-response';
// Create a simple HTTP server to simulate a compressed response
const server = http.createServer((request, serverResponse) => {
if (request.url === '/compressed-data') {
serverResponse.writeHead(200, {
'Content-Type': 'text/plain',
'Content-Encoding': 'gzip' // Indicate gzipped content
});
// Compress the response body
zlib.gzip('Hello from decompress-response! This is some example text to be decompressed.', (error, buffer) => {
if (error) {
console.error('Error gzipping:', error);
serverResponse.end('Error gzipping content');
return;
}
serverResponse.end(buffer);
});
} else {
serverResponse.end('Not found');
}
});
server.listen(3000, () => {
console.log('Server listening on http://localhost:3000');
// Client-side usage: Fetch and decompress the response
http.get('http://localhost:3000/compressed-data', response => {
console.log('Original response headers:', response.headers);
let decompressedResponse = decompressResponse(response);
let data = '';
decompressedResponse.on('data', chunk => {
data += chunk.toString();
});
decompressedResponse.on('end', () => {
console.log('Decompressed data:', data); // Should print 'Hello from decompress-response! ...'
server.close(); // Clean up the server after the request
});
decompressedResponse.on('error', error => {
console.error('Error during decompression:', error);
server.close();
});
}).on('error', error => {
console.error('HTTP GET request error:', error);
server.close();
});
});