@cocreate/socket-server 1.16.2 → 1.17.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 +43 -43
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
# [1.17.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.16.3...v1.17.0) (2023-09-07)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* organzations map to store the orgs active status, if the org balance falls bellow 0 false is set resulting in socket and file-server responding with 0 balance error message ([e06b8b1](https://github.com/CoCreate-app/CoCreate-socket-server/commit/e06b8b173fc24b98b60a9bd24494bb1fceea19cc))
|
|
7
|
+
|
|
8
|
+
## [1.16.3](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.16.2...v1.16.3) (2023-08-21)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* update permission functions and variable to authorize for improved clarity and readability ([19c7415](https://github.com/CoCreate-app/CoCreate-socket-server/commit/19c74155e696886f3daab672489febf6009c2e1a))
|
|
14
|
+
|
|
1
15
|
## [1.16.2](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.16.1...v1.16.2) (2023-08-21)
|
|
2
16
|
|
|
3
17
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const WebSocket = require('ws');
|
|
2
|
-
const
|
|
2
|
+
const { URL } = require("url");
|
|
3
3
|
const EventEmitter = require("events").EventEmitter;
|
|
4
4
|
const uid = require('@cocreate/uuid')
|
|
5
5
|
const config = require('@cocreate/config')
|
|
@@ -7,8 +7,9 @@ const config = require('@cocreate/config')
|
|
|
7
7
|
class SocketServer extends EventEmitter {
|
|
8
8
|
constructor(server, prefix) {
|
|
9
9
|
super();
|
|
10
|
+
this.organizations = new Map();
|
|
10
11
|
this.clients = new Map();
|
|
11
|
-
this.prefix = prefix || "
|
|
12
|
+
this.prefix = prefix || "ws";
|
|
12
13
|
config({ organization_id: { prompt: 'Enter your organization_id: ' } })
|
|
13
14
|
|
|
14
15
|
this.wss = new WebSocket.Server({ noServer: true });
|
|
@@ -25,10 +26,11 @@ class SocketServer extends EventEmitter {
|
|
|
25
26
|
|
|
26
27
|
handleUpgrade(req, socket, head) {
|
|
27
28
|
const self = this;
|
|
28
|
-
const
|
|
29
|
-
const
|
|
29
|
+
// const url = new URL(req.url);
|
|
30
|
+
// const pathname = req.url;
|
|
31
|
+
const config = this.getKeyFromUrl(req.url)
|
|
30
32
|
if (config.type == this.prefix) {
|
|
31
|
-
|
|
33
|
+
this.wss.handleUpgrade(req, socket, head, function (socket) {
|
|
32
34
|
socket.config = config
|
|
33
35
|
self.onWebSocket(req, socket);
|
|
34
36
|
})
|
|
@@ -60,6 +62,9 @@ class SocketServer extends EventEmitter {
|
|
|
60
62
|
|
|
61
63
|
addClient(socket) {
|
|
62
64
|
let organization_id = socket.config.organization_id
|
|
65
|
+
if (!this.organizations.has(organization_id))
|
|
66
|
+
this.organizations.set(organization_id, true)
|
|
67
|
+
|
|
63
68
|
let user_id = socket.config.user_id
|
|
64
69
|
let key = socket.config.key
|
|
65
70
|
let clients = this.clients.get(key);
|
|
@@ -72,11 +77,6 @@ class SocketServer extends EventEmitter {
|
|
|
72
77
|
|
|
73
78
|
if (user_id)
|
|
74
79
|
this.emit('userStatus', socket, { user_id, userStatus: 'on', organization_id });
|
|
75
|
-
|
|
76
|
-
this.emit("createMetrics", {
|
|
77
|
-
organization_id,
|
|
78
|
-
clients: clients.length,
|
|
79
|
-
});
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
removeClient(socket) {
|
|
@@ -90,19 +90,12 @@ class SocketServer extends EventEmitter {
|
|
|
90
90
|
clients.splice(index, 1);
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
-
if (
|
|
94
|
-
|
|
95
|
-
this.emit('userStatus', socket, { user_id, status: 'off', organization_id });
|
|
93
|
+
if (user_id)
|
|
94
|
+
this.emit('userStatus', socket, { user_id, status: 'off', organization_id });
|
|
96
95
|
|
|
97
|
-
|
|
98
|
-
this.
|
|
99
|
-
this.emit("deletePermissions", organization_id);
|
|
96
|
+
if (clients.length == 0) {
|
|
97
|
+
this.organizations.delete(organization_id)
|
|
100
98
|
this.emit('disconnect', organization_id)
|
|
101
|
-
} else {
|
|
102
|
-
this.emit("updateMetrics", {
|
|
103
|
-
organization_id,
|
|
104
|
-
clients: clients.length
|
|
105
|
-
});
|
|
106
99
|
}
|
|
107
100
|
|
|
108
101
|
}
|
|
@@ -110,34 +103,40 @@ class SocketServer extends EventEmitter {
|
|
|
110
103
|
async onMessage(req, socket, message) {
|
|
111
104
|
try {
|
|
112
105
|
const organization_id = socket.config.organization_id
|
|
106
|
+
|
|
107
|
+
this.emit("setBandwidth", {
|
|
108
|
+
type: 'in',
|
|
109
|
+
data: message,
|
|
110
|
+
organization_id
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
let active = this.organizations.get(organization_id)
|
|
114
|
+
if (active === false)
|
|
115
|
+
return this.send(socket, { method: 'Access Denied', balance: 'Your balance has fallen bellow 0' })
|
|
116
|
+
|
|
113
117
|
let data = JSON.parse(message)
|
|
114
118
|
|
|
115
119
|
if (data.method) {
|
|
116
|
-
this.emit("setBandwidth", {
|
|
117
|
-
type: 'in',
|
|
118
|
-
data: message,
|
|
119
|
-
organization_id
|
|
120
|
-
});
|
|
121
120
|
let user_id = null;
|
|
122
121
|
if (this.authenticate)
|
|
123
122
|
user_id = await this.authenticate.decodeToken(req);
|
|
124
123
|
|
|
125
124
|
if (this.authorize) {
|
|
126
125
|
data.host = this.getHost(req)
|
|
127
|
-
const
|
|
128
|
-
if (
|
|
126
|
+
const authorized = await this.authorize.check(data, user_id)
|
|
127
|
+
if (authorized.storage === false) {
|
|
129
128
|
data.database = process.env.organization_id
|
|
130
129
|
data.organization_id = process.env.organization_id
|
|
131
130
|
|
|
132
|
-
const
|
|
133
|
-
if (!
|
|
134
|
-
return this.send(socket, { method: 'Access Denied',
|
|
131
|
+
const authorized2 = await this.authorize.check(data, req, user_id)
|
|
132
|
+
if (!authorized2 || authorized2.error) {
|
|
133
|
+
return this.send(socket, { method: 'Access Denied', authorized2, ...data })
|
|
135
134
|
}
|
|
136
|
-
} else if (!
|
|
135
|
+
} else if (!authorized || authorized.error) {
|
|
137
136
|
if (!user_id)
|
|
138
137
|
this.send(socket, { method: 'updateUserStatus', userStatus: 'off', clientId: data.clientId, organization_id })
|
|
139
138
|
|
|
140
|
-
return this.send(socket, { method: 'Access Denied',
|
|
139
|
+
return this.send(socket, { method: 'Access Denied', authorized, ...data })
|
|
141
140
|
}
|
|
142
141
|
// dburl is true and db does not have 'keys' array
|
|
143
142
|
// action: syncCollection data{array: 'keys', object[]}
|
|
@@ -146,8 +145,8 @@ class SocketServer extends EventEmitter {
|
|
|
146
145
|
|
|
147
146
|
// }
|
|
148
147
|
|
|
149
|
-
if (
|
|
150
|
-
data =
|
|
148
|
+
if (authorized.authorized)
|
|
149
|
+
data = authorized.authorized
|
|
151
150
|
|
|
152
151
|
if (user_id) {
|
|
153
152
|
if (!socket.config.user_id) {
|
|
@@ -205,9 +204,9 @@ class SocketServer extends EventEmitter {
|
|
|
205
204
|
if (clients) {
|
|
206
205
|
for (let client of clients) {
|
|
207
206
|
if (socket != client && data.broadcast != false || socket == client && data.broadcastSender != false) {
|
|
208
|
-
const
|
|
209
|
-
if (
|
|
210
|
-
data =
|
|
207
|
+
const authorized = await this.authorize.check(data, socket.config.user_id)
|
|
208
|
+
if (authorized && authorized.authorized)
|
|
209
|
+
data = authorized.authorized
|
|
211
210
|
|
|
212
211
|
let responseData = JSON.stringify(data);
|
|
213
212
|
|
|
@@ -239,13 +238,14 @@ class SocketServer extends EventEmitter {
|
|
|
239
238
|
}
|
|
240
239
|
|
|
241
240
|
getHost(req) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
241
|
+
if (req.headers.origin && req.headers.origin !== 'null') {
|
|
242
|
+
const url = new URL(req.headers.origin);
|
|
243
|
+
return url.host;
|
|
244
|
+
} else if (req.headers.host && req.headers.host !== 'null') {
|
|
245
|
+
return req.headers.host;
|
|
246
|
+
}
|
|
246
247
|
}
|
|
247
248
|
|
|
248
249
|
}
|
|
249
250
|
|
|
250
|
-
|
|
251
251
|
module.exports = SocketServer
|