Memory leak is found in function responseInterceptor of file handlers/response-interceptor.ts:
// concat data stream
_proxyRes.on('data', (chunk) => (buffer = Buffer.concat([buffer, chunk])));
The buffer cannot be released especially when the response data is very large.
Step-by-step reproduction instructions
1. Prepare 2 applications: proxy server, real server.
1. Set options in proxy server
target: {
protocol: 'http',
host: '127.0.0.1',
port: 8080,
},
changeOrigin: true,
router: {
url: 'http://127.0.0.1:8080'
},
agent: new https.Agent({ keepAlive: true }),
ws: false,
selfHandleResponse: true,
onProxyRes: responseInterceptor((responseBuffer, proxyRes, req, res) => {
return responseBuffer;
}
2. Send a request, and the real server respond a large response, for example, 38MB.
3. Repeat the step 2.
4. OOM will be triggered.
Expected behavior (be clear and concise)
No memory leak.
Working code:
let buffers: Buffer[] = [];
let bufferLength = 0;
_proxyRes.on('data', (chunk) => {
bufferLength += chunk.length;
buffers.push(chunk);
});
_proxyRes.on('end', async () => {
// ... other code
let buffer = Buffer.from('', 'utf8');
if (bufferLength > 0) {
buffer = Buffer.concat(buffers, bufferLength);
}
const interceptedBuffer = Buffer.from(await interceptor(buffer, originalProxyRes, req, res));
// ... other code
// clear buffers in the end.
buffers = [];
bufferLength = 0;
});
_proxyRes.on('error', (error) => {
// clear buffers when error.
buffers = [];
bufferLength = 0;
res.end(`Error fetching proxied request: ${error.message}`);
});
How is http-proxy-middleware used in your project?
Checks
http-proxy-middleware
.Describe the bug (be clear and concise)
Memory leak is found in function
responseInterceptor
of filehandlers/response-interceptor.ts
: // concat data stream _proxyRes.on('data', (chunk) => (buffer = Buffer.concat([buffer, chunk])));The buffer cannot be released especially when the response data is very large.
Step-by-step reproduction instructions
Expected behavior (be clear and concise)
No memory leak. Working code:
How is http-proxy-middleware used in your project?
What http-proxy-middleware configuration are you using?
What OS/version and node/version are you seeing the problem?
Additional context (optional)
No response