feat: add handleUpgradeRequest route option #342
+245
−4
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.
At gadget we make extensive use of
@fastify/websocket
. One use case is a websocket proxy that connects vite hmr clients in the browser to a developers serverless development environment. The vite client has a ping protocol with the following semantics:We need a way to handle upgrade events in our proxy layer and not upgrade connections until we know that the upstream vite will also upgrade the connection. To do this I have introduced a route level option
handleUpgradeRequest
that allows for a route by route customization of how the upgrade event should be handled.handleUpgradeRequest
is a function that takes the incomingFastifyRequest
, the source socket and the head buffer; to continue through to the normalwsHandler
the use should return a promise that resolves to a connected websocket; to reject the connection the user can either throw an error (optionally with astatusCode
) or write directly to/teardown the source socketChecklist
npm run test && npm run benchmark --if-present
and the Code of conduct