@cocreate/socket-server 1.27.1 → 1.28.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,10 @@
1
+ # [1.28.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.27.1...v1.28.0) (2024-01-30)
2
+
3
+
4
+ ### Features
5
+
6
+ * seperate onMessage from Message in order to to support internal message ([231f2cc](https://github.com/CoCreate-app/CoCreate-socket-server/commit/231f2cc796a4ddc35aa93960e3e0cae87527853e))
7
+
1
8
  ## [1.27.1](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.27.0...v1.27.1) (2024-01-17)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/socket-server",
3
- "version": "1.27.1",
3
+ "version": "1.28.0",
4
4
  "description": "CoCreate-socket-server",
5
5
  "keywords": [
6
6
  "cocreate-socket",
package/src/index.js CHANGED
@@ -319,105 +319,116 @@ class SocketServer extends EventEmitter {
319
319
 
320
320
  async onMessage(socket, message) {
321
321
  try {
322
- const organization_id = socket.organization_id
323
-
324
322
  this.emit("setBandwidth", {
325
323
  type: 'in',
326
324
  data: message,
327
- organization_id
325
+ organization_id: socket.organization_id
328
326
  });
329
327
 
330
328
 
331
329
  let data = JSON.parse(message)
332
- if (data.method) {
333
- const organization = this.organizations.get(organization_id)
334
- if (organization && organization.organizationBalance == false) {
335
- data.organizationBalance = false
336
- data.error = organization.error
337
- return socket.send(JSON.stringify(data))
338
- }
330
+ if (data.method)
331
+ this.Message(socket, data)
332
+ else {
333
+ data.error = 'method is required'
334
+ return socket.send(JSON.stringify(data))
335
+ }
336
+ } catch (e) {
337
+ console.log(e);
338
+ }
339
+ }
339
340
 
340
- if (data.method === 'region.added' || data.method === 'region.removed')
341
- console.log('data.method: ', data.method)
342
-
343
- if (socket.user_id && socket.expires && new Date(new Date().toISOString()).getTime() >= socket.expires) {
344
- // data.error = 'Token expired'
345
- // socket.send(JSON.stringify(data))
346
- await this.send({
347
- socket, method: 'updateUserStatus', user_id: socket.user_id, clientId: data.clientId, userStatus: 'off', socketId: data.socketId, organization_id
348
- })
349
- socket.user_id = socket.expires = null
350
- return
351
- }
341
+ async Message(socket, data) {
342
+ try {
343
+ const organization_id = socket.organization_id
352
344
 
353
- if (this.authorize) {
354
- if (!this.sockets.has(socket.id)) {
355
- if (organization && organization.organizationBalance == false) {
356
- data.organizationBalance = false
357
- data.error = organization.error
358
- return socket.send(JSON.stringify(data))
359
- }
360
- }
345
+ const organization = this.organizations.get(organization_id)
346
+ if (organization && organization.organizationBalance == false) {
347
+ data.organizationBalance = false
348
+ data.error = organization.error
349
+ return socket.send(JSON.stringify(data))
350
+ }
361
351
 
362
- data.socket = socket
363
- data.host = socket.host
352
+ if (data.method === 'region.added' || data.method === 'region.removed')
353
+ console.log('data.method: ', data.method)
364
354
 
365
- const authorized = await this.authorize.check(data, socket.user_id)
366
- let errors = {}
367
- if (authorized === false) {
368
- delete data.socket
369
- data.error = 'authorization failed'
355
+ if (socket.user_id && socket.expires && new Date(new Date().toISOString()).getTime() >= socket.expires) {
356
+ // data.error = 'Token expired'
357
+ // socket.send(JSON.stringify(data))
358
+ await this.send({
359
+ socket, method: 'updateUserStatus', user_id: socket.user_id, clientId: data.clientId, userStatus: 'off', socketId: data.socketId, organization_id
360
+ })
361
+ socket.user_id = socket.expires = null
362
+ return
363
+ }
364
+
365
+ if (this.authorize) {
366
+ if (!this.sockets.has(socket.id)) {
367
+ if (organization && organization.organizationBalance == false) {
368
+ data.organizationBalance = false
369
+ data.error = organization.error
370
370
  return socket.send(JSON.stringify(data))
371
- } else if (authorized.serverOrganization === false) {
371
+ }
372
+ }
373
+
374
+ data.socket = socket
375
+ data.host = socket.host
376
+
377
+ const authorized = await this.authorize.check(data, socket.user_id)
378
+ let errors = {}
379
+ if (authorized === false) {
380
+ delete data.socket
381
+ data.error = 'authorization failed'
382
+ return socket.send(JSON.stringify(data))
383
+ } else if (authorized.serverOrganization === false) {
384
+ organization.status = errors.status = false;
385
+ organization.serverOrganization = false;
386
+ organization.error = authorized.error
387
+ } else if (authorized.serverStorage === false) {
388
+ data.database = process.env.organization_id
389
+ data.organization_id = process.env.organization_id
390
+
391
+ const authorized2 = await this.authorize.check(data, req, socket.user_id)
392
+ if (!authorized2 || authorized2.error) {
372
393
  organization.status = errors.status = false;
373
- organization.serverOrganization = false;
374
- organization.error = authorized.error
375
- } else if (authorized.serverStorage === false) {
376
- data.database = process.env.organization_id
377
- data.organization_id = process.env.organization_id
378
-
379
- const authorized2 = await this.authorize.check(data, req, socket.user_id)
380
- if (!authorized2 || authorized2.error) {
381
- organization.status = errors.status = false;
382
- organization.error = errors.error = authorized.error
383
- if (authorized2.serverOrganization === false) {
384
- organization.serverOrganization = false;
385
- }
386
- if (authorized2.serverStorage === false) {
387
- organization.serverStorage = false;
388
- }
394
+ organization.error = errors.error = authorized.error
395
+ if (authorized2.serverOrganization === false) {
396
+ organization.serverOrganization = false;
397
+ }
398
+ if (authorized2.serverStorage === false) {
399
+ organization.serverStorage = false;
389
400
  }
390
- } else if (authorized.error) {
391
- organization.status = false;
392
- organization.error = authorized.error
393
401
  }
402
+ } else if (authorized.error) {
403
+ organization.status = false;
404
+ organization.error = authorized.error
405
+ }
394
406
 
395
- if (organization && organization.status === false) {
396
- let errors = {}
397
- data.serverOrganization = organization.serverOrganization
398
- data.serverStorage = organization.serverStorage
399
- data.organizationBalance = organization.organizationBalance
400
- data.error = organization.error
401
- delete data.socket
402
- return socket.send(JSON.stringify(data))
403
- }
407
+ if (organization && organization.status === false) {
408
+ let errors = {}
409
+ data.serverOrganization = organization.serverOrganization
410
+ data.serverStorage = organization.serverStorage
411
+ data.organizationBalance = organization.organizationBalance
412
+ data.error = organization.error
413
+ delete data.socket
414
+ return socket.send(JSON.stringify(data))
415
+ }
404
416
 
405
- // dburl is true and db does not have 'keys' array
406
- // action: syncCollection data{array: 'keys', object[]}
407
- // actions: add keys as once keys are added admin user can do anything
417
+ // dburl is true and db does not have 'keys' array
418
+ // action: syncCollection data{array: 'keys', object[]}
419
+ // actions: add keys as once keys are added admin user can do anything
408
420
 
409
421
 
410
- // }
422
+ // }
411
423
 
412
- if (authorized.authorized)
413
- data = authorized.authorized
424
+ if (authorized.authorized)
425
+ data = authorized.authorized
414
426
 
415
- let mod = data.method.split('.')[0]
416
- if (['storage', 'database', 'array', 'index', 'object'].includes(mod))
417
- this.emit(data.method, data);
418
- else
419
- this.emit(mod, data);
420
- }
427
+ let moduleName = data.method.split('.')[0]
428
+ if (['storage', 'database', 'array', 'index', 'object'].includes(moduleName))
429
+ this.emit(data.method, data);
430
+ else
431
+ this.emit(moduleName, data);
421
432
  }
422
433
  } catch (e) {
423
434
  console.log(e);