@resolveio/server-lib 9.0.32 → 9.0.34

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 (193) hide show
  1. package/client-server-app.js +7 -48
  2. package/client-server-app.js.map +1 -1
  3. package/collections/app-status.collection.js +8 -11
  4. package/collections/app-status.collection.js.map +1 -1
  5. package/collections/app-version.collection.js +8 -11
  6. package/collections/app-version.collection.js.map +1 -1
  7. package/collections/counter.collection.js +8 -11
  8. package/collections/counter.collection.js.map +1 -1
  9. package/collections/cron-job-history.collection.js +9 -12
  10. package/collections/cron-job-history.collection.js.map +1 -1
  11. package/collections/cron-job.collection.js +8 -11
  12. package/collections/cron-job.collection.js.map +1 -1
  13. package/collections/email-history.collection.js +8 -11
  14. package/collections/email-history.collection.js.map +1 -1
  15. package/collections/email-verified.collection.js +9 -12
  16. package/collections/email-verified.collection.js.map +1 -1
  17. package/collections/file.collection.js +8 -11
  18. package/collections/file.collection.js.map +1 -1
  19. package/collections/flag.collection.js +8 -11
  20. package/collections/flag.collection.js.map +1 -1
  21. package/collections/log-method-latency.collection.js +11 -14
  22. package/collections/log-method-latency.collection.js.map +1 -1
  23. package/collections/log-subscription.collection.js +13 -16
  24. package/collections/log-subscription.collection.js.map +1 -1
  25. package/collections/log.collection.js +16 -19
  26. package/collections/log.collection.js.map +1 -1
  27. package/collections/logged-in-users.collection.js +9 -12
  28. package/collections/logged-in-users.collection.js.map +1 -1
  29. package/collections/method-call.collection.js +10 -13
  30. package/collections/method-call.collection.js.map +1 -1
  31. package/collections/method-response.collection.js +10 -13
  32. package/collections/method-response.collection.js.map +1 -1
  33. package/collections/monitor-cpu.collection.js +13 -16
  34. package/collections/monitor-cpu.collection.js.map +1 -1
  35. package/collections/monitor-https-inbound.collection.js +13 -16
  36. package/collections/monitor-https-inbound.collection.js.map +1 -1
  37. package/collections/monitor-https-outbound.collection.js +15 -18
  38. package/collections/monitor-https-outbound.collection.js.map +1 -1
  39. package/collections/monitor-memory.collection.js +12 -15
  40. package/collections/monitor-memory.collection.js.map +1 -1
  41. package/collections/monitor-mongo.collection.js +12 -15
  42. package/collections/monitor-mongo.collection.js.map +1 -1
  43. package/collections/notification.collection.js +8 -11
  44. package/collections/notification.collection.js.map +1 -1
  45. package/collections/queue-flag.collection.js +9 -12
  46. package/collections/queue-flag.collection.js.map +1 -1
  47. package/collections/queue-method.collection.js +9 -12
  48. package/collections/queue-method.collection.js.map +1 -1
  49. package/collections/queue-response.collection.js +9 -12
  50. package/collections/queue-response.collection.js.map +1 -1
  51. package/collections/report-builder-dashboard-builder.collection.js +10 -13
  52. package/collections/report-builder-dashboard-builder.collection.js.map +1 -1
  53. package/collections/report-builder-library.collection.js +11 -14
  54. package/collections/report-builder-library.collection.js.map +1 -1
  55. package/collections/report-builder-report.collection.js +11 -14
  56. package/collections/report-builder-report.collection.js.map +1 -1
  57. package/collections/user-group.collection.js +11 -14
  58. package/collections/user-group.collection.js.map +1 -1
  59. package/collections/user-guide.collection.js +8 -11
  60. package/collections/user-guide.collection.js.map +1 -1
  61. package/collections/user.collection.js +12 -15
  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 +30 -80
  66. package/fixtures/cron-jobs.js.map +1 -1
  67. package/fixtures/init.js +9 -59
  68. package/fixtures/init.js.map +1 -1
  69. package/http/auth.js +344 -460
  70. package/http/auth.js.map +1 -1
  71. package/http/health.js +1 -5
  72. package/http/health.js.map +1 -1
  73. package/http/home.js +7 -11
  74. package/http/home.js.map +1 -1
  75. package/index.js +36 -98
  76. package/index.js.map +1 -1
  77. package/managers/cron.manager.js +78 -157
  78. package/managers/cron.manager.js.map +1 -1
  79. package/managers/method.manager.js +323 -303
  80. package/managers/method.manager.js.map +1 -1
  81. package/managers/mongo.manager.d.ts +14 -14
  82. package/managers/mongo.manager.js +794 -865
  83. package/managers/mongo.manager.js.map +1 -1
  84. package/managers/monitor.manager.js +171 -254
  85. package/managers/monitor.manager.js.map +1 -1
  86. package/managers/queue-method.manager.js +69 -166
  87. package/managers/queue-method.manager.js.map +1 -1
  88. package/managers/subscription.manager.js +515 -708
  89. package/managers/subscription.manager.js.map +1 -1
  90. package/methods/accounts.js +111 -183
  91. package/methods/accounts.js.map +1 -1
  92. package/methods/aws.js +156 -262
  93. package/methods/aws.js.map +1 -1
  94. package/methods/cloudconvert.js +100 -180
  95. package/methods/cloudconvert.js.map +1 -1
  96. package/methods/collections.js +114 -530
  97. package/methods/collections.js.map +1 -1
  98. package/methods/counters.js +29 -79
  99. package/methods/counters.js.map +1 -1
  100. package/methods/cron-jobs.js +819 -884
  101. package/methods/cron-jobs.js.map +1 -1
  102. package/methods/flags.js +1 -5
  103. package/methods/flags.js.map +1 -1
  104. package/methods/logs.js +258 -313
  105. package/methods/logs.js.map +1 -1
  106. package/methods/monitor.js +289 -348
  107. package/methods/monitor.js.map +1 -1
  108. package/methods/pdf.js +341 -340
  109. package/methods/pdf.js.map +1 -1
  110. package/methods/report-builder.js +565 -657
  111. package/methods/report-builder.js.map +1 -1
  112. package/methods/support.js +67 -185
  113. package/methods/support.js.map +1 -1
  114. package/models/app-status.model.js +1 -2
  115. package/models/app-version.model.js +1 -2
  116. package/models/billing-logged-in-users.model.js +1 -2
  117. package/models/collection-document.model.js +1 -2
  118. package/models/counter.model.js +1 -2
  119. package/models/cron-job-history.model.js +1 -2
  120. package/models/cron-job.model.js +1 -2
  121. package/models/dialog.model.js +1 -2
  122. package/models/email-history.model.js +1 -2
  123. package/models/email-verified.model.js +1 -2
  124. package/models/file.model.js +1 -2
  125. package/models/flag.model.js +1 -2
  126. package/models/log-method-latency.model.js +1 -2
  127. package/models/log-subscription.model.js +1 -2
  128. package/models/log.model.js +1 -2
  129. package/models/logged-in-users.model.js +1 -2
  130. package/models/method-call.model.js +1 -2
  131. package/models/method-response.model.js +1 -2
  132. package/models/method.model.js +1 -2
  133. package/models/monitor-cpu.model.js +1 -2
  134. package/models/monitor-https-inbound.model.js +1 -2
  135. package/models/monitor-https-outbound.model.js +1 -2
  136. package/models/monitor-memory.model.js +1 -2
  137. package/models/monitor-mongo.model.js +1 -2
  138. package/models/notification.model.js +1 -2
  139. package/models/pagination.model.js +2 -5
  140. package/models/pagination.model.js.map +1 -1
  141. package/models/permission.model.js +1 -2
  142. package/models/queue-method.model.js +1 -2
  143. package/models/queue-response.model.js +1 -2
  144. package/models/report-builder-dashboard-builder.model.js +1 -2
  145. package/models/report-builder-library.model.js +1 -2
  146. package/models/report-builder-report.model.js +1 -2
  147. package/models/report-builder.model.js +1 -2
  148. package/models/select-data-label.model.js +1 -2
  149. package/models/server-response.model.js +1 -2
  150. package/models/subscription.model.js +1 -2
  151. package/models/support-ticket.model.js +1 -2
  152. package/models/user-group.model.js +1 -2
  153. package/models/user-guide.model.js +1 -2
  154. package/models/user.model.js +1 -2
  155. package/package.json +1 -1
  156. package/public_api.js +70 -82
  157. package/public_api.js.map +1 -1
  158. package/publications/app-status.js +4 -8
  159. package/publications/app-status.js.map +1 -1
  160. package/publications/app-version.js +4 -8
  161. package/publications/app-version.js.map +1 -1
  162. package/publications/cron-jobs.js +9 -13
  163. package/publications/cron-jobs.js.map +1 -1
  164. package/publications/files.js +11 -15
  165. package/publications/files.js.map +1 -1
  166. package/publications/flags.js +7 -11
  167. package/publications/flags.js.map +1 -1
  168. package/publications/logs.js +31 -35
  169. package/publications/logs.js.map +1 -1
  170. package/publications/method-calls.js +4 -8
  171. package/publications/method-calls.js.map +1 -1
  172. package/publications/method-responses.js +4 -8
  173. package/publications/method-responses.js.map +1 -1
  174. package/publications/notifications.js +4 -8
  175. package/publications/notifications.js.map +1 -1
  176. package/publications/report-builder-dashboard-builders.js +8 -12
  177. package/publications/report-builder-dashboard-builders.js.map +1 -1
  178. package/publications/report-builder-libraries.js +16 -66
  179. package/publications/report-builder-libraries.js.map +1 -1
  180. package/publications/report-builder-reports.js +17 -21
  181. package/publications/report-builder-reports.js.map +1 -1
  182. package/publications/super-admin.js +3 -7
  183. package/publications/super-admin.js.map +1 -1
  184. package/publications/user-groups.js +4 -8
  185. package/publications/user-groups.js.map +1 -1
  186. package/publications/user-guides.js +4 -8
  187. package/publications/user-guides.js.map +1 -1
  188. package/server-app.js +393 -534
  189. package/server-app.js.map +1 -1
  190. package/util/common.js +55 -92
  191. package/util/common.js.map +1 -1
  192. package/util/schema-report-builder.js +76 -134
  193. package/util/schema-report-builder.js.map +1 -1
