@resolveio/server-lib 1.0.0-rc2 → 1.0.0-rc20
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/collections/active-subscription.collection.d.ts +8 -0
- package/collections/active-subscription.collection.js +68 -1
- package/collections/app-status.collection.d.ts +8 -0
- package/collections/app-status.collection.js +43 -1
- package/collections/app-version.collection.d.ts +8 -0
- package/collections/app-version.collection.js +56 -1
- package/collections/counter.collection.d.ts +10 -0
- package/collections/counter.collection.js +73 -1
- package/collections/cron-job-history.collection.d.ts +8 -0
- package/collections/cron-job-history.collection.js +86 -1
- package/collections/cron-job.collection.d.ts +8 -0
- package/collections/cron-job.collection.js +89 -1
- package/collections/file.collection.d.ts +10 -0
- package/collections/file.collection.js +73 -1
- package/collections/flag.collection.d.ts +8 -0
- package/collections/flag.collection.js +47 -1
- package/collections/log.collection.d.ts +8 -0
- package/collections/log.collection.js +64 -1
- package/collections/logged-in-users.collection.d.ts +8 -0
- package/collections/logged-in-users.collection.js +70 -1
- package/collections/method-call.collection.d.ts +7 -0
- package/collections/method-call.collection.js +44 -1
- package/collections/method-response.collection.d.ts +7 -0
- package/collections/method-response.collection.js +48 -1
- package/collections/queue-flag.collection.d.ts +8 -0
- package/collections/queue-flag.collection.js +41 -1
- package/collections/queue-method.collection.d.ts +8 -0
- package/collections/queue-method.collection.js +116 -1
- package/collections/queue-response.collection.d.ts +8 -0
- package/collections/queue-response.collection.js +78 -1
- package/collections/support-ticket.collection.d.ts +8 -0
- package/collections/support-ticket.collection.js +146 -1
- package/collections/user.collection.d.ts +9 -0
- package/collections/user.collection.js +108 -1
- package/cron/cron.d.ts +6 -0
- package/cron/cron.js +160 -1
- package/fixtures/cron-jobs.d.ts +2 -0
- package/fixtures/cron-jobs.js +72 -1
- package/fixtures/init.d.ts +2 -0
- package/fixtures/init.js +73 -1
- package/http/auth.d.ts +3 -0
- package/http/auth.js +472 -1
- package/http/health.d.ts +2 -0
- package/http/health.js +8 -1
- package/index.d.ts +15 -0
- package/index.js +123 -1
- package/managers/cron.manager.d.ts +17 -0
- package/managers/cron.manager.js +193 -1
- package/managers/method.manager.d.ts +27 -0
- package/managers/method.manager.js +513 -1
- package/managers/queue-method.manager.d.ts +14 -0
- package/managers/queue-method.manager.js +239 -1
- package/managers/subscription.manager.d.ts +27 -0
- package/managers/subscription.manager.js +700 -1
- package/managers/support.manager.d.ts +18 -0
- package/managers/support.manager.js +208 -1
- package/methods/accounts.d.ts +3 -0
- package/methods/accounts.js +253 -1
- package/methods/aws.d.ts +3 -0
- package/methods/aws.js +323 -1
- package/methods/cloudconvert.d.ts +3 -0
- package/methods/cloudconvert.js +37 -1
- package/methods/collections.d.ts +3 -0
- package/methods/collections.js +738 -1
- package/methods/counters.d.ts +3 -0
- package/methods/counters.js +404 -1
- package/methods/cron-jobs.d.ts +3 -0
- package/methods/cron-jobs.js +63 -1
- package/methods/flags.d.ts +3 -0
- package/methods/flags.js +6 -1
- package/methods/logs.d.ts +3 -0
- package/methods/logs.js +29 -1
- package/methods/pdf.d.ts +3 -0
- package/methods/pdf.js +303 -1
- package/models/active-subscription.model.d.ts +14 -0
- package/models/active-subscription.model.js +2 -1
- package/models/app-status.model.d.ts +5 -0
- package/models/app-status.model.js +2 -1
- package/models/app-version.model.d.ts +5 -0
- package/models/app-version.model.js +2 -1
- package/models/collection-document.model.d.ts +7 -0
- package/models/collection-document.model.js +2 -1
- package/models/counter.model.d.ts +6 -0
- package/models/counter.model.js +2 -1
- package/models/cron-job-history.model.d.ts +13 -0
- package/models/cron-job-history.model.js +2 -1
- package/models/cron-job.model.d.ts +14 -0
- package/models/cron-job.model.js +2 -1
- package/models/dialog.model.d.ts +24 -0
- package/models/dialog.model.js +2 -1
- package/models/file.model.d.ts +10 -0
- package/models/file.model.js +2 -1
- package/models/flag.model.d.ts +6 -0
- package/models/flag.model.js +2 -1
- package/models/log.model.d.ts +12 -0
- package/models/log.model.js +2 -1
- package/models/logged-in-users.model.d.ts +10 -0
- package/models/logged-in-users.model.js +2 -1
- package/models/method-call.model.d.ts +8 -0
- package/models/method-call.model.js +2 -1
- package/models/method-response.model.d.ts +9 -0
- package/models/method-response.model.js +2 -1
- package/models/method.model.d.ts +11 -0
- package/models/method.model.js +2 -1
- package/models/pagination.model.d.ts +12 -0
- package/models/pagination.model.js +20 -1
- package/models/permission.model.d.ts +13 -0
- package/models/permission.model.js +2 -1
- package/models/queue-method.model.d.ts +10 -0
- package/models/queue-method.model.js +2 -1
- package/models/queue-response.model.d.ts +11 -0
- package/models/queue-response.model.js +2 -1
- package/models/select-data-label.model.d.ts +10 -0
- package/models/select-data-label.model.js +2 -1
- package/models/server-response.model.d.ts +6 -0
- package/models/server-response.model.js +2 -1
- package/models/subscription.model.d.ts +31 -0
- package/models/subscription.model.js +2 -1
- package/models/support-method.model.d.ts +10 -0
- package/models/support-method.model.js +2 -1
- package/models/support-ticket.model.d.ts +40 -0
- package/models/support-ticket.model.js +2 -1
- package/models/user.model.d.ts +15 -0
- package/models/user.model.js +2 -1
- package/package.json +5 -2
- package/publications/app-status.d.ts +3 -0
- package/publications/app-status.js +14 -1
- package/publications/app-version.d.ts +3 -0
- package/publications/app-version.js +14 -1
- package/publications/cron-jobs.d.ts +3 -0
- package/publications/cron-jobs.js +14 -1
- package/publications/files.d.ts +3 -0
- package/publications/files.js +71 -1
- package/publications/flags.d.ts +3 -0
- package/publications/flags.js +23 -1
- package/publications/logs.d.ts +3 -0
- package/publications/logs.js +26 -1
- package/publications/method-calls.d.ts +3 -0
- package/publications/method-calls.js +14 -1
- package/publications/method-responses.d.ts +3 -0
- package/publications/method-responses.js +14 -1
- package/publications/super-admin.d.ts +3 -0
- package/publications/super-admin.js +21 -1
- package/publications/support-tickets.d.ts +2 -0
- package/publications/support-tickets.js +118 -1
- package/server-app.d.ts +33 -0
- package/server-app.js +640 -1
- package/support-methods/aws.d.ts +3 -0
- package/support-methods/aws.js +276 -1
- package/support-methods/collections.d.ts +3 -0
- package/support-methods/collections.js +471 -1
- package/support-methods/counters.d.ts +3 -0
- package/support-methods/counters.js +189 -1
- package/support-methods/support.d.ts +3 -0
- package/support-methods/support.js +19 -1
- package/util/common.d.ts +15 -0
- package/util/common.js +334 -1
|
@@ -1 +1,700 @@
|
|
|
1
|
-
"use strict";var __awaiter=this&&this.__awaiter||function(s,c,r,a){return new(r||(r=Promise))(function(t,i){function e(t){try{o(a.next(t))}catch(t){i(t)}}function n(t){try{o(a.throw(t))}catch(t){i(t)}}function o(i){i.done?t(i.value):new r(function(t){t(i.value)}).then(e,n)}o((a=a.apply(s,c||[])).next())})},__generator=this&&this.__generator||function(e,n){var o,s,c,t,r={label:0,sent:function(){if(1&c[0])throw c[1];return c[1]},trys:[],ops:[]};return t={next:i(0),throw:i(1),return:i(2)},"function"==typeof Symbol&&(t[Symbol.iterator]=function(){return this}),t;function i(i){return function(t){return function(i){if(o)throw new TypeError("Generator is already executing.");for(;r;)try{if(o=1,s&&(c=2&i[0]?s.return:i[0]?s.throw||((c=s.return)&&c.call(s),0):s.next)&&!(c=c.call(s,i[1])).done)return c;switch(s=0,c&&(i=[2&i[0],c.value]),i[0]){case 0:case 1:c=i;break;case 4:return r.label++,{value:i[1],done:!1};case 5:r.label++,s=i[1],i=[0];continue;case 7:i=r.ops.pop(),r.trys.pop();continue;default:if(!(c=0<(c=r.trys).length&&c[c.length-1])&&(6===i[0]||2===i[0])){r=0;continue}if(3===i[0]&&(!c||i[1]>c[0]&&i[1]<c[3])){r.label=i[1];break}if(6===i[0]&&r.label<c[1]){r.label=c[1],c=i;break}if(c&&r.label<c[2]){r.label=c[2],r.ops.push(i);break}c[2]&&r.ops.pop(),r.trys.pop();continue}i=n.call(e,r)}catch(t){i=[6,t],s=0}finally{o=c=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,t])}}};Object.defineProperty(exports,"__esModule",{value:!0});var MongoOplog=require("mongo-oplog"),logs_1=require("../publications/logs"),app_status_1=require("../publications/app-status"),app_version_1=require("../publications/app-version"),mongoose_1=require("mongoose"),files_1=require("../publications/files"),super_admin_1=require("../publications/super-admin"),active_subscription_collection_1=require("../collections/active-subscription.collection"),logged_in_users_collection_1=require("../collections/logged-in-users.collection"),cron_jobs_1=require("../publications/cron-jobs"),flags_1=require("../publications/flags"),method_calls_1=require("../publications/method-calls"),method_responses_1=require("../publications/method-responses"),support_tickets_1=require("../publications/support-tickets"),index_1=require("../index"),SubscriptionManager=function(){function t(t,i,e){var n=this;this._publications={},this._subscriptions=[],this._sendQueue=[],this._runningQueue=!1,this._mainServer=t,this.serverConfig=e,this._wss=i,super_admin_1.loadSuperAdminPublications(this),app_version_1.loadAppVersionPublications(this),app_status_1.loadAppStatusPublications(this),logs_1.loadLogPublications(this),files_1.loadFilePublications(this),cron_jobs_1.loadCronJobPublications(this),flags_1.loadFlagsPublications(this),method_calls_1.loadMethodCallsPublications(this),method_responses_1.loadMethodResponsePublications(this),support_tickets_1.loadSupportTicketPublications(this),this.tailOpLog(),index_1.default.getSupportDB()&&this.tailOpLogSupport(),setInterval(function(){if(!n._runningQueue&&n._sendQueue.length){n._runningQueue=!0;for(var t=function(t){var i=n._mainServer.getWS(n._sendQueue[t].id_ws);if(i&&i.readyState===i.OPEN){if(i.bufferedAmount<20480){var e=n._sendQueue.pop();i.send(JSON.stringify(e.data),function(t){t&&(console.log("Error on WS: ",t),n._mainServer.unsubscribeWS(i))})}}else n._sendQueue.splice(t,1)},i=n._sendQueue.length-1;0<=i;i--)t(i);n._runningQueue=!1}},50)}return t.prototype.publications=function(t){this._publications=Object.assign(this._publications,t)},t.prototype.loggedInLatency=function(e){return __awaiter(this,void 0,void 0,function(){var i;return __generator(this,function(t){switch(t.label){case 0:return[4,logged_in_users_collection_1.LoggedInUsers.findOne({id_ws:e.id_socket})];case 1:return(i=t.sent())?logged_in_users_collection_1.LoggedInUsers.updateOne({_id:i._id},{$set:{latency:e.latency}}).exec():this.unsubscribeAll(e),[2]}})})},t.prototype.subscribe=function(p,_,d){for(var b=[],t=3;t<arguments.length;t++)b[t-3]=arguments[t];return __awaiter(this,void 0,void 0,function(){var i,e,n,o,s,c,r,a,u,l;return __generator(this,function(t){switch(t.label){case 0:return(n=(e=this)._publications[d])?[3,1]:(console.error("No Publication: "+d),[2]);case 1:if(1<b.length||b[0]){if(!n.check)return console.error("No Check Function For Pub "+d),[2];if(!n.check._schema)return console.error("No Check Schema For Pub "+d),[2];for(o={},s=Object.keys(n.check._schema),c=s.filter(function(t){return!t.includes(".")}),r=0;r<b.length;r++)o[c[r]]=b[r];try{n.check.validate(o)}catch(t){if(t)return console.error("Error in Pub Check ("+d+")",t),[2]}}return(a=e._subscriptions.filter(function(t){return t.publication===d&&JSON.stringify(t.subscriptionData)===JSON.stringify(b)})[0])?a.clients.push({id_user:p.id_user,messageId:_,id_socket:p.id_socket}):e._subscriptions.push({publication:d,subscriptionData:b,collections:e.getPublicationCollections(d),clients:[{id_user:p.id_user,messageId:_,id_socket:p.id_socket}]}),a||(a=e._subscriptions.filter(function(t){return t.publication===d&&JSON.stringify(t.subscriptionData)===JSON.stringify(b)})[0]),e.sendPubDataOnce(p,_,a),!e._publications[a.publication].preFunction||a.preSubscriptionData?[3,3]:(u=a,[4,(i=e._publications[a.publication]).preFunction.apply(i,a.subscriptionData)]);case 2:u.preSubscriptionData=t.sent(),t.label=3;case 3:l=new active_subscription_collection_1.ActiveSubscriptions({date:new Date,publication:d,subData:JSON.stringify(b),collections:n.collections,clients:a.clients}),active_subscription_collection_1.ActiveSubscriptions.findOneAndUpdate({$and:[{publication:d},{subData:JSON.stringify(b)}]},{$setOnInsert:{_id:mongoose_1.Types.ObjectId().toHexString(),__v:0},$set:l},{upsert:!0}).exec(),t.label=4;case 4:return"appversion"===d&&logged_in_users_collection_1.LoggedInUsers.create({_id:mongoose_1.Types.ObjectId().toHexString(),__v:0,date:new Date,id_user:p.id_user,user:p.user,id_ws:p.id_socket}),[2]}})})},t.prototype.unsubscribe=function(s,c,r){for(var a=[],t=3;t<arguments.length;t++)a[t-3]=arguments[t];return __awaiter(this,void 0,void 0,function(){var i,e,n,o;return __generator(this,function(t){switch(t.label){case 0:return(i=this)._publications[r]?[3,1]:(console.log("No Publication: "+r),[2]);case 1:return(e=i._subscriptions.filter(function(t){return t.publication===r&&JSON.stringify(t.subscriptionData)===JSON.stringify(a)})[0])?0<=(n=e.clients.findIndex(function(t){return t.id_user===s.id_user&&t.messageId===c&&t.id_socket===s.id_socket}))?(e.clients.splice(n,1),e.clients.length?[3,2]:(i._subscriptions.splice(i._subscriptions.findIndex(function(t){return t.publication===r&&JSON.stringify(t.subscriptionData)===JSON.stringify(a)}),1),active_subscription_collection_1.ActiveSubscriptions.findOneAndRemove({$and:[{publication:r},{subData:JSON.stringify(a)}]}).exec(),[3,4])):[3,5]:[3,6];case 2:return[4,active_subscription_collection_1.ActiveSubscriptions.findOne({$and:[{publication:r},{subData:JSON.stringify(a)}]})];case 3:(o=t.sent())&&active_subscription_collection_1.ActiveSubscriptions.updateOne({_id:o._id},{$set:{clients:e.clients}}).exec(),t.label=4;case 4:return[3,5];case 5:return[3,6];case 6:return[2]}})})},t.prototype.unsubscribeAll=function(c){return __awaiter(this,void 0,void 0,function(){var n,i,o,e,s;return __generator(this,function(t){switch(t.label){case 0:return n=this,[4,logged_in_users_collection_1.LoggedInUsers.findOne({id_ws:c.id_socket})];case 1:for((i=t.sent())&&logged_in_users_collection_1.LoggedInUsers.deleteOne({_id:i._id}).exec(),o=n._subscriptions.filter(function(t){return t.clients.some(function(t){return t.id_user===c.id_user&&t.id_socket===c.id_socket})}),e=function(t){for(var i=o[t],e=i.clients.length-1;0<=e;e--)i.clients[e].id_socket===c.id_socket&&i.clients.splice(e,1);i.clients.length||n._subscriptions.splice(n._subscriptions.findIndex(function(t){return t.publication===i.publication&&JSON.stringify(t.subscriptionData)===JSON.stringify(i.subscriptionData)}),1)},s=o.length-1;0<=s;s--)e(s);return[4,active_subscription_collection_1.ActiveSubscriptions.find({"clients.id_socket":c.id_socket})];case 2:return t.sent().forEach(function(t){1===t.clients.length?active_subscription_collection_1.ActiveSubscriptions.deleteOne({_id:t._id}).exec():(t.clients.splice(t.clients.map(function(t){return t.id_socket}).indexOf(c.id_socket),1),active_subscription_collection_1.ActiveSubscriptions.updateOne({_id:t._id},{$set:{clients:t.clients}}).exec())}),[2]}})})},t.prototype.getPublicationCollections=function(t){return this._publications[t].collections},t.prototype.tailOpLog=function(){var n=this,o=this,t=MongoOplog(o.serverConfig.OPLOG_URL);t.tail().then(function(){console.log("tail started")}),t.on("insert",function(t){var i=t.ns.split("."),e=i[1];i[2]&&(e+="."+i[2]),o.checkPubsForChanges(e,t.o),"cron-jobs"===e?n._mainServer.getCronManager().checkCronJobsForUpdates():"queue-methods"===e?n._mainServer.getQueueManager().runMethodQueue():"queue-responses"===e&&n._mainServer.getQueueManager().runMethodResponse()}),t.on("update",function(t){var i=t.ns.split("."),e=i[1];i[2]&&(e+="."+i[2]),t.o._id=t.o2._id,o.checkPubsForChanges(e,t.o),"cron-jobs"===e&&n._mainServer.getCronManager().checkCronJobsForUpdates()}),t.on("delete",function(t){var i=t.ns.split("."),e=i[1];i[2]&&(e+="."+i[2]),o.resendPubsForDelete(e),"cron-jobs"===e&&n._mainServer.getCronManager().checkCronJobsForUpdates()}),t.on("error",function(t){console.log("Error",t)}),t.on("end",function(){console.log("oplog ended"),process.exit()}),t.stop(function(){console.log("oplog stopped")})},t.prototype.tailOpLogSupport=function(){var n=this,t=MongoOplog(n.serverConfig.RESOLVEIO_SUPPORT_OPLOG_URL);t.tail().then(function(){console.log("tail started support")}),t.on("insert",function(t){var i=t.ns.split("."),e=i[1];i[2]&&(e+="."+i[2]),"support-tickets"===e&&n.checkSupportPubsForChanges(e,t.o)}),t.on("update",function(t){var i=t.ns.split("."),e=i[1];i[2]&&(e+="."+i[2]),t.o._id=t.o2._id,"support-tickets"===e&&n.checkSupportPubsForChanges(e,t.o)}),t.on("delete",function(t){var i=t.ns.split("."),e=i[1];i[2]&&(e+="."+i[2]),"support-tickets"===e&&n.resendPubsForDelete(e)}),t.on("error",function(t){console.log("Error support",t)}),t.on("end",function(){console.log("oplog ended support"),process.exit()}),t.stop(function(){console.log("oplog stopped support")})},t.prototype.resendPubsForDelete=function(i){var n=this;n._subscriptions.filter(function(t){return t.collections.includes(i)}).forEach(function(e){n._publications[e.publication].ws_specific?e.clients.forEach(function(i){n._wss.clients.forEach(function(t){t.id_socket===i.id_socket&&n.sendPubDataOnce(t,i.messageId,e)})}):n.sendPubData(e)})},t.prototype.checkPubsForChanges=function(s,c){var t=this,r=this;r._subscriptions.filter(function(t){return t.collections.includes(s)}).forEach(function(o){return __awaiter(t,void 0,void 0,function(){var i,e,n;return __generator(this,function(t){switch(t.label){case 0:e=null;try{index_1.default.getMainDB().model(s).simplschema.validate(c)}catch(t){e=t}return e?[4,index_1.default.getMainDB().model(s).findById(c._id)]:[3,2];case 1:c=t.sent(),t.label=2;case 2:return r._publications[o.publication].preFunction?(n=o,[4,r._publications[o.publication].preFunction(o.subscriptionData)]):[3,4];case 3:return n.preSubscriptionData=t.sent(),r._publications[o.publication].fetchFunction(c,o.preSubscriptionData)&&r.sendPubData(o),[3,5];case 4:r._publications[o.publication].ws_specific?o.clients.forEach(function(i){var t;r._publications[o.publication].fetchFunction?(t=r._publications[o.publication]).fetchFunction.apply(t,[c,i.id_user].concat(o.subscriptionData))&&r._wss.clients.forEach(function(t){t.id_socket===i.id_socket&&r.sendPubDataOnce(t,i.messageId,o)}):r._wss.clients.forEach(function(t){t.id_socket===i.id_socket&&r.sendPubDataOnce(t,i.messageId,o)})}):r._publications[o.publication].fetchFunction?(i=r._publications[o.publication]).fetchFunction.apply(i,[c].concat(o.subscriptionData))&&r.sendPubData(o):r.sendPubData(o),t.label=5;case 5:return[2]}})})})},t.prototype.checkSupportPubsForChanges=function(a,u){var t=this,l=this;l._subscriptions.filter(function(t){return t.collections.includes(a)}).forEach(function(r){return __awaiter(t,void 0,void 0,function(){var i,e,n,o,s,c;return __generator(this,function(t){switch(t.label){case 0:o=null;try{index_1.default.getSupportDB().model(a).simplschema.validate(u)}catch(t){o=t}return o?[4,index_1.default.getSupportDB().model(a).findById(u._id)]:[3,2];case 1:u=t.sent(),t.label=2;case 2:return r.preSubscriptionData&&l._publications[r.publication].fetchFunction(u,r.preSubscriptionData)?(l.sendPubData(r),s=r,[4,(i=l._publications[r.publication]).preFunction.apply(i,r.subscriptionData)]):[3,4];case 3:return s.preSubscriptionData=t.sent(),[2];case 4:return l._publications[r.publication].preFunction?(c=r,[4,(e=l._publications[r.publication]).preFunction.apply(e,r.subscriptionData)]):[3,6];case 5:return c.preSubscriptionData=t.sent(),l._publications[r.publication].fetchFunction(u,r.preSubscriptionData)&&l.sendPubData(r),[3,7];case 6:l._publications[r.publication].ws_specific?r.clients.forEach(function(i){var t;l._publications[r.publication].fetchFunction?(t=l._publications[r.publication]).fetchFunction.apply(t,[u,i.id_user].concat(r.subscriptionData))&&l._wss.clients.forEach(function(t){t.id_socket===i.id_socket&&l.sendPubDataOnce(t,i.messageId,r)}):l._wss.clients.forEach(function(t){t.id_socket===i.id_socket&&l.sendPubDataOnce(t,i.messageId,r)})}):l._publications[r.publication].fetchFunction?(n=l._publications[r.publication]).fetchFunction.apply(n,[u].concat(r.subscriptionData))&&l.sendPubData(r):l.sendPubData(r),t.label=7;case 7:return[2]}})})})},t.prototype.sendPubDataOnce=function(s,c,r){return __awaiter(this,void 0,void 0,function(){var i,e,n,o=this;return __generator(this,function(t){return(n=this)._publications[r.publication].ws_specific?(e=n._publications[r.publication].function).call.apply(e,[n,s.id_user].concat(r.subscriptionData)).then(function(t){var i={messageId:c,hasError:!1,data:t};o.sendWS(s,i)},function(t){var i={messageId:c,hasError:!0,data:t};o.sendWS(s,i)}):(i=n._publications[r.publication].function).call.apply(i,[n].concat(r.subscriptionData)).then(function(t){var i={messageId:c,hasError:!1,data:t};o.sendWS(s,i)},function(t){var i={messageId:c,hasError:!0,data:t};o.sendWS(s,i)}),[2]})})},t.prototype.sendPubData=function(e){return __awaiter(this,void 0,void 0,function(){var i,o,s=this;return __generator(this,function(t){return(i=(o=this)._publications[e.publication].function).call.apply(i,[o].concat(e.subscriptionData)).then(function(n){e.clients.forEach(function(e){o._wss.clients.forEach(function(t){if(t.id_socket===e.id_socket){var i={messageId:e.messageId,hasError:!1,data:n};s.sendWS(t,i)}})})},function(n){e.clients.forEach(function(e){o._wss.clients.forEach(function(t){if(t.id_socket===e.id_socket){var i={messageId:e.messageId,hasError:!0,data:n};s.sendWS(t,i)}})})}),[2]})})},t.prototype.sendWS=function(t,i){this._sendQueue.splice(0,0,{id_ws:t.id_socket,data:i})},t}();exports.SubscriptionManager=SubscriptionManager;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
12
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
+
function step(op) {
|
|
15
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
+
while (_) try {
|
|
17
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
+
switch (op[0]) {
|
|
20
|
+
case 0: case 1: t = op; break;
|
|
21
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
+
default:
|
|
25
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
+
if (t[2]) _.ops.pop();
|
|
30
|
+
_.trys.pop(); continue;
|
|
31
|
+
}
|
|
32
|
+
op = body.call(thisArg, _);
|
|
33
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
var MongoOplog = require("mongo-oplog");
|
|
39
|
+
var logs_1 = require("../publications/logs");
|
|
40
|
+
var app_status_1 = require("../publications/app-status");
|
|
41
|
+
var app_version_1 = require("../publications/app-version");
|
|
42
|
+
var mongoose_1 = require("mongoose");
|
|
43
|
+
var files_1 = require("../publications/files");
|
|
44
|
+
var super_admin_1 = require("../publications/super-admin");
|
|
45
|
+
var active_subscription_collection_1 = require("../collections/active-subscription.collection");
|
|
46
|
+
var logged_in_users_collection_1 = require("../collections/logged-in-users.collection");
|
|
47
|
+
var cron_jobs_1 = require("../publications/cron-jobs");
|
|
48
|
+
var flags_1 = require("../publications/flags");
|
|
49
|
+
var method_calls_1 = require("../publications/method-calls");
|
|
50
|
+
var method_responses_1 = require("../publications/method-responses");
|
|
51
|
+
var support_tickets_1 = require("../publications/support-tickets");
|
|
52
|
+
var index_1 = require("../index");
|
|
53
|
+
var SubscriptionManager = /** @class */ (function () {
|
|
54
|
+
function SubscriptionManager(mainServer, wss, serverConfig) {
|
|
55
|
+
var _this = this;
|
|
56
|
+
this._publications = {};
|
|
57
|
+
this._subscriptions = [];
|
|
58
|
+
this._sendQueue = [];
|
|
59
|
+
this._runningQueue = false;
|
|
60
|
+
this._mainServer = mainServer;
|
|
61
|
+
this.serverConfig = serverConfig;
|
|
62
|
+
this._wss = wss;
|
|
63
|
+
// Publications
|
|
64
|
+
super_admin_1.loadSuperAdminPublications(this);
|
|
65
|
+
app_version_1.loadAppVersionPublications(this);
|
|
66
|
+
app_status_1.loadAppStatusPublications(this);
|
|
67
|
+
logs_1.loadLogPublications(this);
|
|
68
|
+
files_1.loadFilePublications(this);
|
|
69
|
+
cron_jobs_1.loadCronJobPublications(this);
|
|
70
|
+
flags_1.loadFlagsPublications(this);
|
|
71
|
+
method_calls_1.loadMethodCallsPublications(this);
|
|
72
|
+
method_responses_1.loadMethodResponsePublications(this);
|
|
73
|
+
support_tickets_1.loadSupportTicketPublications(this);
|
|
74
|
+
this.tailOpLog();
|
|
75
|
+
if (index_1.ResolveIOServer.getSupportDB()) {
|
|
76
|
+
this.tailOpLogSupport();
|
|
77
|
+
}
|
|
78
|
+
setInterval(function () {
|
|
79
|
+
if (!_this._runningQueue) {
|
|
80
|
+
if (_this._sendQueue.length) {
|
|
81
|
+
_this._runningQueue = true;
|
|
82
|
+
var _loop_1 = function (i) {
|
|
83
|
+
var ws = _this._mainServer.getWS(_this._sendQueue[i].id_ws);
|
|
84
|
+
if (ws && ws.readyState === ws.OPEN) {
|
|
85
|
+
if (ws.bufferedAmount < 20480) {
|
|
86
|
+
var sendItem = _this._sendQueue.pop();
|
|
87
|
+
ws.send(JSON.stringify(sendItem.data), function (error) {
|
|
88
|
+
if (error) {
|
|
89
|
+
console.log('Error on WS: ', error);
|
|
90
|
+
_this._mainServer.unsubscribeWS(ws);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
_this._sendQueue.splice(i, 1);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
for (var i = _this._sendQueue.length - 1; i >= 0; i--) {
|
|
100
|
+
_loop_1(i);
|
|
101
|
+
}
|
|
102
|
+
_this._runningQueue = false;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}, 50);
|
|
106
|
+
}
|
|
107
|
+
// Add all files to publications private object
|
|
108
|
+
SubscriptionManager.prototype.publications = function (method) {
|
|
109
|
+
this._publications = Object.assign(this._publications, method);
|
|
110
|
+
};
|
|
111
|
+
SubscriptionManager.prototype.loggedInLatency = function (ws) {
|
|
112
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
113
|
+
var loggedInUser;
|
|
114
|
+
return __generator(this, function (_a) {
|
|
115
|
+
switch (_a.label) {
|
|
116
|
+
case 0: return [4 /*yield*/, logged_in_users_collection_1.LoggedInUsers.findOne({ id_ws: ws['id_socket'] })];
|
|
117
|
+
case 1:
|
|
118
|
+
loggedInUser = _a.sent();
|
|
119
|
+
if (loggedInUser) {
|
|
120
|
+
logged_in_users_collection_1.LoggedInUsers.updateOne({ _id: loggedInUser._id }, { $set: { latency: ws['latency'] } }).exec();
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
this.unsubscribeAll(ws);
|
|
124
|
+
}
|
|
125
|
+
return [2 /*return*/];
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
};
|
|
130
|
+
// Subscribe to publication
|
|
131
|
+
SubscriptionManager.prototype.subscribe = function (ws, messageId, publication) {
|
|
132
|
+
var subscriptionData = [];
|
|
133
|
+
for (var _i = 3; _i < arguments.length; _i++) {
|
|
134
|
+
subscriptionData[_i - 3] = arguments[_i];
|
|
135
|
+
}
|
|
136
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
137
|
+
var _a, that, pub, valObj, valKeys, rootKeys, i, sub, _b, newActiveSub;
|
|
138
|
+
return __generator(this, function (_c) {
|
|
139
|
+
switch (_c.label) {
|
|
140
|
+
case 0:
|
|
141
|
+
that = this;
|
|
142
|
+
pub = that._publications[publication];
|
|
143
|
+
if (!!pub) return [3 /*break*/, 1];
|
|
144
|
+
console.error('No Publication: ' + publication);
|
|
145
|
+
return [2 /*return*/];
|
|
146
|
+
case 1:
|
|
147
|
+
if (subscriptionData.length > 1 || subscriptionData[0]) {
|
|
148
|
+
if (!pub.check) {
|
|
149
|
+
console.error('No Check Function For Pub ' + publication);
|
|
150
|
+
return [2 /*return*/];
|
|
151
|
+
}
|
|
152
|
+
else if (!pub.check._schema) {
|
|
153
|
+
console.error('No Check Schema For Pub ' + publication);
|
|
154
|
+
return [2 /*return*/];
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
valObj = {};
|
|
158
|
+
valKeys = Object.keys(pub.check._schema);
|
|
159
|
+
rootKeys = valKeys.filter(function (a) { return !a.includes('.'); });
|
|
160
|
+
for (i = 0; i < subscriptionData.length; i++) {
|
|
161
|
+
valObj[rootKeys[i]] = subscriptionData[i];
|
|
162
|
+
}
|
|
163
|
+
try {
|
|
164
|
+
pub.check.validate(valObj);
|
|
165
|
+
}
|
|
166
|
+
catch (errors) {
|
|
167
|
+
if (errors) {
|
|
168
|
+
console.error('Error in Pub Check (' + publication + ')', errors);
|
|
169
|
+
return [2 /*return*/];
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
sub = that._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
|
|
175
|
+
// If sub found (another user watching same data), add client to same sub
|
|
176
|
+
if (sub) {
|
|
177
|
+
sub.clients.push({
|
|
178
|
+
id_user: ws['id_user'],
|
|
179
|
+
messageId: messageId,
|
|
180
|
+
id_socket: ws['id_socket']
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
// If sub not found, create new sub
|
|
184
|
+
else {
|
|
185
|
+
that._subscriptions.push({
|
|
186
|
+
publication: publication,
|
|
187
|
+
subscriptionData: subscriptionData,
|
|
188
|
+
collections: that.getPublicationCollections(publication),
|
|
189
|
+
clients: [{
|
|
190
|
+
id_user: ws['id_user'],
|
|
191
|
+
messageId: messageId,
|
|
192
|
+
id_socket: ws['id_socket']
|
|
193
|
+
}]
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
if (!sub) {
|
|
197
|
+
sub = that._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
|
|
198
|
+
}
|
|
199
|
+
// Send pub data to client who just subscribed only, don't want to send to existing clients since nothing has changed
|
|
200
|
+
that.sendPubDataOnce(ws, messageId, sub);
|
|
201
|
+
if (!(that._publications[sub.publication].preFunction && !sub.preSubscriptionData)) return [3 /*break*/, 3];
|
|
202
|
+
_b = sub;
|
|
203
|
+
return [4 /*yield*/, (_a = that._publications[sub.publication]).preFunction.apply(_a, sub.subscriptionData)];
|
|
204
|
+
case 2:
|
|
205
|
+
_b.preSubscriptionData = _c.sent();
|
|
206
|
+
_c.label = 3;
|
|
207
|
+
case 3:
|
|
208
|
+
newActiveSub = new active_subscription_collection_1.ActiveSubscriptions({
|
|
209
|
+
date: new Date(),
|
|
210
|
+
publication: publication,
|
|
211
|
+
subData: JSON.stringify(subscriptionData),
|
|
212
|
+
collections: pub.collections,
|
|
213
|
+
clients: sub.clients
|
|
214
|
+
});
|
|
215
|
+
active_subscription_collection_1.ActiveSubscriptions.findOneAndUpdate({
|
|
216
|
+
$and: [
|
|
217
|
+
{ publication: publication },
|
|
218
|
+
{ subData: JSON.stringify(subscriptionData) }
|
|
219
|
+
]
|
|
220
|
+
}, { $setOnInsert: { _id: mongoose_1.Types.ObjectId().toHexString(), __v: 0 }, $set: newActiveSub }, { upsert: true }).exec();
|
|
221
|
+
_c.label = 4;
|
|
222
|
+
case 4:
|
|
223
|
+
if (publication === 'appversion') {
|
|
224
|
+
logged_in_users_collection_1.LoggedInUsers.create({
|
|
225
|
+
_id: mongoose_1.Types.ObjectId().toHexString(),
|
|
226
|
+
__v: 0,
|
|
227
|
+
date: new Date(),
|
|
228
|
+
id_user: ws['id_user'],
|
|
229
|
+
user: ws['user'],
|
|
230
|
+
id_ws: ws['id_socket']
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
return [2 /*return*/];
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
};
|
|
238
|
+
// Unsubscribe from publication
|
|
239
|
+
SubscriptionManager.prototype.unsubscribe = function (ws, messageId, publication) {
|
|
240
|
+
var subscriptionData = [];
|
|
241
|
+
for (var _i = 3; _i < arguments.length; _i++) {
|
|
242
|
+
subscriptionData[_i - 3] = arguments[_i];
|
|
243
|
+
}
|
|
244
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
245
|
+
var that, sub, index, activeSub;
|
|
246
|
+
return __generator(this, function (_a) {
|
|
247
|
+
switch (_a.label) {
|
|
248
|
+
case 0:
|
|
249
|
+
that = this;
|
|
250
|
+
if (!!that._publications[publication]) return [3 /*break*/, 1];
|
|
251
|
+
console.log('No Publication: ' + publication);
|
|
252
|
+
return [2 /*return*/];
|
|
253
|
+
case 1:
|
|
254
|
+
sub = that._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
|
|
255
|
+
if (!sub) return [3 /*break*/, 6];
|
|
256
|
+
index = sub.clients.findIndex(function (a) { return a.id_user === ws['id_user'] && a.messageId === messageId && a.id_socket === ws['id_socket']; });
|
|
257
|
+
if (!(index >= 0)) return [3 /*break*/, 5];
|
|
258
|
+
sub.clients.splice(index, 1);
|
|
259
|
+
if (!!sub.clients.length) return [3 /*break*/, 2];
|
|
260
|
+
that._subscriptions.splice(that._subscriptions.findIndex(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); }), 1);
|
|
261
|
+
active_subscription_collection_1.ActiveSubscriptions.findOneAndRemove({
|
|
262
|
+
$and: [
|
|
263
|
+
{ publication: publication },
|
|
264
|
+
{ subData: JSON.stringify(subscriptionData) }
|
|
265
|
+
]
|
|
266
|
+
}).exec();
|
|
267
|
+
return [3 /*break*/, 4];
|
|
268
|
+
case 2: return [4 /*yield*/, active_subscription_collection_1.ActiveSubscriptions.findOne({
|
|
269
|
+
$and: [
|
|
270
|
+
{ publication: publication },
|
|
271
|
+
{ subData: JSON.stringify(subscriptionData) }
|
|
272
|
+
]
|
|
273
|
+
})];
|
|
274
|
+
case 3:
|
|
275
|
+
activeSub = _a.sent();
|
|
276
|
+
if (activeSub) {
|
|
277
|
+
active_subscription_collection_1.ActiveSubscriptions.updateOne({ _id: activeSub._id }, { $set: { clients: sub.clients } }).exec();
|
|
278
|
+
}
|
|
279
|
+
_a.label = 4;
|
|
280
|
+
case 4: return [3 /*break*/, 5];
|
|
281
|
+
case 5: return [3 /*break*/, 6];
|
|
282
|
+
case 6: return [2 /*return*/];
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
};
|
|
287
|
+
// Unsubscribe from publication
|
|
288
|
+
SubscriptionManager.prototype.unsubscribeAll = function (ws) {
|
|
289
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
290
|
+
var that, loggedInUser, userSubs, _loop_2, i, activeSubs;
|
|
291
|
+
return __generator(this, function (_a) {
|
|
292
|
+
switch (_a.label) {
|
|
293
|
+
case 0:
|
|
294
|
+
that = this;
|
|
295
|
+
return [4 /*yield*/, logged_in_users_collection_1.LoggedInUsers.findOne({ id_ws: ws['id_socket'] })];
|
|
296
|
+
case 1:
|
|
297
|
+
loggedInUser = _a.sent();
|
|
298
|
+
if (loggedInUser) {
|
|
299
|
+
logged_in_users_collection_1.LoggedInUsers.deleteOne({ _id: loggedInUser._id }).exec();
|
|
300
|
+
}
|
|
301
|
+
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']; }); });
|
|
302
|
+
_loop_2 = function (i) {
|
|
303
|
+
var sub = userSubs[i];
|
|
304
|
+
for (var j = sub.clients.length - 1; j >= 0; j--) {
|
|
305
|
+
if (sub.clients[j].id_socket === ws['id_socket']) {
|
|
306
|
+
sub.clients.splice(j, 1);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
if (!sub.clients.length) {
|
|
310
|
+
that._subscriptions.splice(that._subscriptions.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
for (i = userSubs.length - 1; i >= 0; i--) {
|
|
314
|
+
_loop_2(i);
|
|
315
|
+
}
|
|
316
|
+
return [4 /*yield*/, active_subscription_collection_1.ActiveSubscriptions.find({ 'clients.id_socket': ws['id_socket'] })];
|
|
317
|
+
case 2:
|
|
318
|
+
activeSubs = _a.sent();
|
|
319
|
+
activeSubs.forEach(function (sub) {
|
|
320
|
+
if (sub.clients.length === 1) {
|
|
321
|
+
active_subscription_collection_1.ActiveSubscriptions.deleteOne({ _id: sub._id }).exec();
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
sub.clients.splice(sub.clients.map(function (a) { return a.id_socket; }).indexOf(ws['id_socket']), 1);
|
|
325
|
+
active_subscription_collection_1.ActiveSubscriptions.updateOne({ _id: sub._id }, { $set: { clients: sub.clients } }).exec();
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
return [2 /*return*/];
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
};
|
|
333
|
+
// Get publication collection
|
|
334
|
+
SubscriptionManager.prototype.getPublicationCollections = function (publication) {
|
|
335
|
+
return this._publications[publication].collections;
|
|
336
|
+
};
|
|
337
|
+
// Watch (tail) Mongo's operation log on the entire database (all insert/modify/delete will trigger this function)
|
|
338
|
+
SubscriptionManager.prototype.tailOpLog = function () {
|
|
339
|
+
var _this = this;
|
|
340
|
+
var that = this;
|
|
341
|
+
var oplog = MongoOplog(that.serverConfig['OPLOG_URL']);
|
|
342
|
+
oplog.tail().then(function () {
|
|
343
|
+
console.log('tail started');
|
|
344
|
+
});
|
|
345
|
+
oplog.on('insert', function (doc) {
|
|
346
|
+
var nsArray = doc.ns.split('.');
|
|
347
|
+
var collection = nsArray[1];
|
|
348
|
+
if (nsArray[2]) {
|
|
349
|
+
collection += '.' + nsArray[2];
|
|
350
|
+
}
|
|
351
|
+
that.checkPubsForChanges(collection, doc.o);
|
|
352
|
+
if (collection === 'cron-jobs') {
|
|
353
|
+
_this._mainServer.getCronManager().checkCronJobsForUpdates();
|
|
354
|
+
}
|
|
355
|
+
else if (collection === 'queue-methods') {
|
|
356
|
+
_this._mainServer.getQueueManager().runMethodQueue();
|
|
357
|
+
}
|
|
358
|
+
else if (collection === 'queue-responses') {
|
|
359
|
+
_this._mainServer.getQueueManager().runMethodResponse();
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
oplog.on('update', function (doc) {
|
|
363
|
+
var nsArray = doc.ns.split('.');
|
|
364
|
+
var collection = nsArray[1];
|
|
365
|
+
if (nsArray[2]) {
|
|
366
|
+
collection += '.' + nsArray[2];
|
|
367
|
+
}
|
|
368
|
+
doc.o['_id'] = doc.o2['_id'];
|
|
369
|
+
that.checkPubsForChanges(collection, doc.o);
|
|
370
|
+
if (collection === 'cron-jobs') {
|
|
371
|
+
_this._mainServer.getCronManager().checkCronJobsForUpdates();
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
oplog.on('delete', function (doc) {
|
|
375
|
+
var nsArray = doc.ns.split('.');
|
|
376
|
+
var collection = nsArray[1];
|
|
377
|
+
if (nsArray[2]) {
|
|
378
|
+
collection += '.' + nsArray[2];
|
|
379
|
+
}
|
|
380
|
+
that.resendPubsForDelete(collection);
|
|
381
|
+
if (collection === 'cron-jobs') {
|
|
382
|
+
_this._mainServer.getCronManager().checkCronJobsForUpdates();
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
oplog.on('error', function (error) {
|
|
386
|
+
console.log('Error', error);
|
|
387
|
+
});
|
|
388
|
+
oplog.on('end', function () {
|
|
389
|
+
console.log('oplog ended');
|
|
390
|
+
process.exit();
|
|
391
|
+
});
|
|
392
|
+
oplog.stop(function () {
|
|
393
|
+
console.log('oplog stopped');
|
|
394
|
+
});
|
|
395
|
+
};
|
|
396
|
+
SubscriptionManager.prototype.tailOpLogSupport = function () {
|
|
397
|
+
var that = this;
|
|
398
|
+
var oplog = MongoOplog(that.serverConfig['RESOLVEIO_SUPPORT_OPLOG_URL']);
|
|
399
|
+
oplog.tail().then(function () {
|
|
400
|
+
console.log('tail started support');
|
|
401
|
+
});
|
|
402
|
+
oplog.on('insert', function (doc) {
|
|
403
|
+
var nsArray = doc.ns.split('.');
|
|
404
|
+
var collection = nsArray[1];
|
|
405
|
+
if (nsArray[2]) {
|
|
406
|
+
collection += '.' + nsArray[2];
|
|
407
|
+
}
|
|
408
|
+
if (collection === 'support-tickets') {
|
|
409
|
+
that.checkSupportPubsForChanges(collection, doc.o);
|
|
410
|
+
}
|
|
411
|
+
});
|
|
412
|
+
oplog.on('update', function (doc) {
|
|
413
|
+
var nsArray = doc.ns.split('.');
|
|
414
|
+
var collection = nsArray[1];
|
|
415
|
+
if (nsArray[2]) {
|
|
416
|
+
collection += '.' + nsArray[2];
|
|
417
|
+
}
|
|
418
|
+
doc.o['_id'] = doc.o2['_id'];
|
|
419
|
+
if (collection === 'support-tickets') {
|
|
420
|
+
that.checkSupportPubsForChanges(collection, doc.o);
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
oplog.on('delete', function (doc) {
|
|
424
|
+
var nsArray = doc.ns.split('.');
|
|
425
|
+
var collection = nsArray[1];
|
|
426
|
+
if (nsArray[2]) {
|
|
427
|
+
collection += '.' + nsArray[2];
|
|
428
|
+
}
|
|
429
|
+
if (collection === 'support-tickets') {
|
|
430
|
+
that.resendPubsForDelete(collection);
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
oplog.on('error', function (error) {
|
|
434
|
+
console.log('Error support', error);
|
|
435
|
+
});
|
|
436
|
+
oplog.on('end', function () {
|
|
437
|
+
console.log('oplog ended support');
|
|
438
|
+
process.exit();
|
|
439
|
+
});
|
|
440
|
+
oplog.stop(function () {
|
|
441
|
+
console.log('oplog stopped support');
|
|
442
|
+
});
|
|
443
|
+
};
|
|
444
|
+
SubscriptionManager.prototype.resendPubsForDelete = function (collection) {
|
|
445
|
+
var that = this;
|
|
446
|
+
that._subscriptions.filter(function (a) { return a.collections.includes(collection); }).forEach(function (sub) {
|
|
447
|
+
if (that._publications[sub.publication].ws_specific) {
|
|
448
|
+
sub.clients.forEach(function (client) {
|
|
449
|
+
that._wss.clients.forEach(function (ws) {
|
|
450
|
+
if (ws['id_socket'] === client.id_socket) {
|
|
451
|
+
that.sendPubDataOnce(ws, client.messageId, sub);
|
|
452
|
+
}
|
|
453
|
+
});
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
else {
|
|
457
|
+
that.sendPubData(sub);
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
};
|
|
461
|
+
// Document: Inserted/Modified/Deleted Document
|
|
462
|
+
// Collection: Collection Document Lives In
|
|
463
|
+
// Check to see if any pubs need to refetch due to Mongo operation
|
|
464
|
+
SubscriptionManager.prototype.checkPubsForChanges = function (collection, document) {
|
|
465
|
+
var _this = this;
|
|
466
|
+
var that = this;
|
|
467
|
+
that._subscriptions.filter(function (a) { return a.collections.includes(collection); }).forEach(function (sub) { return __awaiter(_this, void 0, void 0, function () {
|
|
468
|
+
var _a, schemaErrors, _b;
|
|
469
|
+
return __generator(this, function (_c) {
|
|
470
|
+
switch (_c.label) {
|
|
471
|
+
case 0:
|
|
472
|
+
schemaErrors = null;
|
|
473
|
+
try {
|
|
474
|
+
index_1.ResolveIOServer.getMainDB().model(collection)['simplschema'].validate(document);
|
|
475
|
+
}
|
|
476
|
+
catch (errors) {
|
|
477
|
+
schemaErrors = errors;
|
|
478
|
+
}
|
|
479
|
+
if (!schemaErrors) return [3 /*break*/, 2];
|
|
480
|
+
return [4 /*yield*/, index_1.ResolveIOServer.getMainDB().model(collection).findById(document['_id'])];
|
|
481
|
+
case 1:
|
|
482
|
+
document = _c.sent();
|
|
483
|
+
_c.label = 2;
|
|
484
|
+
case 2:
|
|
485
|
+
if (!that._publications[sub.publication].preFunction) return [3 /*break*/, 4];
|
|
486
|
+
_b = sub;
|
|
487
|
+
return [4 /*yield*/, that._publications[sub.publication].preFunction(sub.subscriptionData)];
|
|
488
|
+
case 3:
|
|
489
|
+
_b.preSubscriptionData = _c.sent();
|
|
490
|
+
if (that._publications[sub.publication].fetchFunction(document, sub.preSubscriptionData)) {
|
|
491
|
+
that.sendPubData(sub);
|
|
492
|
+
}
|
|
493
|
+
return [3 /*break*/, 5];
|
|
494
|
+
case 4:
|
|
495
|
+
if (that._publications[sub.publication].ws_specific) {
|
|
496
|
+
sub.clients.forEach(function (client) {
|
|
497
|
+
var _a;
|
|
498
|
+
if (that._publications[sub.publication].fetchFunction) {
|
|
499
|
+
if ((_a = that._publications[sub.publication]).fetchFunction.apply(_a, [document, client.id_user].concat(sub.subscriptionData))) {
|
|
500
|
+
that._wss.clients.forEach(function (ws) {
|
|
501
|
+
if (ws['id_socket'] === client.id_socket) {
|
|
502
|
+
that.sendPubDataOnce(ws, client.messageId, sub);
|
|
503
|
+
}
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
else {
|
|
508
|
+
that._wss.clients.forEach(function (ws) {
|
|
509
|
+
if (ws['id_socket'] === client.id_socket) {
|
|
510
|
+
that.sendPubDataOnce(ws, client.messageId, sub);
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
else {
|
|
517
|
+
if (that._publications[sub.publication].fetchFunction) {
|
|
518
|
+
if ((_a = that._publications[sub.publication]).fetchFunction.apply(_a, [document].concat(sub.subscriptionData))) {
|
|
519
|
+
that.sendPubData(sub);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
else {
|
|
523
|
+
that.sendPubData(sub);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
_c.label = 5;
|
|
527
|
+
case 5: return [2 /*return*/];
|
|
528
|
+
}
|
|
529
|
+
});
|
|
530
|
+
}); });
|
|
531
|
+
};
|
|
532
|
+
SubscriptionManager.prototype.checkSupportPubsForChanges = function (collection, document) {
|
|
533
|
+
var _this = this;
|
|
534
|
+
var that = this;
|
|
535
|
+
that._subscriptions.filter(function (a) { return a.collections.includes(collection); }).forEach(function (sub) { return __awaiter(_this, void 0, void 0, function () {
|
|
536
|
+
var _a, _b, _c, schemaErrors, _d, _e;
|
|
537
|
+
return __generator(this, function (_f) {
|
|
538
|
+
switch (_f.label) {
|
|
539
|
+
case 0:
|
|
540
|
+
schemaErrors = null;
|
|
541
|
+
try {
|
|
542
|
+
index_1.ResolveIOServer.getSupportDB().model(collection)['simplschema'].validate(document);
|
|
543
|
+
}
|
|
544
|
+
catch (errors) {
|
|
545
|
+
schemaErrors = errors;
|
|
546
|
+
}
|
|
547
|
+
if (!schemaErrors) return [3 /*break*/, 2];
|
|
548
|
+
return [4 /*yield*/, index_1.ResolveIOServer.getSupportDB().model(collection).findById(document['_id'])];
|
|
549
|
+
case 1:
|
|
550
|
+
document = _f.sent();
|
|
551
|
+
_f.label = 2;
|
|
552
|
+
case 2:
|
|
553
|
+
if (!sub.preSubscriptionData) return [3 /*break*/, 4];
|
|
554
|
+
if (!that._publications[sub.publication].fetchFunction(document, sub.preSubscriptionData)) return [3 /*break*/, 4];
|
|
555
|
+
that.sendPubData(sub);
|
|
556
|
+
_d = sub;
|
|
557
|
+
return [4 /*yield*/, (_a = that._publications[sub.publication]).preFunction.apply(_a, sub.subscriptionData)];
|
|
558
|
+
case 3:
|
|
559
|
+
_d.preSubscriptionData = _f.sent();
|
|
560
|
+
return [2 /*return*/];
|
|
561
|
+
case 4:
|
|
562
|
+
if (!that._publications[sub.publication].preFunction) return [3 /*break*/, 6];
|
|
563
|
+
_e = sub;
|
|
564
|
+
return [4 /*yield*/, (_b = that._publications[sub.publication]).preFunction.apply(_b, sub.subscriptionData)];
|
|
565
|
+
case 5:
|
|
566
|
+
_e.preSubscriptionData = _f.sent();
|
|
567
|
+
if (that._publications[sub.publication].fetchFunction(document, sub.preSubscriptionData)) {
|
|
568
|
+
that.sendPubData(sub);
|
|
569
|
+
}
|
|
570
|
+
return [3 /*break*/, 7];
|
|
571
|
+
case 6:
|
|
572
|
+
if (that._publications[sub.publication].ws_specific) {
|
|
573
|
+
sub.clients.forEach(function (client) {
|
|
574
|
+
var _a;
|
|
575
|
+
if (that._publications[sub.publication].fetchFunction) {
|
|
576
|
+
if ((_a = that._publications[sub.publication]).fetchFunction.apply(_a, [document, client.id_user].concat(sub.subscriptionData))) {
|
|
577
|
+
that._wss.clients.forEach(function (ws) {
|
|
578
|
+
if (ws['id_socket'] === client.id_socket) {
|
|
579
|
+
that.sendPubDataOnce(ws, client.messageId, sub);
|
|
580
|
+
}
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
else {
|
|
585
|
+
that._wss.clients.forEach(function (ws) {
|
|
586
|
+
if (ws['id_socket'] === client.id_socket) {
|
|
587
|
+
that.sendPubDataOnce(ws, client.messageId, sub);
|
|
588
|
+
}
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
else {
|
|
594
|
+
if (that._publications[sub.publication].fetchFunction) {
|
|
595
|
+
if ((_c = that._publications[sub.publication]).fetchFunction.apply(_c, [document].concat(sub.subscriptionData))) {
|
|
596
|
+
that.sendPubData(sub);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
else {
|
|
600
|
+
that.sendPubData(sub);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
_f.label = 7;
|
|
604
|
+
case 7: return [2 /*return*/];
|
|
605
|
+
}
|
|
606
|
+
});
|
|
607
|
+
}); });
|
|
608
|
+
};
|
|
609
|
+
// Fetch pub once, send to all clients linked to this pub
|
|
610
|
+
SubscriptionManager.prototype.sendPubDataOnce = function (ws, messageId, subscription) {
|
|
611
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
612
|
+
var _a, _b, that;
|
|
613
|
+
var _this = this;
|
|
614
|
+
return __generator(this, function (_c) {
|
|
615
|
+
that = this;
|
|
616
|
+
if (!that._publications[subscription.publication].ws_specific) { // Same pub for all users
|
|
617
|
+
(_a = that._publications[subscription.publication].function).call.apply(_a, [that].concat(subscription.subscriptionData)).then(function (res) {
|
|
618
|
+
var serverRes = {
|
|
619
|
+
messageId: messageId,
|
|
620
|
+
hasError: false,
|
|
621
|
+
data: res
|
|
622
|
+
};
|
|
623
|
+
_this.sendWS(ws, serverRes);
|
|
624
|
+
}, function (err) {
|
|
625
|
+
var serverRes = {
|
|
626
|
+
messageId: messageId,
|
|
627
|
+
hasError: true,
|
|
628
|
+
data: err
|
|
629
|
+
};
|
|
630
|
+
_this.sendWS(ws, serverRes);
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
else { // Pub is different for each user (Look at users publication) [MUCH SLOWER, RE-RUNS FOR EACH USER]
|
|
634
|
+
(_b = that._publications[subscription.publication].function).call.apply(_b, [that, ws['id_user']].concat(subscription.subscriptionData)).then(function (res) {
|
|
635
|
+
var serverRes = {
|
|
636
|
+
messageId: messageId,
|
|
637
|
+
hasError: false,
|
|
638
|
+
data: res
|
|
639
|
+
};
|
|
640
|
+
_this.sendWS(ws, serverRes);
|
|
641
|
+
}, function (err) {
|
|
642
|
+
var serverRes = {
|
|
643
|
+
messageId: messageId,
|
|
644
|
+
hasError: true,
|
|
645
|
+
data: err
|
|
646
|
+
};
|
|
647
|
+
_this.sendWS(ws, serverRes);
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
return [2 /*return*/];
|
|
651
|
+
});
|
|
652
|
+
});
|
|
653
|
+
};
|
|
654
|
+
// Fetch pub once, send to all clients linked to this pub
|
|
655
|
+
SubscriptionManager.prototype.sendPubData = function (subscription) {
|
|
656
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
657
|
+
var _a, that;
|
|
658
|
+
var _this = this;
|
|
659
|
+
return __generator(this, function (_b) {
|
|
660
|
+
that = this;
|
|
661
|
+
(_a = that._publications[subscription.publication].function).call.apply(_a, [that].concat(subscription.subscriptionData)).then(function (res) {
|
|
662
|
+
subscription.clients.forEach(function (client) {
|
|
663
|
+
that._wss.clients.forEach(function (ws) {
|
|
664
|
+
if (ws['id_socket'] === client.id_socket) {
|
|
665
|
+
var serverRes = {
|
|
666
|
+
messageId: client.messageId,
|
|
667
|
+
hasError: false,
|
|
668
|
+
data: res
|
|
669
|
+
};
|
|
670
|
+
_this.sendWS(ws, serverRes);
|
|
671
|
+
}
|
|
672
|
+
});
|
|
673
|
+
});
|
|
674
|
+
}, function (err) {
|
|
675
|
+
subscription.clients.forEach(function (client) {
|
|
676
|
+
that._wss.clients.forEach(function (ws) {
|
|
677
|
+
if (ws['id_socket'] === client.id_socket) {
|
|
678
|
+
var serverRes = {
|
|
679
|
+
messageId: client.messageId,
|
|
680
|
+
hasError: true,
|
|
681
|
+
data: err
|
|
682
|
+
};
|
|
683
|
+
_this.sendWS(ws, serverRes);
|
|
684
|
+
}
|
|
685
|
+
});
|
|
686
|
+
});
|
|
687
|
+
});
|
|
688
|
+
return [2 /*return*/];
|
|
689
|
+
});
|
|
690
|
+
});
|
|
691
|
+
};
|
|
692
|
+
SubscriptionManager.prototype.sendWS = function (ws, data) {
|
|
693
|
+
this._sendQueue.splice(0, 0, {
|
|
694
|
+
id_ws: ws['id_socket'],
|
|
695
|
+
data: data
|
|
696
|
+
});
|
|
697
|
+
};
|
|
698
|
+
return SubscriptionManager;
|
|
699
|
+
}());
|
|
700
|
+
exports.SubscriptionManager = SubscriptionManager;
|