comty.js 0.60.6 → 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 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;
@@ -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 _models = require('./models');
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 = typeof window === "undefined" && typeof global !== "undefined"
11
+ globalThis.isServerMode =
12
+ typeof window === "undefined" && typeof global !== "undefined"
12
13
 
13
14
  if (globalThis.isServerMode) {
14
- const { Buffer } = require("buffer")
15
-
16
- globalThis.b64Decode = (data) => {
17
- return Buffer.from(data, "base64").toString("utf-8")
18
- }
19
- globalThis.b64Encode = (data) => {
20
- return Buffer.from(data, "utf-8").toString("base64")
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
- if (!_remote2.default.websockets) {
33
- return false
34
- }
35
-
36
- const instances = globalThis.__comty_shared_state.sockets
37
-
38
- for (let [key, instance] of Object.entries(instances)) {
39
- if (instance.connected) {
40
- // disconnect first
41
- instance.disconnect()
42
- }
43
-
44
- // remove current listeners
45
- instance.removeAllListeners()
46
-
47
- delete globalThis.__comty_shared_state.sockets[key]
48
- }
49
-
50
- for (let ws of _remote2.default.websockets) {
51
- let opts = {
52
- transports: ["websocket"],
53
- autoConnect: _nullishCoalesce(ws.autoConnect, () => ( true)),
54
- forceNew: true,
55
- path: ws.path,
56
- ..._nullishCoalesce(ws.params, () => ( {})),
57
- }
58
-
59
- if (ws.noAuth !== true) {
60
- opts.auth = {
61
- token: _withStorage2.default.engine.get("token"),
62
- }
63
- }
64
-
65
- globalThis.__comty_shared_state.sockets[ws.namespace] = _socketioclient.io.call(void 0, _remote2.default.origin, opts)
66
- }
67
-
68
- // regsister events
69
- for (let [key, instance] of Object.entries(instances)) {
70
- instance.on("connect", () => {
71
- //console.debug(`[WS-API][${key}] Connected`)
72
-
73
- globalThis.__comty_shared_state.eventBus.emit(`${key}:connected`)
74
- })
75
-
76
- instance.on("disconnect", () => {
77
- //console.debug(`[WS-API][${key}] Disconnected`)
78
-
79
- globalThis.__comty_shared_state.eventBus.emit(`${key}:disconnected`)
80
- })
81
-
82
- instance.on("reconnect", () => {
83
- // console.debug(`[WS-API][${key}] Reconnected`)
84
-
85
- globalThis.__comty_shared_state.eventBus.emit(`${key}:reconnected`)
86
-
87
- reauthenticateWebsockets()
88
- })
89
-
90
- instance.on("error", (error) => {
91
- //console.error(`[WS-API][${key}] Error`, error)
92
-
93
- globalThis.__comty_shared_state.eventBus.emit(`${key}:error`, error)
94
- })
95
-
96
- instance.onAny((event, ...args) => {
97
- //console.debug(`[WS-API][${key}] Event (${event})`, ...args)
98
-
99
- globalThis.__comty_shared_state.eventBus.emit(`${key}:${event}`, ...args)
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
- const instances = globalThis.__comty_shared_state.sockets
117
+ const instances = globalThis.__comty_shared_state.sockets
111
118
 
112
- for (let [key, instance] of Object.entries(instances)) {
113
- if (instance.connected) {
114
- instance.disconnect()
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
- const instances = globalThis.__comty_shared_state.sockets
132
+ const instances = globalThis.__comty_shared_state.sockets
126
133
 
127
- for (let [key, instance] of Object.entries(instances)) {
128
- if (instance.connected) {
129
- // disconnect first
130
- instance.disconnect()
131
- }
134
+ for (let [key, instance] of Object.entries(instances)) {
135
+ if (instance.connected) {
136
+ // disconnect first
137
+ instance.disconnect()
138
+ }
132
139
 
133
- instance.auth = {
134
- token: _withStorage2.default.engine.get("token"),
135
- }
140
+ instance.auth = {
141
+ token: _withStorage2.default.engine.get("token"),
142
+ }
136
143
 
137
- instance.connect()
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
- const instances = globalThis.__comty_shared_state.sockets
154
+ const instances = globalThis.__comty_shared_state.sockets
148
155
 
149
- for (let [key, instance] of Object.entries(instances)) {
150
- const token = _withStorage2.default.engine.get("token")
156
+ for (let [key, instance] of Object.entries(instances)) {
157
+ const token = _withStorage2.default.engine.get("token")
151
158
 
152
- instance.auth = {
153
- token: token,
154
- }
159
+ instance.auth = {
160
+ token: token,
161
+ }
155
162
 
156
- if (!instance.connected) {
157
- instance.connect()
158
- } else {
159
- instance.emit("auth:reauth", token)
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
- accessKey = null,
172
- privateKey = null,
173
- enableWs = false,
174
- origin = _remote2.default.origin,
178
+ accessKey = null,
179
+ privateKey = null,
180
+ enableWs = false,
181
+ origin = _remote2.default.origin,
175
182
  } = {}) {
176
- const sharedState = globalThis.__comty_shared_state = {
177
- eventBus: new (0, _events2.default)(),
178
- mainOrigin: origin,
179
- baseRequest: null,
180
- sockets: new Map(),
181
- rest: null,
182
- version: _packagejson2.default.version,
183
- }
184
-
185
- sharedState.rest = _models.createHandlers.call(void 0, )
186
-
187
- if (privateKey && accessKey && globalThis.isServerMode) {
188
- _withStorage2.default.engine.set("token", `${accessKey}:${privateKey}`)
189
- }
190
-
191
- sharedState.baseRequest = _axios2.default.create({
192
- baseURL: origin,
193
- headers: {
194
- "Content-Type": "application/json",
195
- }
196
- })
197
-
198
- // create a interceptor to attach the token every request
199
- sharedState.baseRequest.interceptors.request.use((config) => {
200
- // check if current request has no Authorization header, if so, attach the token
201
- if (!config.headers["Authorization"]) {
202
- const sessionToken = _withStorage2.default.engine.get("token")
203
-
204
- if (sessionToken) {
205
- config.headers["Authorization"] = `${globalThis.isServerMode ? "Server" : "Bearer"} ${sessionToken}`
206
- } else {
207
- console.warn("Making a request with no session token")
208
- }
209
- }
210
-
211
- return config
212
- })
213
-
214
- if (enableWs) {
215
- createWebsockets()
216
- }
217
-
218
- return sharedState
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