@@ -1,73 +1,26 @@
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;
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) {
71
24
  this._publications = {};
72
25
  this._subscriptions = [];
73
26
  this._loggedInUsers = [];
@@ -81,348 +34,269 @@ var SubscriptionManager = /** @class */ (function () {
81
34
  this.serverConfig = serverConfig;
82
35
  this._wss = wss;
83
36
  // Publications
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);
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);
99
52
  this.tailOpLog();
100
53
  this.runOpLogQueue();
101
- setInterval(function () {
54
+ setInterval(() => {
102
55
  console.log(new Date());
103
- console.log('Sub Cache Length: ' + _this._subCache.length);
104
- console.log('Sub Length: ' + _this._subscriptions.length);
105
- console.log(_this._nodeCache.getStats().vsize);
56
+ console.log('Sub Cache Length: ' + this._subCache.length);
57
+ console.log('Sub Length: ' + this._subscriptions.length);
58
+ console.log(this._nodeCache.getStats().vsize);
106
59
  }, 43200000);
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);
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);
113
66
  if (ws && ws.readyState === ws.OPEN) {
114
67
  if (ws.bufferedAmount < 20480) {
115
- var sendItem = _this._sendQueue.pop();
116
- ws.send(JSON.stringify(sendItem.data), function (error) {
68
+ let sendItem = this._sendQueue.pop();
69
+ ws.send(JSON.stringify(sendItem.data), (error) => {
117
70
  if (error) {
118
71
  console.log('Error on WS: ', error);
119
- _this.unsubscribeAll(ws);
72
+ this.unsubscribeAll(ws);
120
73
  }
121
74
  });
122
75
  }
123
76
  }
124
77
  else {
125
- _this._sendQueue.splice(i, 1);
78
+ this._sendQueue.splice(i, 1);
126
79
  }
127
- };
128
- for (var i = _this._sendQueue.length - 1; i >= 0; i--) {
129
- _loop_1(i);
130
80
  }
131
- _this._runningQueue = false;
81
+ this._runningQueue = false;
132
82
  }
133
83
  }
