@cocreate/socket-server 1.25.0 → 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,15 @@
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
+
1
13
  # [1.25.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.24.3...v1.25.0) (2023-12-31)
2
14
 
3
15
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/socket-server",
3
- "version": "1.25.0",
3
+ "version": "1.26.0",
4
4
  "description": "CoCreate-socket-server",
5
5
  "keywords": [
6
6
  "cocreate-socket",
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
- 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 (!await self.acme.checkCertificate(socket.host, organization_id))
67
- return socket.send(JSON.stringify({ method: 'Access Denied', error: 'Host not whitelisted' }))
68
-
69
- if (!organization || organization && organization.status !== false) {
70
- let data = {
71
- socket,
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
- if (options.lastSynced)
84
- data.$filter.query = [
85
- { 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' }
86
81
  ]
87
- else
88
- data.$filter.limit = 1
89
-
90
- self.emit('object.read', data);
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
- 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
+ }
104
106
 
105
- self.onWebSocket(socket);
107
+ self.emit('userStatus', userStatus);
106
108
 
107
- } else
108
- self.onWebSocket(socket);
109
- }
109
+ self.onWebSocket(socket);
110
110
 
111
- } else {
112
- socket.send(JSON.stringify({ method: 'Access Denied', error: 'An organization_id is required' }))
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) {