@cocreate/socket-server 1.8.1 → 1.9.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.
Files changed (3) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/package.json +1 -1
  3. package/src/index.js +98 -103
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [1.9.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.8.2...v1.9.0) (2023-05-24)
2
+
3
+
4
+ ### Features
5
+
6
+ * Refactored SocketServer permission checking and added a new function getHost. ([c6a51d1](https://github.com/CoCreate-app/CoCreate-socket-server/commit/c6a51d143fda143aca3d059958bc668f9fac6d04))
7
+
8
+ ## [1.8.2](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.8.1...v1.8.2) (2023-05-21)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * Refactor socket server to track and limit server bandwidth usage. ([0c4c0f4](https://github.com/CoCreate-app/CoCreate-socket-server/commit/0c4c0f438a27e75d659044bfdcc19f422df67347))
14
+
1
15
  ## [1.8.1](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.8.0...v1.8.1) (2023-05-20)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/socket-server",
3
- "version": "1.8.1",
3
+ "version": "1.9.0",
4
4
  "description": "CoCreate-socket-server",
5
5
  "keywords": [
6
6
  "cocreate-socket",
package/src/index.js CHANGED
@@ -87,7 +87,7 @@ class SocketServer extends EventEmitter {
87
87
  let total_cnt = 0;
88
88
  this.clients.forEach((c) => total_cnt += c.length)
89
89
 
90
- this.emit("createMetrics", null, {
90
+ this.emit("createMetrics", {
91
91
  organization_id,
92
92
  client_cnt: room_clients.length,
93
93
  total_cnt: total_cnt
@@ -109,7 +109,7 @@ class SocketServer extends EventEmitter {
109
109
  if (user_id)
110
110
  this.emit('userStatus', socket, { user_id, status: 'off', organization_id });
111
111
 
112
- this.emit("deleteMetrics", null, { organization_id });
112
+ this.emit("deleteMetrics", { organization_id });
113
113
  this.emit("deletePermissions", organization_id);
114
114
  this.emit('disconnect', organization_id)
115
115
  this.asyncMessages.delete(key);
@@ -117,7 +117,7 @@ class SocketServer extends EventEmitter {
117
117
  let total_cnt = 0;
118
118
  this.clients.forEach((c) => total_cnt += c.length)
119
119
 
120
- this.emit("changeCountMetrics", null, {
120
+ this.emit("changeCountMetrics", {
121
121
  organization_id,
122
122
  total_cnt,
123
123
  client_cnt: room_clients.length
@@ -129,78 +129,76 @@ class SocketServer extends EventEmitter {
129
129
  async onMessage(req, socket, message) {
130
130
  try {
131
131
  const organization_id = socket.config.organization_id
132
-
133
- // TODO: remove
134
- // if (message instanceof Buffer) {
135
- // this.emit('importFile2DB', socket, message);
136
- // console.log('importFile2DB', socket, message);
137
- // return;
138
- // }
139
-
140
- const { action, data } = JSON.parse(message)
132
+ let { action, data } = JSON.parse(message)
141
133
 
142
134
  if (action) {
143
- this.recordTransfer('in', message, organization_id)
144
-
135
+ this.emit("setBandwidth", {
136
+ type: 'in',
137
+ data: message,
138
+ organization_id
139
+ });
145
140
  let user_id = null;
146
141
  if (this.authInstance)
147
142
  user_id = await this.authInstance.getUserId(req);
148
143
 
149
144
  if (this.permissionInstance) {
150
- const permission = await this.permissionInstance.check(action, data, req, user_id)
151
- if (!permission || permission.error) {
152
- // if (action == 'syncServer' && permission.database === true)
153
- // if (action == 'syncServer')
154
- // this.emit('createDocument', socket, data);
155
- // else
156
- if (permission.dbUrl === false) {
157
- data.database = process.env.organization_id
158
- data.organization_id = process.env.organization_id
159
-
160
- const permission2 = await this.permissionInstance.check(action, data, req, user_id)
161
- if (!permission2 || permission2.error) {
162
- return this.send(socket, 'Access Denied', { action, permission2, ...data })
163
- }
164
- } else
165
- return this.send(socket, 'Access Denied', { action, permission, ...data })
145
+ data.host = this.getHost(req)
146
+ const permission = await this.permissionInstance.check(action, data, user_id)
147
+ if (permission.dbUrl === false) {
148
+ data.database = process.env.organization_id
149
+ data.organization_id = process.env.organization_id
150
+
151
+ const permission2 = await this.permissionInstance.check(action, data, req, user_id)
152
+ if (!permission2 || permission2.error) {
153
+ return this.send(socket, 'Access Denied', { action, permission2, ...data })
154
+ }
155
+ } else if (!permission || permission.error) {
156
+ if (!user_id)
157
+ this.send(socket, 'updateUserStatus', { userStatus: 'off', clientId: data.clientId, organization_id })
158
+
159
+ return this.send(socket, 'Access Denied', { action, permission, ...data })
166
160
  }
167
- }
161
+ // dburl is true and db does not have 'keys' collection
162
+ // action: syncCollection data{collection: 'keys', document[]}
163
+ // actions: add keys as once keys are added admin user can do anything
164
+
165
+
166
+ // }
167
+
168
+ if (permission.authorized)
169
+ data = permission.authorized
168
170
 
169
- if (user_id) {
170
- if (!socket.config.user_id) {
171
- socket.config.user_id = user_id
172
- this.emit('userStatus', socket, { user_id, userStatus: 'on', organization_id });
171
+ if (user_id) {
172
+ if (!socket.config.user_id) {
173
+ socket.config.user_id = user_id
174
+ this.emit('userStatus', socket, { user_id, userStatus: 'on', organization_id });
175
+ }
176
+ } else {
177
+ this.send(socket, 'updateUserStatus', { userStatus: 'off', clientId: data.clientId, organization_id })
173
178
  }
174
- } else {
175
- this.send(socket, 'updateUserStatus', { userStatus: 'off', clientId: data.clientId, organization_id })
176
- }
177
179
 
178
- //. checking async status....
179
- if (data.async == true) {
180
- console.log('async true')
181
- const asyncMessage = this.asyncMessages.get(socket.config.key);
182
- socket.config.asyncId = uid.generate();
183
- if (asyncMessage) {
184
- asyncMessage.defineMessage(socket.config.asyncId);
180
+ //. checking async status....
181
+ if (data.async == true) {
182
+ console.log('async true')
183
+ const asyncMessage = this.asyncMessages.get(socket.config.key);
184
+ socket.config.asyncId = uid.generate();
185
+ if (asyncMessage) {
186
+ asyncMessage.defineMessage(socket.config.asyncId);
187
+ }
185
188
  }
186
- }
187
189
 
188
- this.emit(action, socket, data);
189
- }
190
+ this.emit(action, socket, data);
190
191
 
192
+ }
193
+ }
191
194
  } catch (e) {
192
195
  console.log(e);
193
196
  }
194
197
  }
195
198
 
196
199
  broadcast(socket, action, data) {
197
- const self = this;
198
200
  if (!data.uid)
199
201
  data.uid = uid.generate()
200
- const responseData = JSON.stringify({
201
- action,
202
- data
203
- });
204
202
 
205
203
  const asyncId = socket.config.asyncId
206
204
  let isAsync = false;
@@ -217,6 +215,7 @@ class SocketServer extends EventEmitter {
217
215
  url += `/${namespace}`
218
216
  }
219
217
 
218
+
220
219
  let rooms = data.room;
221
220
  if (rooms) {
222
221
  if (!rooms.isArray())
@@ -225,61 +224,63 @@ class SocketServer extends EventEmitter {
225
224
  let url = url;
226
225
  url += `/${room}`;
227
226
 
228
- const clients = this.clients.get(url);
229
- if (clients) {
230
- clients.forEach((client) => {
231
- if (socket != client && data.broadcast != false || socket == client && data.broadcastSender != false) {
232
- if (isAsync) {
233
- asyncData.push({ socket: client, message: responseData })
234
- } else {
235
- client.send(responseData);
236
- }
237
- self.recordTransfer('out', responseData, organization_id)
238
- }
239
- })
240
- }
227
+ this.send(socket, action, data, url, isAsync, asyncData)
241
228
  }
242
-
243
229
  } else {
244
- this.clients.forEach((value, key) => {
245
- if (key.includes(url)) {
246
- value.forEach(client => {
247
- if (socket != client && data.broadcast != false || socket == client && data.broadcastSender != false) {
248
- if (isAsync) {
249
- asyncData.push({ socket: client, message: responseData })
250
- } else {
251
- client.send(responseData);
252
- }
253
- self.recordTransfer('out', responseData, organization_id)
254
- }
255
- })
256
- }
257
- })
230
+ this.send(socket, action, data, url, isAsync, asyncData)
258
231
  }
259
232
 
260
- //. set async processing
261
233
  if (isAsync) {
262
234
  this.asyncMessages.get(socket.config.key).setMessage(asyncId, asyncData)
263
235
  }
264
236
 
265
237
  }
266
238
 
267
- send(socket, action, data) {
239
+ async send(socket, action, data, url, isAsync, asyncData) {
268
240
  const asyncId = socket.config.asyncId
269
- let responseData = JSON.stringify({
270
- action,
271
- data
272
- });
273
241
 
274
- if (asyncId && socket.config && socket.config.key) {
242
+ if (!url && asyncId && socket.config && socket.config.key) {
243
+ let responseData = JSON.stringify({
244
+ action,
245
+ data
246
+ });
275
247
  this.asyncMessages.get(socket.config.key).setMessage(asyncId, [{ socket, message: responseData }]);
276
248
  } else {
277
- socket.send(responseData);
249
+ let clients
250
+ if (!url)
251
+ clients = [socket]
252
+ else
253
+ clients = this.clients.get(url);
254
+
255
+ if (clients) {
256
+ for (let client of clients) {
257
+ if (socket != client && data.broadcast != false || socket == client && data.broadcastSender != false) {
258
+ if (isAsync) {
259
+ asyncData.push({ socket, message: JSON.stringify({ action, data }) })
260
+ } else {
261
+ const permission = await this.permissionInstance.check(action, data, socket.config.user_id)
262
+ if (permission && permission.authorized)
263
+ data = permission.authorized
264
+
265
+ let responseData = JSON.stringify({
266
+ action,
267
+ data
268
+ });
269
+
270
+ socket.send(responseData);
271
+
272
+ if (socket.config && socket.config.organization_id)
273
+ this.emit("setBandwidth", {
274
+ type: 'out',
275
+ data: responseData,
276
+ organization_id: socket.config.organization_id
277
+ })
278
+ }
279
+ }
280
+ }
281
+ }
278
282
  }
279
283
 
280
- if (socket.config && socket.config.organization_id)
281
- this.recordTransfer('out', responseData, socket.config.organization_id)
282
-
283
284
  }
284
285
 
285
286
  // addAsyncMessage(key) {
@@ -302,19 +303,13 @@ class SocketServer extends EventEmitter {
302
303
  return params
303
304
  }
304
305
 
305
-
306
- sendBinary(socket, data, organization_id) {
307
- socket.send(data, { binary: true });
308
- this.recordTransfer('out', data, organization_id)
306
+ getHost(req) {
307
+ const headers = req['headers']
308
+ let origin = headers['origin'];
309
+ if (origin && origin !== 'null')
310
+ return url.parse(origin).hostname;
309
311
  }
310
312
 
311
- recordTransfer(type, data, organization_id) {
312
- this.emit("setBandwidth", null, {
313
- type,
314
- data,
315
- organization_id
316
- });
317
- }
318
313
  }
319
314
 
320
315