134
84
  }, 10);
135
- setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
136
- var _a, userCopy, _loop_2, this_1, i, i, sub, _loop_3, this_2, j;
137
- return __generator(this, function (_b) {
138
- switch (_b.label) {
139
- case 0:
140
- _a = this;
141
- return [4 /*yield*/, logged_in_users_collection_1.LoggedInUsers.find()];
142
- case 1:
143
- _a._loggedInUsers = _b.sent();
144
- userCopy = common_1.deepCopy(this._loggedInUsers);
145
- _loop_2 = function (i) {
146
- var loggedInUser;
147
- return __generator(this, function (_a) {
148
- switch (_a.label) {
149
- case 0:
150
- loggedInUser = userCopy[i];
151
- if (!(!loggedInUser.date || moment().diff(moment(loggedInUser.date), 'seconds') >= 60)) return [3 /*break*/, 3];
152
- if (!this_1._mainServer.getWS(loggedInUser.id_ws)) return [3 /*break*/, 1];
153
- this_1.unsubscribeAll(this_1._mainServer.getWS(loggedInUser.id_ws));
154
- return [3 /*break*/, 3];
155
- case 1:
156
- this_1._subscriptions.forEach(function (sub) {
157
- for (var j = sub.clients.length - 1; j >= 0; j--) {
158
- var client = sub.clients[j];
159
- if (client.id_socket === loggedInUser.id_ws) {
160
- sub.clients.splice(j, 1);
161
- }
162
- }
163
- });
164
- return [4 /*yield*/, logged_in_users_collection_1.LoggedInUsers.findById(loggedInUser._id)];
165
- case 2:
166
- if (_a.sent()) {
167
- logged_in_users_collection_1.LoggedInUsers.deleteOne({ _id: loggedInUser._id });
168
- }
169
- this_1._loggedInUsers.splice(i, 1);
170
- _a.label = 3;
171
- case 3: return [2 /*return*/];
172
- }
173
- });
174
- };
175
- this_1 = this;
176
- i = this._loggedInUsers.length - 1;
177
- _b.label = 2;
178
- case 2:
179
- if (!(i >= 0)) return [3 /*break*/, 5];
180
- return [5 /*yield**/, _loop_2(i)];
181
- case 3:
182
- _b.sent();
183
- _b.label = 4;
184
- case 4:
185
- i--;
186
- return [3 /*break*/, 2];
187
- case 5:
188
- for (i = 0; i < this._subscriptions.length; i++) {
189
- sub = this._subscriptions[i];
190
- _loop_3 = function (j) {
191
- var client = sub.clients[j];
192
- if (!this_2._loggedInUsers.some(function (a) { return a.id_ws === client.id_socket; })) {
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) {
193
99
  sub.clients.splice(j, 1);
194
100
  }
195
- };
196
- this_2 = this;
197
- for (j = sub.clients.length - 1; j >= 0; j--) {
198
- _loop_3(j);
199
101
  }
102
+ });
103
+ if (await LoggedInUsers.findById(loggedInUser._id)) {
104
+ LoggedInUsers.deleteOne({ _id: loggedInUser._id });
200
105
  }
201
- return [2 /*return*/];
106
+ this._loggedInUsers.splice(i, 1);
107
+ }
202
108
  }
