@resolveio/server-lib 9.0.34 → 9.0.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (192) hide show
  1. package/client-server-app.js +48 -7
  2. package/client-server-app.js.map +1 -1
  3. package/collections/app-status.collection.js +11 -8
  4. package/collections/app-status.collection.js.map +1 -1
  5. package/collections/app-version.collection.js +11 -8
  6. package/collections/app-version.collection.js.map +1 -1
  7. package/collections/counter.collection.js +11 -8
  8. package/collections/counter.collection.js.map +1 -1
  9. package/collections/cron-job-history.collection.js +12 -9
  10. package/collections/cron-job-history.collection.js.map +1 -1
  11. package/collections/cron-job.collection.js +11 -8
  12. package/collections/cron-job.collection.js.map +1 -1
  13. package/collections/email-history.collection.js +11 -8
  14. package/collections/email-history.collection.js.map +1 -1
  15. package/collections/email-verified.collection.js +12 -9
  16. package/collections/email-verified.collection.js.map +1 -1
  17. package/collections/file.collection.js +11 -8
  18. package/collections/file.collection.js.map +1 -1
  19. package/collections/flag.collection.js +11 -8
  20. package/collections/flag.collection.js.map +1 -1
  21. package/collections/log-method-latency.collection.js +14 -11
  22. package/collections/log-method-latency.collection.js.map +1 -1
  23. package/collections/log-subscription.collection.js +16 -13
  24. package/collections/log-subscription.collection.js.map +1 -1
  25. package/collections/log.collection.js +19 -16
  26. package/collections/log.collection.js.map +1 -1
  27. package/collections/logged-in-users.collection.js +12 -9
  28. package/collections/logged-in-users.collection.js.map +1 -1
  29. package/collections/method-call.collection.js +13 -10
  30. package/collections/method-call.collection.js.map +1 -1
  31. package/collections/method-response.collection.js +13 -10
  32. package/collections/method-response.collection.js.map +1 -1
  33. package/collections/monitor-cpu.collection.js +16 -13
  34. package/collections/monitor-cpu.collection.js.map +1 -1
  35. package/collections/monitor-https-inbound.collection.js +16 -13
  36. package/collections/monitor-https-inbound.collection.js.map +1 -1
  37. package/collections/monitor-https-outbound.collection.js +18 -15
  38. package/collections/monitor-https-outbound.collection.js.map +1 -1
  39. package/collections/monitor-memory.collection.js +15 -12
  40. package/collections/monitor-memory.collection.js.map +1 -1
  41. package/collections/monitor-mongo.collection.js +15 -12
  42. package/collections/monitor-mongo.collection.js.map +1 -1
  43. package/collections/notification.collection.js +11 -8
  44. package/collections/notification.collection.js.map +1 -1
  45. package/collections/queue-flag.collection.js +12 -9
  46. package/collections/queue-flag.collection.js.map +1 -1
  47. package/collections/queue-method.collection.js +12 -9
  48. package/collections/queue-method.collection.js.map +1 -1
  49. package/collections/queue-response.collection.js +12 -9
  50. package/collections/queue-response.collection.js.map +1 -1
  51. package/collections/report-builder-dashboard-builder.collection.js +13 -10
  52. package/collections/report-builder-dashboard-builder.collection.js.map +1 -1
  53. package/collections/report-builder-library.collection.js +14 -11
  54. package/collections/report-builder-library.collection.js.map +1 -1
  55. package/collections/report-builder-report.collection.js +14 -11
  56. package/collections/report-builder-report.collection.js.map +1 -1
  57. package/collections/user-group.collection.js +14 -11
  58. package/collections/user-group.collection.js.map +1 -1
  59. package/collections/user-guide.collection.js +11 -8
  60. package/collections/user-guide.collection.js.map +1 -1
  61. package/collections/user.collection.js +15 -12
  62. package/collections/user.collection.js.map +1 -1
  63. package/cron/cron.js +25 -25
  64. package/cron/cron.js.map +1 -1
  65. package/fixtures/cron-jobs.js +80 -30
  66. package/fixtures/cron-jobs.js.map +1 -1
  67. package/fixtures/init.js +59 -9
  68. package/fixtures/init.js.map +1 -1
  69. package/http/auth.js +460 -344
  70. package/http/auth.js.map +1 -1
  71. package/http/health.js +5 -1
  72. package/http/health.js.map +1 -1
  73. package/http/home.js +11 -7
  74. package/http/home.js.map +1 -1
  75. package/index.js +98 -36
  76. package/index.js.map +1 -1
  77. package/managers/cron.manager.js +157 -78
  78. package/managers/cron.manager.js.map +1 -1
  79. package/managers/method.manager.js +302 -314
  80. package/managers/method.manager.js.map +1 -1
  81. package/managers/mongo.manager.js +1010 -741
  82. package/managers/mongo.manager.js.map +1 -1
  83. package/managers/monitor.manager.js +254 -171
  84. package/managers/monitor.manager.js.map +1 -1
  85. package/managers/queue-method.manager.js +166 -69
  86. package/managers/queue-method.manager.js.map +1 -1
  87. package/managers/subscription.manager.js +714 -515
  88. package/managers/subscription.manager.js.map +1 -1
  89. package/methods/accounts.js +183 -111
  90. package/methods/accounts.js.map +1 -1
  91. package/methods/aws.js +262 -156
  92. package/methods/aws.js.map +1 -1
  93. package/methods/cloudconvert.js +180 -100
  94. package/methods/cloudconvert.js.map +1 -1
  95. package/methods/collections.js +110 -103
  96. package/methods/collections.js.map +1 -1
  97. package/methods/counters.js +79 -29
  98. package/methods/counters.js.map +1 -1
  99. package/methods/cron-jobs.js +884 -819
  100. package/methods/cron-jobs.js.map +1 -1
  101. package/methods/flags.js +5 -1
  102. package/methods/flags.js.map +1 -1
  103. package/methods/logs.js +313 -258
  104. package/methods/logs.js.map +1 -1
  105. package/methods/monitor.js +348 -289
  106. package/methods/monitor.js.map +1 -1
  107. package/methods/pdf.js +340 -341
  108. package/methods/pdf.js.map +1 -1
  109. package/methods/report-builder.js +657 -565
  110. package/methods/report-builder.js.map +1 -1
  111. package/methods/support.js +185 -67
  112. package/methods/support.js.map +1 -1
  113. package/models/app-status.model.js +2 -1
  114. package/models/app-version.model.js +2 -1
  115. package/models/billing-logged-in-users.model.js +2 -1
  116. package/models/collection-document.model.js +2 -1
  117. package/models/counter.model.js +2 -1
  118. package/models/cron-job-history.model.js +2 -1
  119. package/models/cron-job.model.js +2 -1
  120. package/models/dialog.model.js +2 -1
  121. package/models/email-history.model.js +2 -1
  122. package/models/email-verified.model.js +2 -1
  123. package/models/file.model.js +2 -1
  124. package/models/flag.model.js +2 -1
  125. package/models/log-method-latency.model.js +2 -1
  126. package/models/log-subscription.model.js +2 -1
  127. package/models/log.model.js +2 -1
  128. package/models/logged-in-users.model.js +2 -1
  129. package/models/method-call.model.js +2 -1
  130. package/models/method-response.model.js +2 -1
  131. package/models/method.model.js +2 -1
  132. package/models/monitor-cpu.model.js +2 -1
  133. package/models/monitor-https-inbound.model.js +2 -1
  134. package/models/monitor-https-outbound.model.js +2 -1
  135. package/models/monitor-memory.model.js +2 -1
  136. package/models/monitor-mongo.model.js +2 -1
  137. package/models/notification.model.js +2 -1
  138. package/models/pagination.model.js +5 -2
  139. package/models/pagination.model.js.map +1 -1
  140. package/models/permission.model.js +2 -1
  141. package/models/queue-method.model.js +2 -1
  142. package/models/queue-response.model.js +2 -1
  143. package/models/report-builder-dashboard-builder.model.js +2 -1
  144. package/models/report-builder-library.model.js +2 -1
  145. package/models/report-builder-report.model.js +2 -1
  146. package/models/report-builder.model.js +2 -1
  147. package/models/select-data-label.model.js +2 -1
  148. package/models/server-response.model.js +2 -1
  149. package/models/subscription.model.js +2 -1
  150. package/models/support-ticket.model.js +2 -1
  151. package/models/user-group.model.js +2 -1
  152. package/models/user-guide.model.js +2 -1
  153. package/models/user.model.js +2 -1
  154. package/package.json +1 -1
  155. package/public_api.js +82 -70
  156. package/public_api.js.map +1 -1
  157. package/publications/app-status.js +8 -4
  158. package/publications/app-status.js.map +1 -1
  159. package/publications/app-version.js +8 -4
  160. package/publications/app-version.js.map +1 -1
  161. package/publications/cron-jobs.js +13 -9
  162. package/publications/cron-jobs.js.map +1 -1
  163. package/publications/files.js +15 -11
  164. package/publications/files.js.map +1 -1
  165. package/publications/flags.js +11 -7
  166. package/publications/flags.js.map +1 -1
  167. package/publications/logs.js +35 -31
  168. package/publications/logs.js.map +1 -1
  169. package/publications/method-calls.js +8 -4
  170. package/publications/method-calls.js.map +1 -1
  171. package/publications/method-responses.js +8 -4
  172. package/publications/method-responses.js.map +1 -1
  173. package/publications/notifications.js +8 -4
  174. package/publications/notifications.js.map +1 -1
  175. package/publications/report-builder-dashboard-builders.js +12 -8
  176. package/publications/report-builder-dashboard-builders.js.map +1 -1
  177. package/publications/report-builder-libraries.js +66 -16
  178. package/publications/report-builder-libraries.js.map +1 -1
  179. package/publications/report-builder-reports.js +21 -17
  180. package/publications/report-builder-reports.js.map +1 -1
  181. package/publications/super-admin.js +7 -3
  182. package/publications/super-admin.js.map +1 -1
  183. package/publications/user-groups.js +8 -4
  184. package/publications/user-groups.js.map +1 -1
  185. package/publications/user-guides.js +8 -4
  186. package/publications/user-guides.js.map +1 -1
  187. package/server-app.js +534 -393
  188. package/server-app.js.map +1 -1
  189. package/util/common.js +92 -55
  190. package/util/common.js.map +1 -1
  191. package/util/schema-report-builder.js +134 -76
  192. package/util/schema-report-builder.js.map +1 -1
