fix(websocket): capture upgrade request and pass wrapped socket to lifecycle hooks#207
fix(websocket): capture upgrade request and pass wrapped socket to lifecycle hooks#207nicoske wants to merge 1 commit into
Conversation
…fecycle hooks The adapter never installed an upgrade handler, so the HTTP upgrade request (path, query string, headers) was thrown away and there was no way for a gateway to read auth tokens or client metadata from the connection. handleConnection/handleDisconnect also received the raw uWS socket while message handlers get the wrapped UwsSocket, so data attached during handleConnection was invisible to handlers and the documented UwsSocket API (emit, join, data) crashed inside lifecycle hooks. Capture the upgrade request in an upgrade handler, expose it as socket.handshake (url, parsed query, headers, remote address), and hand the same wrapped socket instance to lifecycle hooks and message handlers. Matches the Lifecycle.md examples, which already type the hook parameter as UwsSocket and call client.emit() in handleConnection.
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
test/websocket/gateway-lifecycle.e2e.spec.ts (1)
100-107: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick winUse the shared ephemeral-port helper instead of a hardcoded port.
const port = 13390binds the test server to a fixed port, risking collisions with other e2e spec files or parallel CI workers.Based on learnings, uWS e2e tests should use the shared helper (
test/helpers/get-port.ts, e.g.startAppOnEphemeralPort()) for ephemeral port allocation rather than a fixed port orserver.address().port, to keep port-allocation logic consistent across e2e/spec files.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/websocket/gateway-lifecycle.e2e.spec.ts` around lines 100 - 107, The websocket lifecycle test currently uses the hardcoded port constant 13390; replace it with the shared ephemeral-port helper, such as startAppOnEphemeralPort from get-port.ts. Update the beforeEach setup around UwsAdapter.create and any dependent port usage to use the helper’s allocated port while preserving the existing test lifecycle behavior.Source: Learnings
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@test/websocket/gateway-lifecycle.e2e.spec.ts`:
- Around line 100-107: The websocket lifecycle test currently uses the hardcoded
port constant 13390; replace it with the shared ephemeral-port helper, such as
startAppOnEphemeralPort from get-port.ts. Update the beforeEach setup around
UwsAdapter.create and any dependent port usage to use the helper’s allocated
port while preserving the existing test lifecycle behavior.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 66d18584-6197-4763-bbdf-bc9d32292bf2
📒 Files selected for processing (4)
src/websocket/adapter/uws.adapter.tssrc/websocket/core/socket.tssrc/websocket/interfaces/uws-socket.interface.tstest/websocket/gateway-lifecycle.e2e.spec.ts
Fixes #205
Two changes in the WebSocket adapter:
upgradehandler now copies the upgrade request (path, query string, headers) before uWS invalidates it, and the adapter exposes it assocket.handshakewith the parsed query and the remote addressUwsSocketinstead of the raw uWS socket, soclient.dataset inhandleConnectionis the samedatathe message handlers see, and the documented API (emit,join, ...) actually works inside the hooks. The disconnect hook gets the same instance, grabbed before the socket maps are cleaned up.This is what the Lifecycle.md examples already describe, including the one that calls
client.emit()insidehandleConnection.The handshake is set only on the wrapped socket on purpose. The raw socket also flows into the Nest websockets controller callback, and putting connection state there as well would invite the two paths to drift apart again.
New e2e suite (
test/websocket/gateway-lifecycle.e2e.spec.ts) runs a real server with the built-in WebSocket client and covers: handshake url/query/headers/address,emitfromhandleConnection,datacontinuity between hook and handler, and instance identity across connect/message/disconnect.Lint, unit (975) and e2e (238) suites are green on Node 24. No existing test needed changes.
Summary by CodeRabbit
New Features
Bug Fixes