@nka212bg/backend-utils 0.1.41 → 0.1.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/socket.js +9 -7
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nka212bg/backend-utils",
3
3
  "author": "nka212bg",
4
- "version": "0.1.41",
4
+ "version": "0.1.42",
5
5
  "main": "index.js",
6
6
  "dependencies": {
7
7
  "@maxmind/geoip2-node": "^5.0.0",
package/socket.js CHANGED
@@ -8,15 +8,17 @@ const parseParams = (url) => {
8
8
  }
9
9
  };
10
10
 
11
- // options.authenticate (params, req) => boolean, rejects the connection with 401 during the handshake when falsy
12
11
  module.exports = (server, { authenticate } = {}) => {
13
- const socket = new WebSocket.Server({
14
- server,
15
- noServer: !server,
16
- verifyClient: authenticate && (({ req }) => !!authenticate(parseParams(req.url), req)),
17
- });
12
+ const socket = new WebSocket.Server({ server, noServer: !server });
18
13
  socket.on("connection", (ws, req) => {
19
- ws.params = parseParams(req.url);
14
+ const params = parseParams(req.url);
15
+
16
+ if (authenticate && !authenticate(params, req)) {
17
+ ws.params = {};
18
+ return ws.close(4001, "Unauthorized");
19
+ }
20
+
21
+ ws.params = params;
20
22
  const userId = ws.params.userId;
21
23
 
22
24
  // first tab of this user -> online