@@ -1,26 +1,73 @@
1
- import { loadLogPublications } from '../publications/logs';
2
- import { loadAppStatusPublications } from '../publications/app-status';
3
- import { loadAppVersionPublications } from '../publications/app-version';
4
- import { loadFilePublications } from '../publications/files';
5
- import { loadSuperAdminPublications } from '../publications/super-admin';
6
- import { LoggedInUsers } from '../collections/logged-in-users.collection';
7
- import { loadCronJobPublications } from '../publications/cron-jobs';
8
- import { loadFlagsPublications } from '../publications/flags';
9
- import { loadMethodCallsPublications } from '../publications/method-calls';
10
- import { loadMethodResponsePublications } from '../publications/method-responses';
11
- import { ResolveIOServer } from '../index';
12
- import { loadNotificationPublications } from '../publications/notifications';
13
- import { deepCopy } from '../util/common';
14
- import * as moment from 'moment';
15
- import { loadReportBuilderReportPublications } from '../publications/report-builder-reports';
16
- import * as NodeCache from 'node-cache';
17
- import { loadReportBuilderLibraryPublications } from '../publications/report-builder-libraries';
18
- import { loadUserGroupPublications } from '../publications/user-groups';
19
- import { loadUserGuidePublications } from '../publications/user-guides';
20
- import { loadReportBuilderDashboardBuilderPublications } from '../publications/report-builder-dashboard-builders';
21
- import { objectIdHexString } from './mongo.manager';
22
- export class SubscriptionManager {
23
- constructor(mainServer, wss, serverConfig) {
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ 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;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __spreadArrays = (this && this.__spreadArrays) || function () {
39
+ for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
40
+ for (var r = Array(s), k = 0, i = 0; i < il; i++)
41
+ for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
42
+ r[k] = a[j];
43
+ return r;
44
+ };
45
+ Object.defineProperty(exports, "__esModule", { value: true });
46
+ exports.SubscriptionManager = void 0;
47
+ var logs_1 = require("../publications/logs");
48
+ var app_status_1 = require("../publications/app-status");
49
+ var app_version_1 = require("../publications/app-version");
50
+ var files_1 = require("../publications/files");
51
+ var super_admin_1 = require("../publications/super-admin");
52
+ var logged_in_users_collection_1 = require("../collections/logged-in-users.collection");
53
+ var cron_jobs_1 = require("../publications/cron-jobs");
54
+ var flags_1 = require("../publications/flags");
55
+ var method_calls_1 = require("../publications/method-calls");
56
+ var method_responses_1 = require("../publications/method-responses");
57
+ var index_1 = require("../index");
58
+ var notifications_1 = require("../publications/notifications");
59
+ var common_1 = require("../util/common");
60
+ var moment = require("moment");
61
+ var report_builder_reports_1 = require("../publications/report-builder-reports");
62
+ var NodeCache = require("node-cache");
63
+ var report_builder_libraries_1 = require("../publications/report-builder-libraries");
64
+ var user_groups_1 = require("../publications/user-groups");
65
+ var user_guides_1 = require("../publications/user-guides");
66
+ var report_builder_dashboard_builders_1 = require("../publications/report-builder-dashboard-builders");
67
+ var mongo_manager_1 = require("./mongo.manager");
68
+ var SubscriptionManager = /** @class */ (function () {
69
+ function SubscriptionManager(mainServer, wss, serverConfig) {
70
+ var _this = this;
24
71
  this._publications = {};
25
72
  this._subscriptions = [];
26
73
  this._loggedInUsers = [];
@@ -34,269 +81,348 @@ export class SubscriptionManager {
34
81
  this.serverConfig = serverConfig;
35
82
  this._wss = wss;
36
83
  // Publications
37
- loadSuperAdminPublications(this);
38
- loadAppVersionPublications(this);
39
- loadAppStatusPublications(this);
40
- loadLogPublications(this);
41
- loadFilePublications(this);
42
- loadCronJobPublications(this);
43
- loadFlagsPublications(this);
44
- loadMethodCallsPublications(this);
45
- loadMethodResponsePublications(this);
46
- loadNotificationPublications(this);
47
- loadReportBuilderReportPublications(this);
48
- loadReportBuilderLibraryPublications(this);
49
- loadUserGroupPublications(this);
50
- loadUserGuidePublications(this);
51
- loadReportBuilderDashboardBuilderPublications(this);
84
+ super_admin_1.loadSuperAdminPublications(this);
85
+ app_version_1.loadAppVersionPublications(this);
86
+ app_status_1.loadAppStatusPublications(this);
87
+ logs_1.loadLogPublications(this);
88
+ files_1.loadFilePublications(this);
89
+ cron_jobs_1.loadCronJobPublications(this);
90
+ flags_1.loadFlagsPublications(this);
91
+ method_calls_1.loadMethodCallsPublications(this);
92
+ method_responses_1.loadMethodResponsePublications(this);
93
+ notifications_1.loadNotificationPublications(this);
94
+ report_builder_reports_1.loadReportBuilderReportPublications(this);
95
+ report_builder_libraries_1.loadReportBuilderLibraryPublications(this);
96
+ user_groups_1.loadUserGroupPublications(this);
97
+ user_guides_1.loadUserGuidePublications(this);
98
+ report_builder_dashboard_builders_1.loadReportBuilderDashboardBuilderPublications(this);
52
99
  this.tailOpLog();
53
100
  this.runOpLogQueue();
54
- setInterval(() => {
101
+ setInterval(function () {
55
102
  console.log(new Date());
56
- console.log('Sub Cache Length: ' + this._subCache.length);
57
- console.log('Sub Length: ' + this._subscriptions.length);
58
- console.log(this._nodeCache.getStats().vsize);
103
+ console.log('Sub Cache Length: ' + _this._subCache.length);
104
+ console.log('Sub Length: ' + _this._subscriptions.length);
105
+ console.log(_this._nodeCache.getStats().vsize);
59
106
  }, 43200000);
60
- setInterval(() => {
61
- if (!this._runningQueue) {
62
- if (this._sendQueue.length) {
63
- this._runningQueue = true;
64
- for (let i = this._sendQueue.length - 1; i >= 0; i--) {
65
- let ws = this._mainServer.getWS(this._sendQueue[i].id_ws);
107
+ setInterval(function () {
108
+ if (!_this._runningQueue) {
109
+ if (_this._sendQueue.length) {
110
+ _this._runningQueue = true;
111
+ var _loop_1 = function (i) {
112
+ var ws = _this._mainServer.getWS(_this._sendQueue[i].id_ws);
66
113
  if (ws && ws.readyState === ws.OPEN) {
67
114
  if (ws.bufferedAmount < 20480) {
68
- let sendItem = this._sendQueue.pop();
69
- ws.send(JSON.stringify(sendItem.data), (error) => {
115
+ var sendItem = _this._sendQueue.pop();
116
+ ws.send(JSON.stringify(sendItem.data), function (error) {
70
117
  if (error) {
71
118
  console.log('Error on WS: ', error);
72
- this.unsubscribeAll(ws);
119
+ _this.unsubscribeAll(ws);
73
120
  }
74
121
  });
75
122
  }
76
123
  }
77
124
  else {
78
- this._sendQueue.splice(i, 1);
125
+ _this._sendQueue.splice(i, 1);
79
126
  }
127
+ };
128
+ for (var i = _this._sendQueue.length - 1; i >= 0; i--) {
129
+ _loop_1(i);
80
130
  }
81
- this._runningQueue = false;
131
+ _this._runningQueue = false;
82
132
  }
83
133
  }
84
134
  }, 10);
85
- setInterval(async () => {
86
- this._loggedInUsers = await LoggedInUsers.find();
87
- let userCopy = deepCopy(this._loggedInUsers);
88
- for (let i = this._loggedInUsers.length - 1; i >= 0; i--) {
89
- let loggedInUser = userCopy[i];
90
- if (!loggedInUser.date || moment().diff(moment(loggedInUser.date), 'seconds') >= 60) {
91
- if (this._mainServer.getWS(loggedInUser.id_ws)) {
92
- this.unsubscribeAll(this._mainServer.getWS(loggedInUser.id_ws));
93
- }
94
- else {
95
- this._subscriptions.forEach(sub => {
96
- for (let j = sub.clients.length - 1; j >= 0; j--) {
97
- let client = sub.clients[j];
98
- if (client.id_socket === loggedInUser.id_ws) {
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; })) {
99
193
  sub.clients.splice(j, 1);
100
194
  }
195
+ };
196
+ this_2 = this;
197
+ for (j = sub.clients.length - 1; j >= 0; j--) {
198
+ _loop_3(j);
101
199
  }
102
- });
103
- if (await LoggedInUsers.findById(loggedInUser._id)) {
104
- LoggedInUsers.deleteOne({ _id: loggedInUser._id });
105
200
  }
106
- this._loggedInUsers.splice(i, 1);
107
- }
201
+ return [2 /*return*/];
108
202
  }
109
- }
110
- for (let i = 0; i < this._subscriptions.length; i++) {
111
- let sub = this._subscriptions[i];
112
- for (let j = sub.clients.length - 1; j >= 0; j--) {
113
- let client = sub.clients[j];
114
- if (!this._loggedInUsers.some(a => a.id_ws === client.id_socket)) {
115
- sub.clients.splice(j, 1);
116
- }
117
- }
118
- }
119
- }, 60000);
203
+ });
204
+ }); }, 60000);
120
205
  }
121
206
  // Add all files to publications private object
122
- publications(method) {
207
+ SubscriptionManager.prototype.publications = function (method) {
123
208
  this._publications = Object.assign(this._publications, method);
124
- }
125
- async loggedInLatency(ws) {
126
- let date = new Date();
127
- let loggedInUser = this._loggedInUsers.filter(a => a.id_ws === ws['id_socket'])[0];
128
- if (loggedInUser) {
129
- loggedInUser.date = date;
130
- }
131
- LoggedInUsers.findOneAndUpdate({ id_ws: ws['id_socket'] }, { $set: { latency: ws['latency'], date: date } }).then(res => {
132
- if (!res) {
133
- this.unsubscribeAll(ws);
134
- }
135
- }, () => {
136
- this.unsubscribeAll(ws);
209
+ };
210
+ SubscriptionManager.prototype.loggedInLatency = function (ws) {
211
+ return __awaiter(this, void 0, void 0, function () {
212
+ var date, loggedInUser;
213
+ var _this = this;
214
+ return __generator(this, function (_a) {
215
+ date = new Date();
216
+ loggedInUser = this._loggedInUsers.filter(function (a) { return a.id_ws === ws['id_socket']; })[0];
217
+ if (loggedInUser) {
218
+ loggedInUser.date = date;
219
+ }
220
+ logged_in_users_collection_1.LoggedInUsers.findOneAndUpdate({ id_ws: ws['id_socket'] }, { $set: { latency: ws['latency'], date: date } }).then(function (res) {
221
+ if (!res) {
222
+ _this.unsubscribeAll(ws);
223
+ }
224
+ }, function () {
225
+ _this.unsubscribeAll(ws);
226
+ });
227
+ return [2 /*return*/];
228
+ });
137
229
  });
138
- }
230
+ };
139
231
  // Subscribe to publication
140
- async subscribe(messageDate, ws, messageId, publication, ...subscriptionData) {
141
- let that = this;
142
- let pub = that._publications[publication];
143
- if (!pub) {
144
- console.error('No Publication: ' + publication);
145
- return;
232
+ SubscriptionManager.prototype.subscribe = function (messageDate, ws, messageId, publication) {
233
+ var subscriptionData = [];
234
+ for (var _i = 4; _i < arguments.length; _i++) {
235
+ subscriptionData[_i - 4] = arguments[_i];
146
236
  }
147
- else {
148
- if (subscriptionData.length > 1 || subscriptionData[0]) {
149
- if (!pub.check) {
150
- console.error('No Check Function For Pub ' + publication);
151
- return;
152
- }
153
- else if (!pub.check._schema) {
154
- console.error('No Check Schema For Pub ' + publication);
155
- return;
156
- }
157
- else {
158
- let valObj = {};
159
- let valKeys = Object.keys(pub.check._schema);
160
- let rootKeys = valKeys.filter(a => !a.includes('.'));
161
- for (let i = 0; i < subscriptionData.length; i++) {
162
- valObj[rootKeys[i]] = subscriptionData[i];
163
- }
164
- try {
165
- pub.check.validate(valObj);
166
- }
167
- catch (errors) {
168
- if (errors) {
169
- console.error('Error in Pub Check (' + publication + ')', errors);
170
- return;
237
+ 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*/];
253
+ }
254
+ else if (!pub.check._schema) {
255
+ console.error('No Check Schema For Pub ' + publication);
256
+ return [2 /*return*/];
257
+ }
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
+ }
273
+ }
274
+ }
171
275
  }
172
- }
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', {
324
+ 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*/];
173
332
  }
174
- }
175
- let sub = that._subscriptions.filter(a => a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData))[0];
176
- // If sub found (another user watching same data), add client to same sub
177
- if (sub) {
178
- sub.clients.push({
179
- id_user: ws['id_user'],
180
- messageId: messageId,
181
- id_socket: ws['id_socket']
182
- });
183
- }
184
- // If sub not found, create new sub
185
- else {
186
- that._subscriptions.push({
187
- publication: publication,
188
- subscriptionData: subscriptionData,
189
- collections: that.getPublicationCollections(publication),
190
- clients: [{
191
- id_user: ws['id_user'],
192
- messageId: messageId,
193
- id_socket: ws['id_socket']
194
- }]
195
- });
196
- }
197
- if (!sub) {
198
- sub = that._subscriptions.filter(a => a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData))[0];
199
- }
200
- // console.log('Subscribe', new Date(), 'Success', sub);
201
- // Send pub data to client who just subscribed only, don't want to send to existing clients since nothing has changed
202
- that.sendPubDataOnce(ws, messageId, sub, 'newSub', that.getPublicationCollections(publication)[0]);
203
- if (that._publications[sub.publication].preFunction && !sub.preSubscriptionData) {
204
- sub.preSubscriptionData = await that._publications[sub.publication].preFunction(...sub.subscriptionData);
205
- }
206
- }
207
- if (publication === 'appversion') {
208
- let newLoggedInUser = {
209
- _id: objectIdHexString(),
210
- __v: 0,
211
- date: new Date(),
212
- id_user: ws['id_user'],
213
- user: ws['user'],
214
- id_ws: ws['id_socket']
215
- };
216
- this._loggedInUsers.push(newLoggedInUser);
217
- LoggedInUsers.create(newLoggedInUser);
218
- if (ws['user'] !== 'Admin' && this.serverConfig['CLIENT_NAME'] !== 'ResolveIO') {
219
- this._mainServer.getMethodManager().callMethodInternal('supportCreateBillingUser', {
220
- id_user: ws['id_user'],
221
- user: ws['user'],
222
- date: new Date(),
223
- client: ResolveIOServer.getClientName()
224
- });
225
- }
226
- }
227
- }
333
+ });
334
+ });
335
+ };
228
336
  // Unsubscribe from publication
