nxtedition/node-http2-proxy

onReq with finalhandler = Error

Muhaah opened this issue · 5 comments

Why this code is not working?

HttpProxy2.web(req, source.http.res, {
	hostname: domainTarget.target.split("/")[0],
	port: domainTarget.port,

	onRes: function (req: Http2.Http2ServerRequest, res: Http2.Http2ServerResponse, proxyRes: Http.ServerResponse, callback: ()=>any) {
		callback();
	}
}, function (err: Error, req: Http.IncomingMessage | Http2.Http2ServerRequest, res: Http.ServerResponse | Http2.Http2ServerResponse){
	if(err){
		FinalHandler(req, res)(err);
	}
});

Exception:

TypeError: Cannot read property 'Symbol(req)' of undefined
    at onComplete (E:\***\***\node_modules\http2-proxy\index.js:165:18)
    at Object.onRes (E:\***\***\lib\Worker.ts:332:29)
    at ClientRequest.onProxyResponse (E:\***\***\node_modules\http2-proxy\index.js:269:24)
    at ClientRequest.emit (events.js:182:13)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:546:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:109:17)
    at Socket.socketOnData (_http_client.js:432:20)
    at Socket.emit (events.js:182:13)
    at addChunk (_stream_readable.js:283:12)
    at readableAddChunk (_stream_readable.js:264:11)
    at Socket.Readable.push (_stream_readable.js:219:10)
    at TCP.onread (net.js:639:20)
ronag commented

Fixed in 4.2.14.

Thanks for the fast response.
Now the error is gone, but the proxy sends no response to the Client (Pending) after calling the callback from onRes.

ronag commented

@Muhaah: don't override the onRes handler. Now that I look at it, the semantics are weird and should be changed.

ronag commented

If you want to use it you have to handle the proxyRes => res logic, i.e.

    res.statusCode = proxyRes.statusCode
    for (const [ key, value ] of Object.entries(proxyRes.headers)) {
      res.setHeader(key, value)
    }
    proxyRes.pipe(res)

You only need the callback if you want to finish before the usual life cycle of proxyRes and res.

That works, but I think its kind of confusing and ugly for my use case. I only want to set some Headers and with the old version 1.3.2 its easy to accomplish. A function to add Headers to the response would be nice.

By the way: The proxyRes from onRes is not a real Http.ServerResponse | Http2.Http2ServerResponse object .. proxyRes.getHeaders() failed.

And thanks for your support and help