comty.js 0.60.6 → 0.61.0

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.
@@ -0,0 +1,131 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); class RTEngineClient {
2
+ constructor(params = {}) {;RTEngineClient.prototype.__init.call(this);RTEngineClient.prototype.__init2.call(this);RTEngineClient.prototype.__init3.call(this);RTEngineClient.prototype.__init4.call(this);RTEngineClient.prototype.__init5.call(this);RTEngineClient.prototype.__init6.call(this);RTEngineClient.prototype.__init7.call(this);RTEngineClient.prototype.__init8.call(this);RTEngineClient.prototype.__init9.call(this);
3
+ this.params = params
4
+ }
5
+
6
+ __init() {this.socket = null}
7
+
8
+ __init2() {this.stateSubscribers = []}
9
+
10
+ __init3() {this.joinedTopics = new Set()}
11
+ __init4() {this.handlers = new Set()}
12
+
13
+ async connect() {
14
+ return new Promise((resolve, reject) => {
15
+ if (this.socket) {
16
+ this.disconnect()
17
+ }
18
+
19
+ let url = `${this.params.url}`
20
+
21
+ if (this.params.token) {
22
+ url += `?token=${this.params.token}`
23
+ }
24
+
25
+ this.socket = new WebSocket(url)
26
+
27
+ this.socket.onopen = () => {
28
+ resolve()
29
+ this._emit("connect")
30
+ }
31
+ this.socket.onclose = () => {
32
+ this._emit("disconnect")
33
+ }
34
+ this.socket.onerror = () => {
35
+ reject()
36
+ this._emit("error")
37
+ }
38
+ this.socket.onmessage = (event) => this.handleMessage(event)
39
+ })
40
+ }
41
+
42
+ async disconnect() {
43
+ if (!this.socket) {
44
+ return false
45
+ }
46
+
47
+ for await (const topic of this.joinedTopics) {
48
+ this.leaveTopic(topic)
49
+ }
50
+
51
+ this.socket.close()
52
+ this.socket = null
53
+ }
54
+
55
+ _emit(event, data) {
56
+ for (const handler of this.handlers) {
57
+ if (handler.event === event) {
58
+ handler.handler(data)
59
+ }
60
+ }
61
+ }
62
+
63
+ __init5() {this.on = (event, handler) => {
64
+ this.handlers.add({
65
+ event,
66
+ handler,
67
+ })
68
+ }}
69
+
70
+ __init6() {this.off = (event, handler) => {
71
+ this.handlers.delete({
72
+ event,
73
+ handler,
74
+ })
75
+ }}
76
+
77
+ __init7() {this.emit = (event, data) => {
78
+ if (!this.socket) {
79
+ throw new Error("Failed to send, socket not connected")
80
+ }
81
+
82
+ this.socket.send(JSON.stringify({ event, data }))
83
+ }}
84
+
85
+ __init8() {this.joinTopic = (topic) => {
86
+ this.emit("topic:join", topic)
87
+ this.joinedTopics.add(topic)
88
+ }}
89
+
90
+ __init9() {this.leaveTopic = (topic) => {
91
+ this.emit("topic:leave", topic)
92
+ this.joinedTopics.delete(topic)
93
+ }}
94
+
95
+ updateState(state) {
96
+ this.stateSubscribers.forEach((callback) => callback(state))
97
+ }
98
+
99
+ //* HANDLERS
100
+ handleMessage(event) {
101
+ try {
102
+ const payload = JSON.parse(event.data)
103
+
104
+ if (typeof payload.event !== "string") {
105
+ return false
106
+ }
107
+
108
+ if (payload.event === "error") {
109
+ console.error(payload.data)
110
+ return false
111
+ }
112
+
113
+ this._emit(payload.event, payload.data)
114
+ } catch (error) {
115
+ console.error("Error handling message:", error)
116
+ }
117
+ }
118
+
119
+ // UPDATERS
120
+ onStateChange(callback) {
121
+ this.stateSubscribers.push(callback)
122
+
123
+ return () => {
124
+ this.stateSubscribers = this.stateSubscribers.filter(
125
+ (cb) => cb !== callback,
126
+ )
127
+ }
128
+ }
129
+ } exports.RTEngineClient = RTEngineClient;
130
+
131
+ exports. default = RTEngineClient
@@ -0,0 +1,18 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }exports. default = (base, obj) => {
2
+ const validGroups = Object.keys(obj).filter(
3
+ (key) => Array.isArray(_optionalChain([obj, 'access', _ => _[key], 'optionalAccess', _2 => _2.items])) && obj[key].items.length > 0,
4
+ )
5
+
6
+ return validGroups.reduce(
7
+ (acc, group) => {
8
+ if (!acc[group]) {
9
+ acc[group] = { items: [], total_items: 0 }
10
+ }
11
+
12
+ acc[group].items = [...acc[group].items, ...obj[group].items]
13
+ acc[group].total_items += obj[group].total_items || 0
14
+ return acc
15
+ },
16
+ { ...base },
17
+ )
18
+ }
package/dist/ws.js ADDED
@@ -0,0 +1,139 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }var _remotes = require('./remotes'); var _remotes2 = _interopRequireDefault(_remotes);
2
+ var _withStorage = require('./helpers/withStorage'); var _withStorage2 = _interopRequireDefault(_withStorage);
3
+
4
+ var _socketioclient = require('socket.io-client');
5
+ var _rtclient = require('./rtclient'); var _rtclient2 = _interopRequireDefault(_rtclient);
6
+
7
+ class WebsocketManager {constructor() { WebsocketManager.prototype.__init.call(this); }
8
+ __init() {this.sockets = new Map()}
9
+
10
+ async connect(remote) {
11
+ let opts = {
12
+ transports: ["websocket"],
13
+ autoConnect: _nullishCoalesce(remote.autoConnect, () => ( true)),
14
+ forceNew: true,
15
+ path: remote.path,
16
+ ...(_nullishCoalesce(remote.params, () => ( {}))),
17
+ }
18
+
19
+ if (remote.noAuth !== true) {
20
+ opts.auth = {
21
+ token: _withStorage2.default.engine.get("token"),
22
+ }
23
+ }
24
+
25
+ const socket = _socketioclient.io.call(void 0, _remotes2.default.origin, opts)
26
+
27
+ socket.on("connect", () => {
28
+ globalThis.__comty_shared_state.eventBus.emit(
29
+ `wsmanager:${remote.namespace}:connected`,
30
+ )
31
+ })
32
+
33
+ socket.on("disconnect", () => {
34
+ globalThis.__comty_shared_state.eventBus.emit(
35
+ `wsmanager:${remote.namespace}:disconnected`,
36
+ )
37
+ })
38
+
39
+ socket.on("error", (error) => {
40
+ globalThis.__comty_shared_state.eventBus.emit(
41
+ `wsmanager:${remote.namespace}:error`,
42
+ error,
43
+ )
44
+ })
45
+
46
+ this.sockets.set(remote.namespace, socket)
47
+
48
+ return socket
49
+ }
50
+
51
+ async connectNg(remote) {
52
+ console.warn(
53
+ `Creating experimental socket client, some features may not work as expected:`,
54
+ remote,
55
+ )
56
+
57
+ const client = new (0, _rtclient2.default)({
58
+ url: `${_remotes2.default.origin}/${remote.namespace}`,
59
+ token: _withStorage2.default.engine.get("token"),
60
+ })
61
+
62
+ client.on("connect", () => {
63
+ globalThis.__comty_shared_state.eventBus.emit(
64
+ `wsmanager:${remote.namespace}:connected`,
65
+ )
66
+ })
67
+
68
+ client.on("disconnect", () => {
69
+ globalThis.__comty_shared_state.eventBus.emit(
70
+ `wsmanager:${remote.namespace}:disconnected`,
71
+ )
72
+ })
73
+
74
+ client.on("error", (error) => {
75
+ globalThis.__comty_shared_state.eventBus.emit(
76
+ `wsmanager:${remote.namespace}:error`,
77
+ error,
78
+ )
79
+ })
80
+
81
+ await client.connect()
82
+
83
+ this.sockets.set(remote.namespace, client)
84
+
85
+ return client
86
+ }
87
+
88
+ async disconnect(key) {
89
+ const socket = this.sockets.get(key)
90
+
91
+ if (!socket) {
92
+ return null
93
+ }
94
+
95
+ if (
96
+ socket.connected === true &&
97
+ typeof socket.disconnect === "function"
98
+ ) {
99
+ await socket.disconnect()
100
+ }
101
+
102
+ if (typeof socket.removeAllListeners === "function") {
103
+ await socket.removeAllListeners()
104
+ }
105
+
106
+ this.sockets.delete(key)
107
+ }
108
+
109
+ async connectAll() {
110
+ if (this.sockets.size > 0) {
111
+ await this.disconnectAll()
112
+ }
113
+
114
+ for await (const remote of _remotes2.default.websockets) {
115
+ try {
116
+ if (remote.ng === true) {
117
+ await this.connectNg(remote)
118
+ } else {
119
+ await this.connect(remote)
120
+ }
121
+ } catch (error) {
122
+ globalThis.__comty_shared_state.eventBus.emit(
123
+ `wsmanager:${remote.namespace}:error`,
124
+ error,
125
+ )
126
+ }
127
+ }
128
+
129
+ globalThis.__comty_shared_state.eventBus.emit("wsmanager:all:connected")
130
+ }
131
+
132
+ async disconnectAll() {
133
+ for (const [key, socket] of this.sockets) {
134
+ await this.disconnect(key)
135
+ }
136
+ }
137
+ }
138
+
139
+ exports. default = WebsocketManager
package/package.json CHANGED
@@ -1,27 +1,27 @@
1
1
  {
2
- "name": "comty.js",
3
- "version": "0.60.6",
4
- "main": "./dist/index.js",
5
- "author": "RageStudio <support@ragestudio.net>",
6
- "scripts": {
7
- "test": "ava",
8
- "build": "hermes build"
9
- },
10
- "files": [
11
- "dist"
12
- ],
13
- "license": "MIT",
14
- "dependencies": {
15
- "@foxify/events": "^2.1.0",
16
- "ava": "^6.1.2",
17
- "axios": "^1.4.0",
18
- "js-cookie": "^3.0.5",
19
- "jsonwebtoken": "^9.0.0",
20
- "jwt-decode": "^4.0.0",
21
- "luxon": "^3.3.0",
22
- "socket.io-client": "^4.6.1"
23
- },
24
- "devDependencies": {
25
- "@ragestudio/hermes": "^0.1.0"
26
- }
2
+ "name": "comty.js",
3
+ "version": "0.61.0",
4
+ "main": "./dist/index.js",
5
+ "author": "RageStudio <support@ragestudio.net>",
6
+ "scripts": {
7
+ "test": "ava",
8
+ "build": "hermes build"
9
+ },
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "license": "MIT",
14
+ "dependencies": {
15
+ "@foxify/events": "^2.1.0",
16
+ "axios": "^1.4.0",
17
+ "js-cookie": "^3.0.5",
18
+ "jsonwebtoken": "^9.0.0",
19
+ "jwt-decode": "^4.0.0",
20
+ "luxon": "^3.3.0",
21
+ "socket.io-client": "^4.6.1"
22
+ },
23
+ "devDependencies": {
24
+ "ava": "^6.1.2",
25
+ "@ragestudio/hermes": "^1.0.0"
26
+ }
27
27
  }