@cocreate/socket-server 1.27.0 → 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 +14 -0
- package/package.json +1 -1
- package/src/index.js +102 -91
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
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
|
+
|
|
8
|
+
## [1.27.1](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.27.0...v1.27.1) (2024-01-17)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* handling of host and origin. Updated to support new query system ([61a1c68](https://github.com/CoCreate-app/CoCreate-socket-server/commit/61a1c685a1d3466836b19867558842632c7994a4))
|
|
14
|
+
|
|
1
15
|
# [1.27.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.26.0...v1.27.0) (2024-01-08)
|
|
2
16
|
|
|
3
17
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -25,11 +25,11 @@ class SocketServer extends EventEmitter {
|
|
|
25
25
|
socket.destroy();
|
|
26
26
|
});
|
|
27
27
|
|
|
28
|
-
server.https.on('upgrade', (request, socket, head) => this.upgrade(request, socket, head))
|
|
29
|
-
server.http.on('upgrade', (request, socket, head) => this.upgrade(request, socket, head))
|
|
28
|
+
server.https.on('upgrade', (request, socket, head) => this.upgrade(request, socket, head, 'wss'))
|
|
29
|
+
server.http.on('upgrade', (request, socket, head) => this.upgrade(request, socket, head, 'ws'))
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
upgrade(request, socket, head) {
|
|
32
|
+
upgrade(request, socket, head, protocol) {
|
|
33
33
|
const self = this;
|
|
34
34
|
let organization_id = request.url.split('/')
|
|
35
35
|
organization_id = organization_id[organization_id.length - 1]
|
|
@@ -53,20 +53,19 @@ class SocketServer extends EventEmitter {
|
|
|
53
53
|
socket.id = options.socketId;
|
|
54
54
|
socket.clientId = options.clientId;
|
|
55
55
|
socket.pathname = request.url
|
|
56
|
-
socket.origin = request.headers.origin
|
|
56
|
+
socket.origin = request.headers.origin
|
|
57
|
+
socket.host = request.headers.host
|
|
57
58
|
|
|
58
|
-
if (socket.origin.
|
|
59
|
-
socket.origin = socket.origin.replace('http', 'ws')
|
|
60
|
-
|
|
61
|
-
socket.socketUrl = socket.origin + socket.pathname
|
|
62
|
-
|
|
63
|
-
if (socket.origin && socket.origin !== 'null') {
|
|
59
|
+
if (!socket.host && socket.origin && socket.origin !== 'null') {
|
|
64
60
|
if (socket.origin.includes('://'))
|
|
65
61
|
socket.host = new URL(socket.origin).host
|
|
66
62
|
else
|
|
67
63
|
socket.host = socket.origin;
|
|
68
64
|
}
|
|
69
65
|
|
|
66
|
+
socket.socketUrl = protocol + '://' + socket.host + socket.pathname
|
|
67
|
+
|
|
68
|
+
|
|
70
69
|
// if (!await server.acme.checkCertificate(socket.host, organization_id))
|
|
71
70
|
// return socket.send(JSON.stringify({ method: 'Access Denied', error: 'Host not whitelisted' }))
|
|
72
71
|
|
|
@@ -85,9 +84,10 @@ class SocketServer extends EventEmitter {
|
|
|
85
84
|
}
|
|
86
85
|
|
|
87
86
|
if (options.lastSynced)
|
|
88
|
-
data.$filter.query =
|
|
89
|
-
|
|
90
|
-
|
|
87
|
+
data.$filter.query = {
|
|
88
|
+
_id: { $gt: options.lastSynced }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
91
|
else
|
|
92
92
|
data.$filter.limit = 1
|
|
93
93
|
|
|
@@ -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
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
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
|
-
|
|
341
|
-
|
|
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
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
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
|
-
|
|
363
|
-
|
|
352
|
+
if (data.method === 'region.added' || data.method === 'region.removed')
|
|
353
|
+
console.log('data.method: ', data.method)
|
|
364
354
|
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
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
|
-
}
|
|
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.
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
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
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
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
|
-
|
|
406
|
-
|
|
407
|
-
|
|
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
|
-
|
|
413
|
-
|
|
424
|
+
if (authorized.authorized)
|
|
425
|
+
data = authorized.authorized
|
|
414
426
|
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
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);
|