HTTP Proxy Response Rewrite
http-proxy-response-rewrite is a Node.js library designed to simplify the modification of response bodies when using `http-proxy`. Its core functionality involves transparently handling common compression formats like `gzip` and `deflate` to decompress the response body, allowing developers to manipulate the plain text or JSON content, and then re-compress it before sending it to the client. The library is currently at version 0.0.1 and has not been updated in approximately 8 years, indicating it is no longer actively maintained. Its primary differentiator was providing a streamlined way to intercept and modify compressed `http-proxy` responses, a task that would otherwise require manual zlib handling. Due to its abandoned status, developers should consider more modern and actively maintained alternatives for proxy response manipulation.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'on') or similar errors related to `gzip` or `deflate`.
cause This error often occurs when the `Content-Encoding` header from the proxied response is not 'gzip' or 'deflate', and the `modifyResponse` function tries to initialize a zlib stream for an unsupported or missing encoding.fixVerify that the `proxyRes.headers['content-encoding']` is correctly set by the target server and is either 'gzip' or 'deflate' or completely absent (for uncompressed). If the target sends an unsupported encoding, the library will fail. -
Error: premature close or similar stream-related errors.
cause These errors can arise if the response stream is closed prematurely or handled incorrectly after modification, especially if the `modifyResponse` callback takes too long or encounters an error before the stream operations are complete.fixEnsure that your modification logic within the `modifyResponse` callback is robust and handles all potential data states (e.g., empty body, invalid JSON). Also, confirm that `http-proxy` and Node.js `http` stream handling are correctly configured and not being interfered with by other middleware.
Warnings
- deprecated The package `http-proxy-response-rewrite` is effectively abandoned. It has not received updates in approximately 8 years and is stuck at version 0.0.1. It is not recommended for new projects and existing projects should seek alternatives for security and maintenance reasons.
- gotcha This library only supports `gzip`, `deflate`, and uncompressed response bodies for modification. If the upstream server uses other compression formats (e.g., Brotli), this library will not be able to decompress and modify the response, potentially leading to corrupt or unhandled data.
- gotcha The `modifyResponse` callback expects a string or null return. If the callback function returns an `undefined` value, the `http-proxy` might send an empty or incomplete response to the client, leading to unexpected behavior in client-side applications.
Install
-
npm install http-proxy-response-rewrite -
yarn add http-proxy-response-rewrite -
pnpm add http-proxy-response-rewrite
Imports
- modifyResponse
const modifyResponse = require('http-proxy-response-rewrite/index.js');import modifyResponse from 'http-proxy-response-rewrite';
Quickstart
const zlib = require('zlib');
const http = require('http');
const httpProxy = require('http-proxy');
const modifyResponse = require('http-proxy-response-rewrite');
// Create a proxy server
const proxy = httpProxy.createProxyServer({
target: 'http://localhost:5001'
});
// Listen for the `proxyRes` event on `proxy` to modify responses
proxy.on('proxyRes', function (proxyRes, req, res) {
modifyResponse(res, proxyRes.headers['content-encoding'], function (body) {
if (body) {
// Modify the response body (e.g., JSON manipulation)
let modifiedBody = JSON.parse(body);
modifiedBody.age = 2;
delete modifiedBody.version;
return JSON.stringify(modifiedBody);
}
return body;
});
});
// Create your server and then proxies the request
const server = http.createServer(function (req, res) {
proxy.web(req, res);
}).listen(5000, () => console.log('Proxy server listening on port 5000'));
// Create a target server with gzipped content
const targetServer = http.createServer(function (req, res) {
const gzip = zlib.createGzip();
const _write = res.write;
const _end = res.end;
gzip.on('data', function (buf) {
_write.call(res, buf);
});
gzip.on('end', function () {
_end.call(res);
});
res.write = function (data) {
gzip.write(data);
};
res.end = function () {
gzip.end();
};
res.writeHead(200, {'Content-Type': 'application/json', 'Content-Encoding': 'gzip'});
res.write(JSON.stringify({name: 'http-proxy-json', age: 1, version: '1.0.0'}));
res.end();
}).listen(5001, () => console.log('Target server listening on port 5001'));