229
- async unsubscribe(messageDate, ws, messageId, publication, ...subscriptionData) {
230
- let that = this;
231
- // console.log('Before');
232
- // console.dir(this._subscriptions.map(a => a.publication));
233
- if (!that._publications[publication]) {
234
- console.log('No Publication: ' + publication);
235
- return;
337
+ SubscriptionManager.prototype.unsubscribe = function (messageDate, ws, messageId, publication) {
338
+ var subscriptionData = [];
339
+ for (var _i = 4; _i < arguments.length; _i++) {
340
+ subscriptionData[_i - 4] = arguments[_i];
236
341
  }
237
- else {
238
- let sub = that._subscriptions.filter(a => a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData))[0];
239
- if (sub) {
240
- for (let i = sub.clients.length - 1; i >= 0; i--) {
241
- if (sub.clients[i].id_user === ws['id_user'] && sub.clients[i].messageId === messageId && sub.clients[i].id_socket === ws['id_socket']) {
242
- sub.clients.splice(i, 1);
342
+ return __awaiter(this, void 0, void 0, function () {
343
+ var that, sub, i;
344
+ return __generator(this, function (_a) {
345
+ that = this;
346
+ // console.log('Before');
347
+ // console.dir(this._subscriptions.map(a => a.publication));
348
+ if (!that._publications[publication]) {
349
+ console.log('No Publication: ' + publication);
350
+ return [2 /*return*/];
351
+ }
352
+ else {
353
+ sub = that._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
354
+ if (sub) {
355
+ for (i = sub.clients.length - 1; i >= 0; i--) {
356
+ if (sub.clients[i].id_user === ws['id_user'] && sub.clients[i].messageId === messageId && sub.clients[i].id_socket === ws['id_socket']) {
357
+ sub.clients.splice(i, 1);
358
+ }
359
+ }
243
360
  }
244
361
  }
245
- }
246
- }
247
- }
362
+ return [2 /*return*/];
363
+ });
364
+ });
365
+ };
248
366
  // Unsubscribe from publication
249
- async unsubscribeAll(ws) {
250
- let that = this;
251
- // console.log('Before');
252
- // console.dir(this._subscriptions.map(a => a.publication));
253
- let loggedInUser = await LoggedInUsers.findOne({ id_ws: ws['id_socket'] });
254
- if (loggedInUser) {
255
- if (this._loggedInUsers.map(a => a._id).indexOf(loggedInUser._id) >= 0) {
256
- this._loggedInUsers.splice(this._loggedInUsers.map(a => a._id).indexOf(loggedInUser._id), 1);
257
- }
258
- LoggedInUsers.deleteOne({ _id: loggedInUser._id });
259
- }
260
- let userSubs = that._subscriptions.filter(a => a.clients.some(b => b.id_user === ws['id_user'] && b.id_socket === ws['id_socket']));
261
- for (let i = userSubs.length - 1; i >= 0; i--) {
262
- let sub = userSubs[i];
263
- for (let j = sub.clients.length - 1; j >= 0; j--) {
264
- if (sub.clients[j].id_socket === ws['id_socket']) {
265
- sub.clients.splice(j, 1);
367
+ SubscriptionManager.prototype.unsubscribeAll = function (ws) {
368
+ return __awaiter(this, void 0, void 0, function () {
369
+ var that, loggedInUser, userSubs, i, sub, j;
370
+ return __generator(this, function (_a) {
371
+ 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'] })];
375
+ case 1:
376
+ loggedInUser = _a.sent();
377
+ if (loggedInUser) {
378
+ if (this._loggedInUsers.map(function (a) { return a._id; }).indexOf(loggedInUser._id) >= 0) {
379
+ this._loggedInUsers.splice(this._loggedInUsers.map(function (a) { return a._id; }).indexOf(loggedInUser._id), 1);
380
+ }
381
+ logged_in_users_collection_1.LoggedInUsers.deleteOne({ _id: loggedInUser._id });
382
+ }
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']; }); });
384
+ for (i = userSubs.length - 1; i >= 0; i--) {
385
+ sub = userSubs[i];
386
+ for (j = sub.clients.length - 1; j >= 0; j--) {
387
+ if (sub.clients[j].id_socket === ws['id_socket']) {
388
+ sub.clients.splice(j, 1);
389
+ }
390
+ }
391
+ }
392
+ ws.terminate();
393
+ return [2 /*return*/];
266
394
  }
267
- }
268
- }
269
- ws.terminate();
270
- // console.log('After');
271
- // console.dir(this._subscriptions.map(a => a.publication));
272
- }
395
+ });
396
+ });
397
+ };
273
398
  // Get publication collection
274
- getPublicationCollections(publication) {
399
+ SubscriptionManager.prototype.getPublicationCollections = function (publication) {
275
400
  return this._publications[publication].collections;
276
- }
401
+ };
277
402
  // Watch (tail) Mongo's operation log on the entire database (all insert/modify/delete will trigger this function)
278
- tailOpLog() {
279
- this._oplog = ResolveIOServer.getMainDB().watch([], { fullDocument: 'updateLookup' });
280
- this._oplog.on('change', (doc) => {
281
- let collection = doc.ns.coll;
403
+ SubscriptionManager.prototype.tailOpLog = function () {
404
+ var _this = this;
405
+ this._oplog = index_1.ResolveIOServer.getMainDB().watch([], { fullDocument: 'updateLookup' });
406
+ this._oplog.on('change', function (doc) {
407
+ var collection = doc.ns.coll;
282
408
  if (collection && !collection.endsWith('.versions') && !collection.startsWith('monitor-')) {
283
409
  if (doc.operationType === 'insert') {
284
410
  if (collection === 'queue-methods') {
285
- this._mainServer.getQueueManager().runMethodQueue();
411
+ _this._mainServer.getQueueManager().runMethodQueue();
286
412
  }
287
413
  else if (collection === 'queue-responses') {
288
- this._mainServer.getQueueManager().runMethodResponse();
414
+ _this._mainServer.getQueueManager().runMethodResponse();
289
415
  }
290
416
  else {
291
417
  if (collection === 'cron-jobs') {
292
- this._mainServer.getCronManager().checkCronJobsForUpdates();
418
+ _this._mainServer.getCronManager().checkCronJobsForUpdates();
293
419
  }
294
420
  else if (collection === 'support-tickets') {
295
- if (this.serverConfig['ROOT_URL'] === 'https://resolveio.com') {
296
- this._mainServer.getMethodManager().callMethodInternal('sendSupportTicketEmail', doc.fullDocument._id);
421
+ if (_this.serverConfig['ROOT_URL'] === 'https://resolveio.com') {
422
+ _this._mainServer.getMethodManager().callMethodInternal('sendSupportTicketEmail', doc.fullDocument._id);
297
423
  }
298
424
  }
299
- this._oplogQueue.push({
425
+ _this._oplogQueue.push({
300
426
  type: 'insert',
301
427
  collection: collection,
302
428
  doc: doc.fullDocument
@@ -305,370 +431,443 @@ export class SubscriptionManager {
305
431
  }
306
432
  else if (doc.operationType === 'update' || doc.operationType === 'replace') {
307
433
  if (collection === 'cron-jobs') {
308
- this._mainServer.getCronManager().checkCronJobsForUpdates();
434
+ _this._mainServer.getCronManager().checkCronJobsForUpdates();
309
435
  }
310
- this._oplogQueue.push({
436
+ _this._oplogQueue.push({
311
437
  type: 'update',
312
438
  collection: collection,
313
439
  doc: doc.fullDocument
314
440
  });
315
441
  }
316
442
  else if (doc.operationType === 'delete') {
317
- this.resendPubsForDelete(collection);
443
+ _this.resendPubsForDelete(collection);
318
444
  if (collection === 'cron-jobs') {
319
- this._mainServer.getCronManager().checkCronJobsForUpdates();
445
+ _this._mainServer.getCronManager().checkCronJobsForUpdates();
320
446
  }
321
447
  }
322
448
  }
323
449
  });
324
- this._oplog.on('error', error => {
450
+ this._oplog.on('error', function (error) {
325
451
  console.log('oplog error', error);
326
452
  process.exit();
327
453
  });
328
- this._oplog.on('end', () => {
454
+ this._oplog.on('end', function () {
329
455
  console.log('oplog ended');
330
456
  process.exit();
331
457
  });
332
- }
333
- runOpLogQueue() {
334
- let that = this;
335
- setInterval(async () => {
336
- if (this._oplogQueue.length) {
337
- let uniqueOplog = deepCopy(this._oplogQueue);
338
- this._oplogQueue = [];
339
- let uniquePubs = [];
340
- for (let i = uniqueOplog.length - 1; i >= 0; i--) {
341
- if (!that._subscriptions.some(a => a.collections.includes(uniqueOplog[i].collection))) {
342
- for (let j = this._subCache.length - 1; j >= 0; j--) {
343
- if (this._subCache[j].collection === uniqueOplog[i].collection) {
344
- this._nodeCache.del(this._subCache[j].id);
345
- this._subCache.splice(j, 1);
346
- }
347
- }
348
- uniqueOplog.splice(i, 1);
349
- }
350
- }
351
- for (let i = uniqueOplog.length - 1; i >= 0; i--) {
352
- let oplog = uniqueOplog[i];
353
- for (let j = that._subscriptions.length - 1; j >= 0; j--) {
354
- let sub = that._subscriptions[j];
355
- if (sub) {
356
- if (sub.collections.includes(oplog.collection) && !uniquePubs.filter(a => a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData)).length) {
357
- if (sub.preSubscriptionData) {
358
- if (that._publications[sub.publication].fetchFunction(oplog.doc, sub.preSubscriptionData)) {
359
- sub['oplog'] = oplog;
360
- uniquePubs.push(deepCopy(sub));
361
- continue;
458
+ };
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);
362
477
  }
363
478
  }
364
- if (that._publications[sub.publication].preFunction) {
365
- if (sub.clients.length) {
366
- sub.preSubscriptionData = await that._publications[sub.publication].preFunction(...sub.subscriptionData);
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();
367
513
  if (that._publications[sub.publication].fetchFunction(oplog.doc, sub.preSubscriptionData)) {
368
514
  sub['oplog'] = oplog;
369
- uniquePubs.push(deepCopy(sub));
515
+ uniquePubs.push(common_1.deepCopy(sub));
370
516
  }
371
- }
372
- else {
373
- for (let i = this._subCache.length - 1; i >= 0; i--) {
374
- if (this._subCache[i].publication === sub.publication && JSON.stringify(this._subCache[i].subscriptionData) === JSON.stringify(sub.subscriptionData)) {
375
- this._nodeCache.del(this._subCache[i].id);
376
- this._subCache.splice(i, 1);
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);
377
523
  }
378
524
  }
379
- if (that._subscriptions.findIndex(a => a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData)) >= 0) {
380
- that._subscriptions.splice(that._subscriptions.findIndex(a => a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData)), 1);
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);
381
527
  }
382
- }
383
- }
384
- else {
385
- if (that._publications[sub.publication].fetchFunction) {
386
- if (that._publications[sub.publication].fetchFunction(oplog.doc, ...sub.subscriptionData)) {
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 {
387
538
  sub['oplog'] = oplog;
388
- uniquePubs.push(deepCopy(sub));
539
+ uniquePubs.push(common_1.deepCopy(sub));
389
540
  }
390
- }
391
- else {
392
- sub['oplog'] = oplog;
393
- uniquePubs.push(deepCopy(sub));
394
- }
541
+ _d.label = 5;
542
+ case 5: return [2 /*return*/];
395
543
  }
396
- }
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);
397
565
  }
398
- }
566
+ _a.label = 7;
567
+ case 7: return [2 /*return*/];
399
568
  }
400
- for (let i = 0; i < uniquePubs.length; i++) {
401
- let pub = uniquePubs[i];
402
- this.checkPubsForChanges(pub['oplog'].collection, pub['oplog'].doc, pub['oplog'].type, pub);
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);
403
588
  }
404
- }
405
- }, 100);
406
- }
407
- async resendPubsForDelete(collection) {
408
- let that = this;
409
- let subCopy = deepCopy(that._subscriptions.filter(a => a.collections.includes(collection)));
410
- for (let i = this._subCache.length - 1; i >= 0; i--) {
411
- if (this._subCache[i].collection === collection || subCopy.some(a => a.publication === this._subCache[i].publication)) {
412
- this._nodeCache.del(this._subCache[i].id);
413
- this._subCache.splice(i, 1);
414
- }
415
- }
416
- for (let zz = subCopy.length - 1; zz >= 0; zz--) {
417
- let sub = subCopy[zz];
418
- if (that._publications[sub.publication].ws_specific) {
419
- for (let aa = 0; aa < sub.clients.length; aa++) {
420
- let client = sub.clients[aa];
421
- for (let bb = 0; bb < that._wss.clients.size; bb++) {
422
- let ws = Array.from(that._wss.clients)[bb];
423
- if (ws['id_socket'] === client.id_socket) {
424
- that.sendPubDataOnce(ws, client.messageId, sub, 'delete', collection);
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
+ }
425
600
  }
426
601
  }
602
+ else {
603
+ that.sendPubData(sub, 'delete', collection);
604
+ }
427
605
  }
428
- }
429
- else {
430
- that.sendPubData(sub, 'delete', collection);
431
- }
432
- }
433
- }
606
+ return [2 /*return*/];
607
+ });
608
+ });
609
+ };
434
610
  // Document: Inserted/Modified Document
435
611
  // Collection: Collection Document Lives In
436
612
  // Check to see if any pubs need to refetch due to Mongo operation
437
- async checkPubsForChanges(collection, document, type, sub) {
438
- let that = this;
439
- if (sub.preSubscriptionData) {
440
- if (that._publications[sub.publication].fetchFunction(document, sub.preSubscriptionData)) {
441
- that.sendPubData(sub, type, collection);
442
- return;
443
- }
444
- }
445
- if (!that._publications[sub.publication].preFunction) {
446
- if (that._publications[sub.publication].ws_specific) {
447
- for (let k = 0; k < sub.clients.length; k++) {
448
- let client = sub.clients[k];
449
- if (that._publications[sub.publication].fetchFunction) {
450
- if (that._publications[sub.publication].fetchFunction(document, client.id_user, ...sub.subscriptionData)) {
451
- for (let j = 0; j < that._wss.clients.size; j++) {
452
- let ws = Array.from(that._wss.clients)[j];
453
- if (ws['id_socket'] === client.id_socket) {
454
- that.sendPubDataOnce(ws, client.messageId, sub, type, collection);
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
+ }
455
645
  }
456
646
  }
457
647
  }
458
648
  }
459
649
  else {
460
- for (let j = 0; j < that._wss.clients.size; j++) {
461
- let ws = Array.from(that._wss.clients)[j];
462
- if (ws['id_socket'] === client.id_socket) {
463
- that.sendPubDataOnce(ws, client.messageId, sub, type, collection);
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);
464
653
  }
654
+ // else
655
+ }
656
+ else {
657
+ that.sendPubData(sub, type, collection);
465
658
  }
466
659
  }
467
660
  }
468
- }
469
- else {
470
- if (that._publications[sub.publication].fetchFunction) {
471
- if (that._publications[sub.publication].fetchFunction(document, ...sub.subscriptionData)) {
472
- that.sendPubData(sub, type, collection);
473
- }
474
- // else
475
- }
476
- else {
477
- that.sendPubData(sub, type, collection);
478
- }
479
- }
480
- }
481
- }
661
+ return [2 /*return*/];
662
+ });
663
+ });
664
+ };
482
665
  // Fetch pub once, send to all clients linked to this pub
