@cocreate/socket-server 1.19.4 → 1.19.5
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 +7 -0
- package/package.json +1 -1
- package/src/index.js +90 -36
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
## [1.19.5](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.19.4...v1.19.5) (2023-10-16)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* improved error handling ([a3e5d31](https://github.com/CoCreate-app/CoCreate-socket-server/commit/a3e5d314143f135816de7b51696626c53e6a88f0))
|
|
7
|
+
|
|
1
8
|
## [1.19.4](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.19.3...v1.19.4) (2023-10-14)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -31,8 +31,20 @@ class SocketServer extends EventEmitter {
|
|
|
31
31
|
let organization_id = request.url.split('/')
|
|
32
32
|
organization_id = organization_id[organization_id.length - 1]
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
this.wss.handleUpgrade(request, socket, head, function (socket) {
|
|
35
|
+
if (organization_id) {
|
|
36
|
+
let organization = self.organizations.get(organization_id)
|
|
37
|
+
if (organization && organization.status === false) {
|
|
38
|
+
let errors = {}
|
|
39
|
+
errors.serverOrganization = organization.serverOrganization
|
|
40
|
+
errors.serverStorage = organization.serverStorage
|
|
41
|
+
errors.organizationBalance = organization.organizationBalance
|
|
42
|
+
errors.error = organization.error
|
|
43
|
+
return socket.send(JSON.stringify({ method: 'Access Denied', error: errors }))
|
|
44
|
+
}
|
|
45
|
+
// if (!organization)
|
|
46
|
+
// return socket.send(JSON.stringify({ method: 'Access Denied', error: organization.error }))
|
|
47
|
+
|
|
36
48
|
let options = decodeURIComponent(request.headers['sec-websocket-protocol'])
|
|
37
49
|
options = JSON.parse(options)
|
|
38
50
|
|
|
@@ -86,11 +98,12 @@ class SocketServer extends EventEmitter {
|
|
|
86
98
|
|
|
87
99
|
self.onWebSocket(socket);
|
|
88
100
|
// })
|
|
89
|
-
}
|
|
90
|
-
else
|
|
101
|
+
} else
|
|
91
102
|
self.onWebSocket(socket);
|
|
92
|
-
}
|
|
93
|
-
|
|
103
|
+
} else {
|
|
104
|
+
socket.send(JSON.stringify({ method: 'Access Denied', error: 'An organization_id is required' }))
|
|
105
|
+
}
|
|
106
|
+
})
|
|
94
107
|
});
|
|
95
108
|
}
|
|
96
109
|
|
|
@@ -140,11 +153,17 @@ class SocketServer extends EventEmitter {
|
|
|
140
153
|
organization_id
|
|
141
154
|
});
|
|
142
155
|
|
|
143
|
-
}
|
|
156
|
+
} else
|
|
157
|
+
clearTimeout(organization.debounce);
|
|
158
|
+
|
|
144
159
|
|
|
145
160
|
if (!this.clients.has(socket.clientId)) {
|
|
146
161
|
this.clients.set(socket.clientId, []);
|
|
147
|
-
|
|
162
|
+
|
|
163
|
+
if (!organization.clients)
|
|
164
|
+
organization.clients = { [socket.clientId]: [] }
|
|
165
|
+
else
|
|
166
|
+
organization.clients[socket.clientId] = []
|
|
148
167
|
}
|
|
149
168
|
|
|
150
169
|
if (!this.sockets.has(socket.id)) {
|
|
@@ -179,9 +198,6 @@ class SocketServer extends EventEmitter {
|
|
|
179
198
|
else
|
|
180
199
|
sockets.push(...clients[client])
|
|
181
200
|
}
|
|
182
|
-
} else {
|
|
183
|
-
// TODO: requires a messageQueue that expires
|
|
184
|
-
console.log('organization could not be found', data)
|
|
185
201
|
}
|
|
186
202
|
} else if (data.broadcastSender !== false) {
|
|
187
203
|
sockets.push(data.socket)
|
|
@@ -192,8 +208,9 @@ class SocketServer extends EventEmitter {
|
|
|
192
208
|
delete(socket) {
|
|
193
209
|
let organization_id = socket.organization_id
|
|
194
210
|
if (this.organizations.has(organization_id)) {
|
|
195
|
-
|
|
196
|
-
|
|
211
|
+
let clients = this.organizations.get(organization_id)
|
|
212
|
+
if (clients)
|
|
213
|
+
clients.clients;
|
|
197
214
|
// Check if the client exists
|
|
198
215
|
if (clients && clients[socket.clientId]) {
|
|
199
216
|
const client = clients[socket.clientId]
|
|
@@ -241,21 +258,34 @@ class SocketServer extends EventEmitter {
|
|
|
241
258
|
}
|
|
242
259
|
|
|
243
260
|
if (this.clients.size === 0) {
|
|
244
|
-
this.organizations.
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
261
|
+
let organization = this.organizations.get(socket.organization_id)
|
|
262
|
+
let debounceTimer
|
|
263
|
+
if (organization)
|
|
264
|
+
debounceTimer = organization.debounce
|
|
265
|
+
|
|
266
|
+
clearTimeout(debounceTimer);
|
|
267
|
+
debounceTimer = setTimeout(() => {
|
|
268
|
+
this.organizations.delete(socket.organization_id);
|
|
269
|
+
}, 10000);
|
|
270
|
+
|
|
271
|
+
if (!organization)
|
|
272
|
+
this.organizations.set(socket.organization_id, { debounce: debounceTimer })
|
|
273
|
+
else
|
|
274
|
+
organization.debounce = debounceTimer
|
|
275
|
+
|
|
276
|
+
this.sockets.delete(socket.id);
|
|
277
|
+
|
|
278
|
+
if (socket.user_id) {
|
|
279
|
+
let sockets = this.users.get(socket.user_id)
|
|
280
|
+
if (sockets) {
|
|
281
|
+
const index = sockets.findIndex(item => item.id === socket.id);
|
|
282
|
+
if (index !== -1) {
|
|
283
|
+
sockets.splice(index, 1);
|
|
284
|
+
}
|
|
285
|
+
if (!sockets.length) {
|
|
286
|
+
this.users.delete(socket.user_id);
|
|
287
|
+
this.emit('userStatus', { socket, user_id, status: 'off', organization_id });
|
|
288
|
+
}
|
|
259
289
|
}
|
|
260
290
|
}
|
|
261
291
|
}
|
|
@@ -271,8 +301,9 @@ class SocketServer extends EventEmitter {
|
|
|
271
301
|
organization_id
|
|
272
302
|
});
|
|
273
303
|
|
|
274
|
-
|
|
275
|
-
|
|
304
|
+
const organization = this.organizations.get(organization_id)
|
|
305
|
+
if (organization && organization.organizationBalance == false)
|
|
306
|
+
return socket.send({ method: 'Access Denied', organizationBalance: false, error: organization.error })
|
|
276
307
|
|
|
277
308
|
let data = JSON.parse(message)
|
|
278
309
|
if (data.method) {
|
|
@@ -286,24 +317,47 @@ class SocketServer extends EventEmitter {
|
|
|
286
317
|
|
|
287
318
|
if (this.authorize) {
|
|
288
319
|
if (!this.sockets.has(socket.id)) {
|
|
289
|
-
if (!
|
|
290
|
-
return
|
|
320
|
+
if (organization && !organization.organizationBalance == false)
|
|
321
|
+
return socket.send({ method: 'Access Denied', organizationBalance: false, error: organization.error })
|
|
291
322
|
}
|
|
292
323
|
|
|
293
324
|
data.socket = socket
|
|
294
325
|
|
|
295
326
|
const authorized = await this.authorize.check(data, socket.user_id)
|
|
296
|
-
|
|
327
|
+
let errors = {}
|
|
328
|
+
if (authorized.serverOrganization === false) {
|
|
329
|
+
organization.status = errors.status = false;
|
|
330
|
+
organization.serverOrganization = false;
|
|
331
|
+
organization.error = authorized.error
|
|
332
|
+
} else if (authorized.serverStorage === false) {
|
|
297
333
|
data.database = process.env.organization_id
|
|
298
334
|
data.organization_id = process.env.organization_id
|
|
299
335
|
|
|
300
336
|
const authorized2 = await this.authorize.check(data, req, socket.user_id)
|
|
301
337
|
if (!authorized2 || authorized2.error) {
|
|
302
|
-
|
|
338
|
+
organization.status = errors.status = false;
|
|
339
|
+
organization.error = errors.error = authorized.error
|
|
340
|
+
if (authorized2.serverOrganization === false) {
|
|
341
|
+
organization.serverOrganization = false;
|
|
342
|
+
}
|
|
343
|
+
if (authorized2.serverStorage === false) {
|
|
344
|
+
organization.serverStorage = false;
|
|
345
|
+
}
|
|
303
346
|
}
|
|
304
|
-
} else if (
|
|
305
|
-
|
|
347
|
+
} else if (authorized.error) {
|
|
348
|
+
organization.status = false;
|
|
349
|
+
organization.error = authorized.error
|
|
306
350
|
}
|
|
351
|
+
|
|
352
|
+
if (organization && organization.status === false) {
|
|
353
|
+
let errors = {}
|
|
354
|
+
errors.serverOrganization = organization.serverOrganization
|
|
355
|
+
errors.serverStorage = organization.serverStorage
|
|
356
|
+
errors.organizationBalance = organization.organizationBalance
|
|
357
|
+
errors.error = organization.error
|
|
358
|
+
return socket.send(JSON.stringify({ method: 'Access Denied', ...errors }))
|
|
359
|
+
}
|
|
360
|
+
|
|
307
361
|
// dburl is true and db does not have 'keys' array
|
|
308
362
|
// action: syncCollection data{array: 'keys', object[]}
|
|
309
363
|
// actions: add keys as once keys are added admin user can do anything
|