Conversation
- just trying this out
- add a verifyClient handler in options
- specify initial subprotocol of XAPWS1 - set sec-websocket-protocol headers on response if they match - verify sec-websocket-protocol on request
- check the sub protocol header before upgrading connection
| * @param protocols set of subprotocols from header | ||
| * @returns subprotocol accepted or false | ||
| */ | ||
| handleProtocols: (protocols: Set<string>) => { |
There was a problem hiding this comment.
i'd replace handleProtocols with:
verifyClient: (info: { origin: string; req: IncomingMessage; secure: boolean }) => {
return (
(info.req.headers['sec-websocket-protocol'] &&
validSubprotocols.find((x) => info.req.headers['sec-websocket-protocol'] === x)) ||
false
)
}If there is no subprotocol this will not be executed. See: websockets/ws#1552 (comment)
There was a problem hiding this comment.
In the notes they recommended not using this hook. I wasn't sure why
There was a problem hiding this comment.
Handle protocols only sets the headers as per the spec. It doesn't seem to care about verification
There was a problem hiding this comment.
I tried verify client in an earlier version of patch but the docs pointed at prevalidatoon for some reaspn
There was a problem hiding this comment.
we can have prevalidation, so we need to use app.route and add prehandler on it. We cannot use the shorthand route definition like app.get
| }) | ||
|
|
||
| app.register(async (fastify: FastifyInstance) => { | ||
| fastify.addHook('preValidation', async (req: FastifyRequest, reply: FastifyReply) => { |
There was a problem hiding this comment.
With the comment on handleProtocols we can get rid of prevalidation hook.
also you don't need to await the reply.send() just return it.
| @@ -1,4 +1,4 @@ | |||
| import fastify from 'fastify' | |||
| import fastify, { FastifyInstance } from 'fastify' | |||
There was a problem hiding this comment.
| import fastify, { FastifyInstance } from 'fastify' | |
| import fastify from 'fastify' | |
| import type { FastifyInstance } from 'fastify' |
This seems like an interesting idea, specify a websocket sub protocol
https://websockets.spec.whatwg.org/#ref-for-dom-websocket-websocket%E2%91%A0
I'm not sure how to do this in fastify with a websocket connection, so add the check on the message for now.
Description