203
- });
204
- }); }, 60000);
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);
205
120
  }
206
121
  // Add all files to publications private object
207
- SubscriptionManager.prototype.publications = function (method) {
122
+ publications(method) {
208
123
  this._publications = Object.assign(this._publications, method);
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 (err) {
225
- _this.unsubscribeAll(ws);
226
- });
227
- return [2 /*return*/];
228
- });
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);
229
137
  });
230
- };
138
+ }
231
139
  // Subscribe to publication
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];
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;
236
146
  }
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
- }
275
- }
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*/];
147
+ else {
148
+ if (subscriptionData.length > 1 || subscriptionData[0]) {
149
+ if (!pub.check) {
150
+ console.error('No Check Function For Pub ' + publication);
151
+ return;
332
152
  }
333
- });
334
- });
335
- };
336
- // Unsubscribe from publication
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];
341
- }
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*/];
153
+ else if (!pub.check._schema) {
154
+ console.error('No Check Schema For Pub ' + publication);
155
+ return;
351
156
  }
352
157
  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
- }
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;
359
171
  }
360
172
  }
361
173
  }
362
- return [2 /*return*/];
363
- });
364
- });
365
- };
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
+ }
228
+ // 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;
236
+ }
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);
243
+ }
244
+ }
245
+ }
246
+ }
247
+ }
366
248
  // Unsubscribe from publication
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*/];
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);
394
266
  }
395
- });
396
- });
397
- };
267
+ }
268
+ }
269
+ ws.terminate();
270
+ // console.log('After');
271
+ // console.dir(this._subscriptions.map(a => a.publication));
272
+ }
398
273
  // Get publication collection
399
- SubscriptionManager.prototype.getPublicationCollections = function (publication) {
274
+ getPublicationCollections(publication) {
400
275
  return this._publications[publication].collections;
401
- };
276
+ }
402
277
  // Watch (tail) Mongo's operation log on the entire database (all insert/modify/delete will trigger this function)
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;
278
+ tailOpLog() {
279
+ this._oplog = ResolveIOServer.getMainDB().watch([], { fullDocument: 'updateLookup' });
280
+ this._oplog.on('change', (doc) => {
281
+ let collection = doc.ns.coll;
408
282
  if (collection && !collection.endsWith('.versions') && !collection.startsWith('monitor-')) {
409
283
  if (doc.operationType === 'insert') {
410
284
  if (collection === 'queue-methods') {
411
- _this._mainServer.getQueueManager().runMethodQueue();
285
+ this._mainServer.getQueueManager().runMethodQueue();
412
286
  }
413
287
  else if (collection === 'queue-responses') {
414
- _this._mainServer.getQueueManager().runMethodResponse();
288
+ this._mainServer.getQueueManager().runMethodResponse();
415
289
  }
416
290
  else {
417
291
  if (collection === 'cron-jobs') {
418
- _this._mainServer.getCronManager().checkCronJobsForUpdates();
292
+ this._mainServer.getCronManager().checkCronJobsForUpdates();
419
293
  }
420
294
  else if (collection === 'support-tickets') {
421
- if (_this.serverConfig['ROOT_URL'] === 'https://resolveio.com') {
422
- _this._mainServer.getMethodManager().callMethodInternal('sendSupportTicketEmail', doc.fullDocument._id);
295
+ if (this.serverConfig['ROOT_URL'] === 'https://resolveio.com') {
296
+ this._mainServer.getMethodManager().callMethodInternal('sendSupportTicketEmail', doc.fullDocument._id);
423
297
  }
424
298
  }
425
- _this._oplogQueue.push({
299
+ this._oplogQueue.push({
426
300
  type: 'insert',
427
301
  collection: collection,
428
302
  doc: doc.fullDocument
@@ -431,437 +305,370 @@ var SubscriptionManager = /** @class */ (function () {
431
305
  }
432
306
  else if (doc.operationType === 'update' || doc.operationType === 'replace') {
433
307
  if (collection === 'cron-jobs') {
434
- _this._mainServer.getCronManager().checkCronJobsForUpdates();
308
+ this._mainServer.getCronManager().checkCronJobsForUpdates();
435
309
  }
436
- _this._oplogQueue.push({
310
+ this._oplogQueue.push({
437
311
  type: 'update',
438
312
  collection: collection,
439
313
  doc: doc.fullDocument
440
314
  });
441
315
  }
442
316
  else if (doc.operationType === 'delete') {
443
- _this.resendPubsForDelete(collection);
317
+ this.resendPubsForDelete(collection);
444
318
  if (collection === 'cron-jobs') {
445
- _this._mainServer.getCronManager().checkCronJobsForUpdates();
319
+ this._mainServer.getCronManager().checkCronJobsForUpdates();
446
320
  }
447
321
  }
448
322
  }
449
323
  });
450
- this._oplog.on('error', function (error) {
324
+ this._oplog.on('error', error => {
451
325
  console.log('oplog error', error);
452
326
  process.exit();
453
327
  });
454
- this._oplog.on('end', function () {
328
+ this._oplog.on('end', () => {
455
329
  console.log('oplog ended');
456
330
  process.exit();
457
331
  });
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);
477
- }
478
- }
479
- uniqueOplog_1.splice(i, 1);
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);
480
346
  }
481
- };
482
- this_3 = this;
483
- for (i = uniqueOplog_1.length - 1; i >= 0; i--) {
484
- _loop_4(i);
485
347
  }
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();
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;
362
+ }
363
+ }
364
+ if (that._publications[sub.publication].preFunction) {
365
+ if (sub.clients.length) {
366
+ sub.preSubscriptionData = await that._publications[sub.publication].preFunction(...sub.subscriptionData);
513
367
  if (that._publications[sub.publication].fetchFunction(oplog.doc, sub.preSubscriptionData)) {
514
368
  sub['oplog'] = oplog;
515
- uniquePubs.push(common_1.deepCopy(sub));
369
+ uniquePubs.push(deepCopy(sub));
516
370
  }
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);
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);
523
377
  }
524
378
  }
525
- if (that._subscriptions.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }) >= 0) {
526
- that._subscriptions.splice(that._subscriptions.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
527
- }
528
- _d.label = 3;
529
- case 3: return [3 /*break*/, 5];
530
- case 4:
531
- if (that._publications[sub.publication].fetchFunction) {
532
- if ((_c = that._publications[sub.publication]).fetchFunction.apply(_c, __spreadArrays([oplog.doc], sub.subscriptionData))) {
533
- sub['oplog'] = oplog;
534
- uniquePubs.push(common_1.deepCopy(sub));
535
- }
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);
536
381
  }
537
- else {
382
+ }
383
+ }
384
+ else {
385
+ if (that._publications[sub.publication].fetchFunction) {
386
+ if (that._publications[sub.publication].fetchFunction(oplog.doc, ...sub.subscriptionData)) {
538
387
  sub['oplog'] = oplog;
539
- uniquePubs.push(common_1.deepCopy(sub));
388
+ uniquePubs.push(deepCopy(sub));
540
389
  }
541
- _d.label = 5;
542
- case 5: return [2 /*return*/];
390
+ }
391
+ else {
392
+ sub['oplog'] = oplog;
393
+ uniquePubs.push(deepCopy(sub));
394
+ }
543
395
  }
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);
396
+ }
565
397
  }
