@resolveio/server-lib 9.0.44 → 9.1.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.
@@ -56,10 +56,7 @@ var method_calls_1 = require("../publications/method-calls");
56
56
  var method_responses_1 = require("../publications/method-responses");
57
57
  var index_1 = require("../index");
58
58
  var notifications_1 = require("../publications/notifications");
59
- var common_1 = require("../util/common");
60
- var moment = require("moment");
61
59
  var report_builder_reports_1 = require("../publications/report-builder-reports");
62
- var NodeCache = require("node-cache");
63
60
  var report_builder_libraries_1 = require("../publications/report-builder-libraries");
64
61
  var user_groups_1 = require("../publications/user-groups");
65
62
  var user_guides_1 = require("../publications/user-guides");
@@ -71,13 +68,9 @@ var SubscriptionManager = /** @class */ (function () {
71
68
  this._publications = {};
72
69
  this._subscriptions = [];
73
70
  this._loggedInUsers = [];
74
- this._oplogQueue = [];
75
- this._subCache = [];
76
- this._cacheId = 0;
77
71
  this._sendQueue = [];
78
72
  this._runningQueue = false;
79
73
  this._mainServer = mainServer;
80
- this._nodeCache = new NodeCache({ stdTTL: 0, checkperiod: 0 });
81
74
  this.serverConfig = serverConfig;
82
75
  this._wss = wss;
83
76
  // Publications
@@ -97,13 +90,6 @@ var SubscriptionManager = /** @class */ (function () {
97
90
  user_guides_1.loadUserGuidePublications(this);
98
91
  report_builder_dashboard_builders_1.loadReportBuilderDashboardBuilderPublications(this);
99
92
  this.tailOpLog();
100
- this.runOpLogQueue();
101
- setInterval(function () {
102
- console.log(new Date());
103
- console.log('Sub Cache Length: ' + _this._subCache.length);
104
- console.log('Sub Length: ' + _this._subscriptions.length);
105
- console.log(_this._nodeCache.getStats().vsize);
106
- }, 43200000);
107
93
  setInterval(function () {
108
94
  if (!_this._runningQueue) {
109
95
  if (_this._sendQueue.length) {
@@ -131,77 +117,7 @@ var SubscriptionManager = /** @class */ (function () {
131
117
  _this._runningQueue = false;
132
118
  }
133
119
  }
134
- }, 10);
135
- setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
136
- var _a, userCopy, _loop_2, this_1, i, i, sub, _loop_3, this_2, j;
137
- return __generator(this, function (_b) {
138
- switch (_b.label) {
139
- case 0:
140
- _a = this;
141
- return [4 /*yield*/, logged_in_users_collection_1.LoggedInUsers.find()];
142
- case 1:
143
- _a._loggedInUsers = _b.sent();
144
- userCopy = common_1.deepCopy(this._loggedInUsers);
145
- _loop_2 = function (i) {
146
- var loggedInUser;
147
- return __generator(this, function (_a) {
148
- switch (_a.label) {
149
- case 0:
150
- loggedInUser = userCopy[i];
151
- if (!(!loggedInUser.date || moment().diff(moment(loggedInUser.date), 'seconds') >= 60)) return [3 /*break*/, 3];
152
- if (!this_1._mainServer.getWS(loggedInUser.id_ws)) return [3 /*break*/, 1];
153
- this_1.unsubscribeAll(this_1._mainServer.getWS(loggedInUser.id_ws));
154
- return [3 /*break*/, 3];
155
- case 1:
156
- this_1._subscriptions.forEach(function (sub) {
157
- for (var j = sub.clients.length - 1; j >= 0; j--) {
158
- var client = sub.clients[j];
159
- if (client.id_socket === loggedInUser.id_ws) {
160
- sub.clients.splice(j, 1);
161
- }
162
- }
163
- });
164
- return [4 /*yield*/, logged_in_users_collection_1.LoggedInUsers.findById(loggedInUser._id)];
165
- case 2:
166
- if (_a.sent()) {
167
- logged_in_users_collection_1.LoggedInUsers.deleteOne({ _id: loggedInUser._id });
168
- }
169
- this_1._loggedInUsers.splice(i, 1);
170
- _a.label = 3;
171
- case 3: return [2 /*return*/];
172
- }
173
- });
174
- };
175
- this_1 = this;
176
- i = this._loggedInUsers.length - 1;
177
- _b.label = 2;
178
- case 2:
179
- if (!(i >= 0)) return [3 /*break*/, 5];
180
- return [5 /*yield**/, _loop_2(i)];
181
- case 3:
182
- _b.sent();
183
- _b.label = 4;
184
- case 4:
185
- i--;
186
- return [3 /*break*/, 2];
187
- case 5:
188
- for (i = 0; i < this._subscriptions.length; i++) {
189
- sub = this._subscriptions[i];
190
- _loop_3 = function (j) {
191
- var client = sub.clients[j];
192
- if (!this_2._loggedInUsers.some(function (a) { return a.id_ws === client.id_socket; })) {
193
- sub.clients.splice(j, 1);
194
- }
195
- };
196
- this_2 = this;
197
- for (j = sub.clients.length - 1; j >= 0; j--) {
198
- _loop_3(j);
199
- }
200
- }
201
- return [2 /*return*/];
202
- }
203
- });
204
- }); }, 60000);
120
+ }, 1);
205
121
  }
206
122
  // Add all files to publications private object
207
123
  SubscriptionManager.prototype.publications = function (method) {
@@ -235,101 +151,91 @@ var SubscriptionManager = /** @class */ (function () {
235
151
  subscriptionData[_i - 4] = arguments[_i];
236
152
  }
237
153
  return __awaiter(this, void 0, void 0, function () {
238
- var that, pub, valObj, valKeys, rootKeys, i, sub, _a, newLoggedInUser;
239
- var _b;
240
- return __generator(this, function (_c) {
241
- switch (_c.label) {
242
- case 0:
243
- that = this;
244
- pub = that._publications[publication];
245
- if (!!pub) return [3 /*break*/, 1];
246
- console.error('No Publication: ' + publication);
247
- return [2 /*return*/];
248
- case 1:
249
- if (subscriptionData.length > 1 || subscriptionData[0]) {
250
- if (!pub.check) {
251
- console.error('No Check Function For Pub ' + publication);
252
- return [2 /*return*/];
154
+ var pub, valObj, valKeys, rootKeys, i, sub, newLoggedInUser;
155
+ return __generator(this, function (_a) {
156
+ pub = this._publications[publication];
157
+ if (!pub) {
158
+ console.error('No Publication: ' + publication);
159
+ return [2 /*return*/];
160
+ }
161
+ else {
162
+ if (subscriptionData.length > 1 || subscriptionData[0]) {
163
+ if (!pub.check) {
164
+ console.error('No Check Function For Pub ' + publication);
165
+ return [2 /*return*/];
166
+ }
167
+ else if (!pub.check._schema) {
168
+ console.error('No Check Schema For Pub ' + publication);
169
+ return [2 /*return*/];
170
+ }
171
+ else {
172
+ valObj = {};
173
+ valKeys = Object.keys(pub.check._schema);
174
+ rootKeys = valKeys.filter(function (a) { return !a.includes('.'); });
175
+ for (i = 0; i < subscriptionData.length; i++) {
176
+ valObj[rootKeys[i]] = subscriptionData[i];
253
177
  }
254
- else if (!pub.check._schema) {
255
- console.error('No Check Schema For Pub ' + publication);
256
- return [2 /*return*/];
178
+ try {
179
+ pub.check.validate(valObj);
257
180
  }
258
- else {
259
- valObj = {};
260
- valKeys = Object.keys(pub.check._schema);
261
- rootKeys = valKeys.filter(function (a) { return !a.includes('.'); });
262
- for (i = 0; i < subscriptionData.length; i++) {
263
- valObj[rootKeys[i]] = subscriptionData[i];
264
- }
265
- try {
266
- pub.check.validate(valObj);
267
- }
268
- catch (errors) {
269
- if (errors) {
270
- console.error('Error in Pub Check (' + publication + ')', errors);
271
- return [2 /*return*/];
272
- }
181
+ catch (errors) {
182
+ if (errors) {
183
+ console.error('Error in Pub Check (' + publication + ')', errors);
184
+ return [2 /*return*/];
273
185
  }
274
186
  }
275
187
  }
276
- sub = that._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
277
- // If sub found (another user watching same data), add client to same sub
278
- if (sub) {
279
- sub.clients.push({
280
- id_user: ws['id_user'],
281
- messageId: messageId,
282
- id_socket: ws['id_socket']
283
- });
284
- }
285
- // If sub not found, create new sub
286
- else {
287
- that._subscriptions.push({
288
- publication: publication,
289
- subscriptionData: subscriptionData,
290
- collections: that.getPublicationCollections(publication),
291
- clients: [{
292
- id_user: ws['id_user'],
293
- messageId: messageId,
294
- id_socket: ws['id_socket']
295
- }]
296
- });
297
- }
298
- if (!sub) {
299
- sub = that._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
300
- }
301
- // console.log('Subscribe', new Date(), 'Success', sub);
302
- // Send pub data to client who just subscribed only, don't want to send to existing clients since nothing has changed
303
- that.sendPubDataOnce(ws, messageId, sub, 'newSub', that.getPublicationCollections(publication)[0]);
304
- if (!(that._publications[sub.publication].preFunction && !sub.preSubscriptionData)) return [3 /*break*/, 3];
305
- _a = sub;
306
- return [4 /*yield*/, (_b = that._publications[sub.publication]).preFunction.apply(_b, sub.subscriptionData)];
307
- case 2:
308
- _a.preSubscriptionData = _c.sent();
309
- _c.label = 3;
310
- case 3:
311
- if (publication === 'appversion') {
312
- newLoggedInUser = {
313
- _id: mongo_manager_1.objectIdHexString(),
314
- __v: 0,
315
- date: new Date(),
316
- id_user: ws['id_user'],
317
- user: ws['user'],
318
- id_ws: ws['id_socket']
319
- };
320
- this._loggedInUsers.push(newLoggedInUser);
321
- logged_in_users_collection_1.LoggedInUsers.create(newLoggedInUser);
322
- if (ws['user'] !== 'Admin' && this.serverConfig['CLIENT_NAME'] !== 'ResolveIO') {
323
- this._mainServer.getMethodManager().callMethodInternal('supportCreateBillingUser', {
188
+ }
189
+ sub = this._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
190
+ // If sub found (another user watching same data), add client to same sub
191
+ if (sub) {
192
+ sub.clients.push({
193
+ id_user: ws['id_user'],
194
+ messageId: messageId,
195
+ id_socket: ws['id_socket']
196
+ });
197
+ }
198
+ // If sub not found, create new sub
199
+ else {
200
+ this._subscriptions.push({
201
+ publication: publication,
202
+ subscriptionData: subscriptionData,
203
+ collections: this.getPublicationCollections(publication),
204
+ clients: [{
324
205
  id_user: ws['id_user'],
325
- user: ws['user'],
326
- date: new Date(),
327
- client: index_1.ResolveIOServer.getClientName()
328
- });
329
- }
330
- }
331
- return [2 /*return*/];
206
+ messageId: messageId,
207
+ id_socket: ws['id_socket']
208
+ }]
209
+ });
210
+ }
211
+ if (!sub) {
212
+ sub = this._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
213
+ }
214
+ // console.log('Subscribe', new Date(), 'Success', sub);
215
+ // Send pub data to client who just subscribed only, don't want to send to existing clients since nothing has changed
216
+ this.sendPubDataOnce(ws, messageId, sub, 'newSub', this.getPublicationCollections(publication)[0]);
332
217
  }
218
+ if (publication === 'appversion') {
219
+ newLoggedInUser = {
220
+ _id: mongo_manager_1.objectIdHexString(),
221
+ __v: 0,
222
+ date: new Date(),
223
+ id_user: ws['id_user'],
224
+ user: ws['user'],
225
+ id_ws: ws['id_socket']
226
+ };
227
+ this._loggedInUsers.push(newLoggedInUser);
228
+ logged_in_users_collection_1.LoggedInUsers.create(newLoggedInUser);
229
+ if (ws['user'] !== 'Admin' && this.serverConfig['CLIENT_NAME'] !== 'ResolveIO') {
230
+ this._mainServer.getMethodManager().callMethodInternal('supportCreateBillingUser', {
231
+ id_user: ws['id_user'],
232
+ user: ws['user'],
233
+ date: new Date(),
234
+ client: index_1.ResolveIOServer.getClientName()
235
+ });
236
+ }
237
+ }
238
+ return [2 /*return*/];
333
239
  });
334
240
  });
335
241
  };
@@ -340,17 +246,16 @@ var SubscriptionManager = /** @class */ (function () {
340
246
  subscriptionData[_i - 4] = arguments[_i];
341
247
  }
342
248
  return __awaiter(this, void 0, void 0, function () {
343
- var that, sub, i;
249
+ var sub, i;
344
250
  return __generator(this, function (_a) {
345
- that = this;
346
251
  // console.log('Before');
347
252
  // console.dir(this._subscriptions.map(a => a.publication));
348
- if (!that._publications[publication]) {
253
+ if (!this._publications[publication]) {
349
254
  console.log('No Publication: ' + publication);
350
255
  return [2 /*return*/];
351
256
  }
352
257
  else {
353
- sub = that._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
258
+ sub = this._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
354
259
  if (sub) {
355
260
  for (i = sub.clients.length - 1; i >= 0; i--) {
356
261
  if (sub.clients[i].id_user === ws['id_user'] && sub.clients[i].messageId === messageId && sub.clients[i].id_socket === ws['id_socket']) {
@@ -366,12 +271,10 @@ var SubscriptionManager = /** @class */ (function () {
366
271
  // Unsubscribe from publication
367
272
  SubscriptionManager.prototype.unsubscribeAll = function (ws) {
368
273
  return __awaiter(this, void 0, void 0, function () {
369
- var that, loggedInUser, userSubs, i, sub, j;
274
+ var loggedInUser, userSubs, i, sub, j;
370
275
  return __generator(this, function (_a) {
371
276
  switch (_a.label) {
372
- case 0:
373
- that = this;
374
- return [4 /*yield*/, logged_in_users_collection_1.LoggedInUsers.findOne({ id_ws: ws['id_socket'] })];
277
+ case 0: return [4 /*yield*/, logged_in_users_collection_1.LoggedInUsers.findOne({ id_ws: ws['id_socket'] })];
375
278
  case 1:
376
279
  loggedInUser = _a.sent();
377
280
  if (loggedInUser) {
@@ -380,7 +283,7 @@ var SubscriptionManager = /** @class */ (function () {
380
283
  }
381
284
  logged_in_users_collection_1.LoggedInUsers.deleteOne({ _id: loggedInUser._id });
382
285
  }
383
- userSubs = that._subscriptions.filter(function (a) { return a.clients.some(function (b) { return b.id_user === ws['id_user'] && b.id_socket === ws['id_socket']; }); });
286
+ userSubs = this._subscriptions.filter(function (a) { return a.clients.some(function (b) { return b.id_user === ws['id_user'] && b.id_socket === ws['id_socket']; }); });
384
287
  for (i = userSubs.length - 1; i >= 0; i--) {
385
288
  sub = userSubs[i];
386
289
  for (j = sub.clients.length - 1; j >= 0; j--) {
@@ -402,8 +305,15 @@ var SubscriptionManager = /** @class */ (function () {
402
305
  // Watch (tail) Mongo's operation log on the entire database (all insert/modify/delete will trigger this function)
403
306
  SubscriptionManager.prototype.tailOpLog = function () {
404
307
  var _this = this;
308
+ if (index_1.ResolveIOServer.getMainDB() && index_1.ResolveIOServer.getMainDB().watch) {
309
+ console.log('Yea');
310
+ }
311
+ else {
312
+ console.log('Nah');
313
+ }
405
314
  this._oplog = index_1.ResolveIOServer.getMainDB().watch([], { fullDocument: 'updateLookup' });
406
315
  this._oplog.on('change', function (doc) {
316
+ // console.log(doc);
407
317
  var collection = doc.ns.coll;
408
318
  if (collection && !collection.endsWith('.versions') && !collection.startsWith('monitor-')) {
409
319
  if (doc.operationType === 'insert') {
@@ -419,31 +329,23 @@ var SubscriptionManager = /** @class */ (function () {
419
329
  }
420
330
  else if (collection === 'support-tickets') {
421
331
  if (_this.serverConfig['ROOT_URL'] === 'https://resolveio.com') {
422
- _this._mainServer.getMethodManager().callMethodInternal('sendSupportTicketEmail', doc.fullDocument._id);
332
+ _this._mainServer.getMethodManager().callMethodInternal('sendSupportTicketEmail', doc.o._id);
423
333
  }
424
334
  }
425
- _this._oplogQueue.push({
426
- type: 'insert',
427
- collection: collection,
428
- doc: doc.fullDocument
429
- });
335
+ _this.checkPublicationsForChange(collection, 'insert');
430
336
  }
431
337
  }
432
338
  else if (doc.operationType === 'update' || doc.operationType === 'replace') {
433
339
  if (collection === 'cron-jobs') {
434
340
  _this._mainServer.getCronManager().checkCronJobsForUpdates();
435
341
  }
436
- _this._oplogQueue.push({
437
- type: 'update',
438
- collection: collection,
439
- doc: doc.fullDocument
440
- });
342
+ _this.checkPublicationsForChange(collection, 'update');
441
343
  }
442
344
  else if (doc.operationType === 'delete') {
443
- _this.resendPubsForDelete(collection);
444
345
  if (collection === 'cron-jobs') {
445
346
  _this._mainServer.getCronManager().checkCronJobsForUpdates();
446
347
  }
348
+ _this.checkPublicationsForChange(collection, 'delete');
447
349
  }
448
350
  }
449
351
  });
@@ -456,290 +358,63 @@ var SubscriptionManager = /** @class */ (function () {
456
358
  process.exit();
457
359
  });
458
360
  };
459
- SubscriptionManager.prototype.runOpLogQueue = function () {
460
- var _this = this;
461
- var that = this;
462
- setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
463
- var uniqueOplog_1, uniquePubs, _loop_4, this_3, i, i, oplog, _loop_5, this_4, j, i, pub;
464
- return __generator(this, function (_a) {
465
- switch (_a.label) {
466
- case 0:
467
- if (!this._oplogQueue.length) return [3 /*break*/, 7];
468
- uniqueOplog_1 = common_1.deepCopy(this._oplogQueue);
469
- this._oplogQueue = [];
470
- uniquePubs = [];
471
- _loop_4 = function (i) {
472
- if (!that._subscriptions.some(function (a) { return a.collections.includes(uniqueOplog_1[i].collection); })) {
473
- for (var j = this_3._subCache.length - 1; j >= 0; j--) {
474
- if (this_3._subCache[j].collection === uniqueOplog_1[i].collection) {
475
- this_3._nodeCache.del(this_3._subCache[j].id);
476
- this_3._subCache.splice(j, 1);
477
- }
478
- }
479
- uniqueOplog_1.splice(i, 1);
480
- }
481
- };
482
- this_3 = this;
483
- for (i = uniqueOplog_1.length - 1; i >= 0; i--) {
484
- _loop_4(i);
485
- }
486
- i = uniqueOplog_1.length - 1;
487
- _a.label = 1;
488
- case 1:
489
- if (!(i >= 0)) return [3 /*break*/, 6];
490
- oplog = uniqueOplog_1[i];
491
- _loop_5 = function (j) {
492
- var sub, _a, i_1;
493
- var _b, _c;
494
- return __generator(this, function (_d) {
495
- switch (_d.label) {
496
- case 0:
497
- sub = that._subscriptions[j];
498
- if (!sub) return [3 /*break*/, 5];
499
- if (!(sub.collections.includes(oplog.collection) && !uniquePubs.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }).length)) return [3 /*break*/, 5];
500
- if (sub.preSubscriptionData) {
501
- if (that._publications[sub.publication].fetchFunction(oplog.doc, sub.preSubscriptionData)) {
502
- sub['oplog'] = oplog;
503
- uniquePubs.push(common_1.deepCopy(sub));
504
- return [2 /*return*/, "continue"];
505
- }
506
- }
507
- if (!that._publications[sub.publication].preFunction) return [3 /*break*/, 4];
508
- if (!sub.clients.length) return [3 /*break*/, 2];
509
- _a = sub;
510
- return [4 /*yield*/, (_b = that._publications[sub.publication]).preFunction.apply(_b, sub.subscriptionData)];
511
- case 1:
512
- _a.preSubscriptionData = _d.sent();
513
- if (that._publications[sub.publication].fetchFunction(oplog.doc, sub.preSubscriptionData)) {
514
- sub['oplog'] = oplog;
515
- uniquePubs.push(common_1.deepCopy(sub));
516
- }
517
- return [3 /*break*/, 3];
518
- case 2:
519
- for (i_1 = this_4._subCache.length - 1; i_1 >= 0; i_1--) {
520
- if (this_4._subCache[i_1].publication === sub.publication && JSON.stringify(this_4._subCache[i_1].subscriptionData) === JSON.stringify(sub.subscriptionData)) {
521
- this_4._nodeCache.del(this_4._subCache[i_1].id);
522
- this_4._subCache.splice(i_1, 1);
523
- }
524
- }
525
- if (that._subscriptions.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }) >= 0) {
526
- that._subscriptions.splice(that._subscriptions.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
527
- }
528
- _d.label = 3;
529
- case 3: return [3 /*break*/, 5];
530
- case 4:
531
- if (that._publications[sub.publication].fetchFunction) {
532
- if ((_c = that._publications[sub.publication]).fetchFunction.apply(_c, __spreadArrays([oplog.doc], sub.subscriptionData))) {
533
- sub['oplog'] = oplog;
534
- uniquePubs.push(common_1.deepCopy(sub));
535
- }
536
- }
537
- else {
538
- sub['oplog'] = oplog;
539
- uniquePubs.push(common_1.deepCopy(sub));
540
- }
541
- _d.label = 5;
542
- case 5: return [2 /*return*/];
543
- }
544
- });
545
- };
546
- this_4 = this;
547
- j = that._subscriptions.length - 1;
548
- _a.label = 2;
549
- case 2:
550
- if (!(j >= 0)) return [3 /*break*/, 5];
551
- return [5 /*yield**/, _loop_5(j)];
552
- case 3:
553
- _a.sent();
554
- _a.label = 4;
555
- case 4:
556
- j--;
557
- return [3 /*break*/, 2];
558
- case 5:
559
- i--;
560
- return [3 /*break*/, 1];
561
- case 6:
562
- for (i = 0; i < uniquePubs.length; i++) {
563
- pub = uniquePubs[i];
564
- this.checkPubsForChanges(pub['oplog'].collection, pub['oplog'].doc, pub['oplog'].type, pub);
565
- }
566
- _a.label = 7;
567
- case 7: return [2 /*return*/];
568
- }
569
- });
570
- }); }, 100);
571
- };
572
- SubscriptionManager.prototype.resendPubsForDelete = function (collection) {
573
- return __awaiter(this, void 0, void 0, function () {
574
- var that, subCopy, _loop_6, this_5, i, zz, sub, aa, client, bb, ws;
575
- var _this = this;
576
- return __generator(this, function (_a) {
577
- that = this;
578
- subCopy = common_1.deepCopy(that._subscriptions.filter(function (a) { return a.collections.includes(collection); }));
579
- _loop_6 = function (i) {
580
- if (this_5._subCache[i].collection === collection || subCopy.some(function (a) { return a.publication === _this._subCache[i].publication; })) {
581
- this_5._nodeCache.del(this_5._subCache[i].id);
582
- this_5._subCache.splice(i, 1);
583
- }
584
- };
585
- this_5 = this;
586
- for (i = this._subCache.length - 1; i >= 0; i--) {
587
- _loop_6(i);
588
- }
589
- for (zz = subCopy.length - 1; zz >= 0; zz--) {
590
- sub = subCopy[zz];
591
- if (that._publications[sub.publication].ws_specific) {
592
- for (aa = 0; aa < sub.clients.length; aa++) {
593
- client = sub.clients[aa];
594
- for (bb = 0; bb < that._wss.clients.size; bb++) {
595
- ws = Array.from(that._wss.clients)[bb];
596
- if (ws['id_socket'] === client.id_socket) {
597
- that.sendPubDataOnce(ws, client.messageId, sub, 'delete', collection);
598
- }
599
- }
600
- }
601
- }
602
- else {
603
- that.sendPubData(sub, 'delete', collection);
604
- }
605
- }
606
- return [2 /*return*/];
607
- });
608
- });
609
- };
610
- // Document: Inserted/Modified Document
611
- // Collection: Collection Document Lives In
612
- // Check to see if any pubs need to refetch due to Mongo operation
613
- SubscriptionManager.prototype.checkPubsForChanges = function (collection, document, type, sub) {
614
- return __awaiter(this, void 0, void 0, function () {
615
- var that, k, client, j, ws, j, ws;
616
- var _a, _b;
617
- return __generator(this, function (_c) {
618
- that = this;
619
- if (sub.preSubscriptionData) {
620
- if (that._publications[sub.publication].fetchFunction(document, sub.preSubscriptionData)) {
621
- that.sendPubData(sub, type, collection);
622
- return [2 /*return*/];
623
- }
624
- }
625
- if (!that._publications[sub.publication].preFunction) {
626
- if (that._publications[sub.publication].ws_specific) {
627
- for (k = 0; k < sub.clients.length; k++) {
628
- client = sub.clients[k];
629
- if (that._publications[sub.publication].fetchFunction) {
630
- if ((_a = that._publications[sub.publication]).fetchFunction.apply(_a, __spreadArrays([document, client.id_user], sub.subscriptionData))) {
631
- for (j = 0; j < that._wss.clients.size; j++) {
632
- ws = Array.from(that._wss.clients)[j];
633
- if (ws['id_socket'] === client.id_socket) {
634
- that.sendPubDataOnce(ws, client.messageId, sub, type, collection);
635
- }
636
- }
637
- }
638
- }
639
- else {
640
- for (j = 0; j < that._wss.clients.size; j++) {
641
- ws = Array.from(that._wss.clients)[j];
642
- if (ws['id_socket'] === client.id_socket) {
643
- that.sendPubDataOnce(ws, client.messageId, sub, type, collection);
644
- }
645
- }
646
- }
647
- }
648
- }
649
- else {
650
- if (that._publications[sub.publication].fetchFunction) {
651
- if ((_b = that._publications[sub.publication]).fetchFunction.apply(_b, __spreadArrays([document], sub.subscriptionData))) {
652
- that.sendPubData(sub, type, collection);
653
- }
654
- // else
655
- }
656
- else {
657
- that.sendPubData(sub, type, collection);
361
+ SubscriptionManager.prototype.checkPublicationsForChange = function (collection, type) {
362
+ var collSubs = this._subscriptions.filter(function (a) { return a.collections.includes(collection); });
363
+ for (var i = collSubs.length - 1; i >= 0; i--) {
364
+ var sub = collSubs[i];
365
+ if (this._publications[sub.publication].ws_specific) {
366
+ for (var j = 0; j < sub.clients.length; j++) {
367
+ var client = sub.clients[j];
368
+ for (var k = 0; k < this._wss.clients.size; k++) {
369
+ var ws = Array.from(this._wss.clients)[k];
370
+ if (ws['id_socket'] === client.id_socket) {
371
+ this.sendPubDataOnce(ws, client.messageId, sub, type, collection);
658
372
  }
659
373
  }
660
374
  }
661
- return [2 /*return*/];
662
- });
663
- });
375
+ }
376
+ else {
377
+ this.sendPubData(sub, type, collection);
378
+ }
379
+ }
664
380
  };
665
381
  // Fetch pub once, send to all clients linked to this pub
666
382
  SubscriptionManager.prototype.sendPubDataOnce = function (ws, messageId, subscription, type, collection) {
667
383
  var _this = this;
668
384
  return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
669
- var that, serverRes, i;
670
385
  var _a, _b;
671
386
  var _this = this;
672
387
  return __generator(this, function (_c) {
673
- that = this;
674
- if (!that._publications[subscription.publication].ws_specific) { // Same pub for all users
675
- if (this._subCache.some(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); })) {
676
- serverRes = {
677
- messageId: messageId,
678
- hasError: false,
679
- data: this._nodeCache.get(this._subCache.filter(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); })[0].id)
680
- };
681
- this.sendWS(ws, serverRes);
682
- resolve(true);
388
+ if (!this._publications[subscription.publication].ws_specific) { // Same pub for all users
389
+ if (subscription.publication !== 'superadminAPM' && subscription.publication !== 'loggedInUsers') {
390
+ this._mainServer.getMethodManager().callMethodInternal('insertSubscriptionLog', type, subscription.publication, collection, JSON.stringify(subscription.subscriptionData));
683
391
  }
684
- else {
685
- if (subscription.publication !== 'superadminAPM' && subscription.publication !== 'loggedInUsers') {
686
- that._mainServer.getMethodManager().callMethodInternal('insertSubscriptionLog', type, subscription.publication, collection, JSON.stringify(subscription.subscriptionData));
687
- }
688
- for (i = this._subCache.length - 1; i >= 0; i--) {
689
- if (this._subCache[i].publication === subscription.publication && JSON.stringify(this._subCache[i].subscriptionData) === JSON.stringify(subscription.subscriptionData)) {
690
- this._nodeCache.del(this._subCache[i].id);
691
- this._subCache.splice(i, 1);
692
- }
693
- }
694
- (_a = that._publications[subscription.publication].function).call.apply(_a, __spreadArrays([Object.assign({}, that, SubscriptionManager.prototype)], subscription.subscriptionData)).catch(function (methodErrs) {
695
- that._mainServer.getMethodManager().sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'Error Detected During Subscription ' + subscription.publication + ' - (sendPubDataOnce - No WS)\n\nData \n' + JSON.stringify(subscription.subscriptionData, null, 2) + '\n\nErrors\n' + JSON.stringify(methodErrs, null, 2));
696
- })
697
- .then(function (res) {
698
- var nodeCacheSize = _this._nodeCache.getStats().vsize;
699
- if (nodeCacheSize > 500000000) {
700
- var neededSizeIndex = 0;
701
- for (var zz = 0; zz < _this._subCache.length; zz++) {
702
- _this._nodeCache.del(_this._subCache[zz].id);
703
- if (_this._nodeCache.getStats().vsize < 500000000) {
704
- neededSizeIndex = zz;
705
- break;
706
- }
707
- }
708
- // console.log('Cache1: ' + 'Too Big - ' + subscription.publication + ' - ' + getBinarySize(JSON.stringify(this._subCache)));
709
- _this._subCache.splice(0, neededSizeIndex);
710
- }
711
- for (var i = _this._subCache.length - 1; i >= 0; i--) {
712
- if (_this._subCache[i].publication === subscription.publication && JSON.stringify(_this._subCache[i].subscriptionData) === JSON.stringify(subscription.subscriptionData)) {
713
- _this._nodeCache.del(_this._subCache[i].id);
714
- _this._subCache.splice(i, 1);
715
- }
716
- }
717
- if (_this._subscriptions.some(function (a) { return a.collections.includes(collection) && a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); })) {
718
- var cacheId = _this._cacheId++;
719
- _this._nodeCache.set(cacheId, res);
720
- _this._subCache.push({ collection: collection, publication: subscription.publication, subscriptionData: subscription.subscriptionData, id: cacheId });
721
- var serverRes = {
722
- messageId: messageId,
723
- hasError: false,
724
- data: res
725
- };
726
- _this.sendWS(ws, serverRes);
727
- }
728
- resolve(true);
729
- }, function (err) {
392
+ (_a = this._publications[subscription.publication].function).call.apply(_a, __spreadArrays([Object.assign({}, this, SubscriptionManager.prototype)], subscription.subscriptionData)).catch(function (methodErrs) {
393
+ _this._mainServer.getMethodManager().sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + _this.serverConfig['CLIENT_NAME'], 'Error Detected During Subscription ' + subscription.publication + ' - (sendPubDataOnce - No WS)\n\nData \n' + JSON.stringify(subscription.subscriptionData, null, 2) + '\n\nErrors\n' + JSON.stringify(methodErrs, null, 2));
394
+ })
395
+ .then(function (res) {
396
+ if (_this._subscriptions.some(function (a) { return a.collections.includes(collection) && a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); })) {
730
397
  var serverRes = {
731
398
  messageId: messageId,
732
- hasError: true,
733
- data: err
399
+ hasError: false,
400
+ data: res
734
401
  };
735
402
  _this.sendWS(ws, serverRes);
736
- reject(err);
737
- });
738
- }
403
+ }
404
+ resolve(true);
405
+ }, function (err) {
406
+ var serverRes = {
407
+ messageId: messageId,
408
+ hasError: true,
409
+ data: err
410
+ };
411
+ _this.sendWS(ws, serverRes);
412
+ reject(err);
413
+ });
739
414
  }
740
415
  else { // Pub is different for each user (Look at users publication) [MUCH SLOWER, RE-RUNS FOR EACH USER]
741
- (_b = that._publications[subscription.publication].function).call.apply(_b, __spreadArrays([Object.assign({}, that, SubscriptionManager.prototype), ws['id_user']], subscription.subscriptionData)).catch(function (methodErrs) {
742
- that._mainServer.getMethodManager().sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'Error Detected During Subscription ' + subscription.publication + ' - (sendPubDataOnce - WS)\n\nData \n' + JSON.stringify(subscription.subscriptionData, null, 2) + '\n\nErrors\n' + JSON.stringify(methodErrs, null, 2));
416
+ (_b = this._publications[subscription.publication].function).call.apply(_b, __spreadArrays([Object.assign({}, this, SubscriptionManager.prototype), ws['id_user']], subscription.subscriptionData)).catch(function (methodErrs) {
417
+ _this._mainServer.getMethodManager().sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + _this.serverConfig['CLIENT_NAME'], 'Error Detected During Subscription ' + subscription.publication + ' - (sendPubDataOnce - WS)\n\nData \n' + JSON.stringify(subscription.subscriptionData, null, 2) + '\n\nErrors\n' + JSON.stringify(methodErrs, null, 2));
743
418
  })
744
419
  .then(function (res) {
745
420
  var serverRes = {
@@ -769,67 +444,31 @@ var SubscriptionManager = /** @class */ (function () {
769
444
  SubscriptionManager.prototype.sendPubData = function (subscription, type, collection) {
770
445
  var _this = this;
771
446
  return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
772
- var that, i, i;
773
447
  var _a;
774
448
  var _this = this;
775
449
  return __generator(this, function (_b) {
776
- that = this;
777
450
  if (!subscription.clients.length) {
778
- for (i = this._subCache.length - 1; i >= 0; i--) {
779
- if (this._subCache[i].publication === subscription.publication && JSON.stringify(this._subCache[i].subscriptionData) === JSON.stringify(subscription.subscriptionData)) {
780
- this._nodeCache.del(this._subCache[i].id);
781
- this._subCache.splice(i, 1);
782
- }
783
- }
784
- if (that._subscriptions.findIndex(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); }) >= 0) {
785
- that._subscriptions.splice(that._subscriptions.findIndex(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); }), 1);
451
+ if (this._subscriptions.findIndex(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); }) >= 0) {
452
+ this._subscriptions.splice(this._subscriptions.findIndex(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); }), 1);
786
453
  }
787
454
  }
788
455
  else {
789
456
  if (subscription.publication !== 'superadminAPM' && subscription.publication !== 'loggedInUsers') {
790
- that._mainServer.getMethodManager().callMethodInternal('insertSubscriptionLog', type, subscription.publication, collection, JSON.stringify(subscription.subscriptionData));
791
- }
792
- for (i = this._subCache.length - 1; i >= 0; i--) {
793
- if (this._subCache[i].publication === subscription.publication && JSON.stringify(this._subCache[i].subscriptionData) === JSON.stringify(subscription.subscriptionData)) {
794
- this._nodeCache.del(this._subCache[i].id);
795
- this._subCache.splice(i, 1);
796
- }
457
+ this._mainServer.getMethodManager().callMethodInternal('insertSubscriptionLog', type, subscription.publication, collection, JSON.stringify(subscription.subscriptionData));
797
458
  }
798
- (_a = that._publications[subscription.publication].function).call.apply(_a, __spreadArrays([Object.assign({}, that, SubscriptionManager.prototype)], subscription.subscriptionData)).catch(function (methodErrs) {
799
- that._mainServer.getMethodManager().sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'Error Detected During Subscription ' + subscription.publication + ' - (sendPubData)\n\nData \n' + JSON.stringify(subscription.subscriptionData, null, 2) + '\n\nErrors\n' + JSON.stringify(methodErrs, null, 2));
459
+ (_a = this._publications[subscription.publication].function).call.apply(_a, __spreadArrays([Object.assign({}, this, SubscriptionManager.prototype)], subscription.subscriptionData)).catch(function (methodErrs) {
460
+ _this._mainServer.getMethodManager().sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + _this.serverConfig['CLIENT_NAME'], 'Error Detected During Subscription ' + subscription.publication + ' - (sendPubData)\n\nData \n' + JSON.stringify(subscription.subscriptionData, null, 2) + '\n\nErrors\n' + JSON.stringify(methodErrs, null, 2));
800
461
  })
801
462
  .then(function (res) {
802
- var nodeCacheSize = _this._nodeCache.getStats().vsize;
803
- if (nodeCacheSize > 500000000) {
804
- var neededSizeIndex = 0;
805
- for (var zz = 0; zz < _this._subCache.length; zz++) {
806
- _this._nodeCache.del(_this._subCache[zz].id);
807
- if (_this._nodeCache.getStats().vsize < 500000000) {
808
- neededSizeIndex = zz;
809
- break;
810
- }
811
- }
812
- // console.log('Cache: ' + 'Too Big - ' + subscription.publication + ' - ' + getBinarySize(JSON.stringify(this._subCache)));
813
- _this._subCache.splice(0, neededSizeIndex);
814
- }
815
- for (var i = _this._subCache.length - 1; i >= 0; i--) {
816
- if (_this._subCache[i].publication === subscription.publication && JSON.stringify(_this._subCache[i].subscriptionData) === JSON.stringify(subscription.subscriptionData)) {
817
- _this._nodeCache.del(_this._subCache[i].id);
818
- _this._subCache.splice(i, 1);
819
- }
820
- }
821
463
  if (!subscription.clients.length) {
822
- if (that._subscriptions.findIndex(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); }) >= 0) {
823
- that._subscriptions.splice(that._subscriptions.findIndex(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); }), 1);
464
+ if (_this._subscriptions.findIndex(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); }) >= 0) {
465
+ _this._subscriptions.splice(_this._subscriptions.findIndex(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); }), 1);
824
466
  }
825
467
  }
826
468
  else {
827
469
  if (_this._subscriptions.some(function (a) { return a.collections.includes(collection) && a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); })) {
828
- var cacheId = _this._cacheId++;
829
- _this._nodeCache.set(cacheId, res);
830
- _this._subCache.push({ collection: collection, publication: subscription.publication, subscriptionData: subscription.subscriptionData, id: cacheId });
831
470
  subscription.clients.forEach(function (client) {
832
- that._wss.clients.forEach(function (ws) {
471
+ _this._wss.clients.forEach(function (ws) {
833
472
  if (ws['id_socket'] === client.id_socket) {
834
473
  var serverRes = {
835
474
  messageId: client.messageId,
@@ -845,7 +484,7 @@ var SubscriptionManager = /** @class */ (function () {
845
484
  resolve(true);
846
485
  }, function (err) {
847
486
  subscription.clients.forEach(function (client) {
848
- that._wss.clients.forEach(function (ws) {
487
+ _this._wss.clients.forEach(function (ws) {
849
488
  if (ws['id_socket'] === client.id_socket) {
850
489
  var serverRes = {
851
490
  messageId: client.messageId,