comty.js 0.60.5 → 0.60.7
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/addons.js +23 -0
- package/dist/helpers/handleAfterRequest.js +14 -16
- package/dist/helpers/processWithAddons.js +30 -0
- package/dist/index.js +164 -157
- package/dist/models/auth/index.js +261 -187
- package/dist/models/follows/index.js +2 -2
- package/dist/models/music/getters/favouriteFolder.js +42 -0
- package/dist/models/music/getters/index.js +31 -34
- package/dist/models/music/getters/isItemFavourited.js +33 -0
- package/dist/models/music/getters/recentlyPlayed.js +11 -0
- package/dist/models/music/index.js +29 -19
- package/dist/models/music/setters/index.js +31 -34
- package/dist/models/music/setters/putTrackLyrics.js +12 -0
- package/dist/models/music/setters/toggleItemFavourite.js +36 -0
- package/dist/models/nfc/index.js +5 -1
- package/dist/models/post/index.js +88 -0
- package/dist/models/radio/getters/list.js +11 -0
- package/dist/models/radio/index.js +5 -0
- package/dist/models/search/index.js +51 -33
- package/dist/models/spectrum/index.js +123 -92
- package/dist/models/user/index.js +1 -3
- package/dist/remote.js +7 -19
- package/dist/utils/standartListMerge.js +18 -0
- package/package.json +25 -25
package/dist/addons.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); class AddonsManager {constructor() { AddonsManager.prototype.__init.call(this); }
|
|
2
|
+
__init() {this.addons = new Map()}
|
|
3
|
+
|
|
4
|
+
register(name, addon) {
|
|
5
|
+
this.addons.set(name, addon)
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
get(name) {
|
|
9
|
+
return this.addons.get(name)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// search all addons registered, and find all addons that has a addon[operation] function
|
|
13
|
+
getByOperation(operation) {
|
|
14
|
+
return Array.from(this.addons.values())
|
|
15
|
+
.filter((addon) => addon[operation])
|
|
16
|
+
.map((addon) => {
|
|
17
|
+
return {
|
|
18
|
+
id: addon.constructor.id,
|
|
19
|
+
fn: addon[operation],
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
} exports.default = AddonsManager;
|
|
@@ -8,27 +8,25 @@ exports. default = async (data, callback) => {
|
|
|
8
8
|
return false
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
if (data.response
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
if (data.response) {
|
|
12
|
+
if (data.response.status === 401) {
|
|
13
|
+
// check if the server issue a refresh token on data
|
|
14
|
+
if (data.response.data.expired) {
|
|
15
|
+
try {
|
|
16
|
+
console.log(`Session expired, trying to regenerate...`)
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
await _refreshToken2.default.call(void 0, )
|
|
19
|
+
} catch (error) {
|
|
20
|
+
__comty_shared_state.eventBus.emit("session.invalid", error.message)
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
console.error(`Failed to regenerate token: ${error.message}`)
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
throw new Error(`Invalid or Expired session`)
|
|
25
|
+
}
|
|
25
26
|
|
|
26
|
-
|
|
27
|
+
return await callback()
|
|
28
|
+
}
|
|
27
29
|
}
|
|
28
30
|
}
|
|
29
|
-
|
|
30
|
-
if (data.response.status === 403) {
|
|
31
|
-
|
|
32
|
-
}
|
|
33
31
|
}
|
|
34
32
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); async function processAddons({
|
|
2
|
+
operation,
|
|
3
|
+
initialData,
|
|
4
|
+
fnArguments,
|
|
5
|
+
normalizeAddonResult,
|
|
6
|
+
}) {
|
|
7
|
+
const addons = __comty_shared_state.addons.getByOperation(operation)
|
|
8
|
+
|
|
9
|
+
let processedData = initialData
|
|
10
|
+
|
|
11
|
+
if (typeof fnArguments === "undefined") {
|
|
12
|
+
fnArguments = []
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
for (const addon of addons) {
|
|
16
|
+
try {
|
|
17
|
+
const addonResult = await addon.fn(...fnArguments)
|
|
18
|
+
|
|
19
|
+
processedData = normalizeAddonResult({
|
|
20
|
+
operation,
|
|
21
|
+
currentData: processedData,
|
|
22
|
+
addonResult,
|
|
23
|
+
})
|
|
24
|
+
} catch (error) {
|
|
25
|
+
console.error(`Error in [${operation}] addon:`, error)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return processedData
|
|
30
|
+
} exports.default = processAddons;
|
package/dist/index.js
CHANGED
|
@@ -4,21 +4,22 @@ var _events = require('@foxify/events'); var _events2 = _interopRequireDefault(_
|
|
|
4
4
|
var _axios = require('axios'); var _axios2 = _interopRequireDefault(_axios);
|
|
5
5
|
var _socketioclient = require('socket.io-client');
|
|
6
6
|
|
|
7
|
-
var
|
|
7
|
+
var _addons = require('./addons'); var _addons2 = _interopRequireDefault(_addons);
|
|
8
8
|
var _withStorage = require('./helpers/withStorage'); var _withStorage2 = _interopRequireDefault(_withStorage);
|
|
9
9
|
var _remote = require('./remote'); var _remote2 = _interopRequireDefault(_remote);
|
|
10
10
|
|
|
11
|
-
globalThis.isServerMode =
|
|
11
|
+
globalThis.isServerMode =
|
|
12
|
+
typeof window === "undefined" && typeof global !== "undefined"
|
|
12
13
|
|
|
13
14
|
if (globalThis.isServerMode) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
15
|
+
const { Buffer } = require("buffer")
|
|
16
|
+
|
|
17
|
+
globalThis.b64Decode = (data) => {
|
|
18
|
+
return Buffer.from(data, "base64").toString("utf-8")
|
|
19
|
+
}
|
|
20
|
+
globalThis.b64Encode = (data) => {
|
|
21
|
+
return Buffer.from(data, "utf-8").toString("base64")
|
|
22
|
+
}
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
/**
|
|
@@ -29,76 +30,82 @@ if (globalThis.isServerMode) {
|
|
|
29
30
|
* @return {Promise<void>} A promise that resolves when all websockets have been created and event listeners have been registered.
|
|
30
31
|
*/
|
|
31
32
|
async function createWebsockets() {
|
|
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
|
-
|
|
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
|
+
}
|
|
102
109
|
} exports.createWebsockets = createWebsockets;
|
|
103
110
|
|
|
104
111
|
/**
|
|
@@ -107,13 +114,13 @@ if (globalThis.isServerMode) {
|
|
|
107
114
|
* @return {Promise<void>} A promise that resolves when all websocket instances have been disconnected.
|
|
108
115
|
*/
|
|
109
116
|
async function disconnectWebsockets() {
|
|
110
|
-
|
|
117
|
+
const instances = globalThis.__comty_shared_state.sockets
|
|
111
118
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
119
|
+
for (let [key, instance] of Object.entries(instances)) {
|
|
120
|
+
if (instance.connected) {
|
|
121
|
+
instance.disconnect()
|
|
122
|
+
}
|
|
123
|
+
}
|
|
117
124
|
} exports.disconnectWebsockets = disconnectWebsockets;
|
|
118
125
|
|
|
119
126
|
/**
|
|
@@ -122,20 +129,20 @@ if (globalThis.isServerMode) {
|
|
|
122
129
|
* @return {Promise<void>} A promise that resolves when all websocket instances have been reconnected.
|
|
123
130
|
*/
|
|
124
131
|
async function reconnectWebsockets() {
|
|
125
|
-
|
|
132
|
+
const instances = globalThis.__comty_shared_state.sockets
|
|
126
133
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
134
|
+
for (let [key, instance] of Object.entries(instances)) {
|
|
135
|
+
if (instance.connected) {
|
|
136
|
+
// disconnect first
|
|
137
|
+
instance.disconnect()
|
|
138
|
+
}
|
|
132
139
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
140
|
+
instance.auth = {
|
|
141
|
+
token: _withStorage2.default.engine.get("token"),
|
|
142
|
+
}
|
|
136
143
|
|
|
137
|
-
|
|
138
|
-
|
|
144
|
+
instance.connect()
|
|
145
|
+
}
|
|
139
146
|
} exports.reconnectWebsockets = reconnectWebsockets;
|
|
140
147
|
|
|
141
148
|
/**
|
|
@@ -144,21 +151,21 @@ if (globalThis.isServerMode) {
|
|
|
144
151
|
* @return {Promise<void>} Promise that resolves when all websocket instances have been reauthenticated.
|
|
145
152
|
*/
|
|
146
153
|
async function reauthenticateWebsockets() {
|
|
147
|
-
|
|
154
|
+
const instances = globalThis.__comty_shared_state.sockets
|
|
148
155
|
|
|
149
|
-
|
|
150
|
-
|
|
156
|
+
for (let [key, instance] of Object.entries(instances)) {
|
|
157
|
+
const token = _withStorage2.default.engine.get("token")
|
|
151
158
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
159
|
+
instance.auth = {
|
|
160
|
+
token: token,
|
|
161
|
+
}
|
|
155
162
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
163
|
+
if (!instance.connected) {
|
|
164
|
+
instance.connect()
|
|
165
|
+
} else {
|
|
166
|
+
instance.emit("auth:reauth", token)
|
|
167
|
+
}
|
|
168
|
+
}
|
|
162
169
|
} exports.reauthenticateWebsockets = reauthenticateWebsockets;
|
|
163
170
|
|
|
164
171
|
/**
|
|
@@ -168,54 +175,54 @@ if (globalThis.isServerMode) {
|
|
|
168
175
|
* @return {Object} sharedState - Object containing eventBus, mainOrigin, baseRequest, sockets, rest, and version
|
|
169
176
|
*/
|
|
170
177
|
function createClient({
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
178
|
+
accessKey = null,
|
|
179
|
+
privateKey = null,
|
|
180
|
+
enableWs = false,
|
|
181
|
+
origin = _remote2.default.origin,
|
|
175
182
|
} = {}) {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
183
|
+
const sharedState = (globalThis.__comty_shared_state = {
|
|
184
|
+
eventBus: new (0, _events2.default)(),
|
|
185
|
+
mainOrigin: origin,
|
|
186
|
+
baseRequest: null,
|
|
187
|
+
sockets: new Map(),
|
|
188
|
+
rest: null,
|
|
189
|
+
version: _packagejson2.default.version,
|
|
190
|
+
addons: new (0, _addons2.default)(),
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
if (privateKey && accessKey && globalThis.isServerMode) {
|
|
194
|
+
_withStorage2.default.engine.set("token", `${accessKey}:${privateKey}`)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
sharedState.baseRequest = _axios2.default.create({
|
|
198
|
+
baseURL: origin,
|
|
199
|
+
headers: {
|
|
200
|
+
"Content-Type": "application/json",
|
|
201
|
+
},
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
// create a interceptor to attach the token every request
|
|
205
|
+
sharedState.baseRequest.interceptors.request.use((config) => {
|
|
206
|
+
// check if current request has no Authorization header, if so, attach the token
|
|
207
|
+
if (!config.headers["Authorization"]) {
|
|
208
|
+
const sessionToken = _withStorage2.default.engine.get("token")
|
|
209
|
+
|
|
210
|
+
if (sessionToken) {
|
|
211
|
+
config.headers["Authorization"] =
|
|
212
|
+
`${globalThis.isServerMode ? "Server" : "Bearer"} ${sessionToken}`
|
|
213
|
+
} else {
|
|
214
|
+
console.warn("Making a request with no session token")
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return config
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
if (enableWs) {
|
|
222
|
+
createWebsockets()
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return sharedState
|
|
219
226
|
} exports.createClient = createClient;
|
|
220
227
|
|
|
221
|
-
exports. default = createClient
|
|
228
|
+
exports. default = createClient
|