Conversation
WebSocketResponse exposes a subset of http.ServerResponse so that Express
middlewares can be applied to an upgrade request, but it was a plain
object. Any middleware that observes the response lifecycle — pino-http
and most other request loggers do, via res.on('close') — threw
"TypeError: res.on is not a function" and killed the upgrade.
Extend EventEmitter and emit "finish" then "close" once the handshake has
been written, so those middlewares run to completion instead of crashing.
statusCode is set to 101 so a logger reports the handshake rather than an
undefined status.
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.
Fixes #5072
The problem
WebSocketResponse(packages/engine.io/lib/server.ts) exists so that Express-style middlewares can be applied to an upgrade request. It documents itself as exposing "a subset of thehttp.ServerResponseinterface", but it was a plain class with onlysetHeader/getHeader/removeHeader/write/writeHead/end.Any middleware that observes the response lifecycle blows up on it.
pino-http— and most other request loggers — do exactly that:Since this happens inside
_applyMiddlewaresduringhandleUpgrade, it doesn't just skip the log — it throws out of the upgrade, so the WebSocket connection never establishes.The change
WebSocketResponse extends EventEmitter, sores.on(...)works like it does for a realServerResponse."finish"and"close"are emitted oncews.handleUpgradehas written the handshake, so a response logger actually completes rather than registering a listener that never fires. That is what the reporter was after — "I expected the upgrade request to be logged at upgrade time".statusCodeis set to101, otherwise a logger reports the handshake with anundefinedstatus. Happy to drop this if you'd rather keep the shim minimal.Scope — uWebSockets.js is not covered
userver.tshas its ownResponseWrapper, which has the same gap. I left it alone because its response lifecycle (polling vsres.upgrade()) is a separate question and I'd rather not guess at the right emit points. The new test therefore skips underEIO_WS_ENGINE=uws, the same wayshould end the request (polling)already does in that file. Say the word and I'll extend it in this PR or a follow-up.Verification
Ran the engine.io suite on a fork:
Uncaught TypeError: res.on is not a function— the exact error from the issuemain(baseline)One note for transparency: the first run of the fixed branch failed on
WebTransport › should close a connection that sends an invalid upgrade (bis)with a 2000 ms timeout. It passed on re-run and is green on the baseline too, so it looks flaky rather than related — but flagging it in case it's a known one.