483
- sendPubDataOnce(ws, messageId, subscription, type, collection) {
484
- return new Promise(async (resolve, reject) => {
485
- let that = this;
486
- if (!that._publications[subscription.publication].ws_specific) { // Same pub for all users
487
- if (this._subCache.some(a => a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData))) {
488
- let serverRes = {
489
- messageId: messageId,
490
- hasError: false,
491
- data: this._nodeCache.get(this._subCache.filter(a => a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData))[0].id)
492
- };
493
- this.sendWS(ws, serverRes);
494
- resolve(true);
495
- }
496
- else {
497
- if (subscription.publication !== 'superadminAPM' && subscription.publication !== 'loggedInUsers') {
498
- that._mainServer.getMethodManager().callMethodInternal('insertSubscriptionLog', type, subscription.publication, collection, JSON.stringify(subscription.subscriptionData));
499
- }
500
- for (let i = this._subCache.length - 1; i >= 0; i--) {
501
- if (this._subCache[i].publication === subscription.publication && JSON.stringify(this._subCache[i].subscriptionData) === JSON.stringify(subscription.subscriptionData)) {
502
- this._nodeCache.del(this._subCache[i].id);
503
- this._subCache.splice(i, 1);
504
- }
666
+ SubscriptionManager.prototype.sendPubDataOnce = function (ws, messageId, subscription, type, collection) {
667
+ var _this = this;
668
+ return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
669
+ var that, serverRes, i;
670
+ var _a, _b;
671
+ var _this = this;
672
+ 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);
505
683
  }
506
- that._publications[subscription.publication].function.call(Object.assign({}, that, SubscriptionManager.prototype), ...subscription.subscriptionData).catch(methodErrs => {
507
- 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));
508
- }).then(res => {
509
- let nodeCacheSize = this._nodeCache.getStats().vsize;
510
- if (nodeCacheSize > 500000000) {
511
- let neededSizeIndex = 0;
512
- for (let zz = 0; zz < this._subCache.length; zz++) {
513
- this._nodeCache.del(this._subCache[zz].id);
514
- if (this._nodeCache.getStats().vsize < 500000000) {
515
- neededSizeIndex = zz;
516
- break;
517
- }
518
- }
519
- // console.log('Cache1: ' + 'Too Big - ' + subscription.publication + ' - ' + getBinarySize(JSON.stringify(this._subCache)));
520
- this._subCache.splice(0, neededSizeIndex);
684
+ else {
685
+ if (subscription.publication !== 'superadminAPM' && subscription.publication !== 'loggedInUsers') {
686
+ that._mainServer.getMethodManager().callMethodInternal('insertSubscriptionLog', type, subscription.publication, collection, JSON.stringify(subscription.subscriptionData));
521
687
  }
522
- for (let i = this._subCache.length - 1; i >= 0; i--) {
688
+ for (i = this._subCache.length - 1; i >= 0; i--) {
523
689
  if (this._subCache[i].publication === subscription.publication && JSON.stringify(this._subCache[i].subscriptionData) === JSON.stringify(subscription.subscriptionData)) {
524
690
  this._nodeCache.del(this._subCache[i].id);
525
691
  this._subCache.splice(i, 1);
526
692
  }
527
693
  }
528
- if (this._subscriptions.some(a => a.collections.includes(collection) && a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData))) {
529
- let cacheId = this._cacheId++;
530
- this._nodeCache.set(cacheId, res);
531
- this._subCache.push({ collection: collection, publication: subscription.publication, subscriptionData: subscription.subscriptionData, id: cacheId });
532
- let serverRes = {
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
+ }).then(function (res) {
697
+ var nodeCacheSize = _this._nodeCache.getStats().vsize;
698
+ if (nodeCacheSize > 500000000) {
699
+ var neededSizeIndex = 0;
700
+ for (var zz = 0; zz < _this._subCache.length; zz++) {
701
+ _this._nodeCache.del(_this._subCache[zz].id);
702
+ if (_this._nodeCache.getStats().vsize < 500000000) {
703
+ neededSizeIndex = zz;
704
+ break;
705
+ }
706
+ }
707
+ // console.log('Cache1: ' + 'Too Big - ' + subscription.publication + ' - ' + getBinarySize(JSON.stringify(this._subCache)));
708
+ _this._subCache.splice(0, neededSizeIndex);
709
+ }
710
+ for (var i = _this._subCache.length - 1; i >= 0; i--) {
711
+ if (_this._subCache[i].publication === subscription.publication && JSON.stringify(_this._subCache[i].subscriptionData) === JSON.stringify(subscription.subscriptionData)) {
712
+ _this._nodeCache.del(_this._subCache[i].id);
713
+ _this._subCache.splice(i, 1);
714
+ }
715
+ }
716
+ if (_this._subscriptions.some(function (a) { return a.collections.includes(collection) && a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); })) {
717
+ var cacheId = _this._cacheId++;
718
+ _this._nodeCache.set(cacheId, res);
719
+ _this._subCache.push({ collection: collection, publication: subscription.publication, subscriptionData: subscription.subscriptionData, id: cacheId });
720
+ var serverRes = {
721
+ messageId: messageId,
722
+ hasError: false,
723
+ data: res
724
+ };
725
+ _this.sendWS(ws, serverRes);
726
+ }
727
+ resolve(true);
728
+ }, function (err) {
729
+ var serverRes = {
533
730
  messageId: messageId,
534
- hasError: false,
535
- data: res
731
+ hasError: true,
732
+ data: err
536
733
  };
537
- this.sendWS(ws, serverRes);
538
- }
734
+ _this.sendWS(ws, serverRes);
735
+ reject(err);
736
+ });
737
+ }
738
+ }
739
+ else { // Pub is different for each user (Look at users publication) [MUCH SLOWER, RE-RUNS FOR EACH USER]
740
+ (_b = that._publications[subscription.publication].function).call.apply(_b, __spreadArrays([Object.assign({}, that, SubscriptionManager.prototype), ws['id_user']], subscription.subscriptionData)).catch(function (methodErrs) {
741
+ 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));
742
+ }).then(function (res) {
743
+ var serverRes = {
744
+ messageId: messageId,
745
+ hasError: false,
746
+ data: res
747
+ };
748
+ // console.log('Send Pub Data Once 2', new Date(), 'Success', subscription);
749
+ _this.sendWS(ws, serverRes);
539
750
  resolve(true);
540
- }, err => {
541
- let serverRes = {
751
+ }, function (err) {
752
+ var serverRes = {
542
753
  messageId: messageId,
543
754
  hasError: true,
544
755
  data: err
545
756
  };
546
- this.sendWS(ws, serverRes);
757
+ // console.log('Send Pub Data Once 2', new Date(), 'Error', subscription);
758
+ _this.sendWS(ws, serverRes);
547
759
  reject(err);
548
760
  });
549
761
  }
