@resolveio/server-lib 6.1.7 → 6.2.1-newrole

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.
@@ -43,7 +43,6 @@ var __spreadArrays = (this && this.__spreadArrays) || function () {
43
43
  return r;
44
44
  };
45
45
  Object.defineProperty(exports, "__esModule", { value: true });
46
- var MongoOplog = require("mongo-oplog");
47
46
  var logs_1 = require("../publications/logs");
48
47
  var app_status_1 = require("../publications/app-status");
49
48
  var app_version_1 = require("../publications/app-version");
@@ -75,8 +74,6 @@ var SubscriptionManager = /** @class */ (function () {
75
74
  this._cacheId = 0;
76
75
  this._sendQueue = [];
77
76
  this._runningQueue = false;
78
- this._initOplogStart = false;
79
- this._initOplogSupportStart = false;
80
77
  this._mainServer = mainServer;
81
78
  this._nodeCache = new NodeCache({ stdTTL: 0, checkperiod: 0 });
82
79
  this.serverConfig = serverConfig;
@@ -431,168 +428,102 @@ var SubscriptionManager = /** @class */ (function () {
431
428
  // Watch (tail) Mongo's operation log on the entire database (all insert/modify/delete will trigger this function)
432
429
  SubscriptionManager.prototype.tailOpLog = function () {
433
430
  var _this = this;
434
- var that = this;
435
- var oplog = MongoOplog(that.serverConfig['OPLOG_URL']);
436
- oplog.tail().then(function () {
437
- console.log('tail started');
438
- _this._initOplogStart = true;
439
- }, function (err) {
440
- console.log(err);
441
- });
442
- oplog.on('insert', function (doc) {
443
- var nsArray = doc.ns.split('.');
444
- var database = nsArray[0];
445
- if (_this.serverConfig['DATABASE'] === database) {
446
- var collection = nsArray[1];
447
- if (nsArray[2]) {
448
- collection += '.' + nsArray[2];
449
- }
450
- if (collection === 'cron-jobs') {
451
- _this._mainServer.getCronManager().checkCronJobsForUpdates();
452
- }
453
- else if (collection === 'queue-methods') {
454
- _this._mainServer.getQueueManager().runMethodQueue();
455
- }
456
- else if (collection === 'queue-responses') {
457
- _this._mainServer.getQueueManager().runMethodResponse();
458
- }
459
- else if (collection === 'support-tickets' && _this.serverConfig['ROOT_URL'] !== 'http://localhost:4200') {
460
- _this._mainServer.getMethodManager().callMethodInternal('sendSupportTicketEmail', doc.o._id);
461
- }
462
- if (!collection.endsWith('.versions') && !collection.startsWith('system.')) {
463
- _this._oplogQueue.push({
464
- type: 'insert',
465
- collection: collection,
466
- doc: doc.o
467
- });
468
- }
469
- }
470
- });
471
- oplog.on('update', function (doc) {
472
- var nsArray = doc.ns.split('.');
473
- var database = nsArray[0];
474
- if (_this.serverConfig['DATABASE'] === database) {
475
- var collection = nsArray[1];
476
- if (nsArray[2]) {
477
- collection += '.' + nsArray[2];
478
- }
479
- doc.o['_id'] = doc.o2['_id'];
480
- if (collection === 'cron-jobs') {
481
- _this._mainServer.getCronManager().checkCronJobsForUpdates();
482
- }
483
- if (!collection.endsWith('.versions') && !collection.startsWith('system.')) {
484
- _this._oplogQueue.push({
485
- type: 'update',
486
- collection: collection,
487
- doc: doc.o
488
- });
431
+ this._oplog = index_1.ResolveIOServer.getMainDB().db.watch({ fullDocument: 'updateLookup' });
432
+ this._oplog.on('change', function (doc) {
433
+ var collection = doc.ns.coll;
434
+ if (!collection.endsWith('.versions')) {
435
+ if (doc.operationType === 'insert') {
436
+ if (collection === 'cron-jobs') {
437
+ _this._mainServer.getCronManager().checkCronJobsForUpdates();
438
+ }
439
+ else if (collection === 'queue-methods') {
440
+ _this._mainServer.getQueueManager().runMethodQueue();
441
+ }
442
+ else if (collection === 'queue-responses') {
443
+ _this._mainServer.getQueueManager().runMethodResponse();
444
+ }
445
+ else if (collection === 'support-tickets' && _this.serverConfig['ROOT_URL'] !== 'http://localhost:4200') {
446
+ _this._mainServer.getMethodManager().callMethodInternal('sendSupportTicketEmail', doc.o._id);
447
+ }
448
+ else {
449
+ // console.log(doc);
450
+ _this._oplogQueue.push({
451
+ type: 'insert',
452
+ collection: collection,
453
+ doc: doc.fullDocument
454
+ });
455
+ }
489
456
  }
490
- }
491
- });
492
- oplog.on('delete', function (doc) {
493
- var nsArray = doc.ns.split('.');
494
- var database = nsArray[0];
495
- if (_this.serverConfig['DATABASE'] === database) {
496
- var collection = nsArray[1];
497
- if (nsArray[2]) {
498
- collection += '.' + nsArray[2];
457
+ else if (doc.operationType === 'update' || doc.operationType === 'replace') {
458
+ if (collection === 'cron-jobs') {
459
+ _this._mainServer.getCronManager().checkCronJobsForUpdates();
460
+ }
461
+ else {
462
+ // console.log(doc);
463
+ _this._oplogQueue.push({
464
+ type: 'update',
465
+ collection: collection,
466
+ doc: doc.fullDocument
467
+ });
468
+ }
499
469
  }
500
- that.resendPubsForDelete(collection);
501
- if (collection === 'cron-jobs') {
502
- _this._mainServer.getCronManager().checkCronJobsForUpdates();
470
+ else if (doc.operationType === 'delete') {
471
+ // console.log(doc);
472
+ _this.resendPubsForDelete(collection);
473
+ if (collection === 'cron-jobs') {
474
+ _this._mainServer.getCronManager().checkCronJobsForUpdates();
475
+ }
503
476
  }
504
477
  }
505
478
  });
506
- oplog.on('error', function (error) {
479
+ this._oplog.on('error', function (error) {
507
480
  console.log('oplog error', error);
508
- if (_this._initOplogStart) {
509
- process.exit();
510
- }
481
+ process.exit();
511
482
  });
512
- oplog.on('end', function () {
483
+ this._oplog.on('end', function () {
513
484
  console.log('oplog ended');
514
- if (_this._initOplogStart) {
515
- process.exit();
516
- }
485
+ process.exit();
517
486
  });
518
487
  };
519
488
  SubscriptionManager.prototype.tailOpLogSupport = function () {
520
489
  var _this = this;
521
- var that = this;
522
- var oplog = MongoOplog(that.serverConfig['RESOLVEIO_SUPPORT_OPLOG_URL']);
523
- oplog.tail().then(function () {
524
- console.log('tail started support');
525
- _this._initOplogSupportStart = true;
526
- }, function (err) {
527
- console.log(err);
528
- });
529
- oplog.on('insert', function (doc) {
530
- var nsArray = doc.ns.split('.');
531
- var database = nsArray[0];
532
- if (_this.serverConfig['DATABASE'] === database) {
533
- var collection = nsArray[1];
534
- if (nsArray[2]) {
535
- collection += '.' + nsArray[2];
536
- }
537
- if (collection === 'support-tickets') {
538
- that.checkSupportPubsForChanges(collection, doc.o);
539
- }
540
- }
541
- });
542
- oplog.on('update', function (doc) {
543
- var nsArray = doc.ns.split('.');
544
- var database = nsArray[0];
545
- if (_this.serverConfig['DATABASE'] === database) {
546
- var collection = nsArray[1];
547
- if (nsArray[2]) {
548
- collection += '.' + nsArray[2];
549
- }
550
- doc.o['_id'] = doc.o2['_id'];
551
- if (collection === 'support-tickets') {
552
- that.checkSupportPubsForChanges(collection, doc.o);
553
- }
554
- }
555
- });
556
- oplog.on('delete', function (doc) {
557
- var nsArray = doc.ns.split('.');
558
- var database = nsArray[0];
559
- if (_this.serverConfig['DATABASE'] === database) {
560
- var collection = nsArray[1];
561
- if (nsArray[2]) {
562
- collection += '.' + nsArray[2];
490
+ this._oplogSupport = index_1.ResolveIOServer.getSupportDB().db.watch({ fullDocument: 'updateLookup' });
491
+ this._oplog.on('change', function (doc) {
492
+ var collection = doc.ns.coll;
493
+ if (!collection.endsWith('.versions')) {
494
+ if (doc.operationType === 'insert' || doc.operationType === 'update' || doc.operationType === 'replace') {
495
+ if (collection === 'support-tickets') {
496
+ _this.checkSupportPubsForChanges(collection, doc.fullDocument);
497
+ }
563
498
  }
564
- if (collection === 'support-tickets') {
565
- that.resendPubsForDelete(collection);
499
+ else if (doc.operationType === 'delete') {
500
+ if (collection === 'support-tickets') {
501
+ _this.resendPubsForDelete(collection);
502
+ }
566
503
  }
567
504
  }
568
505
  });
569
- oplog.on('error', function (error) {
570
- console.log('oplog error support', error);
571
- if (_this._initOplogSupportStart) {
572
- process.exit();
573
- }
506
+ this._oplogSupport.on('error', function (error) {
507
+ console.log('oplog error', error);
508
+ process.exit();
574
509
  });
575
- oplog.on('end', function () {
576
- console.log('oplog ended support');
577
- if (_this._initOplogSupportStart) {
578
- process.exit();
579
- }
510
+ this._oplogSupport.on('end', function () {
511
+ console.log('oplog ended');
512
+ process.exit();
580
513
  });
581
514
  };
582
515
  SubscriptionManager.prototype.runOpLogQueue = function () {
583
516
  var _this = this;
584
517
  var that = this;
585
518
  setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
586
- var uniqueOplog_1, uniquePubs, needIds_1, _loop_4, i, _loop_5, i, _loop_6, this_3, i, i, pub;
587
- var _this = this;
519
+ var uniqueOplog_1, uniquePubs, _loop_4, i, _loop_5, this_3, i, i, pub;
588
520
  return __generator(this, function (_a) {
589
521
  switch (_a.label) {
590
522
  case 0:
591
- if (!this._oplogQueue.length) return [3 /*break*/, 8];
523
+ if (!this._oplogQueue.length) return [3 /*break*/, 4];
592
524
  uniqueOplog_1 = common_1.deepCopy(this._oplogQueue);
593
525
  this._oplogQueue = [];
594
526
  uniquePubs = [];
595
- needIds_1 = {};
596
527
  _loop_4 = function (i) {
597
528
  if (!that._subscriptions.some(function (a) { return a.collections.includes(uniqueOplog_1[i].collection); })) {
598
529
  uniqueOplog_1.splice(i, 1);
@@ -601,68 +532,9 @@ var SubscriptionManager = /** @class */ (function () {
601
532
  for (i = uniqueOplog_1.length - 1; i >= 0; i--) {
602
533
  _loop_4(i);
603
534
  }
604
- uniqueOplog_1.forEach(function (oplog) {
605
- var schemaErrors = null;
606
- try {
607
- index_1.ResolveIOServer.getMainDB().model(oplog.collection)['simplschema'].validate(oplog.doc);
608
- }
609
- catch (errors) {
610
- schemaErrors = errors;
611
- }
612
- if (schemaErrors) {
613
- if (!needIds_1[oplog.collection]) {
614
- needIds_1[oplog.collection] = [oplog.doc['_id']];
615
- }
616
- else {
617
- if (!needIds_1[oplog.collection].filter(function (a) { return a === oplog.doc['_id']; })[0]) {
618
- needIds_1[oplog.collection].push(oplog.doc['_id']);
619
- }
620
- }
621
- }
622
- });
623
535
  _loop_5 = function (i) {
624
- var collection, docs;
625
- return __generator(this, function (_a) {
626
- switch (_a.label) {
627
- case 0:
628
- collection = Object.keys(needIds_1)[i];
629
- return [4 /*yield*/, index_1.ResolveIOServer.getMainDB().model(collection).find({ _id: { $in: needIds_1[collection] } })];
630
- case 1:
631
- docs = _a.sent();
632
- needIds_1[collection].forEach(function (id_doc) {
633
- if (uniqueOplog_1.filter(function (a) { return a.doc && a.doc._id === id_doc; })[0]) {
634
- if (docs.filter(function (a) { return a._id === id_doc; })[0]) {
635
- uniqueOplog_1.filter(function (a) { return a.doc && a.doc._id === id_doc; }).forEach(function (op) {
636
- op.doc = docs.filter(function (a) { return a._id === id_doc; })[0];
637
- });
638
- }
639
- else {
640
- uniqueOplog_1.splice(uniqueOplog_1.findIndex(function (a) { return a.doc && a.doc._id === id_doc; })[0], 1);
641
- }
642
- }
643
- else {
644
- _this._mainServer.getMethodManager().sendEmail('dev@resolveio.com', 'SERVER - Oplog Doc Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'id_doc: ' + id_doc + ', UniqueOplogs: ' + JSON.stringify(uniqueOplog_1, null, 2));
645
- }
646
- });
647
- return [2 /*return*/];
648
- }
649
- });
650
- };
651
- i = 0;
652
- _a.label = 1;
653
- case 1:
654
- if (!(i < Object.keys(needIds_1).length)) return [3 /*break*/, 4];
655
- return [5 /*yield**/, _loop_5(i)];
656
- case 2:
657
- _a.sent();
658
- _a.label = 3;
659
- case 3:
660
- i++;
661
- return [3 /*break*/, 1];
662
- case 4:
663
- _loop_6 = function (i) {
664
536
  var oplog = uniqueOplog_1[i];
665
- var _loop_7 = function (j) {
537
+ var _loop_6 = function (j) {
666
538
  var _a;
667
539
  var sub = that._subscriptions[j];
668
540
  if (sub) {
@@ -738,40 +610,40 @@ var SubscriptionManager = /** @class */ (function () {
738
610
  }
739
611
  };
740
612
  for (var j = that._subscriptions.length - 1; j >= 0; j--) {
741
- _loop_7(j);
613
+ _loop_6(j);
742
614
  }
743
615
  };
744
616
  this_3 = this;
745
617
  for (i = uniqueOplog_1.length - 1; i >= 0; i--) {
746
- _loop_6(i);
618
+ _loop_5(i);
747
619
  }
748
620
  i = 0;
749
- _a.label = 5;
750
- case 5:
751
- if (!(i < uniquePubs.length)) return [3 /*break*/, 8];
621
+ _a.label = 1;
622
+ case 1:
623
+ if (!(i < uniquePubs.length)) return [3 /*break*/, 4];
752
624
  pub = uniquePubs[i];
753
625
  return [4 /*yield*/, this.checkPubsForChanges(pub['oplog'].collection, pub['oplog'].doc, pub['oplog'].type, pub)];
754
- case 6:
626
+ case 2:
755
627
  _a.sent();
756
- _a.label = 7;
757
- case 7:
628
+ _a.label = 3;
629
+ case 3:
758
630
  i++;
759
- return [3 /*break*/, 5];
760
- case 8: return [2 /*return*/];
631
+ return [3 /*break*/, 1];
632
+ case 4: return [2 /*return*/];
761
633
  }
762
634
  });
763
635
  }); }, 1000);
764
636
  };
765
637
  SubscriptionManager.prototype.resendPubsForDelete = function (collection) {
766
638
  return __awaiter(this, void 0, void 0, function () {
767
- var that, subCopy, _loop_8, this_4, jj, _loop_9, zz;
639
+ var that, subCopy, _loop_7, this_4, jj, _loop_8, zz;
768
640
  var _this = this;
769
641
  return __generator(this, function (_a) {
770
642
  switch (_a.label) {
771
643
  case 0:
772
644
  that = this;
773
645
  subCopy = common_1.deepCopy(that._subscriptions.filter(function (a) { return a.collections.includes(collection); }));
774
- _loop_8 = function (jj) {
646
+ _loop_7 = function (jj) {
775
647
  if (this_4._subCache[jj].collection === collection || subCopy.some(function (a) { return a.publication === _this._subCache[jj].publication; })) {
776
648
  this_4._nodeCache.del(this_4._subCache[jj].id);
777
649
  this_4._subCache.splice(jj, 1);
@@ -779,9 +651,9 @@ var SubscriptionManager = /** @class */ (function () {
779
651
  };
780
652
  this_4 = this;
781
653
  for (jj = this._subCache.length - 1; jj >= 0; jj--) {
782
- _loop_8(jj);
654
+ _loop_7(jj);
783
655
  }
784
- _loop_9 = function (zz) {
656
+ _loop_8 = function (zz) {
785
657
  var sub, aa, client, bb, ws;
786
658
  return __generator(this, function (_a) {
787
659
  switch (_a.label) {
@@ -828,7 +700,7 @@ var SubscriptionManager = /** @class */ (function () {
828
700
  _a.label = 1;
829
701
  case 1:
830
702
  if (!(zz >= 0)) return [3 /*break*/, 4];
831
- return [5 /*yield**/, _loop_9(zz)];
703
+ return [5 /*yield**/, _loop_8(zz)];
832
704
  case 2:
833
705
  _a.sent();
834
706
  _a.label = 3;
@@ -952,43 +824,30 @@ var SubscriptionManager = /** @class */ (function () {
952
824
  var _this = this;
953
825
  var that = this;
954
826
  that._subscriptions.filter(function (a) { return a.collections.includes(collection); }).forEach(function (sub) { return __awaiter(_this, void 0, void 0, function () {
955
- var schemaErrors, _a, _b;
827
+ var _a, _b;
956
828
  var _c, _d, _e;
957
829
  return __generator(this, function (_f) {
958
830
  switch (_f.label) {
959
831
  case 0:
960
- schemaErrors = null;
961
- try {
962
- index_1.ResolveIOServer.getSupportDB().model(collection)['simplschema'].validate(document);
963
- }
964
- catch (errors) {
965
- schemaErrors = errors;
966
- }
967
- if (!schemaErrors) return [3 /*break*/, 2];
968
- return [4 /*yield*/, index_1.ResolveIOServer.getSupportDB().model(collection).findById(document['_id'])];
969
- case 1:
970
- document = _f.sent();
971
- _f.label = 2;
972
- case 2:
973
- if (!sub.preSubscriptionData) return [3 /*break*/, 4];
974
- if (!that._publications[sub.publication].fetchFunction(document, sub.preSubscriptionData)) return [3 /*break*/, 4];
832
+ if (!sub.preSubscriptionData) return [3 /*break*/, 2];
833
+ if (!that._publications[sub.publication].fetchFunction(document, sub.preSubscriptionData)) return [3 /*break*/, 2];
975
834
  that.sendPubData(sub, 'support', collection);
976
835
  _a = sub;
977
836
  return [4 /*yield*/, (_c = that._publications[sub.publication]).preFunction.apply(_c, sub.subscriptionData)];
978
- case 3:
837
+ case 1:
979
838
  _a.preSubscriptionData = _f.sent();
980
839
  return [2 /*return*/];
981
- case 4:
982
- if (!that._publications[sub.publication].preFunction) return [3 /*break*/, 6];
840
+ case 2:
841
+ if (!that._publications[sub.publication].preFunction) return [3 /*break*/, 4];
983
842
  _b = sub;
984
843
  return [4 /*yield*/, (_d = that._publications[sub.publication]).preFunction.apply(_d, sub.subscriptionData)];
985
- case 5:
844
+ case 3:
986
845
  _b.preSubscriptionData = _f.sent();
987
846
  if (that._publications[sub.publication].fetchFunction(document, sub.preSubscriptionData)) {
988
847
  that.sendPubData(sub, 'support', collection);
989
848
  }
990
- return [3 /*break*/, 7];
991
- case 6:
849
+ return [3 /*break*/, 5];
850
+ case 4:
992
851
  if (that._publications[sub.publication].ws_specific) {
993
852
  sub.clients.forEach(function (client) {
994
853
  var _a;
@@ -1020,8 +879,8 @@ var SubscriptionManager = /** @class */ (function () {
1020
879
  that.sendPubData(sub, 'support', collection);
1021
880
  }
1022
881
  }
1023
- _f.label = 7;
1024
- case 7: return [2 /*return*/];
882
+ _f.label = 5;
883
+ case 5: return [2 /*return*/];
1025
884
  }
1026
885
  });
1027
886
  }); });