@cocreate/socket-server 1.19.5 → 1.20.0

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.20.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.19.5...v1.20.0) (2023-10-19)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * delete data.socket ([4fd38ff](https://github.com/CoCreate-app/CoCreate-socket-server/commit/4fd38ffd9989502955eb73d7d21f90102ea41e69))
7
+ * improved error handling ([04be91f](https://github.com/CoCreate-app/CoCreate-socket-server/commit/04be91ffcc62c7270fc83cbb1c755125485496fb))
8
+
9
+
10
+ ### Features
11
+
12
+ * delay user deletion for potential reconnect ([34e3a14](https://github.com/CoCreate-app/CoCreate-socket-server/commit/34e3a143776a0e67f8cea56ab2f0334a1e349df2))
13
+ * If authinticated emit 'notification.user' ([ac4779f](https://github.com/CoCreate-app/CoCreate-socket-server/commit/ac4779f0773e5f9b906390648c536e654cc1f40a))
14
+
1
15
  ## [1.19.5](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.19.4...v1.19.5) (2023-10-16)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/socket-server",
3
- "version": "1.19.5",
3
+ "version": "1.20.0",
4
4
  "description": "CoCreate-socket-server",
5
5
  "keywords": [
6
6
  "cocreate-socket",
package/src/index.js CHANGED
@@ -42,8 +42,6 @@ class SocketServer extends EventEmitter {
42
42
  errors.error = organization.error
43
43
  return socket.send(JSON.stringify({ method: 'Access Denied', error: errors }))
44
44
  }
45
- // if (!organization)
46
- // return socket.send(JSON.stringify({ method: 'Access Denied', error: organization.error }))
47
45
 
48
46
  let options = decodeURIComponent(request.headers['sec-websocket-protocol'])
49
47
  options = JSON.parse(options)
@@ -86,18 +84,19 @@ class SocketServer extends EventEmitter {
86
84
 
87
85
  self.emit('read.object', data);
88
86
 
89
- if (self.authenticate && options.token) {
90
- let { user_id, expires } = self.authenticate.decodeToken(options.token)
91
- // self.authenticate.decodeToken(options.token).then(({ user_id, expires }) => {
87
+ if (self.authenticate) {
88
+ const { user_id, expires } = self.authenticate.decodeToken(options.token)
89
+
92
90
  if (user_id) {
93
91
  socket.user_id = user_id;
94
92
  socket.expires = expires;
95
93
  self.emit('userStatus', { socket, method: 'userStatus', user_id, userStatus: 'on', organization_id });
94
+ self.emit("notification.user", socket)
96
95
  } else
97
- self.emit('userStatus', { socket, user_id, status: 'off', organization_id });
96
+ self.emit('userStatus', { socket, user_id: options.user_id, userStatus: 'off', organization_id });
98
97
 
99
98
  self.onWebSocket(socket);
100
- // })
99
+
101
100
  } else
102
101
  self.onWebSocket(socket);
103
102
  } else {
@@ -174,12 +173,13 @@ class SocketServer extends EventEmitter {
174
173
 
175
174
  if (socket.user_id) {
176
175
  this.emit('userStatus', { socket, user_id: socket.user_id, userStatus: 'on', organization_id });
176
+ let user = this.users.get(socket.user_id)
177
177
 
178
- if (!this.users.has(socket.user_id)) {
178
+ if (!Array.isArray(user)) {
179
+ clearTimeout(user)
179
180
  this.users.set(socket.user_id, [socket])
180
- } else {
181
- this.users.get(socket.user_id).push(socket)
182
- }
181
+ } else
182
+ user.push(socket)
183
183
  }
184
184
 
185
185
  }
@@ -278,13 +278,22 @@ class SocketServer extends EventEmitter {
278
278
  if (socket.user_id) {
279
279
  let sockets = this.users.get(socket.user_id)
280
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 });
281
+ if (Array.isArray(sockets) && sockets.length) {
282
+ const index = sockets.findIndex(item => item.id === socket.id);
283
+ if (index !== -1) {
284
+ sockets.splice(index, 1);
285
+ }
286
+ } else {
287
+ let userDebounceTimer = sockets
288
+
289
+ clearTimeout(userDebounceTimer);
290
+ userDebounceTimer = setTimeout(() => {
291
+ this.users.delete(socket.user_id);
292
+ this.emit('userStatus', { socket, user_id: socket.user_id, userStatus: 'off', organization_id });
293
+ }, 10000);
294
+
295
+ this.users.set(socket.user_id, userDebounceTimer)
296
+
288
297
  }
289
298
  }
290
299
  }
@@ -301,12 +310,16 @@ class SocketServer extends EventEmitter {
301
310
  organization_id
302
311
  });
303
312
 
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 })
307
313
 
308
314
  let data = JSON.parse(message)
309
315
  if (data.method) {
316
+ const organization = this.organizations.get(organization_id)
317
+ if (organization && organization.organizationBalance == false) {
318
+ data.organizationBalance = false
319
+ data.error = organization.error
320
+ return socket.send(JSON.stringify(data))
321
+ }
322
+
310
323
  if (data.method === 'region.added' || data.method === 'region.removed')
311
324
  console.log('data.method: ', data.method)
312
325
 
@@ -317,8 +330,11 @@ class SocketServer extends EventEmitter {
317
330
 
318
331
  if (this.authorize) {
319
332
  if (!this.sockets.has(socket.id)) {
320
- if (organization && !organization.organizationBalance == false)
321
- return socket.send({ method: 'Access Denied', organizationBalance: false, error: organization.error })
333
+ if (organization && organization.organizationBalance == false) {
334
+ data.organizationBalance = false
335
+ data.error = organization.error
336
+ return socket.send(JSON.stringify(data))
337
+ }
322
338
  }
323
339
 
324
340
  data.socket = socket
@@ -351,11 +367,12 @@ class SocketServer extends EventEmitter {
351
367
 
352
368
  if (organization && organization.status === false) {
353
369
  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 }))
370
+ data.serverOrganization = organization.serverOrganization
371
+ data.serverStorage = organization.serverStorage
372
+ data.organizationBalance = organization.organizationBalance
373
+ data.error = organization.error
374
+ delete data.socket
375
+ return socket.send(JSON.stringify(data))
359
376
  }
360
377
 
361
378
  // dburl is true and db does not have 'keys' array