@cocreate/socket-server 1.19.3 → 1.19.5

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.19.5](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.19.4...v1.19.5) (2023-10-16)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * improved error handling ([a3e5d31](https://github.com/CoCreate-app/CoCreate-socket-server/commit/a3e5d314143f135816de7b51696626c53e6a88f0))
7
+
8
+ ## [1.19.4](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.19.3...v1.19.4) (2023-10-14)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * bump dependencies ([01451f6](https://github.com/CoCreate-app/CoCreate-socket-server/commit/01451f65ba929621a6ad0ea54194263b292ebcd2))
14
+
1
15
  ## [1.19.3](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.19.2...v1.19.3) (2023-10-09)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/socket-server",
3
- "version": "1.19.3",
3
+ "version": "1.19.5",
4
4
  "description": "CoCreate-socket-server",
5
5
  "keywords": [
6
6
  "cocreate-socket",
@@ -40,7 +40,7 @@
40
40
  },
41
41
  "homepage": "https://cocreate.app/docs/CoCreate-socket-server",
42
42
  "dependencies": {
43
- "@cocreate/config": "^1.6.0",
43
+ "@cocreate/config": "^1.6.1",
44
44
  "@cocreate/uuid": "^1.7.2",
45
45
  "@cocreate/utils": "^1.25.0",
46
46
  "ws": "7.5.9"
package/src/index.js CHANGED
@@ -31,8 +31,20 @@ class SocketServer extends EventEmitter {
31
31
  let organization_id = request.url.split('/')
32
32
  organization_id = organization_id[organization_id.length - 1]
33
33
 
34
- if (organization_id) {
35
- this.wss.handleUpgrade(request, socket, head, function (socket) {
34
+ this.wss.handleUpgrade(request, socket, head, function (socket) {
35
+ if (organization_id) {
36
+ let organization = self.organizations.get(organization_id)
37
+ if (organization && organization.status === false) {
38
+ let errors = {}
39
+ errors.serverOrganization = organization.serverOrganization
40
+ errors.serverStorage = organization.serverStorage
41
+ errors.organizationBalance = organization.organizationBalance
42
+ errors.error = organization.error
43
+ return socket.send(JSON.stringify({ method: 'Access Denied', error: errors }))
44
+ }
45
+ // if (!organization)
46
+ // return socket.send(JSON.stringify({ method: 'Access Denied', error: organization.error }))
47
+
36
48
  let options = decodeURIComponent(request.headers['sec-websocket-protocol'])
37
49
  options = JSON.parse(options)
38
50
 
@@ -86,11 +98,12 @@ class SocketServer extends EventEmitter {
86
98
 
87
99
  self.onWebSocket(socket);
88
100
  // })
89
- }
90
- else
101
+ } else
91
102
  self.onWebSocket(socket);
92
- })
93
- }
103
+ } else {
104
+ socket.send(JSON.stringify({ method: 'Access Denied', error: 'An organization_id is required' }))
105
+ }
106
+ })
94
107
  });
95
108
  }
96
109
 
@@ -140,11 +153,17 @@ class SocketServer extends EventEmitter {
140
153
  organization_id
141
154
  });
142
155
 
143
- }
156
+ } else
157
+ clearTimeout(organization.debounce);
158
+
144
159
 
145
160
  if (!this.clients.has(socket.clientId)) {
146
161
  this.clients.set(socket.clientId, []);
147
- organization.clients[socket.clientId] = []
162
+
163
+ if (!organization.clients)
164
+ organization.clients = { [socket.clientId]: [] }
165
+ else
166
+ organization.clients[socket.clientId] = []
148
167
  }
149
168
 
150
169
  if (!this.sockets.has(socket.id)) {
@@ -179,9 +198,6 @@ class SocketServer extends EventEmitter {
179
198
  else
180
199
  sockets.push(...clients[client])
181
200
  }
182
- } else {
183
- // TODO: requires a messageQueue that expires
184
- console.log('organization could not be found', data)
185
201
  }
186
202
  } else if (data.broadcastSender !== false) {
187
203
  sockets.push(data.socket)
@@ -192,8 +208,9 @@ class SocketServer extends EventEmitter {
192
208
  delete(socket) {
193
209
  let organization_id = socket.organization_id
194
210
  if (this.organizations.has(organization_id)) {
195
- const clients = this.organizations.get(organization_id).clients;
196
-
211
+ let clients = this.organizations.get(organization_id)
212
+ if (clients)
213
+ clients.clients;
197
214
  // Check if the client exists
198
215
  if (clients && clients[socket.clientId]) {
199
216
  const client = clients[socket.clientId]
@@ -241,21 +258,34 @@ class SocketServer extends EventEmitter {
241
258
  }
242
259
 
243
260
  if (this.clients.size === 0) {
244
- this.organizations.delete(socket.organization_id);
245
- }
246
-
247
- this.sockets.delete(socket.id);
248
-
249
- if (socket.user_id) {
250
- let sockets = this.users.get(socket.user_id)
251
- if (sockets) {
252
- const index = sockets.findIndex(item => item.id === socket.id);
253
- if (index !== -1) {
254
- sockets.splice(index, 1);
255
- }
256
- if (!sockets.length) {
257
- this.users.delete(socket.user_id);
258
- this.emit('userStatus', { socket, user_id, status: 'off', organization_id });
261
+ let organization = this.organizations.get(socket.organization_id)
262
+ let debounceTimer
263
+ if (organization)
264
+ debounceTimer = organization.debounce
265
+
266
+ clearTimeout(debounceTimer);
267
+ debounceTimer = setTimeout(() => {
268
+ this.organizations.delete(socket.organization_id);
269
+ }, 10000);
270
+
271
+ if (!organization)
272
+ this.organizations.set(socket.organization_id, { debounce: debounceTimer })
273
+ else
274
+ organization.debounce = debounceTimer
275
+
276
+ this.sockets.delete(socket.id);
277
+
278
+ if (socket.user_id) {
279
+ let sockets = this.users.get(socket.user_id)
280
+ if (sockets) {
281
+ const index = sockets.findIndex(item => item.id === socket.id);
282
+ if (index !== -1) {
283
+ sockets.splice(index, 1);
284
+ }
285
+ if (!sockets.length) {
286
+ this.users.delete(socket.user_id);
287
+ this.emit('userStatus', { socket, user_id, status: 'off', organization_id });
288
+ }
259
289
  }
260
290
  }
261
291
  }
@@ -271,8 +301,9 @@ class SocketServer extends EventEmitter {
271
301
  organization_id
272
302
  });
273
303
 
274
- if (this.organizations.has(organization_id) && !this.organizations.get(organization_id).status)
275
- return this.send({ socket, method: 'Access Denied', balance: 'Your balance has fallen bellow 0' })
304
+ const organization = this.organizations.get(organization_id)
305
+ if (organization && organization.organizationBalance == false)
306
+ return socket.send({ method: 'Access Denied', organizationBalance: false, error: organization.error })
276
307
 
277
308
  let data = JSON.parse(message)
278
309
  if (data.method) {
@@ -286,24 +317,47 @@ class SocketServer extends EventEmitter {
286
317
 
287
318
  if (this.authorize) {
288
319
  if (!this.sockets.has(socket.id)) {
289
- if (!this.organizations.get(organization_id).status)
290
- return this.send({ socket, method: 'Access Denied', balance: 'Your balance has fallen bellow 0' })
320
+ if (organization && !organization.organizationBalance == false)
321
+ return socket.send({ method: 'Access Denied', organizationBalance: false, error: organization.error })
291
322
  }
292
323
 
293
324
  data.socket = socket
294
325
 
295
326
  const authorized = await this.authorize.check(data, socket.user_id)
296
- if (authorized.storage === false) {
327
+ let errors = {}
328
+ if (authorized.serverOrganization === false) {
329
+ organization.status = errors.status = false;
330
+ organization.serverOrganization = false;
331
+ organization.error = authorized.error
332
+ } else if (authorized.serverStorage === false) {
297
333
  data.database = process.env.organization_id
298
334
  data.organization_id = process.env.organization_id
299
335
 
300
336
  const authorized2 = await this.authorize.check(data, req, socket.user_id)
301
337
  if (!authorized2 || authorized2.error) {
302
- return this.send({ socket, method: 'Access Denied', authorized2, ...data })
338
+ organization.status = errors.status = false;
339
+ organization.error = errors.error = authorized.error
340
+ if (authorized2.serverOrganization === false) {
341
+ organization.serverOrganization = false;
342
+ }
343
+ if (authorized2.serverStorage === false) {
344
+ organization.serverStorage = false;
345
+ }
303
346
  }
304
- } else if (!authorized || authorized.error) {
305
- return this.send({ socket, method: 'Access Denied', authorized, ...data })
347
+ } else if (authorized.error) {
348
+ organization.status = false;
349
+ organization.error = authorized.error
306
350
  }
351
+
352
+ if (organization && organization.status === false) {
353
+ let errors = {}
354
+ errors.serverOrganization = organization.serverOrganization
355
+ errors.serverStorage = organization.serverStorage
356
+ errors.organizationBalance = organization.organizationBalance
357
+ errors.error = organization.error
358
+ return socket.send(JSON.stringify({ method: 'Access Denied', ...errors }))
359
+ }
360
+
307
361
  // dburl is true and db does not have 'keys' array
308
362
  // action: syncCollection data{array: 'keys', object[]}
309
363
  // actions: add keys as once keys are added admin user can do anything