@nka212bg/backend-utils 0.1.40 → 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 +49 -13
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nka212bg/backend-utils",
3
3
  "author": "nka212bg",
4
- "version": "0.1.40",
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
@@ -1,13 +1,53 @@
1
1
  const WebSocket = require("ws");
2
2
 
3
- module.exports = (server) => {
3
+ const parseParams = (url) => {
4
+ try {
5
+ return Object.fromEntries(new URLSearchParams(decodeURIComponent(url || "").replace(/^(\/\?|\?|\/)/g, "")));
6
+ } catch (error) {
7
+ return {};
8
+ }
9
+ };
10
+
11
+ module.exports = (server, { authenticate } = {}) => {
4
12
  const socket = new WebSocket.Server({ server, noServer: !server });
5
13
  socket.on("connection", (ws, req) => {
6
- try {
7
- ws.params = Object.fromEntries(new URLSearchParams(decodeURIComponent(req.url || "").replace(/^(\/\?|\?|\/)/g, "")));
8
- } catch (error) {}
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;
22
+ const userId = ws.params.userId;
23
+
24
+ // first tab of this user -> online
25
+ if (userId && !isConnected(userId, ws)) notifyWatchers(userId, 1);
26
+
27
+ // { watchStatuses: [userIds] } -> replies { statuses: { userId: 0 | 1 } }, then pushes { status: { userId, online } } on changes
28
+ ws.on("message", (raw) => {
29
+ try {
30
+ const { watchStatuses } = JSON.parse(raw);
31
+ if (!watchStatuses) return;
32
+ ws.watching = new Set(watchStatuses.map(String));
33
+ ws.send(JSON.stringify({ statuses: socket.getStatuses(watchStatuses) }));
34
+ } catch (error) {}
35
+ });
36
+
37
+ // last tab closed -> offline
38
+ ws.on("close", () => userId && !isConnected(userId, ws) && notifyWatchers(userId, 0));
9
39
  });
10
40
 
41
+ const isConnected = (userId, except) => {
42
+ for (const c of socket.clients) if (c !== except && c.params?.userId === userId) return true;
43
+ return false;
44
+ };
45
+
46
+ const notifyWatchers = (userId, online) => {
47
+ const data = JSON.stringify({ statuses: { [userId]: online } });
48
+ for (const c of socket.clients) if (c.watching?.has(userId)) c.send(data);
49
+ };
50
+
11
51
  socket.broadcast = (data, { skipId, skipToken } = {}) => {
12
52
  if (!data) return 0;
13
53
  if (typeof data === "object") data = JSON.stringify(data);
@@ -50,23 +90,19 @@ module.exports = (server) => {
50
90
 
51
91
  socket.getStatuses = (users) => {
52
92
  users = new Set(String(users).split(/[,"' ]/g).filter(Boolean));
53
-
54
93
  const usersObj = {};
55
- for (const u of users) usersObj[u] = 0;
56
94
 
57
- let remaining = users.size;
58
- for (const client of socket.clients || []) {
59
- if (!remaining) break;
60
- if (!users.has(client.params.userId)) continue;
95
+ for (const u of users) usersObj[u] = 0;
61
96
 
62
- usersObj[client.params.userId] = 1;
63
- remaining--;
97
+ for (const client of socket.clients || []) {
98
+ if (!users.size) break;
99
+ const id = client.params?.userId;
100
+ if (users.delete(id)) usersObj[id] = 1; // delete() is true only the first time -> extra tabs are ignored
64
101
  }
65
102
 
66
103
  return usersObj;
67
104
  };
68
105
 
69
-
70
106
  return socket;
71
107
  };
72
108