Skip to content

Error fetching proxied request: unexpected end of file #658

Open
@jonaseicher

Description

@jonaseicher

yarn why http-proxy-middleware OR npm ls http-proxy-middleware output (mask private folder names with *****)

% npm ls http-proxy-middleware
***/my-node-proxy
└── [email protected]

Describe the bug (be clear and concise)

When receiving a 204 (with no body) proxyResponse, the endpoint using http-proxy-middleware returns a 200 response with body: Error fetching proxied request: unexpected end of file

The error seems to happen in responseInterceptor, maybe in the on('data) handler?:

function responseInterceptor(interceptor) {
return async function proxyRes(proxyRes, req, res) {
const originalProxyRes = proxyRes;
let buffer = Buffer.from('', 'utf8');
// decompress proxy response
const _proxyRes = decompress(proxyRes, proxyRes.headers['content-encoding']);
// concat data stream
_proxyRes.on('data', (chunk) => (buffer = Buffer.concat([buffer, chunk])));
[...]
_proxyRes.on('error', (error) => {
res.end(Error fetching proxied request: ${error.message});
});
};
}

Step-by-step reproduction instructions

Create an endpoint with a middleware with a responeInterceptor like this:

app.use('/abc/someurl', createProxyMiddleware({
        target: 'sometarget',
        changeOrigin: true,
        pathRewrite: {
            [`^/abc`]: '',
        },
        selfHandleResponse: true,
       onProxyRes: responseInterceptor(async (responseBuffer: Buffer) => {
              return responseBuffer;
           }),
}

and use it to call an endpoint that returns a 204 response (without a body)

Expected behavior (be clear and concise)

endpoint should return the 204 response

What http-proxy-middleware configuration are you using?

see step-by-step reproduction

What OS/version and node/version are you seeing the problem?

macOS 11.5.2 (20G95)
npm -v  
6.14.8
node -v
v14.15.1

Additional context (optional)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions