@cocreate/socket-server 1.8.1 → 1.8.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,10 @@
1
+ ## [1.8.2](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.8.1...v1.8.2) (2023-05-21)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * Refactor socket server to track and limit server bandwidth usage. ([0c4c0f4](https://github.com/CoCreate-app/CoCreate-socket-server/commit/0c4c0f438a27e75d659044bfdcc19f422df67347))
7
+
1
8
  ## [1.8.1](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.8.0...v1.8.1) (2023-05-20)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/socket-server",
3
- "version": "1.8.1",
3
+ "version": "1.8.2",
4
4
  "description": "CoCreate-socket-server",
5
5
  "keywords": [
6
6
  "cocreate-socket",
package/src/index.js CHANGED
@@ -87,7 +87,7 @@ class SocketServer extends EventEmitter {
87
87
  let total_cnt = 0;
88
88
  this.clients.forEach((c) => total_cnt += c.length)
89
89
 
90
- this.emit("createMetrics", null, {
90
+ this.emit("createMetrics", {
91
91
  organization_id,
92
92
  client_cnt: room_clients.length,
93
93
  total_cnt: total_cnt
@@ -109,7 +109,7 @@ class SocketServer extends EventEmitter {
109
109
  if (user_id)
110
110
  this.emit('userStatus', socket, { user_id, status: 'off', organization_id });
111
111
 
112
- this.emit("deleteMetrics", null, { organization_id });
112
+ this.emit("deleteMetrics", { organization_id });
113
113
  this.emit("deletePermissions", organization_id);
114
114
  this.emit('disconnect', organization_id)
115
115
  this.asyncMessages.delete(key);
@@ -117,7 +117,7 @@ class SocketServer extends EventEmitter {
117
117
  let total_cnt = 0;
118
118
  this.clients.forEach((c) => total_cnt += c.length)
119
119
 
120
- this.emit("changeCountMetrics", null, {
120
+ this.emit("changeCountMetrics", {
121
121
  organization_id,
122
122
  total_cnt,
123
123
  client_cnt: room_clients.length
@@ -129,19 +129,14 @@ class SocketServer extends EventEmitter {
129
129
  async onMessage(req, socket, message) {
130
130
  try {
131
131
  const organization_id = socket.config.organization_id
132
-
133
- // TODO: remove
134
- // if (message instanceof Buffer) {
135
- // this.emit('importFile2DB', socket, message);
136
- // console.log('importFile2DB', socket, message);
137
- // return;
138
- // }
139
-
140
132
  const { action, data } = JSON.parse(message)
141
133
 
142
134
  if (action) {
143
- this.recordTransfer('in', message, organization_id)
144
-
135
+ this.emit("setBandwidth", {
136
+ type: 'in',
137
+ data: message,
138
+ organization_id
139
+ });
145
140
  let user_id = null;
146
141
  if (this.authInstance)
147
142
  user_id = await this.authInstance.getUserId(req);
@@ -149,10 +144,6 @@ class SocketServer extends EventEmitter {
149
144
  if (this.permissionInstance) {
150
145
  const permission = await this.permissionInstance.check(action, data, req, user_id)
151
146
  if (!permission || permission.error) {
152
- // if (action == 'syncServer' && permission.database === true)
153
- // if (action == 'syncServer')
154
- // this.emit('createDocument', socket, data);
155
- // else
156
147
  if (permission.dbUrl === false) {
157
148
  data.database = process.env.organization_id
158
149
  data.organization_id = process.env.organization_id
@@ -161,33 +152,39 @@ class SocketServer extends EventEmitter {
161
152
  if (!permission2 || permission2.error) {
162
153
  return this.send(socket, 'Access Denied', { action, permission2, ...data })
163
154
  }
164
- } else
155
+ } else if (!user_id) {
165
156
  return this.send(socket, 'Access Denied', { action, permission, ...data })
157
+ }
158
+ // dburl is true and db does not have 'keys' collection
159
+ // action: syncCollection data{collection: 'keys', document[]}
160
+ // actions: add keys as once keys are added admin user can do anything
161
+
162
+
166
163
  }
167
- }
168
164
 
169
- if (user_id) {
170
- if (!socket.config.user_id) {
171
- socket.config.user_id = user_id
172
- this.emit('userStatus', socket, { user_id, userStatus: 'on', organization_id });
165
+ if (user_id) {
166
+ if (!socket.config.user_id) {
167
+ socket.config.user_id = user_id
168
+ this.emit('userStatus', socket, { user_id, userStatus: 'on', organization_id });
169
+ }
170
+ } else {
171
+ this.send(socket, 'updateUserStatus', { userStatus: 'off', clientId: data.clientId, organization_id })
173
172
  }
174
- } else {
175
- this.send(socket, 'updateUserStatus', { userStatus: 'off', clientId: data.clientId, organization_id })
176
- }
177
173
 
178
- //. checking async status....
179
- if (data.async == true) {
180
- console.log('async true')
181
- const asyncMessage = this.asyncMessages.get(socket.config.key);
182
- socket.config.asyncId = uid.generate();
183
- if (asyncMessage) {
184
- asyncMessage.defineMessage(socket.config.asyncId);
174
+ //. checking async status....
175
+ if (data.async == true) {
176
+ console.log('async true')
177
+ const asyncMessage = this.asyncMessages.get(socket.config.key);
178
+ socket.config.asyncId = uid.generate();
179
+ if (asyncMessage) {
180
+ asyncMessage.defineMessage(socket.config.asyncId);
181
+ }
185
182
  }
186
- }
187
183
 
188
- this.emit(action, socket, data);
189
- }
184
+ this.emit(action, socket, data);
190
185
 
186
+ }
187
+ }
191
188
  } catch (e) {
192
189
  console.log(e);
193
190
  }
@@ -232,9 +229,14 @@ class SocketServer extends EventEmitter {
232
229
  if (isAsync) {
233
230
  asyncData.push({ socket: client, message: responseData })
234
231
  } else {
232
+ // TODO: check permission and remove any items client does not have a permission
235
233
  client.send(responseData);
236
234
  }
237
- self.recordTransfer('out', responseData, organization_id)
235
+ this.emit("setBandwidth", {
236
+ type: 'out',
237
+ data: responseData,
238
+ organization_id
239
+ })
238
240
  }
239
241
  })
240
242
  }
@@ -248,9 +250,15 @@ class SocketServer extends EventEmitter {
248
250
  if (isAsync) {
249
251
  asyncData.push({ socket: client, message: responseData })
250
252
  } else {
253
+ // TODO: check permission and remove any items client does not have a permission
251
254
  client.send(responseData);
252
255
  }
253
- self.recordTransfer('out', responseData, organization_id)
256
+ this.emit("setBandwidth", {
257
+ type: 'out',
258
+ data: responseData,
259
+ organization_id
260
+ })
261
+
254
262
  }
255
263
  })
256
264
  }
@@ -274,12 +282,16 @@ class SocketServer extends EventEmitter {
274
282
  if (asyncId && socket.config && socket.config.key) {
275
283
  this.asyncMessages.get(socket.config.key).setMessage(asyncId, [{ socket, message: responseData }]);
276
284
  } else {
285
+ // TODO: check permission and remove any items client does not have a permission
277
286
  socket.send(responseData);
278
287
  }
279
288
 
280
289
  if (socket.config && socket.config.organization_id)
281
- this.recordTransfer('out', responseData, socket.config.organization_id)
282
-
290
+ this.emit("setBandwidth", {
291
+ type: 'out',
292
+ data: responseData,
293
+ organization_id: socket.config.organization_id
294
+ })
283
295
  }
284
296
 
285
297
  // addAsyncMessage(key) {
@@ -301,20 +313,6 @@ class SocketServer extends EventEmitter {
301
313
  }
302
314
  return params
303
315
  }
304
-
305
-
306
- sendBinary(socket, data, organization_id) {
307
- socket.send(data, { binary: true });
308
- this.recordTransfer('out', data, organization_id)
309
- }
310
-
311
- recordTransfer(type, data, organization_id) {
312
- this.emit("setBandwidth", null, {
313
- type,
314
- data,
315
- organization_id
316
- });
317
- }
318
316
  }
319
317
 
320
318