Repository navigation
fix: restore res.req for Node.js when createProxyHandler is set - #213
Merged
Merged
Conversation
Express sets `res.req` to the request it is given, which is the Proxy when `createProxyHandler` is set. Since Node.js 26.11.0 the server's shared 'finish' listener reads `res.req` and asserts that it is the request the server created (nodejs/node#65802), so every request that reaches the Express middleware throws ERR_INTERNAL_ASSERTION from the 'finish' event and the process exits. Put the original request back on `res.req` in a 'finish' listener that runs before the one Node.js adds. Express still sees the Proxy while it handles the request. Signed-off-by: Khagani Mamedov <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Since Node.js 26.11.0, when
createProxyHandleris set, any request that reaches the Express middleware crashes the process withERR_INTERNAL_ASSERTION. It's also why the Node 26 CI job fails onmainright now: the three tests that setcreateProxyHandlerfail on 26.13.0 (for example in #210).Express sets
res.reqto the request it's given, which here is the Proxy. nodejs/node#65802 (shipped in 26.11.0) replaced the per-requestfinishlistener with a shared one that readsres.reqand asserts it's the request the server created, so the assertion is thrown from thefinishevent. 26.10.0 is fine; 26.11.0, 26.13.0 and 26.13.1 crash.This puts the original request back on
res.reqin afinishlistener that runs before the one Node adds. While Express handles the request,res.reqis still the Proxy, so middleware and routes see no change.The new test checks that Express still sees the Proxy during the request and that
reply.raw.reqis the original request inonResponse. Without the fix it fails on Node 22 and 24 (it's still the Proxy) and on 26.13.0 (the assertion). With it,npm testpasses on 22, 24, 26.11.0, 26.13.0 and 26.13.1.POST request without form body workssometimes hits its 100 ms timeout when the machine is busy; I saw that on an unchangedmaintoo.Checklist
npm run test && npm run benchmark --if-presentand the Code of conduct