{"id":16386,"library":"http-proxy-response-rewrite","title":"HTTP Proxy Response Rewrite","description":"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.","status":"abandoned","version":"0.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/saskodh/http-proxy-response-rewrite","tags":["javascript","http-proxy","streaming","json"],"install":[{"cmd":"npm install http-proxy-response-rewrite","lang":"bash","label":"npm"},{"cmd":"yarn add http-proxy-response-rewrite","lang":"bash","label":"yarn"},{"cmd":"pnpm add http-proxy-response-rewrite","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"This library is an extension for http-proxy, specifically designed to modify its proxied responses.","package":"http-proxy","optional":false}],"imports":[{"note":"While the examples use `require('../')`, for an installed package, the correct CommonJS import is `require('http-proxy-response-rewrite')`. For modern Node.js environments supporting ESM, use the `import` statement.","wrong":"const modifyResponse = require('http-proxy-response-rewrite/index.js');","symbol":"modifyResponse","correct":"import modifyResponse from 'http-proxy-response-rewrite';"}],"quickstart":{"code":"const zlib = require('zlib');\nconst http = require('http');\nconst httpProxy = require('http-proxy');\nconst modifyResponse = require('http-proxy-response-rewrite');\n\n// Create a proxy server\nconst proxy = httpProxy.createProxyServer({\n    target: 'http://localhost:5001'\n});\n\n// Listen for the `proxyRes` event on `proxy` to modify responses\nproxy.on('proxyRes', function (proxyRes, req, res) {\n    modifyResponse(res, proxyRes.headers['content-encoding'], function (body) {\n        if (body) {\n            // Modify the response body (e.g., JSON manipulation)\n            let modifiedBody = JSON.parse(body);\n            modifiedBody.age = 2;\n            delete modifiedBody.version;\n            return JSON.stringify(modifiedBody);\n        }\n        return body;\n    });\n});\n\n// Create your server and then proxies the request\nconst server = http.createServer(function (req, res) {\n    proxy.web(req, res);\n}).listen(5000, () => console.log('Proxy server listening on port 5000'));\n\n// Create a target server with gzipped content\nconst targetServer = http.createServer(function (req, res) {\n    const gzip = zlib.createGzip();\n    const _write = res.write;\n    const _end = res.end;\n\n    gzip.on('data', function (buf) {\n        _write.call(res, buf);\n    });\n    gzip.on('end', function () {\n        _end.call(res);\n    });\n\n    res.write = function (data) {\n        gzip.write(data);\n    };\n    res.end = function () {\n        gzip.end();\n    };\n\n    res.writeHead(200, {'Content-Type': 'application/json', 'Content-Encoding': 'gzip'});\n    res.write(JSON.stringify({name: 'http-proxy-json', age: 1, version: '1.0.0'}));\n    res.end();\n}).listen(5001, () => console.log('Target server listening on port 5001'));","lang":"javascript","description":"This quickstart demonstrates setting up an HTTP proxy to intercept and modify a gzipped JSON response from a target server. It parses the body, changes an attribute, deletes another, and then re-serializes and re-compresses the response."},"warnings":[{"fix":"Consider using newer and actively maintained proxy middleware solutions like `http-proxy-middleware` which offer response interception and manipulation capabilities, often with better support for various compression types and modern JavaScript features.","message":"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.","severity":"deprecated","affected_versions":"0.0.1"},{"fix":"Ensure your upstream server's `Content-Encoding` header matches one of the supported formats. For unsupported formats, you would need to implement custom decompression logic or use an alternative library with broader support. Modern alternatives like `http-proxy-middleware` often include Brotli support.","message":"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.","severity":"gotcha","affected_versions":"0.0.1"},{"fix":"Always ensure the callback function passed to `modifyResponse` explicitly returns the modified body (as a string) or the original `body` if no modification is needed. Return `null` only if you intend to send an empty response body.","message":"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.","severity":"gotcha","affected_versions":"0.0.1"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Verify 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.","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.","error":"TypeError: Cannot read properties of undefined (reading 'on') or similar errors related to `gzip` or `deflate`."},{"fix":"Ensure 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.","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.","error":"Error: premature close or similar stream-related errors."}],"ecosystem":"npm"}