@cocreate/socket-server 1.25.0 → 1.27.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 +24 -0
- package/package.json +4 -4
- package/src/index.js +81 -75
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,27 @@
|
|
|
1
|
+
# [1.27.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.26.0...v1.27.0) (2024-01-08)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* applied host to define environment/branch ([fad44e5](https://github.com/CoCreate-app/CoCreate-socket-server/commit/fad44e52babae6dd142c22e88214956e2d3dfd97))
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* bumped CoCreate dependencies to their latest versions ([b043307](https://github.com/CoCreate-app/CoCreate-socket-server/commit/b043307cb515a75a6fafd261ddb6295ceaba0ba9))
|
|
12
|
+
|
|
13
|
+
# [1.26.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.25.0...v1.26.0) (2024-01-03)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Bug Fixes
|
|
17
|
+
|
|
18
|
+
* comment checkCertificate ([f70e8de](https://github.com/CoCreate-app/CoCreate-socket-server/commit/f70e8def302498d1f93cc4badde012333bbe8ea0))
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
### Features
|
|
22
|
+
|
|
23
|
+
* Added '[@cocreate-server](https://github.com/cocreate-server)' handle http and https ([5f530b1](https://github.com/CoCreate-app/CoCreate-socket-server/commit/5f530b135cb25a3af51fe059af57a235d0c3d51c))
|
|
24
|
+
|
|
1
25
|
# [1.25.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.24.3...v1.25.0) (2023-12-31)
|
|
2
26
|
|
|
3
27
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/socket-server",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.27.0",
|
|
4
4
|
"description": "CoCreate-socket-server",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cocreate-socket",
|
|
@@ -40,9 +40,9 @@
|
|
|
40
40
|
},
|
|
41
41
|
"homepage": "https://cocreate.app/docs/CoCreate-socket-server",
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@cocreate/config": "^1.
|
|
44
|
-
"@cocreate/uuid": "^1.
|
|
45
|
-
"@cocreate/utils": "^1.
|
|
43
|
+
"@cocreate/config": "^1.10.0",
|
|
44
|
+
"@cocreate/uuid": "^1.10.0",
|
|
45
|
+
"@cocreate/utils": "^1.30.0",
|
|
46
46
|
"ws": "7.5.9"
|
|
47
47
|
}
|
|
48
48
|
}
|
package/src/index.js
CHANGED
|
@@ -25,94 +25,97 @@ class SocketServer extends EventEmitter {
|
|
|
25
25
|
socket.destroy();
|
|
26
26
|
});
|
|
27
27
|
|
|
28
|
-
server.on('upgrade', (request, socket, head) =>
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if (organization && organization.status === false) {
|
|
37
|
-
let errors = {}
|
|
38
|
-
errors.serverOrganization = organization.serverOrganization
|
|
39
|
-
errors.serverStorage = organization.serverStorage
|
|
40
|
-
errors.organizationBalance = organization.organizationBalance
|
|
41
|
-
errors.error = organization.error
|
|
42
|
-
return socket.send(JSON.stringify({ method: 'Access Denied', error: errors }))
|
|
43
|
-
}
|
|
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))
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
upgrade(request, socket, head) {
|
|
33
|
+
const self = this;
|
|
34
|
+
let organization_id = request.url.split('/')
|
|
35
|
+
organization_id = organization_id[organization_id.length - 1]
|
|
44
36
|
|
|
45
|
-
|
|
46
|
-
|
|
37
|
+
this.wss.handleUpgrade(request, socket, head, async function (socket) {
|
|
38
|
+
if (organization_id) {
|
|
39
|
+
let organization = self.organizations.get(organization_id)
|
|
40
|
+
if (organization && organization.status === false) {
|
|
41
|
+
let errors = {}
|
|
42
|
+
errors.serverOrganization = organization.serverOrganization
|
|
43
|
+
errors.serverStorage = organization.serverStorage
|
|
44
|
+
errors.organizationBalance = organization.organizationBalance
|
|
45
|
+
errors.error = organization.error
|
|
46
|
+
return socket.send(JSON.stringify({ method: 'Access Denied', error: errors }))
|
|
47
|
+
}
|
|
47
48
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
socket.clientId = options.clientId;
|
|
51
|
-
socket.pathname = request.url
|
|
52
|
-
socket.origin = request.headers.origin || request.headers.host
|
|
49
|
+
let options = decodeURIComponent(request.headers['sec-websocket-protocol'])
|
|
50
|
+
options = JSON.parse(options)
|
|
53
51
|
|
|
54
|
-
|
|
55
|
-
|
|
52
|
+
socket.organization_id = organization_id
|
|
53
|
+
socket.id = options.socketId;
|
|
54
|
+
socket.clientId = options.clientId;
|
|
55
|
+
socket.pathname = request.url
|
|
56
|
+
socket.origin = request.headers.origin || request.headers.host
|
|
56
57
|
|
|
57
|
-
|
|
58
|
+
if (socket.origin.startsWith('http'))
|
|
59
|
+
socket.origin = socket.origin.replace('http', 'ws')
|
|
58
60
|
|
|
59
|
-
|
|
60
|
-
if (socket.origin.includes('://'))
|
|
61
|
-
socket.host = new URL(socket.origin).host
|
|
62
|
-
else
|
|
63
|
-
socket.host = socket.origin;
|
|
64
|
-
}
|
|
61
|
+
socket.socketUrl = socket.origin + socket.pathname
|
|
65
62
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
method: 'object.read',
|
|
73
|
-
array: 'message_log',
|
|
74
|
-
$filter: {
|
|
75
|
-
sort: [
|
|
76
|
-
{ key: '_id', direction: 'desc' }
|
|
77
|
-
]
|
|
78
|
-
},
|
|
79
|
-
sync: true,
|
|
80
|
-
organization_id
|
|
81
|
-
}
|
|
63
|
+
if (socket.origin && socket.origin !== 'null') {
|
|
64
|
+
if (socket.origin.includes('://'))
|
|
65
|
+
socket.host = new URL(socket.origin).host
|
|
66
|
+
else
|
|
67
|
+
socket.host = socket.origin;
|
|
68
|
+
}
|
|
82
69
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
70
|
+
// if (!await server.acme.checkCertificate(socket.host, organization_id))
|
|
71
|
+
// return socket.send(JSON.stringify({ method: 'Access Denied', error: 'Host not whitelisted' }))
|
|
72
|
+
|
|
73
|
+
if (!organization || organization && organization.status !== false) {
|
|
74
|
+
let data = {
|
|
75
|
+
socket,
|
|
76
|
+
method: 'object.read',
|
|
77
|
+
array: 'message_log',
|
|
78
|
+
$filter: {
|
|
79
|
+
sort: [
|
|
80
|
+
{ key: '_id', direction: 'desc' }
|
|
86
81
|
]
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (self.authenticate) {
|
|
93
|
-
const { user_id, expires } = await self.authenticate.decodeToken(options.token, organization_id, options.clientId)
|
|
94
|
-
const userStatus = { socket, method: 'userStatus', user_id: options.user_id, clientId: options.clientId, userStatus: 'off', organization_id }
|
|
95
|
-
if (user_id) {
|
|
96
|
-
options.user_id = user_id
|
|
97
|
-
socket.user_id = user_id;
|
|
98
|
-
socket.expires = expires;
|
|
99
|
-
userStatus.userStatus = 'on'
|
|
100
|
-
self.emit("notification.user", socket)
|
|
101
|
-
}
|
|
82
|
+
},
|
|
83
|
+
sync: true,
|
|
84
|
+
organization_id
|
|
85
|
+
}
|
|
102
86
|
|
|
103
|
-
|
|
87
|
+
if (options.lastSynced)
|
|
88
|
+
data.$filter.query = [
|
|
89
|
+
{ key: '_id', value: options.lastSynced, operator: '$gt' }
|
|
90
|
+
]
|
|
91
|
+
else
|
|
92
|
+
data.$filter.limit = 1
|
|
93
|
+
|
|
94
|
+
self.emit('object.read', data);
|
|
95
|
+
|
|
96
|
+
if (self.authenticate) {
|
|
97
|
+
const { user_id, expires } = await self.authenticate.decodeToken(options.token, organization_id, options.clientId)
|
|
98
|
+
const userStatus = { socket, method: 'userStatus', user_id: options.user_id, clientId: options.clientId, userStatus: 'off', organization_id }
|
|
99
|
+
if (user_id) {
|
|
100
|
+
options.user_id = user_id
|
|
101
|
+
socket.user_id = user_id;
|
|
102
|
+
socket.expires = expires;
|
|
103
|
+
userStatus.userStatus = 'on'
|
|
104
|
+
self.emit("notification.user", socket)
|
|
105
|
+
}
|
|
104
106
|
|
|
105
|
-
|
|
107
|
+
self.emit('userStatus', userStatus);
|
|
106
108
|
|
|
107
|
-
|
|
108
|
-
self.onWebSocket(socket);
|
|
109
|
-
}
|
|
109
|
+
self.onWebSocket(socket);
|
|
110
110
|
|
|
111
|
-
|
|
112
|
-
|
|
111
|
+
} else
|
|
112
|
+
self.onWebSocket(socket);
|
|
113
113
|
}
|
|
114
|
-
|
|
115
|
-
|
|
114
|
+
|
|
115
|
+
} else {
|
|
116
|
+
socket.send(JSON.stringify({ method: 'Access Denied', error: 'An organization_id is required' }))
|
|
117
|
+
}
|
|
118
|
+
})
|
|
116
119
|
}
|
|
117
120
|
|
|
118
121
|
onWebSocket(socket) {
|
|
@@ -149,6 +152,7 @@ class SocketServer extends EventEmitter {
|
|
|
149
152
|
|
|
150
153
|
this.emit('object.update', {
|
|
151
154
|
method: 'object.update',
|
|
155
|
+
host: socket.host,
|
|
152
156
|
array: 'organizations',
|
|
153
157
|
object: {
|
|
154
158
|
_id: organization_id, ['$addToSet.activeHost']: socket.socketUrl // needs socketId
|
|
@@ -241,6 +245,7 @@ class SocketServer extends EventEmitter {
|
|
|
241
245
|
this.organizations.delete(socket.organization_id);
|
|
242
246
|
this.emit('object.update', {
|
|
243
247
|
method: 'object.update',
|
|
248
|
+
host: socket.host,
|
|
244
249
|
array: 'organizations',
|
|
245
250
|
object: {
|
|
246
251
|
_id: organization_id, ['$pull.activeHost']: socket.socketUrl
|
|
@@ -355,6 +360,7 @@ class SocketServer extends EventEmitter {
|
|
|
355
360
|
}
|
|
356
361
|
|
|
357
362
|
data.socket = socket
|
|
363
|
+
data.host = socket.host
|
|
358
364
|
|
|
359
365
|
const authorized = await this.authorize.check(data, socket.user_id)
|
|
360
366
|
let errors = {}
|