comty.js 0.60.7 → 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.
@@ -1,42 +1,46 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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; }var _session = require('../models/session'); var _session2 = _interopRequireDefault(_session);
2
- var _ = require('..');
3
- exports. default = async () => {
4
- __comty_shared_state.eventBus.emit("session:refreshing")
5
- __comty_shared_state.refreshingToken = true
6
-
7
- // send request to regenerate token
8
- const response = await __comty_shared_state.baseRequest({
9
- method: "POST",
10
- url: "/auth",
11
- data: {
12
- authToken: await _session2.default.token,
13
- refreshToken: await _session2.default.refreshToken,
14
- }
15
- }).catch((error) => {
16
- return false
17
- })
18
-
19
- if (!response) {
20
- __comty_shared_state.refreshingToken = false
21
-
22
- throw new Error("Failed to regenerate token.")
23
- }
24
-
25
- if (!_optionalChain([response, 'access', _3 => _3.data, 'optionalAccess', _4 => _4.token])) {
26
- __comty_shared_state.refreshingToken = false
27
2
 
28
- throw new Error("Failed to regenerate token, invalid server response.")
29
- }
30
-
31
- // set new token
32
- _session2.default.token = response.data.token
33
- _session2.default.refreshToken = response.data.refreshToken
34
-
35
- // emit event
36
- __comty_shared_state.eventBus.emit("session:refreshed")
37
- __comty_shared_state.refreshingToken = false
38
-
39
- _.reauthenticateWebsockets.call(void 0, )
40
-
41
- return true
42
- }
3
+ exports. default = async () => {
4
+ __comty_shared_state.eventBus.emit("session:refreshing")
5
+ __comty_shared_state.refreshingToken = true
6
+
7
+ // send request to regenerate token
8
+ const response = await __comty_shared_state
9
+ .baseRequest({
10
+ method: "POST",
11
+ url: "/auth",
12
+ data: {
13
+ authToken: await _session2.default.token,
14
+ refreshToken: await _session2.default.refreshToken,
15
+ },
16
+ })
17
+ .catch((error) => {
18
+ return false
19
+ })
20
+
21
+ if (!response) {
22
+ __comty_shared_state.refreshingToken = false
23
+
24
+ throw new Error("Failed to regenerate token.")
25
+ }
26
+
27
+ if (!_optionalChain([response, 'access', _ => _.data, 'optionalAccess', _2 => _2.token])) {
28
+ __comty_shared_state.refreshingToken = false
29
+
30
+ throw new Error("Failed to regenerate token, invalid server response.")
31
+ }
32
+
33
+ // set new token
34
+ _session2.default.token = response.data.token
35
+ _session2.default.refreshToken = response.data.refreshToken
36
+
37
+ // emit event
38
+ __comty_shared_state.eventBus.emit("session:refreshed")
39
+ __comty_shared_state.refreshingToken = false
40
+
41
+ if (typeof __comty_shared_state.ws === "object") {
42
+ await __comty_shared_state.ws.connectAll()
43
+ }
44
+
45
+ return true
46
+ }
package/dist/index.js CHANGED
@@ -1,12 +1,11 @@
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 _packagejson = require('../package.json'); var _packagejson2 = _interopRequireDefault(_packagejson);
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _packagejson = require('../package.json'); var _packagejson2 = _interopRequireDefault(_packagejson);
2
2
  var _events = require('@foxify/events'); var _events2 = _interopRequireDefault(_events);
3
-
4
3
  var _axios = require('axios'); var _axios2 = _interopRequireDefault(_axios);
5
- var _socketioclient = require('socket.io-client');
6
4
 
7
5
  var _addons = require('./addons'); var _addons2 = _interopRequireDefault(_addons);
6
+ var _ws = require('./ws'); var _ws2 = _interopRequireDefault(_ws);
8
7
  var _withStorage = require('./helpers/withStorage'); var _withStorage2 = _interopRequireDefault(_withStorage);
9
- var _remote = require('./remote'); var _remote2 = _interopRequireDefault(_remote);
8
+ var _remotes = require('./remotes'); var _remotes2 = _interopRequireDefault(_remotes);
10
9
 
11
10
  globalThis.isServerMode =
12
11
  typeof window === "undefined" && typeof global !== "undefined"
@@ -22,152 +21,6 @@ if (globalThis.isServerMode) {
22
21
  }
