@cocreate/socket-server 1.10.0 → 1.10.2

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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [1.10.2](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.10.1...v1.10.2) (2023-05-25)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * Delete AsyncMessage.js as by design the results are async & refactor index.js Emitting; authorized data in send method ([b47e6fc](https://github.com/CoCreate-app/CoCreate-socket-server/commit/b47e6fc0386b4beb25eefba90e74f47e91f949f2))
7
+
8
+ ## [1.10.1](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.10.0...v1.10.1) (2023-05-25)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * Update dependencies versions ([d3cad89](https://github.com/CoCreate-app/CoCreate-socket-server/commit/d3cad89ad28f8b90c89b9fd3f30acab59496a623))
14
+
1
15
  # [1.10.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.9.0...v1.10.0) (2023-05-25)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/socket-server",
3
- "version": "1.10.0",
3
+ "version": "1.10.2",
4
4
  "description": "CoCreate-socket-server",
5
5
  "keywords": [
6
6
  "cocreate-socket",
@@ -40,8 +40,8 @@
40
40
  },
41
41
  "homepage": "https://cocreate.app/docs/CoCreate-socket-server",
42
42
  "dependencies": {
43
- "@cocreate/docs": "^1.7.11",
44
- "@cocreate/uuid": "^1.4.10"
43
+ "@cocreate/docs": "^1.7.13",
44
+ "@cocreate/uuid": "^1.4.11"
45
45
  },
46
46
  "devDependencies": {
47
47
  "express": "^4.17.1",
package/src/index.js CHANGED
@@ -1,19 +1,13 @@
1
1
  const WebSocket = require('ws');
2
2
  const url = require("url");
3
3
  const EventEmitter = require("events").EventEmitter;
4
- const AsyncMessage = require("./AsyncMessage")
5
4
  const uid = require('@cocreate/uuid')
6
5
 
7
6
  class SocketServer extends EventEmitter {
8
7
  constructor(prefix) {
9
8
  super();
10
-
11
9
  this.clients = new Map();
12
- this.asyncMessages = new Map();
13
-
14
10
  this.prefix = prefix || "crud";
15
-
16
- //. websocket server
17
11
  this.wss = new WebSocket.Server({ noServer: true });
18
12
  }
19
13
 
@@ -63,12 +57,6 @@ class SocketServer extends EventEmitter {
63
57
  room_clients = [socket];
64
58
  }
65
59
  this.clients.set(key, room_clients);
66
- // this.addAsyncMessage(key)
67
-
68
- let asyncMessage = this.asyncMessages.get(key)
69
- if (!asyncMessage) {
70
- this.asyncMessages.set(key, new AsyncMessage(key));
71
- }
72
60
 
73
61
  if (user_id)
74
62
  this.emit('userStatus', socket, { user_id, userStatus: 'on', organization_id });
@@ -99,10 +87,10 @@ class SocketServer extends EventEmitter {
99
87
  if (user_id)
100
88
  this.emit('userStatus', socket, { user_id, status: 'off', organization_id });
101
89
 
90
+ // TODO: remove if no one else connected to organization
102
91
  this.emit("deleteMetrics", { organization_id });
103
92
  this.emit("deletePermissions", organization_id);
104
93
  this.emit('disconnect', organization_id)
105
- this.asyncMessages.delete(key);
106
94
  } else {
107
95
  let total_cnt = 0;
108
96
  this.clients.forEach((c) => total_cnt += c.length)
@@ -167,16 +155,6 @@ class SocketServer extends EventEmitter {
167
155
  this.send(socket, 'updateUserStatus', { userStatus: 'off', clientId: data.clientId, organization_id })
168
156
  }
169
157
 
170
- //. checking async status....
171
- if (data.async == true) {
172
- console.log('async true')
173
- const asyncMessage = this.asyncMessages.get(socket.config.key);
174
- socket.config.asyncId = uid.generate();
175
- if (asyncMessage) {
176
- asyncMessage.defineMessage(socket.config.asyncId);
177
- }
178
- }
179
-
180
158
  this.emit(action, socket, data);
181
159
 
182
160
  }
@@ -190,13 +168,6 @@ class SocketServer extends EventEmitter {
190
168
  if (!data.uid)
191
169
  data.uid = uid.generate()
192
170
 
193
- const asyncId = socket.config.asyncId
194
- let isAsync = false;
195
- let asyncData = [];
196
- if (asyncId && socket.config && socket.config.key) {
197
- isAsync = true;
198
- }
199
-
200
171
  let organization_id = socket.config.organization_id;
201
172
  let url = `/${this.prefix}/${organization_id}`;
202
173
 
@@ -214,72 +185,46 @@ class SocketServer extends EventEmitter {
214
185
  let url = url;
215
186
  url += `/${room}`;
216
187
 
217
- this.send(socket, action, data, url, isAsync, asyncData)
188
+ this.send(socket, action, data, url)
218
189
  }
219
190
  } else {
220
- this.send(socket, action, data, url, isAsync, asyncData)
191
+ this.send(socket, action, data, url)
221
192
  }
193
+ }
222
194
 
223
- if (isAsync) {
224
- this.asyncMessages.get(socket.config.key).setMessage(asyncId, asyncData)
225
- }
195
+ async send(socket, action, data, url) {
196
+ let clients
197
+ if (!url)
198
+ clients = [socket]
199
+ else
200
+ clients = this.clients.get(url);
201
+
202
+ if (clients) {
203
+ for (let client of clients) {
204
+ if (socket != client && data.broadcast != false || socket == client && data.broadcastSender != false) {
205
+ const permission = await this.authorize.check(action, data, socket.config.user_id)
206
+ if (permission && permission.authorized)
207
+ data = permission.authorized
226
208
 
227
- }
209
+ let responseData = JSON.stringify({
210
+ action,
211
+ data
212
+ });
228
213
 
229
- async send(socket, action, data, url, isAsync, asyncData) {
230
- const asyncId = socket.config.asyncId
214
+ socket.send(responseData);
231
215
 
232
- if (!url && asyncId && socket.config && socket.config.key) {
233
- let responseData = JSON.stringify({
234
- action,
235
- data
236
- });
237
- this.asyncMessages.get(socket.config.key).setMessage(asyncId, [{ socket, message: responseData }]);
238
- } else {
239
- let clients
240
- if (!url)
241
- clients = [socket]
242
- else
243
- clients = this.clients.get(url);
244
-
245
- if (clients) {
246
- for (let client of clients) {
247
- if (socket != client && data.broadcast != false || socket == client && data.broadcastSender != false) {
248
- if (isAsync) {
249
- asyncData.push({ socket, message: JSON.stringify({ action, data }) })
250
- } else {
251
- const permission = await this.authorize.check(action, data, socket.config.user_id)
252
- if (permission && permission.authorized)
253
- data = permission.authorized
254
-
255
- let responseData = JSON.stringify({
256
- action,
257
- data
258
- });
259
-
260
- socket.send(responseData);
261
-
262
- if (socket.config && socket.config.organization_id)
263
- this.emit("setBandwidth", {
264
- type: 'out',
265
- data: responseData,
266
- organization_id: socket.config.organization_id
267
- })
268
- }
269
- }
216
+ if (socket.config && socket.config.organization_id)
217
+ this.emit("setBandwidth", {
218
+ type: 'out',
219
+ data: responseData,
220
+ organization_id: socket.config.organization_id
221
+ })
270
222
  }
271
223
  }
272
224
  }
273
225
 
274
226
  }
275
227
 
276
- // addAsyncMessage(key) {
277
- // let asyncMessage = this.asyncMessages.get(key)
278
- // if (!asyncMessage) {
279
- // this.asyncMessages.set(key, new AsyncMessage(key));
280
- // }
281
- // }
282
-
283
228
  getKeyFromUrl(pathname) {
284
229
  var path = pathname.split("/");
285
230
  var params = {
@@ -1,52 +0,0 @@
1
-
2
- class AsyncMessage {
3
- constructor(key) {
4
- this.key = key;
5
- this.messages = new Map();
6
- this.orders = [];
7
- }
8
-
9
- defineMessage(id) {
10
- this.orders.push(id);
11
- this.messages.set(id, null);
12
- }
13
-
14
- setMessage(id, data) {
15
- this.messages.set(id, data);
16
- if (this.orders.length > 0) {
17
- this.runMessage();
18
- }
19
-
20
- }
21
-
22
- runMessage() {
23
- let runIndex = -1;
24
- this.orders.some((key, index) => {
25
- let messageData = this.messages.get(key)
26
- if (messageData != null) {
27
- messageData.forEach(({socket, message}) => {
28
- try {
29
- if (socket) {
30
- socket.send(message);
31
- }
32
- } catch (error) {
33
- console.log(error);
34
- }
35
- })
36
-
37
- runIndex = index;
38
- this.messages.delete(key);
39
- return false;
40
- } else {
41
- return true;
42
- }
43
- })
44
-
45
- if (runIndex !== -1) {
46
- this.orders.splice(0, runIndex + 1);
47
- }
48
-
49
- }
50
- }
51
-
52
- module.exports = AsyncMessage;