566
- _a.label = 7;
567
- case 7: return [2 /*return*/];
568
- }
569
- });
570
- }); }, 100);
571
- };
572
- SubscriptionManager.prototype.resendPubsForDelete = function (collection) {
573
- return __awaiter(this, void 0, void 0, function () {
574
- var that, subCopy, _loop_6, this_5, i, zz, sub, aa, client, bb, ws;
575
- var _this = this;
576
- return __generator(this, function (_a) {
577
- that = this;
578
- subCopy = common_1.deepCopy(that._subscriptions.filter(function (a) { return a.collections.includes(collection); }));
579
- _loop_6 = function (i) {
580
- if (this_5._subCache[i].collection === collection || subCopy.some(function (a) { return a.publication === _this._subCache[i].publication; })) {
581
- this_5._nodeCache.del(this_5._subCache[i].id);
582
- this_5._subCache.splice(i, 1);
583
398
  }
584
- };
585
- this_5 = this;
586
- for (i = this._subCache.length - 1; i >= 0; i--) {
587
- _loop_6(i);
588
399
  }
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
- }
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);
403
+ }
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);
600
425
  }
601
426
  }
602
- else {
603
- that.sendPubData(sub, 'delete', collection);
604
- }
605
427
  }
606
- return [2 /*return*/];
607
- });
608
- });
609
- };
428
+ }
429
+ else {
430
+ that.sendPubData(sub, 'delete', collection);
431
+ }
432
+ }
433
+ }
610
434
  // Document: Inserted/Modified Document
611
435
  // Collection: Collection Document Lives In
612
436
  // Check to see if any pubs need to refetch due to Mongo operation
613
- SubscriptionManager.prototype.checkPubsForChanges = function (collection, document, type, sub) {
614
- return __awaiter(this, void 0, void 0, function () {
615
- var that, k, client, j, ws, j, ws;
616
- var _a, _b;
617
- return __generator(this, function (_c) {
618
- that = this;
619
- if (sub.preSubscriptionData) {
620
- if (that._publications[sub.publication].fetchFunction(document, sub.preSubscriptionData)) {
621
- that.sendPubData(sub, type, collection);
622
- return [2 /*return*/];
623
- }
624
- }
625
- if (!that._publications[sub.publication].preFunction) {
626
- if (that._publications[sub.publication].ws_specific) {
627
- for (k = 0; k < sub.clients.length; k++) {
628
- client = sub.clients[k];
629
- if (that._publications[sub.publication].fetchFunction) {
630
- if ((_a = that._publications[sub.publication]).fetchFunction.apply(_a, __spreadArrays([document, client.id_user], sub.subscriptionData))) {
631
- for (j = 0; j < that._wss.clients.size; j++) {
632
- ws = Array.from(that._wss.clients)[j];
633
- if (ws['id_socket'] === client.id_socket) {
634
- that.sendPubDataOnce(ws, client.messageId, sub, type, collection);
635
- }
636
- }
637
- }
638
- }
639
- else {
640
- for (j = 0; j < that._wss.clients.size; j++) {
641
- ws = Array.from(that._wss.clients)[j];
642
- if (ws['id_socket'] === client.id_socket) {
643
- that.sendPubDataOnce(ws, client.messageId, sub, type, collection);
644
- }
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);
645
455
  }
646
456
  }
647
457
  }
648
458
  }
649
459
  else {
650
- if (that._publications[sub.publication].fetchFunction) {
651
- if ((_b = that._publications[sub.publication]).fetchFunction.apply(_b, __spreadArrays([document], sub.subscriptionData))) {
652
- that.sendPubData(sub, type, collection);
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);
653
464
  }
654
- // else
655
- }
656
- else {
657
- that.sendPubData(sub, type, collection);
658
465
  }
659
466
  }
660
467
  }
661
- return [2 /*return*/];
662
- });
663
- });
664
- };
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
+ }
665
482
  // Fetch pub once, send to all clients linked to this pub
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);
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));
683
499
  }
684
- else {
685
- if (subscription.publication !== 'superadminAPM' && subscription.publication !== 'loggedInUsers') {
686
- that._mainServer.getMethodManager().callMethodInternal('insertSubscriptionLog', type, subscription.publication, collection, JSON.stringify(subscription.subscriptionData));
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);
687
504
  }
