@tachybase/plugin-online-user 0.23.8

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 (43) hide show
  1. package/.turbo/turbo-build.log +14 -0
  2. package/README.md +1 -0
  3. package/client.d.ts +2 -0
  4. package/client.js +1 -0
  5. package/dist/client/OnlineUserProvider.d.ts +2 -0
  6. package/dist/client/index.d.ts +7 -0
  7. package/dist/client/index.js +1 -0
  8. package/dist/client/locale.d.ts +9 -0
  9. package/dist/externalVersion.js +10 -0
  10. package/dist/index.d.ts +2 -0
  11. package/dist/index.js +39 -0
  12. package/dist/locale/en-US.json +3 -0
  13. package/dist/locale/zh-CN.json +3 -0
  14. package/dist/node_modules/redis/LICENSE +21 -0
  15. package/dist/node_modules/redis/dist/index.d.ts +302 -0
  16. package/dist/node_modules/redis/dist/index.js +1 -0
  17. package/dist/node_modules/redis/package.json +1 -0
  18. package/dist/node_modules/ws/LICENSE +20 -0
  19. package/dist/node_modules/ws/browser.js +8 -0
  20. package/dist/node_modules/ws/index.js +1 -0
  21. package/dist/node_modules/ws/lib/buffer-util.js +131 -0
  22. package/dist/node_modules/ws/lib/constants.js +18 -0
  23. package/dist/node_modules/ws/lib/event-target.js +292 -0
  24. package/dist/node_modules/ws/lib/extension.js +203 -0
  25. package/dist/node_modules/ws/lib/limiter.js +55 -0
  26. package/dist/node_modules/ws/lib/permessage-deflate.js +514 -0
  27. package/dist/node_modules/ws/lib/receiver.js +706 -0
  28. package/dist/node_modules/ws/lib/sender.js +602 -0
  29. package/dist/node_modules/ws/lib/stream.js +159 -0
  30. package/dist/node_modules/ws/lib/subprotocol.js +62 -0
  31. package/dist/node_modules/ws/lib/validation.js +152 -0
  32. package/dist/node_modules/ws/lib/websocket-server.js +540 -0
  33. package/dist/node_modules/ws/lib/websocket.js +1388 -0
  34. package/dist/node_modules/ws/package.json +1 -0
  35. package/dist/server/index.d.ts +1 -0
  36. package/dist/server/index.js +33 -0
  37. package/dist/server/plugin.d.ts +11 -0
  38. package/dist/server/plugin.js +48 -0
  39. package/dist/server/services/connection-manager.d.ts +9 -0
  40. package/dist/server/services/connection-manager.js +181 -0
  41. package/package.json +25 -0
  42. package/server.d.ts +2 -0
  43. package/server.js +1 -0