23
22
  }
24
23
 
25
- /**
26
- * Creates websockets by disconnecting and removing listeners from existing instances,
27
- * then creating new instances for each websocket in the remote.websockets array.
28
- * Registers event listeners for connection, disconnection, reconnection, error, and any other events.
29
- *
30
- * @return {Promise<void>} A promise that resolves when all websockets have been created and event listeners have been registered.
31
- */
32
- async function createWebsockets() {
33
- if (!_remote2.default.websockets) {
34
- return false
35
- }
36
-
37
- const instances = globalThis.__comty_shared_state.sockets
38
-
39
- for (let [key, instance] of Object.entries(instances)) {
40
- if (instance.connected) {
41
- // disconnect first
42
- instance.disconnect()
43
- }
44
-
45
- // remove current listeners
46
- instance.removeAllListeners()
47
-
48
- delete globalThis.__comty_shared_state.sockets[key]
49
- }
50
-
51
- for (let ws of _remote2.default.websockets) {
52
- let opts = {
53
- transports: ["websocket"],
54
- autoConnect: _nullishCoalesce(ws.autoConnect, () => ( true)),
55
- forceNew: true,
56
- path: ws.path,
57
- ...(_nullishCoalesce(ws.params, () => ( {}))),
58
- }
59
-
60
- if (ws.noAuth !== true) {
61
- opts.auth = {
62
- token: _withStorage2.default.engine.get("token"),
63
- }
64
- }
65
-
66
- globalThis.__comty_shared_state.sockets[ws.namespace] = _socketioclient.io.call(void 0,
67
- _remote2.default.origin,
68
- opts,
69
- )
70
- }
71
-
72
- // regsister events
73
- for (let [key, instance] of Object.entries(instances)) {
74
- instance.on("connect", () => {
75
- //console.debug(`[WS-API][${key}] Connected`)
76
-
77
- globalThis.__comty_shared_state.eventBus.emit(`${key}:connected`)
78
- })
79
-
80
- instance.on("disconnect", () => {
81
- console.debug(`[WS-API][${key}] Disconnected`)
82
-
83
- globalThis.__comty_shared_state.eventBus.emit(`${key}:disconnected`)
84
- })
85
-
86
- instance.on("reconnect", () => {
87
- console.debug(`[WS-API][${key}] Reconnected`)
88
-
89
- globalThis.__comty_shared_state.eventBus.emit(`${key}:reconnected`)
90
-
91
- reauthenticateWebsockets()
92
- })
93
-
94
- instance.on("error", (error) => {
95
- console.error(`[WS-API][${key}] Error`, error)
96
-
97
- globalThis.__comty_shared_state.eventBus.emit(`${key}:error`, error)
98
- })
99
-
100
- instance.onAny((event, ...args) => {
101
- console.debug(`[WS-API][${key}] Event (${event})`, ...args)
102
-
103
- globalThis.__comty_shared_state.eventBus.emit(
104
- `${key}:${event}`,
105
- ...args,
106
- )
107
- })
108
- }
109
- } exports.createWebsockets = createWebsockets;
110
-
111
- /**
112
- * Disconnects all websocket instances by calling the `disconnect` method on each instance.
113
- *
114
- * @return {Promise<void>} A promise that resolves when all websocket instances have been disconnected.
115
- */
116
- async function disconnectWebsockets() {
117
- const instances = globalThis.__comty_shared_state.sockets
118
-
119
- for (let [key, instance] of Object.entries(instances)) {
120
- if (instance.connected) {
121
- instance.disconnect()
122
- }
123
- }
124
- } exports.disconnectWebsockets = disconnectWebsockets;
125
-
126
- /**
127
- * Reconnects all websocket instances by disconnecting and reconnecting them with the current token.
128
- *
129
- * @return {Promise<void>} A promise that resolves when all websocket instances have been reconnected.
130
- */
131
- async function reconnectWebsockets() {
132
- const instances = globalThis.__comty_shared_state.sockets
133
-
134
- for (let [key, instance] of Object.entries(instances)) {
135
- if (instance.connected) {
136
- // disconnect first
137
- instance.disconnect()
138
- }
139
-
140
- instance.auth = {
141
- token: _withStorage2.default.engine.get("token"),
142
- }
143
-
144
- instance.connect()
145
- }
146
- } exports.reconnectWebsockets = reconnectWebsockets;
147
-
148
- /**
149
- * Reauthenticates all websocket instances with the current token. If a websocket instance is not connected, it connects to the server. If it is connected, it emits an "auth:reauth" event with the current token.
150
- *
151
- * @return {Promise<void>} Promise that resolves when all websocket instances have been reauthenticated.
152
- */
153
- async function reauthenticateWebsockets() {
154
- const instances = globalThis.__comty_shared_state.sockets
155
-
156
- for (let [key, instance] of Object.entries(instances)) {
157
- const token = _withStorage2.default.engine.get("token")
158
-
159
- instance.auth = {
160
- token: token,
161
- }
162
-
163
- if (!instance.connected) {
164
- instance.connect()
165
- } else {
166
- instance.emit("auth:reauth", token)
167
- }
168
- }
169
- } exports.reauthenticateWebsockets = reauthenticateWebsockets;
170
-
171
24
  /**
172
25
  * Create a client with the specified access key, private key, and websocket enablement.
173
26
  *
@@ -178,13 +31,14 @@ if (globalThis.isServerMode) {
178
31
  accessKey = null,
179
32
  privateKey = null,
180
33
  enableWs = false,
181
- origin = _remote2.default.origin,
34
+ origin = _remotes2.default.origin,
35
+ eventBus = new (0, _events2.default)(),
182
36
  } = {}) {
183
37
  const sharedState = (globalThis.__comty_shared_state = {
184
- eventBus: new (0, _events2.default)(),
38
+ eventBus: eventBus,
185
39
  mainOrigin: origin,
186
40
  baseRequest: null,
187
- sockets: new Map(),
41
+ ws: null,
188
42
  rest: null,
189
43
  version: _packagejson2.default.version,
190
44
  addons: new (0, _addons2.default)(),
@@ -218,8 +72,9 @@ if (globalThis.isServerMode) {
218
72
  return config
219
73
  })
220
74
 
221
- if (enableWs) {
222
- createWebsockets()
75
+ if (enableWs == true) {
76
+ __comty_shared_state.ws = new (0, _ws2.default)()
77
+ sharedState.ws.connectAll()
223
78
  }
224
79
 
225
80
  return sharedState
@@ -26,6 +26,7 @@ var _standartListMerge = require('../../utils/standartListMerge'); var _standart
26
26
  limit: limit,
27
27
  offset: offset,
28
28
  sort: sort,
29
+ fields: params.fields,
29
30
  },
30
31
  })
31
32
 
@@ -124,4 +124,13 @@ async function injectUserData(list) {
124
124
 
125
125
  return data
126
126
  }
127
+
128
+ static async getLivestreamData(livestream_id) {
129
+ const { data } = await Streaming.base({
130
+ method: "get",
131
+ url: `/streaming/${livestream_id}`,
132
+ })
133
+
134
+ return data
135
+ }
127
136
  } Streaming.__initStatic(); exports.default = Streaming;
@@ -2,134 +2,134 @@
2
2
  var _request = require('../../request'); var _request2 = _interopRequireDefault(_request);
3
3
 
4
4
  class User {
5
- /**
6
- * Retrieves the data of a user.
7
- *
8
- * @param {Object} payload - An object containing the username and user_id.
9
- * @param {string} payload.username - The username of the user.
10
- * @param {string} payload.user_id - The ID of the user.
11
- * @return {Promise<Object>} - A promise that resolves with the data of the user.
12
- */
13
- static async data(payload = {}) {
14
- let {
15
- username,
16
- user_id,
17
- } = payload
18
-
19
- if (!username && !user_id) {
20
- user_id = _session2.default.user_id
21
- }
22
-
23
- if (username && !user_id) {
24
- // resolve user_id from username
25
- const resolveResponse = await _request2.default.call(void 0, {
26
- method: "GET",
27
- url: `/users/${username}/resolve-user_id`,
28
- })
29
-
30
- user_id = resolveResponse.data.user_id
31
- }
32
-
33
- const response = await _request2.default.call(void 0, {
34
- method: "GET",
35
- url: `/users/${user_id}/data`,
36
- })
37
-
38
- return response.data
39
- }
40
-
41
- /**
42
- * Updates the user data with the given payload.
43
- *
44
- * @param {Object} payload - The data to update the user with.
45
- * @return {Promise<Object>} - A promise that resolves with the updated user data.
46
- */
47
- static async updateData(payload) {
48
- const response = await _request2.default.call(void 0, {
49
- method: "POST",
50
- url: "/users/self/update",
51
- data: payload,
52
- })
53
-
54
- return response.data
55
- }
56
-
57
- /**
58
- * Update the public name to null in the user data.
59
- *
60
- * @return {Promise} A Promise that resolves with the response data after updating the public name
61
- */
62
- static async unsetPublicName() {
63
- return await User.updateData({
64
- public_name: null,
65
- })
66
- }
67
-
68
- /**
69
- * Retrieves the roles of a user.
70
- *
71
- * @param {string} user_id - The ID of the user. If not provided, the current user ID will be used.
72
- * @return {Promise<Array>} An array of roles for the user.
73
- */
74
- static async getRoles(user_id) {
75
- const response = await _request2.default.call(void 0, {
76
- method: "GET",
77
- url: `/users/${_nullishCoalesce(user_id, () => ( "self"))}/roles`,
78
- })
79
-
80
- return response.data
81
- }
82
-
83
- /**
84
- * Retrieves the badges for a given user.
85
- *
86
- * @param {string} user_id - The ID of the user. If not provided, the current session user ID will be used.
87
- * @return {Promise<Array>} An array of badges for the user.
88
- */
89
- static async getBadges(user_id) {
90
- if (!user_id) {
91
- user_id = _session2.default.user_id
92
- }
93
-
94
- const { data } = await _request2.default.call(void 0, {
95
- method: "GET",
96
- url: `/users/${user_id}/badges`,
97
- })
98
-
99
- return data
100
- }
101
-
102
- /**
103
- * Retrive user config from server
104
- *
105
- * @param {type} key - A key of config
106
- * @return {object} - Config object
107
- */
108
- static async getConfig(key) {
109
- const { data } = await _request2.default.call(void 0, {
110
- method: "GET",
111
- url: "/users/self/config",
112
- params: {
113
- key
114
- }
115
- })
116
-
117
- return data
118
- }
119
-
120
- /**
121
- * Update the configuration with the given update.
122
- *
123
- * @param {Object} update - The object containing the updated configuration data
124
- * @return {Promise} A Promise that resolves with the response data after the configuration is updated
125
- */
126
- static async updateConfig(update) {
127
- const { data } = await _request2.default.call(void 0, {
128
- method: "PUT",
129
- url: "/users/self/config",
130
- data: update
131
- })
132
-
133
- return data
134
- }
135
- } exports.default = User;
5
+ /**
6
+ * Retrieves the data of a user.
7
+ *
8
+ * @param {Object} payload - An object containing the username and user_id.
9
+ * @param {string} payload.username - The username of the user.
10
+ * @param {string} payload.user_id - The ID of the user.
11
+ * @return {Promise<Object>} - A promise that resolves with the data of the user.
12
+ */
13
+ static async data(payload = {}) {
14
+ let { username, user_id, basic = false } = payload
15
+
16
+ if (!username && !user_id) {
17
+ user_id = _session2.default.user_id
18
+ }
19
+
20
+ if (username && !user_id) {
21
+ // resolve user_id from username
22
+ const resolveResponse = await _request2.default.call(void 0, {
23
+ method: "GET",
24
+ url: `/users/${username}/resolve-user_id`,
25
+ })
26
+
27
+ user_id = resolveResponse.data.user_id
28
+ }
29
+
30
+ const response = await _request2.default.call(void 0, {
31
+ method: "GET",
32
+ url: `/users/${user_id}/data`,
33
+ params: {
34
+ basic,
35
+ },
36
+ })
37
+
38
+ return response.data
39
+ }
40
+
41
+ /**
42
+ * Updates the user data with the given payload.
43
+ *
44
+ * @param {Object} payload - The data to update the user with.
45
+ * @return {Promise<Object>} - A promise that resolves with the updated user data.
46
+ */
47
+ static async updateData(payload) {
48
+ const response = await _request2.default.call(void 0, {
49
+ method: "POST",
50
+ url: "/users/self/update",
51
+ data: payload,
52
+ })
53
+
54
+ return response.data
55
+ }
56
+
57
+ /**
58
+ * Update the public name to null in the user data.
59
+ *
60
+ * @return {Promise} A Promise that resolves with the response data after updating the public name
61
+ */
62
+ static async unsetPublicName() {
63
+ return await User.updateData({
64
+ public_name: null,
65
+ })
66
+ }
67
+
68
+ /**
69
+ * Retrieves the roles of a user.
70
+ *
71
+ * @param {string} user_id - The ID of the user. If not provided, the current user ID will be used.
72
+ * @return {Promise<Array>} An array of roles for the user.
73
+ */
74
+ static async getRoles(user_id) {
75
+ const response = await _request2.default.call(void 0, {
76
+ method: "GET",
77
+ url: `/users/${_nullishCoalesce(user_id, () => ( "self"))}/roles`,
78
+ })
79
+
80
+ return response.data
81
+ }
82
+
83
+ /**
84
+ * Retrieves the badges for a given user.
85
+ *
86
+ * @param {string} user_id - The ID of the user. If not provided, the current session user ID will be used.
87
+ * @return {Promise<Array>} An array of badges for the user.
88
+ */
89
+ static async getBadges(user_id) {
90
+ if (!user_id) {
91
+ user_id = _session2.default.user_id
92
+ }
93
+
94
+ const { data } = await _request2.default.call(void 0, {
95
+ method: "GET",
96
+ url: `/users/${user_id}/badges`,
97
+ })
98
+
99
+ return data
100
+ }
101
+
102
+ /**
103
+ * Retrive user config from server
104
+ *
105
+ * @param {type} key - A key of config
106
+ * @return {object} - Config object
107
+ */
108
+ static async getConfig(key) {
109
+ const { data } = await _request2.default.call(void 0, {
110
+ method: "GET",
111
+ url: "/users/self/config",
112
+ params: {
113
+ key,
114
+ },
115
+ })
116
+
117
+ return data
118
+ }
119
+
120
+ /**
121
+ * Update the configuration with the given update.
122
+ *
123
+ * @param {Object} update - The object containing the updated configuration data
124
+ * @return {Promise} A Promise that resolves with the response data after the configuration is updated
125
+ */
126
+ static async updateConfig(update) {
127
+ const { data } = await _request2.default.call(void 0, {
128
+ method: "PUT",
129
+ url: "/users/self/config",
130
+ data: update,
131
+ })
132
+
133
+ return data
134
+ }
135
+ } exports.default = User;
@@ -0,0 +1,36 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }const envOrigins = {
2
+ development: `https://fr01.ragestudio.net:9000`, //`${location.origin}/api`,
3
+ indev: "https://indev.comty.app/api",
4
+ production: "https://api.comty.app",
5
+ }
6
+
7
+ exports. default = {
8
+ origin: envOrigins[_nullishCoalesce(process.env.NODE_ENV, () => ( "production"))],
9
+ websockets: [
10
+ {
11
+ namespace: "posts",
12
+ path: "/posts",
13
+ ng: true,
14
+ },
15
+ {
16
+ namespace: "main",
17
+ path: "/main",
18
+ },
19
+ {
20
+ namespace: "notifications",
21
+ path: "/notifications",
22
+ },
23
+ {
24
+ namespace: "chats",
25
+ path: "/chats",
26
+ },
27
+ {
28
+ namespace: "music",
29
+ path: "/music",
30
+ },
31
+ // {
32
+ // namespace: "payments",
33
+ // path: "/payments",
34
+ // }
35
+ ],
36
+ }
@@ -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
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,6 +1,6 @@
1
1
  {
2
2
  "name": "comty.js",
3
- "version": "0.60.7",
3
+ "version": "0.61.0",
4
4
  "main": "./dist/index.js",
5
5
  "author": "RageStudio <support@ragestudio.net>",
6
6
  "scripts": {
@@ -13,7 +13,6 @@
13
13
  "license": "MIT",
14
14
  "dependencies": {
15
15
  "@foxify/events": "^2.1.0",
16
- "ava": "^6.1.2",
17
16
  "axios": "^1.4.0",
18
17
  "js-cookie": "^3.0.5",
19
18
  "jsonwebtoken": "^9.0.0",
@@ -22,6 +21,7 @@
22
21
  "socket.io-client": "^4.6.1"
23
22
  },
24
23
  "devDependencies": {
24
+ "ava": "^6.1.2",
25
25
  "@ragestudio/hermes": "^1.0.0"
26
26
  }
27
27
  }