@cocreate/socket-server 1.24.3 → 1.26.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,22 @@
1
+ # [1.26.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.25.0...v1.26.0) (2024-01-03)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * comment checkCertificate ([f70e8de](https://github.com/CoCreate-app/CoCreate-socket-server/commit/f70e8def302498d1f93cc4badde012333bbe8ea0))
7
+
8
+
9
+ ### Features
10
+
11
+ * Added '[@cocreate-server](https://github.com/cocreate-server)' handle http and https ([5f530b1](https://github.com/CoCreate-app/CoCreate-socket-server/commit/5f530b135cb25a3af51fe059af57a235d0c3d51c))
12
+
13
+ # [1.25.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.24.3...v1.25.0) (2023-12-31)
14
+
15
+
16
+ ### Features
17
+
18
+ * Added '@cocreate/acme' for dynamic ssl management ([f01831b](https://github.com/CoCreate-app/CoCreate-socket-server/commit/f01831bf2d3a8a4812c369fb61927667af56f7d5))
19
+
1
20
  ## [1.24.3](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.24.2...v1.24.3) (2023-12-18)
2
21
 
3
22
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/socket-server",
3
- "version": "1.24.3",
3
+ "version": "1.26.0",
4
4
  "description": "CoCreate-socket-server",
5
5
  "keywords": [
6
6
  "cocreate-socket",
package/src/index.js CHANGED
@@ -25,91 +25,97 @@ class SocketServer extends EventEmitter {
25
25
  socket.destroy();
26
26
  });
27
27
 
28
- server.on('upgrade', (request, socket, head) => {
29
- const self = this;
30
- let organization_id = request.url.split('/')
31
- organization_id = organization_id[organization_id.length - 1]
32
-
33
- this.wss.handleUpgrade(request, socket, head, async function (socket) {
34
- if (organization_id) {
35
- let organization = self.organizations.get(organization_id)
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
- let options = decodeURIComponent(request.headers['sec-websocket-protocol'])
46
- options = JSON.parse(options)
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
- socket.organization_id = organization_id
49
- socket.id = options.socketId;
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
- if (socket.origin.startsWith('http'))
55
- socket.origin = socket.origin.replace('http', 'ws')
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
- socket.socketUrl = socket.origin + socket.pathname
58
+ if (socket.origin.startsWith('http'))
59
+ socket.origin = socket.origin.replace('http', 'ws')
58
60
 
59
- if (socket.origin && socket.origin !== 'null') {
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
- if (!organization || organization && organization.status !== false) {
67
- let data = {
68
- socket,
69
- method: 'object.read',
70
- array: 'message_log',
71
- $filter: {
72
- sort: [
73
- { key: '_id', direction: 'desc' }
74
- ]
75
- },
76
- sync: true,
77
- organization_id
78
- }
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
+ }
79
69
 
80
- if (options.lastSynced)
81
- data.$filter.query = [
82
- { key: '_id', value: options.lastSynced, operator: '$gt' }
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' }
83
81
  ]
84
- else
85
- data.$filter.limit = 1
86
-
87
- self.emit('object.read', data);
88
-
89
- if (self.authenticate) {
90
- const { user_id, expires } = await self.authenticate.decodeToken(options.token, organization_id, options.clientId)
91
- const userStatus = { socket, method: 'userStatus', user_id: options.user_id, clientId: options.clientId, userStatus: 'off', organization_id }
92
- if (user_id) {
93
- options.user_id = user_id
94
- socket.user_id = user_id;
95
- socket.expires = expires;
96
- userStatus.userStatus = 'on'
97
- self.emit("notification.user", socket)
98
- }
82
+ },
83
+ sync: true,
84
+ organization_id
85
+ }
99
86
 
100
- self.emit('userStatus', userStatus);
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
+ }
101
106
 
102
- self.onWebSocket(socket);
107
+ self.emit('userStatus', userStatus);
103
108
 
104
- } else
105
- self.onWebSocket(socket);
106
- }
109
+ self.onWebSocket(socket);
107
110
 
108
- } else {
109
- socket.send(JSON.stringify({ method: 'Access Denied', error: 'An organization_id is required' }))
111
+ } else
112
+ self.onWebSocket(socket);
110
113
  }
111
- })
112
- });
114
+
115
+ } else {
116
+ socket.send(JSON.stringify({ method: 'Access Denied', error: 'An organization_id is required' }))
117
+ }
118
+ })
113
119
  }
114
120
 
115
121
  onWebSocket(socket) {