-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Enable on headers handler to reject connection modifying headers param #1966
Conversation
The |
What I don't like in the recommended way is that I as a user should be familiar enough with the library so he/she knows weather to call |
This is documented and it is the recommended way to reject connections as it gives the user total freedom. Calling Also, as written above, the Sorry but I'm not ok with this patch. Abusing the |
If I'm understanding the code correctly, I can't omit the callback of I'm not huge fan of my patch also but it looks way better than the proposed way to handle client filtration. As Probably you should remove the stigma from Most simple way, probably, would be to provide a default callback for P.S. I can propose a PR for this approach if you like it better? |
You can't omit the callback but you don't need import WebSocket from 'ws';
import { createServer } from 'http';
const server = createServer();
const wss = new WebSocketServer({ noServer: true });
server.on('upgrade', function upgrade(request, socket, head) {
// This function is not defined on purpose. Implement it with your own logic.
authenticate(request, (err, client) => {
if (err || !client) {
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
socket.destroy();
return;
}
wss.handleUpgrade(request, socket, head, function done(ws) {
// You are done. `ws` is your `WebSocket` instance, you can do
// whatever you want with it.
});
});
});
server.listen(8080);
The |
Documenting a way to break the documented workflow of the library is nonsense. Imagine I've build a websocket server according to the documentation using I'm pretty sure everybody will prefer to maintain only code related to his/her project and emitting the A robust library should strive to keep a stable interface keeping the user code agnostic to library's inner workings. This will allow confident version upgrades for the users and freedom to expand the library with less worries that the user code could be broken from some needed change.
The most important question here: If I create a PR introducing server.handleUpgradePostAuth() that will allow proper, according to me, way to handle client connection filtration, are you willing to consider accepting it? |
When using
I disagree. It is used by a lot projects.
If I understand correctly Thank you anyway. |
Take a look here again #377 and evaluate how user frendly and easy to use is your library. Allowing small, backward compatible and consistent changes that will benefit your users will only help your project. At least allowing passing no callback to |
It seems that overall users are happy with the suggested solution. Some advantages over the
As written in #377 (comment) Again, you might not like the API and that's perfectly fine. We can't make everyone happy. Thank you for sharing your opinion. |
I Don't see how default callback will break anybody's code but mandating copy-paste a snippet from documentation to maintain default behaviour is over the red line for me |
If the upgrade is successful and the `callback` argument is not provided, then emit the `'connection'` event. Refs: #1966
There is no default behavior, just two different modes. If users use |
I personally find this https://en.wikipedia.org/wiki/Principle_of_least_astonishment very important. This leads to easy code refactoring and ease of use. If there is a event called when a connection is established it should be called when connection is established no matter how the connection is established. And this should happen no matter what my code does as long as the library does not crash. But this is just my expectations and all my code tries to be as predictable as possible but this is not my library and you're free to code it as you like |
A common problem is authenticating and rejecting websocket clients. The
headers
handler of the server is in unique position to easily reject the client after expecting all the available information from the request. This minimal patch detects the attempt to close the connection and does so in graceful manner. See the added test for easy client rejection