Skip to content

Conversation

@jannikkeye
Copy link

I am in a situation where I need to parse query params out of the url of a connected client and do some authentication checks. Thats currently impossible with typings provided since when I specify verifyClient as an option for the web socket server I have to specify a handler as well. I don't need a global handler though as I am handling the web socket connections on a route to have access to the request url. This PR makes handle optional to fix this issue. I guess I could also just give it a handler the does nothing as the handler will not be used by "per route web socket handlers" anyway.

Thoughts?

Checklist

  • run npm run test and npm run benchmark
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message and code follows Code of conduct

@mcollina
Copy link
Member

Can you please add a test for these in https://github.com/fastify/fastify-websocket/tree/master/test/types?

@jannikkeye
Copy link
Author

jannikkeye commented Jan 31, 2020

@mcollina I tried doing that but the plugin types weren't found, so wsPlugin was of type any for example and adding new types failed even if the signatures were technically correct.

The tests for the plugin itself have basically no effect as wsPlugin is any:

app.register(wsPlugin, {
  handle: () => null,
});

app.register(wsPlugin, {
  options: {
    url: "/ws"
  }
});

No types are checked.

@mcollina
Copy link
Member

Can you help?

@Ethan-Arrowood
Copy link
Member

@jannikkeye would a per route handler work better for you here? Maybe I’m not understanding your issue completely; if so can you provide some source code and share which parts are not getting typed / the types are breaking on. Thank you!

@jannikkeye
Copy link
Author

@Ethan-Arrowood from my understanding a per route handler doesn't suffice since I have to verify incoming socket connections. And as I understand I can only do this by passing verifyClient via the options to the ws web socket server when the plugin is registered like so:

server.register(fastifyWebSocket, {
    options: {
        verifyClient: (info, next) => {
            // do something
        },
    },
});

this won't work since handler is a required property of the options object passed to the plugin.

server.register(fastifyWebSocket, {
    handle: () => null,
    options: {
        verifyClient: (info, next) => {
            // do something
        },
    },
});

this works. I cannot use the global handler though as I need access to the request as that was the only working way of getting the url of the connection and thus the query params passed from the client. At least the only way that I found.

server.get("/ws", {websocket: true}, (connection, request) => {
        const {query} = parse(request.url as string);
        const parsedQuery = new URLSearchParams(query as string);
        // Do stuff
    });

Also the request here as second parameter to the handler is incorrectly typed as url is an existing property on IcomingMessage but request is typed as FastifyRequest which it isn't entirely.

@jannikkeye
Copy link
Author

As I said above the WebSocketHandler is typed incorrectly. Currently request is of type FastifyRequest even though the README.md states its http.IncomingMessage.

It should be typed like this:

export type WebsocketHandler<
    HttpRequest = IncomingMessage,
    HttpResponse = ServerResponse,
    Query = DefaultQuery,
    Params = DefaultParams,
    Headers = DefaultHeaders,
    Body = DefaultBody
  > = (
    this: FastifyInstance<Server, HttpRequest, HttpResponse>,
    connection: websocketPlugin.SocketStream,
    request: HttpRequest,
    params?: { [key: string]: any }
  ) => void | Promise<any>;
}

Then I don't get the type error I mentioned in my response above.

@stale
Copy link

stale bot commented Oct 21, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Oct 21, 2020
@stale stale bot closed this Nov 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants