@resolveio/server-lib 4.6.19 → 5.0.0-rc-1

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.
@@ -5,7 +5,7 @@ export declare class SubscriptionManager {
5
5
  private _publications;
6
6
  private _subscriptions;
7
7
  private _wss;
8
- private _oplogQueue;
8
+ private _subCache;
9
9
  private _sendQueue;
10
10
  private _runningQueue;
11
11
  private serverConfig;
@@ -18,7 +18,6 @@ export declare class SubscriptionManager {
18
18
  private getPublicationCollections;
19
19
  private tailOpLog;
20
20
  private tailOpLogSupport;
21
- private runOpLogQueue;
22
21
  private resendPubsForDelete;
23
22
  private checkPubsForChanges;
24
23
  private checkSupportPubsForChanges;
@@ -51,7 +51,6 @@ var method_responses_1 = require("../publications/method-responses");
51
51
  var support_tickets_1 = require("../publications/support-tickets");
52
52
  var index_1 = require("../index");
53
53
  var notifications_1 = require("../publications/notifications");
54
- var common_1 = require("../util/common");
55
54
  var billing_logged_in_users_collection_1 = require("../collections/billing-logged-in-users.collection");
56
55
  var moment = require("moment");
57
56
  var billing_logged_in_users_1 = require("../publications/billing-logged-in-users");
@@ -61,7 +60,7 @@ var SubscriptionManager = /** @class */ (function () {
61
60
  var _this = this;
62
61
  this._publications = {};
63
62
  this._subscriptions = [];
64
- this._oplogQueue = [];
63
+ this._subCache = [];
65
64
  this._sendQueue = [];
66
65
  this._runningQueue = false;
67
66
  this._mainServer = mainServer;
@@ -81,7 +80,6 @@ var SubscriptionManager = /** @class */ (function () {
81
80
  notifications_1.loadNotificationPublications(this);
82
81
  report_builder_reports_1.loadReportBuilderReportPublications(this);
83
82
  this.tailOpLog();
84
- this.runOpLogQueue();
85
83
  if (index_1.ResolveIOServer.getSupportDB()) {
86
84
  support_tickets_1.loadSupportTicketPublications(this);
87
85
  this.tailOpLogSupport();
@@ -411,11 +409,7 @@ var SubscriptionManager = /** @class */ (function () {
411
409
  _this._mainServer.getMethodManager().callMethodInternal('sendSupportTicketEmail', doc.o._id);
412
410
  }
413
411
  if (!collection.endsWith('.versions') && !collection.startsWith('system.')) {
414
- _this._oplogQueue.push({
415
- type: 'insert',
416
- collection: collection,
417
- doc: doc.o
418
- });
412
+ _this.checkPubsForChanges(collection, doc.o, 'insert');
419
413
  }
420
414
  });
421
415
  oplog.on('update', function (doc) {
@@ -429,11 +423,7 @@ var SubscriptionManager = /** @class */ (function () {
429
423
  _this._mainServer.getCronManager().checkCronJobsForUpdates();
430
424
  }
431
425
  if (!collection.endsWith('.versions') && !collection.startsWith('system.')) {
432
- _this._oplogQueue.push({
433
- type: 'update',
434
- collection: collection,
435
- doc: doc.o
436
- });
426
+ _this.checkPubsForChanges(collection, doc.o, 'update');
437
427
  }
438
428
  });
439
429
  oplog.on('delete', function (doc) {
@@ -506,174 +496,6 @@ var SubscriptionManager = /** @class */ (function () {
506
496
  console.log('oplog stopped support');
507
497
  });
508
498
  };
509
- SubscriptionManager.prototype.runOpLogQueue = function () {
510
- var _this = this;
511
- var that = this;
512
- setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
513
- var uniqueOplog_1, uniquePubs, needIds_1, _loop_3, i, _loop_4, i, i, oplog, _loop_5, j, state_1;
514
- var _this = this;
515
- return __generator(this, function (_a) {
516
- switch (_a.label) {
517
- case 0:
518
- if (!this._oplogQueue.length) return [3 /*break*/, 11];
519
- uniqueOplog_1 = common_1.deepCopy(this._oplogQueue);
520
- this._oplogQueue = [];
521
- uniquePubs = [];
522
- needIds_1 = {};
523
- _loop_3 = function (i) {
524
- if (!that._subscriptions.some(function (a) { return a.collections.includes(uniqueOplog_1[i].collection); })) {
525
- uniqueOplog_1.splice(i, 1);
526
- }
527
- };
528
- for (i = uniqueOplog_1.length - 1; i >= 0; i--) {
529
- _loop_3(i);
530
- }
531
- uniqueOplog_1.forEach(function (oplog) {
532
- var schemaErrors = null;
533
- try {
534
- index_1.ResolveIOServer.getMainDB().model(oplog.collection)['simplschema'].validate(oplog.doc);
535
- }
536
- catch (errors) {
537
- schemaErrors = errors;
538
- }
539
- if (schemaErrors) {
540
- if (!needIds_1[oplog.collection]) {
541
- needIds_1[oplog.collection] = [oplog.doc['_id']];
542
- }
543
- else {
544
- if (!needIds_1[oplog.collection].filter(function (a) { return a === oplog.doc['_id']; })[0]) {
545
- needIds_1[oplog.collection].push(oplog.doc['_id']);
546
- }
547
- }
548
- }
549
- });
550
- _loop_4 = function (i) {
551
- var collection, docs;
552
- return __generator(this, function (_a) {
553
- switch (_a.label) {
554
- case 0:
555
- collection = Object.keys(needIds_1)[i];
556
- return [4 /*yield*/, index_1.ResolveIOServer.getMainDB().model(collection).find({ _id: { $in: needIds_1[collection] } })];
557
- case 1:
558
- docs = _a.sent();
559
- needIds_1[collection].forEach(function (id_doc) {
560
- if (uniqueOplog_1.filter(function (a) { return a.doc && a.doc._id === id_doc; })[0]) {
561
- uniqueOplog_1.filter(function (a) { return a.doc && a.doc._id === id_doc; })[0].doc = docs.filter(function (a) { return a._id === id_doc; })[0];
562
- }
563
- else {
564
- console.log('PROBLEMAAAAAAAA');
565
- }
566
- });
567
- return [2 /*return*/];
568
- }
569
- });
570
- };
571
- i = 0;
572
- _a.label = 1;
573
- case 1:
574
- if (!(i < Object.keys(needIds_1).length)) return [3 /*break*/, 4];
575
- return [5 /*yield**/, _loop_4(i)];
576
- case 2:
577
- _a.sent();
578
- _a.label = 3;
579
- case 3:
580
- i++;
581
- return [3 /*break*/, 1];
582
- case 4:
583
- i = uniqueOplog_1.length - 1;
584
- _a.label = 5;
585
- case 5:
586
- if (!(i >= 0)) return [3 /*break*/, 10];
587
- oplog = uniqueOplog_1[i];
588
- _loop_5 = function (j) {
589
- var _a, sub, _b;
590
- return __generator(this, function (_c) {
591
- switch (_c.label) {
592
- case 0:
593
- sub = that._subscriptions[j];
594
- if (!sub.collections.includes(oplog.collection)) return [3 /*break*/, 3];
595
- if (sub.preSubscriptionData) {
596
- if (that._publications[sub.publication].fetchFunction(oplog.doc, sub.preSubscriptionData)) {
597
- if (!uniquePubs.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }).length) {
598
- sub['oplog'] = oplog;
599
- uniquePubs.push(sub);
600
- }
601
- else {
602
- uniqueOplog_1.splice(i, 1);
603
- }
604
- return [2 /*return*/, "break"];
605
- }
606
- }
607
- if (!that._publications[sub.publication].preFunction) return [3 /*break*/, 2];
608
- _b = sub;
609
- return [4 /*yield*/, that._publications[sub.publication].preFunction(sub.subscriptionData)];
610
- case 1:
611
- _b.preSubscriptionData = _c.sent();
612
- if (that._publications[sub.publication].fetchFunction(oplog.doc, sub.preSubscriptionData)) {
613
- if (!uniquePubs.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }).length) {
614
- sub['oplog'] = oplog;
615
- uniquePubs.push(sub);
616
- }
617
- else {
618
- uniqueOplog_1.splice(i, 1);
619
- }
620
- return [2 /*return*/, "break"];
621
- }
622
- return [3 /*break*/, 3];
623
- case 2:
624
- if (that._publications[sub.publication].fetchFunction) {
625
- if ((_a = that._publications[sub.publication]).fetchFunction.apply(_a, [oplog.doc].concat(sub.subscriptionData))) {
626
- if (!uniquePubs.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }).length) {
627
- sub['oplog'] = oplog;
628
- uniquePubs.push(sub);
629
- }
630
- else {
631
- uniqueOplog_1.splice(i, 1);
632
- }
633
- return [2 /*return*/, "break"];
634
- }
635
- }
636
- else {
637
- if (!uniquePubs.filter(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }).length) {
638
- sub['oplog'] = oplog;
639
- uniquePubs.push(sub);
640
- }
641
- else {
642
- uniqueOplog_1.splice(i, 1);
643
- }
644
- return [2 /*return*/, "break"];
645
- }
646
- _c.label = 3;
647
- case 3: return [2 /*return*/];
648
- }
649
- });
650
- };
651
- j = 0;
652
- _a.label = 6;
653
- case 6:
654
- if (!(j < that._subscriptions.length)) return [3 /*break*/, 9];
655
- return [5 /*yield**/, _loop_5(j)];
656
- case 7:
657
- state_1 = _a.sent();
658
- if (state_1 === "break")
659
- return [3 /*break*/, 9];
660
- _a.label = 8;
661
- case 8:
662
- j++;
663
- return [3 /*break*/, 6];
664
- case 9:
665
- i--;
666
- return [3 /*break*/, 5];
667
- case 10:
668
- uniquePubs.forEach(function (pub) {
669
- _this.checkPubsForChanges(pub['oplog'].collection, pub['oplog'].doc, pub['oplog'].type);
670
- });
671
- _a.label = 11;
672
- case 11: return [2 /*return*/];
673
- }
674
- });
675
- }); }, 500);
676
- };
677
499
  SubscriptionManager.prototype.resendPubsForDelete = function (collection) {
678
500
  var that = this;
679
501
  that._subscriptions.filter(function (a) { return a.collections.includes(collection); }).forEach(function (sub) {
@@ -696,36 +518,55 @@ var SubscriptionManager = /** @class */ (function () {
696
518
  // Check to see if any pubs need to refetch due to Mongo operation
697
519
  SubscriptionManager.prototype.checkPubsForChanges = function (collection, document, type) {
698
520
  return __awaiter(this, void 0, void 0, function () {
699
- var that, _loop_6, i;
521
+ var that, _loop_3, this_1, i;
700
522
  return __generator(this, function (_a) {
701
523
  switch (_a.label) {
702
524
  case 0:
703
525
  that = this;
704
- _loop_6 = function (i) {
705
- var _a, _b, sub, _c, _d;
526
+ _loop_3 = function (i) {
527
+ var _a, _b, schemaErrors, sub, _c, _d;
706
528
  return __generator(this, function (_e) {
707
529
  switch (_e.label) {
708
530
  case 0:
531
+ schemaErrors = null;
532
+ try {
533
+ index_1.ResolveIOServer.getMainDB().model(collection)['simplschema'].validate(document);
534
+ }
535
+ catch (errors) {
536
+ schemaErrors = errors;
537
+ }
538
+ if (!schemaErrors) return [3 /*break*/, 2];
539
+ return [4 /*yield*/, index_1.ResolveIOServer.getMainDB().model(collection).findById(document['_id'])];
540
+ case 1:
541
+ document = _e.sent();
542
+ _e.label = 2;
543
+ case 2:
709
544
  sub = that._subscriptions.filter(function (a) { return a.collections.includes(collection); })[i];
710
- if (!sub.preSubscriptionData) return [3 /*break*/, 2];
711
- if (!that._publications[sub.publication].fetchFunction(document, sub.preSubscriptionData)) return [3 /*break*/, 2];
545
+ if (!sub.preSubscriptionData) return [3 /*break*/, 4];
546
+ if (!that._publications[sub.publication].fetchFunction(document, sub.preSubscriptionData)) return [3 /*break*/, 4];
547
+ if (this_1._subCache.some(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })) {
548
+ this_1._subCache.splice(this_1._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
549
+ }
712
550
  that.sendPubData(sub, type, collection);
713
551
  _c = sub;
714
552
  return [4 /*yield*/, (_a = that._publications[sub.publication]).preFunction.apply(_a, sub.subscriptionData)];
715
- case 1:
553
+ case 3:
716
554
  _c.preSubscriptionData = _e.sent();
717
555
  return [2 /*return*/, "continue"];
718
- case 2:
719
- if (!that._publications[sub.publication].preFunction) return [3 /*break*/, 4];
556
+ case 4:
557
+ if (!that._publications[sub.publication].preFunction) return [3 /*break*/, 6];
720
558
  _d = sub;
721
559
  return [4 /*yield*/, that._publications[sub.publication].preFunction(sub.subscriptionData)];
722
- case 3:
560
+ case 5:
723
561
  _d.preSubscriptionData = _e.sent();
724
562
  if (that._publications[sub.publication].fetchFunction(document, sub.preSubscriptionData)) {
563
+ if (this_1._subCache.some(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })) {
564
+ this_1._subCache.splice(this_1._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
565
+ }
725
566
  that.sendPubData(sub, type, collection);
726
567
  }
727
- return [3 /*break*/, 5];
728
- case 4:
568
+ return [3 /*break*/, 7];
569
+ case 6:
729
570
  if (that._publications[sub.publication].ws_specific) {
730
571
  sub.clients.forEach(function (client) {
731
572
  var _a;
@@ -750,24 +591,31 @@ var SubscriptionManager = /** @class */ (function () {
750
591
  else {
751
592
  if (that._publications[sub.publication].fetchFunction) {
752
593
  if ((_b = that._publications[sub.publication]).fetchFunction.apply(_b, [document].concat(sub.subscriptionData))) {
594
+ if (this_1._subCache.some(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })) {
595
+ this_1._subCache.splice(this_1._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
596
+ }
753
597
  that.sendPubData(sub, type, collection);
754
598
  }
755
599
  // else
756
600
  }
757
601
  else {
602
+ if (this_1._subCache.some(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); })) {
603
+ this_1._subCache.splice(this_1._subCache.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
604
+ }
758
605
  that.sendPubData(sub, type, collection);
759
606
  }
760
607
  }
761
- _e.label = 5;
762
- case 5: return [2 /*return*/];
608
+ _e.label = 7;
609
+ case 7: return [2 /*return*/];
763
610
  }
764
611
  });
765
612
  };
613
+ this_1 = this;
766
614
  i = 0;
767
615
  _a.label = 1;
768
616
  case 1:
769
617
  if (!(i < that._subscriptions.filter(function (a) { return a.collections.includes(collection); }).length)) return [3 /*break*/, 4];
770
- return [5 /*yield**/, _loop_6(i)];
618
+ return [5 /*yield**/, _loop_3(i)];
771
619
  case 2:
772
620
  _a.sent();
773
621
  _a.label = 3;
@@ -859,7 +707,7 @@ var SubscriptionManager = /** @class */ (function () {
859
707
  // Fetch pub once, send to all clients linked to this pub
860
708
  SubscriptionManager.prototype.sendPubDataOnce = function (ws, messageId, subscription, type, collection) {
861
709
  return __awaiter(this, void 0, void 0, function () {
862
- var _a, _b, that;
710
+ var _a, _b, that, serverRes;
863
711
  var _this = this;
864
712
  return __generator(this, function (_c) {
865
713
  that = this;
@@ -867,23 +715,32 @@ var SubscriptionManager = /** @class */ (function () {
867
715
  that._mainServer.getMethodManager().callMethodInternal('insertSubscriptionLog', type, subscription.publication, collection, JSON.stringify(subscription.subscriptionData));
868
716
  }
869
717
  if (!that._publications[subscription.publication].ws_specific) { // Same pub for all users
870
- (_a = that._publications[subscription.publication].function).call.apply(_a, [Object.assign({}, that, SubscriptionManager.prototype)].concat(subscription.subscriptionData)).then(function (res) {
871
- var serverRes = {
718
+ if (this._subCache.some(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); })) {
719
+ serverRes = {
872
720
  messageId: messageId,
873
721
  hasError: false,
874
- data: res
722
+ data: this._subCache.filter(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); })[0].res
875
723
  };
876
- // console.log('Send Pub Data Once 1', new Date(), 'Success', subscription);
877
- _this.sendWS(ws, serverRes);
878
- }, function (err) {
879
- var serverRes = {
880
- messageId: messageId,
881
- hasError: true,
882
- data: err
883
- };
884
- // console.log('Send Pub Data Once 1', new Date(), 'Error', subscription);
885
- _this.sendWS(ws, serverRes);
886
- });
724
+ this.sendWS(ws, serverRes);
725
+ }
726
+ else {
727
+ (_a = that._publications[subscription.publication].function).call.apply(_a, [Object.assign({}, that, SubscriptionManager.prototype)].concat(subscription.subscriptionData)).then(function (res) {
728
+ _this._subCache.push({ publication: subscription.publication, subscriptionData: subscription.subscriptionData, res: res });
729
+ var serverRes = {
730
+ messageId: messageId,
731
+ hasError: false,
732
+ data: res
733
+ };
734
+ _this.sendWS(ws, serverRes);
735
+ }, function (err) {
736
+ var serverRes = {
737
+ messageId: messageId,
738
+ hasError: true,
739
+ data: err
740
+ };
741
+ _this.sendWS(ws, serverRes);
742
+ });
743
+ }
887
744
  }
888
745
  else { // Pub is different for each user (Look at users publication) [MUCH SLOWER, RE-RUNS FOR EACH USER]
889
746
  (_b = that._publications[subscription.publication].function).call.apply(_b, [Object.assign({}, that, SubscriptionManager.prototype), ws['id_user']].concat(subscription.subscriptionData)).then(function (res) {
@@ -918,33 +775,50 @@ var SubscriptionManager = /** @class */ (function () {
918
775
  if (subscription.publication !== 'superadminAPM' && subscription.publication !== 'loggedInUsers') {
919
776
  that._mainServer.getMethodManager().callMethodInternal('insertSubscriptionLog', type, subscription.publication, collection, JSON.stringify(subscription.subscriptionData));
920
777
  }
921
- (_a = that._publications[subscription.publication].function).call.apply(_a, [Object.assign({}, that, SubscriptionManager.prototype)].concat(subscription.subscriptionData)).then(function (res) {
778
+ if (this._subCache.some(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); })) {
922
779
  subscription.clients.forEach(function (client) {
923
780
  that._wss.clients.forEach(function (ws) {
924
781
  if (ws['id_socket'] === client.id_socket) {
925
782
  var serverRes = {
926
783
  messageId: client.messageId,
927
784
  hasError: false,
928
- data: res
785
+ data: _this._subCache.filter(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); })[0].res
929
786
  };
930
787
  _this.sendWS(ws, serverRes);
931
788
  }
932
789
  });
933
790
  });
934
- }, function (err) {
935
- subscription.clients.forEach(function (client) {
936
- that._wss.clients.forEach(function (ws) {
937
- if (ws['id_socket'] === client.id_socket) {
938
- var serverRes = {
939
- messageId: client.messageId,
940
- hasError: true,
941
- data: err
942
- };
943
- _this.sendWS(ws, serverRes);
944
- }
791
+ }
792
+ else {
793
+ (_a = that._publications[subscription.publication].function).call.apply(_a, [Object.assign({}, that, SubscriptionManager.prototype)].concat(subscription.subscriptionData)).then(function (res) {
794
+ _this._subCache.push({ publication: subscription.publication, subscriptionData: subscription.subscriptionData, res: res });
795
+ subscription.clients.forEach(function (client) {
796
+ that._wss.clients.forEach(function (ws) {
797
+ if (ws['id_socket'] === client.id_socket) {
798
+ var serverRes = {
799
+ messageId: client.messageId,
800
+ hasError: false,
801
+ data: res
802
+ };
803
+ _this.sendWS(ws, serverRes);
804
+ }
805
+ });
806
+ });
807
+ }, function (err) {
808
+ subscription.clients.forEach(function (client) {
809
+ that._wss.clients.forEach(function (ws) {
810
+ if (ws['id_socket'] === client.id_socket) {
811
+ var serverRes = {
812
+ messageId: client.messageId,
813
+ hasError: true,
814
+ data: err
815
+ };
816
+ _this.sendWS(ws, serverRes);
817
+ }
818
+ });
945
819
  });
946
820
  });
947
- });
821
+ }
948
822
  return [2 /*return*/];
949
823
  });
950
824
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@resolveio/server-lib",
3
- "version": "4.6.19",
3
+ "version": "5.0.0-rc-1",
4
4
  "description": "",
5
5
  "main": "index.ts",
6
6
  "scripts": {