- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 83
feat: add handleUpgradeRequest route option #342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| @mcollina any thoughts on this? | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR introduces a new handleUpgradeRequest route-level option to allow custom handling of WebSocket upgrade requests before the standard WebSocket handler is invoked. This enables use cases like implementing a WebSocket proxy that needs to validate upstream connections before upgrading.
- Adds handleUpgradeRequestfunction option that receives request, socket, and head buffer parameters
- Allows custom upgrade logic with error handling and status code customization
- Maintains backward compatibility with existing WebSocket routing behavior
Reviewed Changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 3 comments.
| File | Description | 
|---|---|
| index.js | Implements the core handleUpgradeRequestfunctionality with custom upgrade handling and error management | 
| test/base.test.js | Adds comprehensive tests covering successful upgrades, error handling, and custom status codes | 
| types/index.test-d.ts | Provides TypeScript type definitions and validation for the new upgrade handler | 
| README.md | Documents the new feature with usage examples and API details | 
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
b3a585c    to
    7489d07      
    Compare
  
    
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
handleUpgradeRequestthat allows for a route by route customization of how the upgrade event should be handled.handleUpgradeRequestis a function that takes the incomingFastifyRequest, the source socket and the head buffer; to continue through to the normalwsHandlerthe 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-presentand the Code of conduct