688
- for (i = this._subCache.length - 1; i >= 0; i--) {
505
+ }
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);
521
+ }
522
+ for (let i = this._subCache.length - 1; i >= 0; i--) {
689
523
  if (this._subCache[i].publication === subscription.publication && JSON.stringify(this._subCache[i].subscriptionData) === JSON.stringify(subscription.subscriptionData)) {
690
524
  this._nodeCache.del(this._subCache[i].id);
691
525
  this._subCache.splice(i, 1);
692
526
  }
693
527
  }
694
- (_a = that._publications[subscription.publication].function).call.apply(_a, __spreadArrays([Object.assign({}, that, SubscriptionManager.prototype)], subscription.subscriptionData)).then(function (res) {
695
- var nodeCacheSize = _this._nodeCache.getStats().vsize;
696
- if (nodeCacheSize > 500000000) {
697
- var neededSizeIndex = 0;
698
- for (var zz = 0; zz < _this._subCache.length; zz++) {
699
- _this._nodeCache.del(_this._subCache[zz].id);
700
- if (_this._nodeCache.getStats().vsize < 500000000) {
701
- neededSizeIndex = zz;
702
- break;
703
- }
704
- }
705
- // console.log('Cache1: ' + 'Too Big - ' + subscription.publication + ' - ' + getBinarySize(JSON.stringify(this._subCache)));
706
- _this._subCache.splice(0, neededSizeIndex);
707
- }
708
- for (var i = _this._subCache.length - 1; i >= 0; i--) {
709
- if (_this._subCache[i].publication === subscription.publication && JSON.stringify(_this._subCache[i].subscriptionData) === JSON.stringify(subscription.subscriptionData)) {
710
- _this._nodeCache.del(_this._subCache[i].id);
711
- _this._subCache.splice(i, 1);
712
- }
713
- }
714
- if (_this._subscriptions.some(function (a) { return a.collections.includes(collection) && a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); })) {
715
- var cacheId = _this._cacheId++;
716
- _this._nodeCache.set(cacheId, res);
717
- _this._subCache.push({ collection: collection, publication: subscription.publication, subscriptionData: subscription.subscriptionData, id: cacheId });
718
- var serverRes = {
719
- messageId: messageId,
720
- hasError: false,
721
- data: res
722
- };
723
- _this.sendWS(ws, serverRes);
724
- }
725
- resolve(true);
726
- }, function (err) {
727
- var serverRes = {
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 = {
728
533
  messageId: messageId,
729
- hasError: true,
730
- data: err
534
+ hasError: false,
535
+ data: res
731
536
  };
732
- _this.sendWS(ws, serverRes);
733
- reject(err);
734
- });
735
- }
736
- }
737
- else { // Pub is different for each user (Look at users publication) [MUCH SLOWER, RE-RUNS FOR EACH USER]
738
- (_b = that._publications[subscription.publication].function).call.apply(_b, __spreadArrays([Object.assign({}, that, SubscriptionManager.prototype), ws['id_user']], subscription.subscriptionData)).then(function (res) {
739
- var serverRes = {
740
- messageId: messageId,
741
- hasError: false,
742
- data: res
743
- };
744
- // console.log('Send Pub Data Once 2', new Date(), 'Success', subscription);
745
- _this.sendWS(ws, serverRes);
537
+ this.sendWS(ws, serverRes);
538
+ }
746
539
  resolve(true);
747
- }, function (err) {
748
- var serverRes = {
540
+ }, err => {
541
+ let serverRes = {
749
542
  messageId: messageId,
750
543
  hasError: true,
751
544
  data: err
752
545
  };
753
- // console.log('Send Pub Data Once 2', new Date(), 'Error', subscription);
754
- _this.sendWS(ws, serverRes);
546
+ this.sendWS(ws, serverRes);
755
547
  reject(err);
756
548
  });
757
549
  }
758
- return [2 /*return*/];
759
- });
760
- }); });
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
576
  // Fetch pub once, send to all clients linked to this pub
763
- SubscriptionManager.prototype.sendPubData = function (subscription, type, collection) {
764
- var _this = this;
765
- return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
766
- var that, i, i;
767
- var _a;
768
- var _this = this;
769
- return __generator(this, function (_b) {
770
- that = this;
771
- if (!subscription.clients.length) {
772
- for (i = this._subCache.length - 1; i >= 0; i--) {
773
- if (this._subCache[i].publication === subscription.publication && JSON.stringify(this._subCache[i].subscriptionData) === JSON.stringify(subscription.subscriptionData)) {
774
- this._nodeCache.del(this._subCache[i].id);
775
- this._subCache.splice(i, 1);
776
- }
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);
777
585
  }
778
- if (that._subscriptions.findIndex(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); }) >= 0) {
779
- that._subscriptions.splice(that._subscriptions.findIndex(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); }), 1);
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);
780
599
  }