550
- }
551
- else { // Pub is different for each user (Look at users publication) [MUCH SLOWER, RE-RUNS FOR EACH USER]
552
- that._publications[subscription.publication].function.call(Object.assign({}, that, SubscriptionManager.prototype), ws['id_user'], ...subscription.subscriptionData).catch(methodErrs => {
553
- 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));
554
- }).then(res => {
555
- let serverRes = {
556
- messageId: messageId,
557
- hasError: false,
558
- data: res
559
- };
560
- // console.log('Send Pub Data Once 2', new Date(), 'Success', subscription);
561
- this.sendWS(ws, serverRes);
562
- resolve(true);
563
- }, err => {
564
- let serverRes = {
565
- messageId: messageId,
566
- hasError: true,
567
- data: err
568
- };
569
- // console.log('Send Pub Data Once 2', new Date(), 'Error', subscription);
570
- this.sendWS(ws, serverRes);
571
- reject(err);
572
- });
573
- }
574
- });
575
- }
762
+ return [2 /*return*/];
763
+ });
764
+ }); });
765
+ };
576
766
  // Fetch pub once, send to all clients linked to this pub
577
- sendPubData(subscription, type, collection) {
578
- return new Promise(async (resolve, reject) => {
579
- let that = this;
580
- if (!subscription.clients.length) {
581
- for (let i = this._subCache.length - 1; i >= 0; i--) {
582
- if (this._subCache[i].publication === subscription.publication && JSON.stringify(this._subCache[i].subscriptionData) === JSON.stringify(subscription.subscriptionData)) {
583
- this._nodeCache.del(this._subCache[i].id);
584
- this._subCache.splice(i, 1);
767
+ SubscriptionManager.prototype.sendPubData = function (subscription, type, collection) {
768
+ var _this = this;
769
+ return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
770
+ var that, i, i;
771
+ var _a;
772
+ var _this = this;
773
+ return __generator(this, function (_b) {
774
+ that = this;
775
+ if (!subscription.clients.length) {
776
+ for (i = this._subCache.length - 1; i >= 0; i--) {
777
+ if (this._subCache[i].publication === subscription.publication && JSON.stringify(this._subCache[i].subscriptionData) === JSON.stringify(subscription.subscriptionData)) {
778
+ this._nodeCache.del(this._subCache[i].id);
779
+ this._subCache.splice(i, 1);
780
+ }
585
781
  }
586
- }
587
- if (that._subscriptions.findIndex(a => a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData)) >= 0) {
588
- that._subscriptions.splice(that._subscriptions.findIndex(a => a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData)), 1);
589
- }
590
- }
591
- else {
592
- if (subscription.publication !== 'superadminAPM' && subscription.publication !== 'loggedInUsers') {
593
- that._mainServer.getMethodManager().callMethodInternal('insertSubscriptionLog', type, subscription.publication, collection, JSON.stringify(subscription.subscriptionData));
594
- }
595
- for (let i = this._subCache.length - 1; i >= 0; i--) {
596
- if (this._subCache[i].publication === subscription.publication && JSON.stringify(this._subCache[i].subscriptionData) === JSON.stringify(subscription.subscriptionData)) {
597
- this._nodeCache.del(this._subCache[i].id);
598
- this._subCache.splice(i, 1);
782
+ if (that._subscriptions.findIndex(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); }) >= 0) {
783
+ that._subscriptions.splice(that._subscriptions.findIndex(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); }), 1);
599
784
  }
600
785
  }
601
- that._publications[subscription.publication].function.call(Object.assign({}, that, SubscriptionManager.prototype), ...subscription.subscriptionData).catch(methodErrs => {
602
- 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));
603
- }).then(res => {
604
- let nodeCacheSize = this._nodeCache.getStats().vsize;
605
- if (nodeCacheSize > 500000000) {
606
- let neededSizeIndex = 0;
607
- for (let zz = 0; zz < this._subCache.length; zz++) {
608
- this._nodeCache.del(this._subCache[zz].id);
609
- if (this._nodeCache.getStats().vsize < 500000000) {
610
- neededSizeIndex = zz;
611
- break;
612
- }
613
- }
614
- // console.log('Cache: ' + 'Too Big - ' + subscription.publication + ' - ' + getBinarySize(JSON.stringify(this._subCache)));
615
- this._subCache.splice(0, neededSizeIndex);
786
+ else {
787
+ if (subscription.publication !== 'superadminAPM' && subscription.publication !== 'loggedInUsers') {
788
+ that._mainServer.getMethodManager().callMethodInternal('insertSubscriptionLog', type, subscription.publication, collection, JSON.stringify(subscription.subscriptionData));
616
789
  }
617
- for (let i = this._subCache.length - 1; i >= 0; i--) {
790
+ for (i = this._subCache.length - 1; i >= 0; i--) {
618
791
  if (this._subCache[i].publication === subscription.publication && JSON.stringify(this._subCache[i].subscriptionData) === JSON.stringify(subscription.subscriptionData)) {
619
792
  this._nodeCache.del(this._subCache[i].id);
620
793
  this._subCache.splice(i, 1);
621
794
  }
622
795
  }
623
- if (!subscription.clients.length) {
624
- if (that._subscriptions.findIndex(a => a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData)) >= 0) {
625
- that._subscriptions.splice(that._subscriptions.findIndex(a => a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData)), 1);
796
+ (_a = that._publications[subscription.publication].function).call.apply(_a, __spreadArrays([Object.assign({}, that, SubscriptionManager.prototype)], subscription.subscriptionData)).catch(function (methodErrs) {
797
+ 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));
798
+ }).then(function (res) {
799
+ var nodeCacheSize = _this._nodeCache.getStats().vsize;
800
+ if (nodeCacheSize > 500000000) {
801
+ var neededSizeIndex = 0;
802
+ for (var zz = 0; zz < _this._subCache.length; zz++) {
803
+ _this._nodeCache.del(_this._subCache[zz].id);
804
+ if (_this._nodeCache.getStats().vsize < 500000000) {
805
+ neededSizeIndex = zz;
806
+ break;
807
+ }
808
+ }
809
+ // console.log('Cache: ' + 'Too Big - ' + subscription.publication + ' - ' + getBinarySize(JSON.stringify(this._subCache)));
810
+ _this._subCache.splice(0, neededSizeIndex);
626
811
  }
627
- }
628
- else {
629
- if (this._subscriptions.some(a => a.collections.includes(collection) && a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData))) {
630
- let cacheId = this._cacheId++;
631
- this._nodeCache.set(cacheId, res);
632
- this._subCache.push({ collection: collection, publication: subscription.publication, subscriptionData: subscription.subscriptionData, id: cacheId });
633
- subscription.clients.forEach(client => {
634
- that._wss.clients.forEach(ws => {
635
- if (ws['id_socket'] === client.id_socket) {
636
- let serverRes = {
637
- messageId: client.messageId,
638
- hasError: false,
639
- data: res
640
- };
641
- this.sendWS(ws, serverRes);
642
- }
643
- });
644
- });
812
+ for (var i = _this._subCache.length - 1; i >= 0; i--) {
813
+ if (_this._subCache[i].publication === subscription.publication && JSON.stringify(_this._subCache[i].subscriptionData) === JSON.stringify(subscription.subscriptionData)) {
814
+ _this._nodeCache.del(_this._subCache[i].id);
815
+ _this._subCache.splice(i, 1);
816
+ }
645
817
  }
646
- }
647
- resolve(true);
648
- }, err => {
649
- subscription.clients.forEach(client => {
650
- that._wss.clients.forEach(ws => {
651
- if (ws['id_socket'] === client.id_socket) {
652
- let serverRes = {
653
- messageId: client.messageId,
654
- hasError: true,
655
- data: err
656
- };
657
- this.sendWS(ws, serverRes);
818
+ if (!subscription.clients.length) {
819
+ if (that._subscriptions.findIndex(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); }) >= 0) {
820
+ that._subscriptions.splice(that._subscriptions.findIndex(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); }), 1);
821
+ }
822
+ }
823
+ else {
824
+ if (_this._subscriptions.some(function (a) { return a.collections.includes(collection) && a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); })) {
825
+ var cacheId = _this._cacheId++;
826
+ _this._nodeCache.set(cacheId, res);
827
+ _this._subCache.push({ collection: collection, publication: subscription.publication, subscriptionData: subscription.subscriptionData, id: cacheId });
828
+ subscription.clients.forEach(function (client) {
829
+ that._wss.clients.forEach(function (ws) {
830
+ if (ws['id_socket'] === client.id_socket) {
831
+ var serverRes = {
832
+ messageId: client.messageId,
833
+ hasError: false,
834
+ data: res
835
+ };
836
+ _this.sendWS(ws, serverRes);
837
+ }
838
+ });
839
+ });
658
840
  }
841
+ }
842
+ resolve(true);
843
+ }, function (err) {
844
+ subscription.clients.forEach(function (client) {
845
+ that._wss.clients.forEach(function (ws) {
846
+ if (ws['id_socket'] === client.id_socket) {
847
+ var serverRes = {
848
+ messageId: client.messageId,
849
+ hasError: true,
850
+ data: err
851
+ };
852
+ _this.sendWS(ws, serverRes);
853
+ }
854
+ });
659
855
  });
856
+ reject(err);
660
857
  });
661
- reject(err);
662
- });
663
- }
664
- });
665
- }
666
- sendWS(ws, data) {
858
+ }
859
+ return [2 /*return*/];
860
+ });
861
+ }); });
862
+ };
863
+ SubscriptionManager.prototype.sendWS = function (ws, data) {
667
864
  this._sendQueue.splice(0, 0, {
668
865
  id_ws: ws['id_socket'],
669
866
  data: data
670
867
  });
671
- }
672
- }
868
+ };
869
+ return SubscriptionManager;
870
+ }());
871
+ exports.SubscriptionManager = SubscriptionManager;
673
872
 
674
873
  //# sourceMappingURL=subscription.manager.js.map