@@ -0,0 +1 @@
1
+ {"name":"ws","version":"8.18.0","description":"Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js","keywords":["HyBi","Push","RFC-6455","WebSocket","WebSockets","real-time"],"homepage":"https://github.com/websockets/ws","bugs":"https://github.com/websockets/ws/issues","repository":{"type":"git","url":"git+https://github.com/websockets/ws.git"},"author":"Einar Otto Stangvik <einaros@gmail.com> (http://2x.io)","license":"MIT","main":"index.js","exports":{".":{"browser":"./browser.js","import":"./wrapper.mjs","require":"./index.js"},"./package.json":"./package.json"},"browser":"browser.js","engines":{"node":">=10.0.0"},"files":["browser.js","index.js","lib/*.js","wrapper.mjs"],"scripts":{"test":"nyc --reporter=lcov --reporter=text mocha --throw-deprecation test/*.test.js","integration":"mocha --throw-deprecation test/*.integration.js","lint":"eslint . && prettier --check --ignore-path .gitignore \"**/*.{json,md,yaml,yml}\""},"peerDependencies":{"bufferutil":"^4.0.1","utf-8-validate":">=5.0.2"},"peerDependenciesMeta":{"bufferutil":{"optional":true},"utf-8-validate":{"optional":true}},"devDependencies":{"benchmark":"^2.1.4","bufferutil":"^4.0.1","eslint":"^9.0.0","eslint-config-prettier":"^9.0.0","eslint-plugin-prettier":"^5.0.0","globals":"^15.0.0","mocha":"^8.4.0","nyc":"^15.0.0","prettier":"^3.0.0","utf-8-validate":"^6.0.0"},"_lastModified":"2024-12-22T16:07:05.575Z"}
@@ -0,0 +1 @@
1
+ export { default } from './plugin';
@@ -0,0 +1,33 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var server_exports = {};
29
+ __export(server_exports, {
30
+ default: () => import_plugin.default
31
+ });
32
+ module.exports = __toCommonJS(server_exports);
33
+ var import_plugin = __toESM(require("./plugin"));
@@ -0,0 +1,11 @@
1
+ import { Plugin } from '@tachybase/server';
2
+ export declare class PluginOnlineUserServer extends Plugin {
3
+ afterAdd(): Promise<void>;
4
+ beforeLoad(): Promise<void>;
5
+ load(): Promise<void>;
6
+ install(): Promise<void>;
7
+ afterEnable(): Promise<void>;
8
+ afterDisable(): Promise<void>;
9
+ remove(): Promise<void>;
10
+ }
11
+ export default PluginOnlineUserServer;
@@ -0,0 +1,48 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var plugin_exports = {};
19
+ __export(plugin_exports, {
20
+ PluginOnlineUserServer: () => PluginOnlineUserServer,
21
+ default: () => plugin_default
22
+ });
23
+ module.exports = __toCommonJS(plugin_exports);
24
+ var import_server = require("@tachybase/server");
25
+ var import_utils = require("@tachybase/utils");
26
+ var import_connection_manager = require("./services/connection-manager");
27
+ class PluginOnlineUserServer extends import_server.Plugin {
28
+ async afterAdd() {
29
+ }
30
+ async beforeLoad() {
31
+ }
32
+ async load() {
33
+ await import_utils.Container.get(import_connection_manager.ConnectionManager).load();
34
+ }
35
+ async install() {
36
+ }
37
+ async afterEnable() {
38
+ }
39
+ async afterDisable() {
40
+ }
41
+ async remove() {
42
+ }
43
+ }
44
+ var plugin_default = PluginOnlineUserServer;
45
+ // Annotate the CommonJS export names for ESM import in node:
46
+ 0 && (module.exports = {
47
+ PluginOnlineUserServer
48
+ });
@@ -0,0 +1,9 @@
1
+ export declare class ConnectionManager {
2
+ private redisClient;
3
+ private redisPubClient;
4
+ private redisSubClient;
5
+ private app;
6
+ unload(): Promise<void>;
7
+ load(): Promise<void>;
8
+ loadWsServer(): Promise<void>;
9
+ }
@@ -0,0 +1,181 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
8
+ var __typeError = (msg) => {
9
+ throw TypeError(msg);
10
+ };
11
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
13
+ var __export = (target, all) => {
14
+ for (var name in all)
15
+ __defProp(target, name, { get: all[name], enumerable: true });
16
+ };
17
+ var __copyProps = (to, from, except, desc) => {
18
+ if (from && typeof from === "object" || typeof from === "function") {
19
+ for (let key of __getOwnPropNames(from))
20
+ if (!__hasOwnProp.call(to, key) && key !== except)
21
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
22
+ }
23
+ return to;
24
+ };
25
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
26
+ // If the importer is in node compatibility mode or this is not an ESM
27
+ // file that has been converted to a CommonJS file using a Babel-
28
+ // compatible transform (i.e. "__esModule" has not been set), then set
29
+ // "default" to the CommonJS "module.exports" for node compatibility.
30
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
31
+ mod
32
+ ));
33
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
34
+ var __decoratorStart = (base) => [, , , __create((base == null ? void 0 : base[__knownSymbol("metadata")]) ?? null)];
35
+ var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"];
36
+ var __expectFn = (fn) => fn !== void 0 && typeof fn !== "function" ? __typeError("Function expected") : fn;
37
+ var __decoratorContext = (kind, name, done, metadata, fns) => ({ kind: __decoratorStrings[kind], name, metadata, addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null)) });
38
+ var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]);
39
+ var __runInitializers = (array, flags, self, value) => {
40
+ for (var i = 0, fns = array[flags >> 1], n = fns && fns.length; i < n; i++) flags & 1 ? fns[i].call(self) : value = fns[i].call(self, value);
41
+ return value;
42
+ };
43
+ var __decorateElement = (array, flags, name, decorators, target, extra) => {
44
+ var fn, it, done, ctx, access, k = flags & 7, s = !!(flags & 8), p = !!(flags & 16);
45
+ var j = k > 3 ? array.length + 1 : k ? s ? 1 : 2 : 0, key = __decoratorStrings[k + 5];
46
+ var initializers = k > 3 && (array[j - 1] = []), extraInitializers = array[j] || (array[j] = []);
47
+ var desc = k && (!p && !s && (target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(k < 4 ? target : { get [name]() {
48
+ return __privateGet(this, extra);
49
+ }, set [name](x) {
50
+ return __privateSet(this, extra, x);
51
+ } }, name));
52
+ k ? p && k < 4 && __name(extra, (k > 2 ? "set " : k > 1 ? "get " : "") + name) : __name(target, name);
53
+ for (var i = decorators.length - 1; i >= 0; i--) {
54
+ ctx = __decoratorContext(k, name, done = {}, array[3], extraInitializers);
55
+ if (k) {
56
+ ctx.static = s, ctx.private = p, access = ctx.access = { has: p ? (x) => __privateIn(target, x) : (x) => name in x };
57
+ if (k ^ 3) access.get = p ? (x) => (k ^ 1 ? __privateGet : __privateMethod)(x, target, k ^ 4 ? extra : desc.get) : (x) => x[name];
58
+ if (k > 2) access.set = p ? (x, y) => __privateSet(x, target, y, k ^ 4 ? extra : desc.set) : (x, y) => x[name] = y;
59
+ }
60
+ it = (0, decorators[i])(k ? k < 4 ? p ? extra : desc[key] : k > 4 ? void 0 : { get: desc.get, set: desc.set } : target, ctx), done._ = 1;
61
+ if (k ^ 4 || it === void 0) __expectFn(it) && (k > 4 ? initializers.unshift(it) : k ? p ? extra = it : desc[key] = it : target = it);
62
+ else if (typeof it !== "object" || it === null) __typeError("Object expected");
63
+ else __expectFn(fn = it.get) && (desc.get = fn), __expectFn(fn = it.set) && (desc.set = fn), __expectFn(fn = it.init) && initializers.unshift(fn);
64
+ }
65
+ return k || __decoratorMetadata(array, target), desc && __defProp(target, name, desc), p ? k ^ 4 ? extra : desc : target;
66
+ };
67
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
68
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
69
+ var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use the "in" operator on this value') : member.has(obj);
70
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
71
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
72
+ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
73
+ var connection_manager_exports = {};
74
+ __export(connection_manager_exports, {
75
+ ConnectionManager: () => ConnectionManager
76
+ });
77
+ module.exports = __toCommonJS(connection_manager_exports);
78
+ var import_server = require("@tachybase/server");
79
+ var import_utils = require("@tachybase/utils");
80
+ var import_jsonwebtoken = __toESM(require("jsonwebtoken"));
81
+ var import_redis = require("redis");
82
+ var _app_dec, _ConnectionManager_decorators, _init;
83
+ const KEY_ONLINE_USERS = "online_users";
84
+ _ConnectionManager_decorators = [(0, import_utils.Service)()], _app_dec = [(0, import_utils.App)()];
85
+ class ConnectionManager {
86
+ constructor() {
87
+ __publicField(this, "redisClient", (0, import_redis.createClient)({
88
+ url: process.env.REDIS_URL ?? "redis://127.0.0.1:6379"
89
+ }));
90
+ __publicField(this, "redisPubClient", this.redisClient.duplicate());
91
+ __publicField(this, "redisSubClient", this.redisClient.duplicate());
92
+ __publicField(this, "app", __runInitializers(_init, 8, this)), __runInitializers(_init, 11, this);
93
+ }
94
+ async unload() {
95
+ for (const client of [this.redisClient, this.redisPubClient, this.redisSubClient]) {
96
+ if (client.isOpen) {
97
+ await client.disconnect();
98
+ }
99
+ }
100
+ }
101
+ async load() {
102
+ if (this.redisClient.isOpen) {
103
+ return;
104
+ }
105
+ await this.redisClient.connect();
106
+ await this.redisPubClient.connect();
107
+ await this.redisSubClient.connect();
108
+ this.app.on("afterStop", () => {
109
+ this.unload();
110
+ });
111
+ if ((0, import_utils.isMain)()) {
112
+ const keysToDelete = await this.redisClient.KEYS(`${KEY_ONLINE_USERS}*`);
113
+ if (keysToDelete.length > 0) {
114
+ await this.redisClient.DEL(...keysToDelete);
115
+ }
116
+ }
117
+ await this.loadWsServer();
118
+ }
119
+ async loadWsServer() {
120
+ const appName = this.app.name;
121
+ const gateway = import_server.Gateway.getInstance();
122
+ const ws = gateway["wsServer"];
123
+ await this.redisSubClient.SUBSCRIBE(KEY_ONLINE_USERS + appName, async (num) => {
124
+ if (num !== (0, import_utils.currentProcessNum)()) {
125
+ await notifyAllClients(false);
126
+ }
127
+ });
128
+ const notifyAllClients = async (broadcast = true) => {
129
+ const users = (await this.redisClient.HVALS(KEY_ONLINE_USERS + appName)).map((u) => JSON.parse(u));
130
+ ws.sendToConnectionsByTag("app", appName, {
131
+ type: "plugin-online-user",
132
+ payload: {
133
+ users
134
+ }
135
+ });
136
+ if (broadcast) {
137
+ await this.redisPubClient.PUBLISH(KEY_ONLINE_USERS + appName, (0, import_utils.currentProcessNum)());
138
+ }
139
+ };
140
+ const getUserById = async (id) => {
141
+ return await this.app.db.getModel("users").findOne({
142
+ where: {
143
+ id
144
+ }
145
+ });
146
+ };
147
+ ws == null ? void 0 : ws.wss.on("connection", (ws2) => {
148
+ ws2.on("error", async () => {
149
+ await notifyAllClients();
150
+ });
151
+ ws2.on("close", async () => {
152
+ await this.redisClient.HDEL(KEY_ONLINE_USERS + appName, ws2.id);
153
+ await notifyAllClients();
154
+ });
155
+ ws2.on("message", async (data) => {
156
+ if (data.toString() !== "ping") {
157
+ const userMeg = JSON.parse(data.toString());
158
+ if (userMeg.type === "plugin-online-user:client") {
159
+ try {
160
+ const analysis = import_jsonwebtoken.default.verify(userMeg.payload.token, process.env.APP_KEY);
161
+ const userId = analysis.userId;
162
+ const user = await getUserById(userId);
163
+ await this.redisClient.HSET(KEY_ONLINE_USERS + appName, ws2.id, JSON.stringify(user));
164
+ await notifyAllClients();
165
+ } catch (error) {
166
+ console.warn(error.message);
167
+ }
168
+ }
169
+ }
170
+ });
171
+ });
172
+ }
173
+ }
174
+ _init = __decoratorStart(null);
175
+ __decorateElement(_init, 5, "app", _app_dec, ConnectionManager);
176
+ ConnectionManager = __decorateElement(_init, 0, "ConnectionManager", _ConnectionManager_decorators, ConnectionManager);
177
+ __runInitializers(_init, 1, ConnectionManager);
178
+ // Annotate the CommonJS export names for ESM import in node:
179
+ 0 && (module.exports = {
180
+ ConnectionManager
181
+ });
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@tachybase/plugin-online-user",
3
+ "version": "0.23.8",
4
+ "main": "dist/server/index.js",
5
+ "devDependencies": {
6
+ "antd": "5.22.5",
7
+ "jsonwebtoken": "^8.5.1",
8
+ "lodash": "4.17.21",
9
+ "react": "^18.3.1",
10
+ "redis": "^4.7.0",
11
+ "ws": "^8.18.0"
12
+ },
13
+ "peerDependencies": {
14
+ "@tachybase/test": "0.23.8",
15
+ "@tachybase/client": "0.23.8",
16
+ "@tachybase/server": "0.23.8",
17
+ "@tachybase/schema": "0.23.8",
18
+ "@tachybase/utils": "0.23.8"
19
+ },
20
+ "description.zh-CN": "在线用户管理(建设中)",
21
+ "displayName.zh-CN": "在线用户管理(建设中)",
22
+ "scripts": {
23
+ "build": "tachybase-build --no-dts @tachybase/plugin-online-user"
24
+ }
25
+ }
package/server.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './dist/server';
2
+ export { default } from './dist/server';
package/server.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./dist/server/index.js');