781
600
  }
782
- else {
783
- if (subscription.publication !== 'superadminAPM' && subscription.publication !== 'loggedInUsers') {
784
- that._mainServer.getMethodManager().callMethodInternal('insertSubscriptionLog', type, subscription.publication, collection, JSON.stringify(subscription.subscriptionData));
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);
785
616
  }
786
- for (i = this._subCache.length - 1; i >= 0; i--) {
617
+ for (let i = this._subCache.length - 1; i >= 0; i--) {
787
618
  if (this._subCache[i].publication === subscription.publication && JSON.stringify(this._subCache[i].subscriptionData) === JSON.stringify(subscription.subscriptionData)) {
788
619
  this._nodeCache.del(this._subCache[i].id);
789
620
  this._subCache.splice(i, 1);
790
621
  }
791
622
  }
792
- (_a = that._publications[subscription.publication].function).call.apply(_a, __spreadArrays([Object.assign({}, that, SubscriptionManager.prototype)], subscription.subscriptionData)).then(function (res) {
793
- var nodeCacheSize = _this._nodeCache.getStats().vsize;
794
- if (nodeCacheSize > 500000000) {
795
- var neededSizeIndex = 0;
796
- for (var zz = 0; zz < _this._subCache.length; zz++) {
797
- _this._nodeCache.del(_this._subCache[zz].id);
798
- if (_this._nodeCache.getStats().vsize < 500000000) {
799
- neededSizeIndex = zz;
800
- break;
801
- }
802
- }
803
- // console.log('Cache: ' + 'Too Big - ' + subscription.publication + ' - ' + getBinarySize(JSON.stringify(this._subCache)));
804
- _this._subCache.splice(0, neededSizeIndex);
805
- }
806
- for (var i = _this._subCache.length - 1; i >= 0; i--) {
807
- if (_this._subCache[i].publication === subscription.publication && JSON.stringify(_this._subCache[i].subscriptionData) === JSON.stringify(subscription.subscriptionData)) {
808
- _this._nodeCache.del(_this._subCache[i].id);
809
- _this._subCache.splice(i, 1);
810
- }
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);
811
626
  }
812
- if (!subscription.clients.length) {
813
- if (that._subscriptions.findIndex(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); }) >= 0) {
814
- that._subscriptions.splice(that._subscriptions.findIndex(function (a) { return a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); }), 1);
815
- }
816
- }
817
- else {
818
- if (_this._subscriptions.some(function (a) { return a.collections.includes(collection) && a.publication === subscription.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscription.subscriptionData); })) {
819
- var cacheId = _this._cacheId++;
820
- _this._nodeCache.set(cacheId, res);
821
- _this._subCache.push({ collection: collection, publication: subscription.publication, subscriptionData: subscription.subscriptionData, id: cacheId });
822
- subscription.clients.forEach(function (client) {
823
- that._wss.clients.forEach(function (ws) {
824
- if (ws['id_socket'] === client.id_socket) {
825
- var serverRes = {
826
- messageId: client.messageId,
827
- hasError: false,
828
- data: res
829
- };
830
- _this.sendWS(ws, serverRes);
831
- }
832
- });
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
+ }
833
643
  });
834
- }
835
- }
836
- resolve(true);
837
- }, function (err) {
838
- subscription.clients.forEach(function (client) {
839
- that._wss.clients.forEach(function (ws) {
840
- if (ws['id_socket'] === client.id_socket) {
841
- var serverRes = {
842
- messageId: client.messageId,
843
- hasError: true,
844
- data: err
845
- };
846
- _this.sendWS(ws, serverRes);
847
- }
848
644
  });
645
+ }
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);
658
+ }
849
659
  });
850
- reject(err);
851
660
  });
852
- }
853
- return [2 /*return*/];
854
- });
855
- }); });
856
- };
857
- SubscriptionManager.prototype.sendWS = function (ws, data) {
661
+ reject(err);
662
+ });
663
+ }
664
+ });
665
+ }
666
+ sendWS(ws, data) {
858
667
  this._sendQueue.splice(0, 0, {
859
668
  id_ws: ws['id_socket'],
860
669
  data: data
861
670
  });
862
- };
863
- return SubscriptionManager;
864
- }());
865
- exports.SubscriptionManager = SubscriptionManager;
671
+ }
672
+ }
866
673
 
867
674
  //# sourceMappingURL=subscription.manager.js.map