@nka212bg/backend-utils 0.1.42 → 0.1.44
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.
- package/package.json +1 -1
- package/session.js +6 -4
- package/socket.js +13 -4
package/package.json
CHANGED
package/session.js
CHANGED
|
@@ -44,7 +44,7 @@ exports.serverState = new Proxy(
|
|
|
44
44
|
// const s = serverState["abc123"]; // => {name: 'alice', ..., expireIn: 604859999}
|
|
45
45
|
// const s = serverState[req.headers.authorization];
|
|
46
46
|
// const all = serverState.getAll; // => whole STATE map
|
|
47
|
-
// const alices = serverState["name=alice"]; // => all entries where name == "alice",
|
|
47
|
+
// const alices = serverState["name=alice"]; // => all entries where name == "alice", keyed by token, same shape as STATE
|
|
48
48
|
get(_, token) {
|
|
49
49
|
if (/^\s*(undefined|null|0)?\s*$/.test(token)) return false;
|
|
50
50
|
token = String(token).trim();
|
|
@@ -53,9 +53,11 @@ exports.serverState = new Proxy(
|
|
|
53
53
|
|
|
54
54
|
if (token.includes("=")) {
|
|
55
55
|
const [field, value] = token.trim().split(/\s*=\s*/);
|
|
56
|
-
return Object.
|
|
57
|
-
.
|
|
58
|
-
|
|
56
|
+
return Object.fromEntries(
|
|
57
|
+
Object.entries(STATE)
|
|
58
|
+
.filter(([, s]) => s[field] == value)
|
|
59
|
+
.map(([token, s]) => [token, { ...s }]),
|
|
60
|
+
);
|
|
59
61
|
}
|
|
60
62
|
|
|
61
63
|
if (!STATE[token]) return false;
|
package/socket.js
CHANGED
|
@@ -9,6 +9,7 @@ const parseParams = (url) => {
|
|
|
9
9
|
};
|
|
10
10
|
|
|
11
11
|
module.exports = (server, { authenticate } = {}) => {
|
|
12
|
+
const hidden = new Set();
|
|
12
13
|
const socket = new WebSocket.Server({ server, noServer: !server });
|
|
13
14
|
socket.on("connection", (ws, req) => {
|
|
14
15
|
const params = parseParams(req.url);
|
|
@@ -22,7 +23,7 @@ module.exports = (server, { authenticate } = {}) => {
|
|
|
22
23
|
const userId = ws.params.userId;
|
|
23
24
|
|
|
24
25
|
// first tab of this user -> online
|
|
25
|
-
if (userId && !isConnected(userId, ws)) notifyWatchers(userId, 1);
|
|
26
|
+
if (userId && !hidden.has(userId) && !isConnected(userId, ws)) notifyWatchers(userId, 1);
|
|
26
27
|
|
|
27
28
|
// { watchStatuses: [userIds] } -> replies { statuses: { userId: 0 | 1 } }, then pushes { status: { userId, online } } on changes
|
|
28
29
|
ws.on("message", (raw) => {
|
|
@@ -35,7 +36,7 @@ module.exports = (server, { authenticate } = {}) => {
|
|
|
35
36
|
});
|
|
36
37
|
|
|
37
38
|
// last tab closed -> offline
|
|
38
|
-
ws.on("close", () => userId && !isConnected(userId, ws) && notifyWatchers(userId, 0));
|
|
39
|
+
ws.on("close", () => userId && !hidden.has(userId) && !isConnected(userId, ws) && notifyWatchers(userId, 0));
|
|
39
40
|
});
|
|
40
41
|
|
|
41
42
|
const isConnected = (userId, except) => {
|
|
@@ -43,7 +44,7 @@ module.exports = (server, { authenticate } = {}) => {
|
|
|
43
44
|
return false;
|
|
44
45
|
};
|
|
45
46
|
|
|
46
|
-
const notifyWatchers = (userId, online) => {
|
|
47
|
+
const notifyWatchers = (userId, online) => {
|
|
47
48
|
const data = JSON.stringify({ statuses: { [userId]: online } });
|
|
48
49
|
for (const c of socket.clients) if (c.watching?.has(userId)) c.send(data);
|
|
49
50
|
};
|
|
@@ -97,12 +98,20 @@ module.exports = (server, { authenticate } = {}) => {
|
|
|
97
98
|
for (const client of socket.clients || []) {
|
|
98
99
|
if (!users.size) break;
|
|
99
100
|
const id = client.params?.userId;
|
|
100
|
-
if (users.delete(id)) usersObj[id] = 1;
|
|
101
|
+
if (!hidden.has(id) && users.delete(id)) usersObj[id] = 1;
|
|
101
102
|
}
|
|
102
103
|
|
|
103
104
|
return usersObj;
|
|
104
105
|
};
|
|
105
106
|
|
|
107
|
+
socket.setStatuses = (userId, offline = true) => {
|
|
108
|
+
userId = String(userId);
|
|
109
|
+
if (!!offline === hidden.has(userId)) return; // no change
|
|
110
|
+
|
|
111
|
+
offline ? hidden.add(userId) : hidden.delete(userId);
|
|
112
|
+
if (isConnected(userId)) notifyWatchers(userId, offline ? 0 : 1);
|
|
113
|
+
};
|
|
114
|
+
|
|
106
115
|
return socket;
|
|
107
116
|
};
|
|
108
117
|
|