comty.js 0.60.7 → 0.61.1
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/dist/helpers/refreshToken.js +44 -40
- package/dist/index.js +19 -156
- package/dist/models/search/index.js +1 -0
- package/dist/models/spectrum/index.js +9 -0
- package/dist/models/user/index.js +131 -131
- package/dist/remotes.js +36 -0
- package/dist/rtclient.js +131 -0
- package/dist/ws.js +144 -0
- package/package.json +5 -6
|
@@ -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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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 }; }
|
|
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
|
|
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
|
*
|
|
@@ -177,14 +30,18 @@ if (globalThis.isServerMode) {
|
|
|
177
30
|
function createClient({
|
|
178
31
|
accessKey = null,
|
|
179
32
|
privateKey = null,
|
|
180
|
-
|
|
181
|
-
|
|
33
|
+
ws = {
|
|
34
|
+
enable: false,
|
|
35
|
+
autoConnect: false,
|
|
36
|
+
},
|
|
37
|
+
origin = _remotes2.default.origin,
|
|
38
|
+
eventBus = new (0, _events2.default)(),
|
|
182
39
|
} = {}) {
|
|
183
40
|
const sharedState = (globalThis.__comty_shared_state = {
|
|
184
|
-
eventBus:
|
|
41
|
+
eventBus: eventBus,
|
|
185
42
|
mainOrigin: origin,
|
|
186
43
|
baseRequest: null,
|
|
187
|
-
|
|
44
|
+
ws: null,
|
|
188
45
|
rest: null,
|
|
189
46
|
version: _packagejson2.default.version,
|
|
190
47
|
addons: new (0, _addons2.default)(),
|
|
@@ -218,8 +75,14 @@ if (globalThis.isServerMode) {
|
|
|
218
75
|
return config
|
|
219
76
|
})
|
|
220
77
|
|
|
221
|
-
if (
|
|
222
|
-
|
|
78
|
+
if (typeof ws === "object") {
|
|
79
|
+
if (ws.enable === true) {
|
|
80
|
+
__comty_shared_state.ws = new (0, _ws2.default)()
|
|
81
|
+
|
|
82
|
+
if (ws.autoConnect === true) {
|
|
83
|
+
sharedState.ws.connectAll()
|
|
84
|
+
}
|
|
85
|
+
}
|
|
223
86
|
}
|
|
224
87
|
|
|
225
88
|
return sharedState
|
|
@@ -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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
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;
|
package/dist/remotes.js
ADDED
|
@@ -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
|
+
}
|
package/dist/rtclient.js
ADDED
|
@@ -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,144 @@
|
|
|
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
|
+
//import { RTEngineClient } from "linebridge-client"
|
|
6
|
+
var _src = require('../../linebridge/client/src');
|
|
7
|
+
|
|
8
|
+
class WebsocketManager {constructor() { WebsocketManager.prototype.__init.call(this); }
|
|
9
|
+
__init() {this.sockets = new Map()}
|
|
10
|
+
|
|
11
|
+
async connect(remote) {
|
|
12
|
+
let opts = {
|
|
13
|
+
transports: ["websocket"],
|
|
14
|
+
autoConnect: _nullishCoalesce(remote.autoConnect, () => ( true)),
|
|
15
|
+
forceNew: true,
|
|
16
|
+
path: remote.path,
|
|
17
|
+
...(_nullishCoalesce(remote.params, () => ( {}))),
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (remote.noAuth !== true) {
|
|
21
|
+
opts.auth = {
|
|
22
|
+
token: _withStorage2.default.engine.get("token"),
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const socket = _socketioclient.io.call(void 0, _remotes2.default.origin, opts)
|
|
27
|
+
|
|
28
|
+
socket.on("connect", () => {
|
|
29
|
+
globalThis.__comty_shared_state.eventBus.emit(
|
|
30
|
+
`wsmanager:${remote.namespace}:connected`,
|
|
31
|
+
)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
socket.on("disconnect", () => {
|
|
35
|
+
globalThis.__comty_shared_state.eventBus.emit(
|
|
36
|
+
`wsmanager:${remote.namespace}:disconnected`,
|
|
37
|
+
)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
socket.on("error", (error) => {
|
|
41
|
+
globalThis.__comty_shared_state.eventBus.emit(
|
|
42
|
+
`wsmanager:${remote.namespace}:error`,
|
|
43
|
+
error,
|
|
44
|
+
)
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
this.sockets.set(remote.namespace, socket)
|
|
48
|
+
|
|
49
|
+
return socket
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async connectNg(remote) {
|
|
53
|
+
console.warn(
|
|
54
|
+
`Creating experimental socket client, some features may not work as expected:`,
|
|
55
|
+
remote,
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
const client = new (0, _src.RTEngineClient)({
|
|
59
|
+
url: `${_remotes2.default.origin}/${remote.namespace}`,
|
|
60
|
+
token: _withStorage2.default.engine.get("token"),
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
client.on("connect", () => {
|
|
64
|
+
globalThis.__comty_shared_state.eventBus.emit(
|
|
65
|
+
`wsmanager:${remote.namespace}:connected`,
|
|
66
|
+
)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
client.on("disconnect", () => {
|
|
70
|
+
globalThis.__comty_shared_state.eventBus.emit(
|
|
71
|
+
`wsmanager:${remote.namespace}:disconnected`,
|
|
72
|
+
)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
client.on("error", (error) => {
|
|
76
|
+
globalThis.__comty_shared_state.eventBus.emit(
|
|
77
|
+
`wsmanager:${remote.namespace}:error`,
|
|
78
|
+
error,
|
|
79
|
+
)
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
await client.connect()
|
|
83
|
+
|
|
84
|
+
this.sockets.set(remote.namespace, client)
|
|
85
|
+
|
|
86
|
+
return client
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async disconnect(key) {
|
|
90
|
+
const socket = this.sockets.get(key)
|
|
91
|
+
|
|
92
|
+
if (!socket) {
|
|
93
|
+
return null
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (
|
|
97
|
+
socket.connected === true &&
|
|
98
|
+
typeof socket.disconnect === "function"
|
|
99
|
+
) {
|
|
100
|
+
await socket.disconnect()
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (typeof socket.removeAllListeners === "function") {
|
|
104
|
+
await socket.removeAllListeners()
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
this.sockets.delete(key)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async connectAll() {
|
|
111
|
+
if (this.sockets.size > 0) {
|
|
112
|
+
await this.disconnectAll()
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
for await (const remote of _remotes2.default.websockets) {
|
|
116
|
+
try {
|
|
117
|
+
if (remote.ng === true) {
|
|
118
|
+
await this.connectNg(remote)
|
|
119
|
+
} else {
|
|
120
|
+
await this.connect(remote)
|
|
121
|
+
}
|
|
122
|
+
} catch (error) {
|
|
123
|
+
console.error(
|
|
124
|
+
`Failed to connect to [${remote.namespace}]:`,
|
|
125
|
+
error,
|
|
126
|
+
)
|
|
127
|
+
globalThis.__comty_shared_state.eventBus.emit(
|
|
128
|
+
`wsmanager:${remote.namespace}:error`,
|
|
129
|
+
error,
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
globalThis.__comty_shared_state.eventBus.emit("wsmanager:all:connected")
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async disconnectAll() {
|
|
138
|
+
for (const [key, socket] of this.sockets) {
|
|
139
|
+
await this.disconnect(key)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
exports. default = WebsocketManager
|
package/package.json
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "comty.js",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.61.1",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"author": "RageStudio <support@ragestudio.net>",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "ava",
|
|
8
7
|
"build": "hermes build"
|
|
9
8
|
},
|
|
10
9
|
"files": [
|
|
@@ -13,13 +12,13 @@
|
|
|
13
12
|
"license": "MIT",
|
|
14
13
|
"dependencies": {
|
|
15
14
|
"@foxify/events": "^2.1.0",
|
|
16
|
-
"
|
|
17
|
-
"axios": "^1.4.0",
|
|
15
|
+
"axios": "^1.8.4",
|
|
18
16
|
"js-cookie": "^3.0.5",
|
|
19
17
|
"jsonwebtoken": "^9.0.0",
|
|
20
18
|
"jwt-decode": "^4.0.0",
|
|
21
|
-
"
|
|
22
|
-
"
|
|
19
|
+
"linebridge-client": "^0.2.0",
|
|
20
|
+
"luxon": "^3.6.0",
|
|
21
|
+
"socket.io-client": "^4.8.1"
|
|
23
22
|
},
|
|
24
23
|
"devDependencies": {
|
|
25
24
|
"@ragestudio/hermes": "^1.0.0"
|