@resolveio/server-lib 22.2.34 → 22.2.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 (649) hide show
  1. package/ai/assistant-core-heuristics.d.ts +11 -0
  2. package/ai/assistant-core-heuristics.js +531 -0
  3. package/ai/assistant-core-heuristics.js.map +1 -0
  4. package/client-server-app.d.ts +1 -0
  5. package/client-server-app.js +68 -0
  6. package/client-server-app.js.map +1 -0
  7. package/collections/ai-terminal-conversation.collection.d.ts +2 -0
  8. package/collections/ai-terminal-conversation.collection.js +140 -0
  9. package/collections/ai-terminal-conversation.collection.js.map +1 -0
  10. package/collections/ai-terminal-issue-report.collection.d.ts +2 -0
  11. package/collections/ai-terminal-issue-report.collection.js +148 -0
  12. package/collections/ai-terminal-issue-report.collection.js.map +1 -0
  13. package/collections/ai-terminal-message.collection.d.ts +2 -0
  14. package/collections/ai-terminal-message.collection.js +121 -0
  15. package/collections/ai-terminal-message.collection.js.map +1 -0
  16. package/collections/app-setting.collection.d.ts +3 -0
  17. package/collections/app-setting.collection.js +103 -0
  18. package/collections/app-setting.collection.js.map +1 -0
  19. package/collections/app-status.collection.d.ts +3 -0
  20. package/collections/app-status.collection.js +57 -0
  21. package/collections/app-status.collection.js.map +1 -0
  22. package/collections/communication-metric.collection.d.ts +2 -0
  23. package/collections/communication-metric.collection.js +133 -0
  24. package/collections/communication-metric.collection.js.map +1 -0
  25. package/collections/counter.collection.d.ts +3 -0
  26. package/collections/counter.collection.js +56 -0
  27. package/collections/counter.collection.js.map +1 -0
  28. package/collections/cron-job-history.collection.d.ts +3 -0
  29. package/collections/cron-job-history.collection.js +137 -0
  30. package/collections/cron-job-history.collection.js.map +1 -0
  31. package/collections/cron-job.collection.d.ts +3 -0
  32. package/collections/cron-job.collection.js +92 -0
  33. package/collections/cron-job.collection.js.map +1 -0
  34. package/collections/customer-notification.collection.d.ts +3 -0
  35. package/collections/customer-notification.collection.js +130 -0
  36. package/collections/customer-notification.collection.js.map +1 -0
  37. package/collections/customer-portal-password.collection.d.ts +3 -0
  38. package/collections/customer-portal-password.collection.js +75 -0
  39. package/collections/customer-portal-password.collection.js.map +1 -0
  40. package/collections/email-history.collection.d.ts +3 -0
  41. package/collections/email-history.collection.js +121 -0
  42. package/collections/email-history.collection.js.map +1 -0
  43. package/collections/email-verified.collection.d.ts +3 -0
  44. package/collections/email-verified.collection.js +61 -0
  45. package/collections/email-verified.collection.js.map +1 -0
  46. package/collections/file.collection.d.ts +3 -0
  47. package/collections/file.collection.js +74 -0
  48. package/collections/file.collection.js.map +1 -0
  49. package/collections/flag-update.collection.d.ts +3 -0
  50. package/collections/flag-update.collection.js +57 -0
  51. package/collections/flag-update.collection.js.map +1 -0
  52. package/collections/flag.collection.d.ts +3 -0
  53. package/collections/flag.collection.js +57 -0
  54. package/collections/flag.collection.js.map +1 -0
  55. package/collections/log-method-latency.collection.d.ts +3 -0
  56. package/collections/log-method-latency.collection.js +77 -0
  57. package/collections/log-method-latency.collection.js.map +1 -0
  58. package/collections/log-subscription.collection.d.ts +3 -0
  59. package/collections/log-subscription.collection.js +80 -0
  60. package/collections/log-subscription.collection.js.map +1 -0
  61. package/collections/log.collection.d.ts +3 -0
  62. package/collections/log.collection.js +93 -0
  63. package/collections/log.collection.js.map +1 -0
  64. package/collections/logged-in-users.collection.d.ts +3 -0
  65. package/collections/logged-in-users.collection.js +67 -0
  66. package/collections/logged-in-users.collection.js.map +1 -0
  67. package/collections/monitor-cpu.collection.d.ts +3 -0
  68. package/collections/monitor-cpu.collection.js +65 -0
  69. package/collections/monitor-cpu.collection.js.map +1 -0
  70. package/collections/monitor-function.collection.d.ts +3 -0
  71. package/collections/monitor-function.collection.js +74 -0
  72. package/collections/monitor-function.collection.js.map +1 -0
  73. package/collections/monitor-memory.collection.d.ts +3 -0
  74. package/collections/monitor-memory.collection.js +77 -0
  75. package/collections/monitor-memory.collection.js.map +1 -0
  76. package/collections/monitor-mongo.collection.d.ts +3 -0
  77. package/collections/monitor-mongo.collection.js +71 -0
  78. package/collections/monitor-mongo.collection.js.map +1 -0
  79. package/collections/notification.collection.d.ts +3 -0
  80. package/collections/notification.collection.js +57 -0
  81. package/collections/notification.collection.js.map +1 -0
  82. package/collections/openai-usage-ledger.collection.d.ts +2 -0
  83. package/collections/openai-usage-ledger.collection.js +124 -0
  84. package/collections/openai-usage-ledger.collection.js.map +1 -0
  85. package/collections/report-builder-dashboard-builder.collection.d.ts +3 -0
  86. package/collections/report-builder-dashboard-builder.collection.js +109 -0
  87. package/collections/report-builder-dashboard-builder.collection.js.map +1 -0
  88. package/collections/report-builder-library.collection.d.ts +3 -0
  89. package/collections/report-builder-library.collection.js +87 -0
  90. package/collections/report-builder-library.collection.js.map +1 -0
  91. package/collections/report-builder-report.collection.d.ts +4 -0
  92. package/collections/report-builder-report.collection.js +180 -0
  93. package/collections/report-builder-report.collection.js.map +1 -0
  94. package/collections/user-group.collection.d.ts +4 -0
  95. package/collections/user-group.collection.js +89 -0
  96. package/collections/user-group.collection.js.map +1 -0
  97. package/collections/user-guide.collection.d.ts +3 -0
  98. package/collections/user-guide.collection.js +57 -0
  99. package/collections/user-guide.collection.js.map +1 -0
  100. package/collections/user.collection.d.ts +4 -0
  101. package/collections/user.collection.js +180 -0
  102. package/collections/user.collection.js.map +1 -0
  103. package/cron/cron.d.ts +14 -0
  104. package/cron/cron.js +216 -0
  105. package/cron/cron.js.map +1 -0
  106. package/fixtures/cron-jobs.d.ts +1 -0
  107. package/fixtures/cron-jobs.js +150 -0
  108. package/fixtures/cron-jobs.js.map +1 -0
  109. package/fixtures/init.d.ts +1 -0
  110. package/fixtures/init.js +91 -0
  111. package/fixtures/init.js.map +1 -0
  112. package/http/auth.d.ts +2 -0
  113. package/http/auth.js +903 -0
  114. package/http/auth.js.map +1 -0
  115. package/http/health.d.ts +1 -0
  116. package/http/health.js +11 -0
  117. package/http/health.js.map +1 -0
  118. package/http/home.d.ts +1 -0
  119. package/http/home.js +134 -0
  120. package/http/home.js.map +1 -0
  121. package/http/slow-query-publication.d.ts +2 -0
  122. package/http/slow-query-publication.js +99 -0
  123. package/http/slow-query-publication.js.map +1 -0
  124. package/index.d.ts +1 -0
  125. package/index.js +19 -0
  126. package/index.js.map +1 -0
  127. package/managers/communication-metric.manager.d.ts +16 -0
  128. package/managers/communication-metric.manager.js +134 -0
  129. package/managers/communication-metric.manager.js.map +1 -0
  130. package/managers/cron.manager.d.ts +20 -0
  131. package/managers/cron.manager.js +534 -0
  132. package/managers/cron.manager.js.map +1 -0
  133. package/managers/customer-notification-content.manager.d.ts +55 -0
  134. package/managers/customer-notification-content.manager.js +158 -0
  135. package/managers/customer-notification-content.manager.js.map +1 -0
  136. package/managers/diagnostic-manager-bootstrap.d.ts +9 -0
  137. package/managers/diagnostic-manager-bootstrap.js +260 -0
  138. package/managers/diagnostic-manager-bootstrap.js.map +1 -0
  139. package/managers/error-auto-fix.manager.d.ts +149 -0
  140. package/managers/error-auto-fix.manager.js +3064 -0
  141. package/managers/error-auto-fix.manager.js.map +1 -0
  142. package/managers/local-log.manager.d.ts +18 -0
  143. package/managers/local-log.manager.js +88 -0
  144. package/managers/local-log.manager.js.map +1 -0
  145. package/managers/method.manager.d.ts +77 -0
  146. package/managers/method.manager.js +1701 -0
  147. package/managers/method.manager.js.map +1 -0
  148. package/managers/mongo.manager.d.ts +222 -0
  149. package/managers/mongo.manager.js +4984 -0
  150. package/managers/mongo.manager.js.map +1 -0
  151. package/managers/monitor.manager.d.ts +69 -0
  152. package/managers/monitor.manager.js +534 -0
  153. package/managers/monitor.manager.js.map +1 -0
  154. package/managers/openai-usage-ledger.manager.d.ts +15 -0
  155. package/managers/openai-usage-ledger.manager.js +144 -0
  156. package/managers/openai-usage-ledger.manager.js.map +1 -0
  157. package/managers/slow-query-verifier.manager.d.ts +144 -0
  158. package/managers/slow-query-verifier.manager.js +3857 -0
  159. package/managers/slow-query-verifier.manager.js.map +1 -0
  160. package/managers/slow-query.manager.d.ts +28 -0
  161. package/managers/slow-query.manager.js +468 -0
  162. package/managers/slow-query.manager.js.map +1 -0
  163. package/managers/subscription.manager.d.ts +169 -0
  164. package/managers/subscription.manager.js +3422 -0
  165. package/managers/subscription.manager.js.map +1 -0
  166. package/managers/websocket.manager.d.ts +73 -0
  167. package/managers/websocket.manager.js +673 -0
  168. package/managers/websocket.manager.js.map +1 -0
  169. package/managers/worker-dispatcher.manager.d.ts +117 -0
  170. package/managers/worker-dispatcher.manager.js +1210 -0
  171. package/managers/worker-dispatcher.manager.js.map +1 -0
  172. package/managers/worker-server.manager.d.ts +16 -0
  173. package/managers/worker-server.manager.js +530 -0
  174. package/managers/worker-server.manager.js.map +1 -0
  175. package/methods/accounts.d.ts +2 -0
  176. package/methods/accounts.js +624 -0
  177. package/methods/accounts.js.map +1 -0
  178. package/methods/ai-terminal.d.ts +304 -0
  179. package/methods/ai-terminal.js +25096 -0
  180. package/methods/ai-terminal.js.map +1 -0
  181. package/methods/app-settings.d.ts +2 -0
  182. package/methods/app-settings.js +169 -0
  183. package/methods/app-settings.js.map +1 -0
  184. package/methods/aws.d.ts +2 -0
  185. package/methods/aws.js +874 -0
  186. package/methods/aws.js.map +1 -0
  187. package/methods/collections.d.ts +2 -0
  188. package/methods/collections.js +626 -0
  189. package/methods/collections.js.map +1 -0
  190. package/methods/counters.d.ts +2 -0
  191. package/methods/counters.js +111 -0
  192. package/methods/counters.js.map +1 -0
  193. package/methods/cron-jobs.d.ts +2 -0
  194. package/methods/cron-jobs.js +2471 -0
  195. package/methods/cron-jobs.js.map +1 -0
  196. package/methods/customer-notifications.d.ts +2 -0
  197. package/methods/customer-notifications.js +528 -0
  198. package/methods/customer-notifications.js.map +1 -0
  199. package/methods/diagnostics.d.ts +2 -0
  200. package/methods/diagnostics.js +514 -0
  201. package/methods/diagnostics.js.map +1 -0
  202. package/methods/flag-updates.d.ts +2 -0
  203. package/methods/flag-updates.js +8 -0
  204. package/methods/flag-updates.js.map +1 -0
  205. package/methods/flags.d.ts +2 -0
  206. package/methods/flags.js +8 -0
  207. package/methods/flags.js.map +1 -0
  208. package/methods/logs.d.ts +2 -0
  209. package/methods/logs.js +750 -0
  210. package/methods/logs.js.map +1 -0
  211. package/methods/mongo-explorer.d.ts +2 -0
  212. package/methods/mongo-explorer.js +1811 -0
  213. package/methods/mongo-explorer.js.map +1 -0
  214. package/methods/monitor.d.ts +2 -0
  215. package/methods/monitor.js +543 -0
  216. package/methods/monitor.js.map +1 -0
  217. package/methods/pdf.d.ts +2 -0
  218. package/methods/pdf.js +1195 -0
  219. package/methods/pdf.js.map +1 -0
  220. package/methods/publications.d.ts +1 -0
  221. package/methods/publications.js +183 -0
  222. package/methods/publications.js.map +1 -0
  223. package/methods/report-builder.d.ts +2 -0
  224. package/methods/report-builder.js +3099 -0
  225. package/methods/report-builder.js.map +1 -0
  226. package/methods/support.d.ts +2 -0
  227. package/methods/support.js +322 -0
  228. package/methods/support.js.map +1 -0
  229. package/models/ai-terminal-conversation.model.d.ts +17 -0
  230. package/models/ai-terminal-conversation.model.js +4 -0
  231. package/models/ai-terminal-conversation.model.js.map +1 -0
  232. package/models/ai-terminal-issue-report.model.d.ts +19 -0
  233. package/models/ai-terminal-issue-report.model.js +4 -0
  234. package/models/ai-terminal-issue-report.model.js.map +1 -0
  235. package/models/ai-terminal-message.model.d.ts +22 -0
  236. package/models/ai-terminal-message.model.js +4 -0
  237. package/models/ai-terminal-message.model.js.map +1 -0
  238. package/models/app-setting.model.d.ts +16 -0
  239. package/models/app-setting.model.js +4 -0
  240. package/models/app-setting.model.js.map +1 -0
  241. package/{src/models/app-status.model.ts → models/app-status.model.d.ts} +2 -3
  242. package/models/app-status.model.js +4 -0
  243. package/models/app-status.model.js.map +1 -0
  244. package/{src/models/billing-logged-in-users.model.ts → models/billing-logged-in-users.model.d.ts} +4 -5
  245. package/models/billing-logged-in-users.model.js +4 -0
  246. package/models/billing-logged-in-users.model.js.map +1 -0
  247. package/models/collection-document.model.d.ts +21 -0
  248. package/models/collection-document.model.js +4 -0
  249. package/models/collection-document.model.js.map +1 -0
  250. package/models/communication-metric.model.d.ts +20 -0
  251. package/models/communication-metric.model.js +4 -0
  252. package/models/communication-metric.model.js.map +1 -0
  253. package/{src/models/counter.model.ts → models/counter.model.d.ts} +3 -4
  254. package/models/counter.model.js +4 -0
  255. package/models/counter.model.js.map +1 -0
  256. package/models/cron-job-history.model.d.ts +15 -0
  257. package/models/cron-job-history.model.js +4 -0
  258. package/models/cron-job-history.model.js.map +1 -0
  259. package/models/cron-job.model.d.ts +14 -0
  260. package/models/cron-job.model.js +4 -0
  261. package/models/cron-job.model.js.map +1 -0
  262. package/models/customer-notification.model.d.ts +26 -0
  263. package/models/customer-notification.model.js +4 -0
  264. package/models/customer-notification.model.js.map +1 -0
  265. package/models/customer-portal-password.model.d.ts +11 -0
  266. package/models/customer-portal-password.model.js +4 -0
  267. package/models/customer-portal-password.model.js.map +1 -0
  268. package/models/dialog.model.d.ts +23 -0
  269. package/models/dialog.model.js +4 -0
  270. package/models/dialog.model.js.map +1 -0
  271. package/models/email-history.model.d.ts +30 -0
  272. package/{src/models/email-history.model.ts → models/email-history.model.js} +4 -34
  273. package/models/email-history.model.js.map +1 -0
  274. package/{src/models/email-verified.model.ts → models/email-verified.model.d.ts} +5 -6
  275. package/models/email-verified.model.js +4 -0
  276. package/models/email-verified.model.js.map +1 -0
  277. package/{src/models/file.model.ts → models/file.model.d.ts} +7 -8
  278. package/models/file.model.js +4 -0
  279. package/models/file.model.js.map +1 -0
  280. package/{src/models/flag-update.model.ts → models/flag-update.model.d.ts} +3 -4
  281. package/models/flag-update.model.js +4 -0
  282. package/models/flag-update.model.js.map +1 -0
  283. package/{src/models/flag.model.ts → models/flag.model.d.ts} +3 -4
  284. package/models/flag.model.js +4 -0
  285. package/models/flag.model.js.map +1 -0
  286. package/models/log-method-latency.model.d.ts +10 -0
  287. package/models/log-method-latency.model.js +4 -0
  288. package/models/log-method-latency.model.js.map +1 -0
  289. package/{src/models/log-subscription.model.ts → models/log-subscription.model.d.ts} +9 -11
  290. package/models/log-subscription.model.js +4 -0
  291. package/models/log-subscription.model.js.map +1 -0
  292. package/models/log.model.d.ts +17 -0
  293. package/models/log.model.js +4 -0
  294. package/models/log.model.js.map +1 -0
  295. package/{src/models/logged-in-users.model.ts → models/logged-in-users.model.d.ts} +5 -6
  296. package/models/logged-in-users.model.js +4 -0
  297. package/models/logged-in-users.model.js.map +1 -0
  298. package/{src/models/method-response.model.ts → models/method-response.model.d.ts} +6 -7
  299. package/models/method-response.model.js +4 -0
  300. package/models/method-response.model.js.map +1 -0
  301. package/models/method.model.d.ts +24 -0
  302. package/models/method.model.js +4 -0
  303. package/models/method.model.js.map +1 -0
  304. package/{src/models/monitor-cpu.model.ts → models/monitor-cpu.model.d.ts} +7 -9
  305. package/models/monitor-cpu.model.js +4 -0
  306. package/models/monitor-cpu.model.js.map +1 -0
  307. package/models/monitor-function.model.d.ts +14 -0
  308. package/models/monitor-function.model.js +4 -0
  309. package/models/monitor-function.model.js.map +1 -0
  310. package/models/monitor-memory.model.d.ts +15 -0
  311. package/models/monitor-memory.model.js +4 -0
  312. package/models/monitor-memory.model.js.map +1 -0
  313. package/models/monitor-mongo.model.d.ts +13 -0
  314. package/models/monitor-mongo.model.js +4 -0
  315. package/models/monitor-mongo.model.js.map +1 -0
  316. package/{src/models/notification.model.ts → models/notification.model.d.ts} +4 -6
  317. package/models/notification.model.js +4 -0
  318. package/models/notification.model.js.map +1 -0
  319. package/models/openai-usage-ledger.model.d.ts +15 -0
  320. package/models/openai-usage-ledger.model.js +4 -0
  321. package/models/openai-usage-ledger.model.js.map +1 -0
  322. package/models/pagination.model.d.ts +11 -0
  323. package/models/pagination.model.js +28 -0
  324. package/models/pagination.model.js.map +1 -0
  325. package/models/permission.model.d.ts +12 -0
  326. package/models/permission.model.js +4 -0
  327. package/models/permission.model.js.map +1 -0
  328. package/models/report-builder-dashboard-builder.model.d.ts +25 -0
  329. package/models/report-builder-dashboard-builder.model.js +4 -0
  330. package/models/report-builder-dashboard-builder.model.js.map +1 -0
  331. package/models/report-builder-library.model.d.ts +17 -0
  332. package/models/report-builder-library.model.js +4 -0
  333. package/models/report-builder-library.model.js.map +1 -0
  334. package/models/report-builder-report.model.d.ts +120 -0
  335. package/models/report-builder-report.model.js +4 -0
  336. package/models/report-builder-report.model.js.map +1 -0
  337. package/models/report-builder.model.d.ts +61 -0
  338. package/models/report-builder.model.js +4 -0
  339. package/models/report-builder.model.js.map +1 -0
  340. package/models/select-data-label.model.d.ts +9 -0
  341. package/models/select-data-label.model.js +4 -0
  342. package/models/select-data-label.model.js.map +1 -0
  343. package/models/server-message.model.d.ts +32 -0
  344. package/models/server-message.model.js +4 -0
  345. package/models/server-message.model.js.map +1 -0
  346. package/models/slow-query-report.model.d.ts +23 -0
  347. package/models/slow-query-report.model.js +4 -0
  348. package/models/slow-query-report.model.js.map +1 -0
  349. package/models/subscription.model.d.ts +31 -0
  350. package/models/subscription.model.js +4 -0
  351. package/models/subscription.model.js.map +1 -0
  352. package/models/support-ticket.model.d.ts +87 -0
  353. package/models/support-ticket.model.js +4 -0
  354. package/models/support-ticket.model.js.map +1 -0
  355. package/models/user-group.model.d.ts +20 -0
  356. package/models/user-group.model.js +4 -0
  357. package/models/user-group.model.js.map +1 -0
  358. package/{src/models/user-guide.model.ts → models/user-guide.model.d.ts} +4 -5
  359. package/models/user-guide.model.js +4 -0
  360. package/models/user-guide.model.js.map +1 -0
  361. package/models/user.model.d.ts +84 -0
  362. package/models/user.model.js +4 -0
  363. package/models/user.model.js.map +1 -0
  364. package/package.json +1 -1
  365. package/private/images/ResolveIO.png +0 -0
  366. package/public_api.js +107 -0
  367. package/public_api.js.map +1 -0
  368. package/publications/ai-terminal.d.ts +1 -0
  369. package/publications/ai-terminal.js +122 -0
  370. package/publications/ai-terminal.js.map +1 -0
  371. package/publications/app-settings.d.ts +2 -0
  372. package/publications/app-settings.js +28 -0
  373. package/publications/app-settings.js.map +1 -0
  374. package/publications/app-status.d.ts +2 -0
  375. package/publications/app-status.js +16 -0
  376. package/publications/app-status.js.map +1 -0
  377. package/publications/cron-jobs.d.ts +2 -0
  378. package/publications/cron-jobs.js +32 -0
  379. package/publications/cron-jobs.js.map +1 -0
  380. package/publications/customer-notifications.d.ts +2 -0
  381. package/publications/customer-notifications.js +161 -0
  382. package/publications/customer-notifications.js.map +1 -0
  383. package/publications/files.d.ts +2 -0
  384. package/publications/files.js +36 -0
  385. package/publications/files.js.map +1 -0
  386. package/publications/flags-update.d.ts +2 -0
  387. package/publications/flags-update.js +22 -0
  388. package/publications/flags-update.js.map +1 -0
  389. package/publications/flags.d.ts +2 -0
  390. package/publications/flags.js +22 -0
  391. package/publications/flags.js.map +1 -0
  392. package/publications/logs.d.ts +2 -0
  393. package/publications/logs.js +164 -0
  394. package/publications/logs.js.map +1 -0
  395. package/publications/notifications.d.ts +2 -0
  396. package/publications/notifications.js +16 -0
  397. package/publications/notifications.js.map +1 -0
  398. package/publications/report-builder-dashboard-builders.d.ts +2 -0
  399. package/publications/report-builder-dashboard-builders.js +42 -0
  400. package/publications/report-builder-dashboard-builders.js.map +1 -0
  401. package/publications/report-builder-libraries.d.ts +2 -0
  402. package/publications/report-builder-libraries.js +90 -0
  403. package/publications/report-builder-libraries.js.map +1 -0
  404. package/publications/report-builder-reports.d.ts +2 -0
  405. package/publications/report-builder-reports.js +50 -0
  406. package/publications/report-builder-reports.js.map +1 -0
  407. package/publications/super-admin.d.ts +2 -0
  408. package/publications/super-admin.js +16 -0
  409. package/publications/super-admin.js.map +1 -0
  410. package/publications/user-groups.d.ts +1 -0
  411. package/publications/user-groups.js +16 -0
  412. package/publications/user-groups.js.map +1 -0
  413. package/publications/user-guides.d.ts +1 -0
  414. package/publications/user-guides.js +16 -0
  415. package/publications/user-guides.js.map +1 -0
  416. package/resolveio-server-app.d.ts +70 -0
  417. package/resolveio-server-app.js +801 -0
  418. package/resolveio-server-app.js.map +1 -0
  419. package/server-app.d.ts +167 -0
  420. package/server-app.js +2784 -0
  421. package/server-app.js.map +1 -0
  422. package/services/codex-client.d.ts +119 -0
  423. package/services/codex-client.js +1470 -0
  424. package/services/codex-client.js.map +1 -0
  425. package/services/openai-client.d.ts +46 -0
  426. package/services/openai-client.js +318 -0
  427. package/services/openai-client.js.map +1 -0
  428. package/types/error-report.d.ts +25 -0
  429. package/types/error-report.js +4 -0
  430. package/types/error-report.js.map +1 -0
  431. package/types/slow-query-report.d.ts +27 -0
  432. package/types/slow-query-report.js +6 -0
  433. package/types/slow-query-report.js.map +1 -0
  434. package/util/common.d.ts +31 -0
  435. package/util/common.js +683 -0
  436. package/util/common.js.map +1 -0
  437. package/util/customer-portal-password.d.ts +13 -0
  438. package/util/customer-portal-password.js +209 -0
  439. package/util/customer-portal-password.js.map +1 -0
  440. package/util/error-reporter.d.ts +52 -0
  441. package/util/error-reporter.js +326 -0
  442. package/util/error-reporter.js.map +1 -0
  443. package/util/error-tracking.d.ts +13 -0
  444. package/util/error-tracking.js +120 -0
  445. package/util/error-tracking.js.map +1 -0
  446. package/util/report-builder-unwinds.d.ts +15 -0
  447. package/util/report-builder-unwinds.js +156 -0
  448. package/util/report-builder-unwinds.js.map +1 -0
  449. package/util/schema-report-builder.d.ts +6 -0
  450. package/util/schema-report-builder.js +481 -0
  451. package/util/schema-report-builder.js.map +1 -0
  452. package/util/slow-query-reporter.d.ts +28 -0
  453. package/util/slow-query-reporter.js +226 -0
  454. package/util/slow-query-reporter.js.map +1 -0
  455. package/util/subscription-dependency-context.d.ts +34 -0
  456. package/util/subscription-dependency-context.js +1283 -0
  457. package/util/subscription-dependency-context.js.map +1 -0
  458. package/util/tokenizer.d.ts +5 -0
  459. package/util/tokenizer.js +41 -0
  460. package/util/tokenizer.js.map +1 -0
  461. package/workers/codex-runner.worker.d.ts +1 -0
  462. package/workers/codex-runner.worker.js +192 -0
  463. package/workers/codex-runner.worker.js.map +1 -0
  464. package/.github/workflows/ai-assistant-nightly-eval.yml +0 -224
  465. package/.github/workflows/ai-assistant-pr-guardrails.yml +0 -60
  466. package/.nodemon.json +0 -5
  467. package/.vscode/settings.json +0 -21
  468. package/AGENTS.md +0 -179
  469. package/README.md +0 -22
  470. package/build_package.sh +0 -5
  471. package/compileDTS.pl +0 -64
  472. package/docs/ai-assistant-nightly-eval.md +0 -65
  473. package/docs/ai-assistant-preflight-checklist.md +0 -23
  474. package/docs/ai-assistant-report-builder-bridge-playbook.md +0 -115
  475. package/eslint-plugin-custom/index.js +0 -7
  476. package/eslint-plugin-custom/rules/no-filter-zero-index.js +0 -44
  477. package/eslint.config.js +0 -103
  478. package/gulpfile.js +0 -216
  479. package/methodAndPublicationListGenerator.py +0 -319
  480. package/mongodbensurers.js +0 -2
  481. package/mongostop.js +0 -3
  482. package/settings.development.json +0 -25
  483. package/settings.development.redacted.json +0 -25
  484. package/src/.env +0 -12
  485. package/src/ai/assistant-core-heuristics.ts +0 -577
  486. package/src/client-server-app.ts +0 -12
  487. package/src/collections/ai-terminal-conversation.collection.ts +0 -91
  488. package/src/collections/ai-terminal-issue-report.collection.ts +0 -99
  489. package/src/collections/ai-terminal-message.collection.ts +0 -77
  490. package/src/collections/app-setting.collection.ts +0 -104
  491. package/src/collections/app-status.collection.ts +0 -58
  492. package/src/collections/communication-metric.collection.ts +0 -84
  493. package/src/collections/counter.collection.ts +0 -56
  494. package/src/collections/cron-job-history.collection.ts +0 -94
  495. package/src/collections/cron-job.collection.ts +0 -92
  496. package/src/collections/customer-notification.collection.ts +0 -131
  497. package/src/collections/customer-portal-password.collection.ts +0 -76
  498. package/src/collections/email-history.collection.ts +0 -121
  499. package/src/collections/email-verified.collection.ts +0 -61
  500. package/src/collections/file.collection.ts +0 -74
  501. package/src/collections/flag-update.collection.ts +0 -57
  502. package/src/collections/flag.collection.ts +0 -57
  503. package/src/collections/log-method-latency.collection.ts +0 -77
  504. package/src/collections/log-subscription.collection.ts +0 -80
  505. package/src/collections/log.collection.ts +0 -93
  506. package/src/collections/logged-in-users.collection.ts +0 -67
  507. package/src/collections/monitor-cpu.collection.ts +0 -65
  508. package/src/collections/monitor-function.collection.ts +0 -74
  509. package/src/collections/monitor-memory.collection.ts +0 -77
  510. package/src/collections/monitor-mongo.collection.ts +0 -71
  511. package/src/collections/notification.collection.ts +0 -57
  512. package/src/collections/openai-usage-ledger.collection.ts +0 -77
  513. package/src/collections/report-builder-dashboard-builder.collection.ts +0 -109
  514. package/src/collections/report-builder-library.collection.ts +0 -89
  515. package/src/collections/report-builder-report.collection.ts +0 -180
  516. package/src/collections/user-group.collection.ts +0 -89
  517. package/src/collections/user-guide.collection.ts +0 -57
  518. package/src/collections/user.collection.ts +0 -181
  519. package/src/cron/cron.ts +0 -117
  520. package/src/fixtures/cron-jobs.ts +0 -95
  521. package/src/fixtures/init.ts +0 -35
  522. package/src/http/auth.ts +0 -764
  523. package/src/http/health.ts +0 -7
  524. package/src/http/home.ts +0 -90
  525. package/src/http/slow-query-publication.ts +0 -49
  526. package/src/index.ts +0 -1
  527. package/src/managers/communication-metric.manager.ts +0 -82
  528. package/src/managers/cron.manager.ts +0 -333
  529. package/src/managers/customer-notification-content.manager.ts +0 -236
  530. package/src/managers/diagnostic-manager-bootstrap.ts +0 -165
  531. package/src/managers/error-auto-fix.manager.ts +0 -2767
  532. package/src/managers/local-log.manager.ts +0 -113
  533. package/src/managers/method.manager.ts +0 -1557
  534. package/src/managers/mongo.manager.ts +0 -4566
  535. package/src/managers/monitor.manager.ts +0 -489
  536. package/src/managers/openai-usage-ledger.manager.ts +0 -116
  537. package/src/managers/slow-query-verifier.manager.ts +0 -3590
  538. package/src/managers/slow-query.manager.ts +0 -519
  539. package/src/managers/subscription.manager.ts +0 -3120
  540. package/src/managers/websocket.manager.ts +0 -746
  541. package/src/managers/worker-dispatcher.manager.ts +0 -1318
  542. package/src/managers/worker-server.manager.ts +0 -468
  543. package/src/methods/accounts.ts +0 -532
  544. package/src/methods/ai-terminal.ts +0 -25505
  545. package/src/methods/app-settings.ts +0 -114
  546. package/src/methods/aws.ts +0 -646
  547. package/src/methods/collections.ts +0 -544
  548. package/src/methods/counters.ts +0 -67
  549. package/src/methods/cron-jobs.ts +0 -2610
  550. package/src/methods/customer-notifications.ts +0 -458
  551. package/src/methods/diagnostics.ts +0 -447
  552. package/src/methods/flag-updates.ts +0 -7
  553. package/src/methods/flags.ts +0 -7
  554. package/src/methods/logs.ts +0 -656
  555. package/src/methods/mongo-explorer.ts +0 -1883
  556. package/src/methods/monitor.ts +0 -540
  557. package/src/methods/pdf.ts +0 -1210
  558. package/src/methods/publications.ts +0 -128
  559. package/src/methods/report-builder.ts +0 -3305
  560. package/src/methods/support.ts +0 -210
  561. package/src/models/ai-terminal-conversation.model.ts +0 -19
  562. package/src/models/ai-terminal-issue-report.model.ts +0 -21
  563. package/src/models/ai-terminal-message.model.ts +0 -24
  564. package/src/models/app-setting.model.ts +0 -17
  565. package/src/models/collection-document.model.ts +0 -24
  566. package/src/models/communication-metric.model.ts +0 -23
  567. package/src/models/cron-job-history.model.ts +0 -16
  568. package/src/models/cron-job.model.ts +0 -15
  569. package/src/models/customer-notification.model.ts +0 -28
  570. package/src/models/customer-portal-password.model.ts +0 -12
  571. package/src/models/dialog.model.ts +0 -25
  572. package/src/models/log-method-latency.model.ts +0 -11
  573. package/src/models/log.model.ts +0 -19
  574. package/src/models/method.model.ts +0 -23
  575. package/src/models/monitor-function.model.ts +0 -16
  576. package/src/models/monitor-memory.model.ts +0 -17
  577. package/src/models/monitor-mongo.model.ts +0 -15
  578. package/src/models/openai-usage-ledger.model.ts +0 -16
  579. package/src/models/pagination.model.ts +0 -35
  580. package/src/models/permission.model.ts +0 -14
  581. package/src/models/report-builder-dashboard-builder.model.ts +0 -29
  582. package/src/models/report-builder-library.model.ts +0 -20
  583. package/src/models/report-builder-report.model.ts +0 -135
  584. package/src/models/report-builder.model.ts +0 -68
  585. package/src/models/select-data-label.model.ts +0 -9
  586. package/src/models/server-message.model.ts +0 -31
  587. package/src/models/slow-query-report.model.ts +0 -23
  588. package/src/models/subscription.model.ts +0 -73
  589. package/src/models/support-ticket.model.ts +0 -96
  590. package/src/models/user-group.model.ts +0 -24
  591. package/src/models/user.model.ts +0 -96
  592. package/src/private/images/ResolveIO.png +0 -0
  593. package/src/publications/ai-terminal.ts +0 -73
  594. package/src/publications/app-settings.ts +0 -25
  595. package/src/publications/app-status.ts +0 -13
  596. package/src/publications/cron-jobs.ts +0 -29
  597. package/src/publications/customer-notifications.ts +0 -101
  598. package/src/publications/files.ts +0 -33
  599. package/src/publications/flags-update.ts +0 -19
  600. package/src/publications/flags.ts +0 -19
  601. package/src/publications/logs.ts +0 -163
  602. package/src/publications/notifications.ts +0 -13
  603. package/src/publications/report-builder-dashboard-builders.ts +0 -39
  604. package/src/publications/report-builder-libraries.ts +0 -41
  605. package/src/publications/report-builder-reports.ts +0 -47
  606. package/src/publications/super-admin.ts +0 -13
  607. package/src/publications/user-groups.ts +0 -12
  608. package/src/publications/user-guides.ts +0 -12
  609. package/src/resolveio-server-app.ts +0 -617
  610. package/src/server-app.ts +0 -2616
  611. package/src/services/codex-client.ts +0 -1117
  612. package/src/services/openai-client.ts +0 -265
  613. package/src/types/error-report.ts +0 -26
  614. package/src/types/js-tiktoken.d.ts +0 -11
  615. package/src/types/slow-query-report.ts +0 -28
  616. package/src/util/common.ts +0 -649
  617. package/src/util/customer-portal-password.ts +0 -183
  618. package/src/util/error-reporter.ts +0 -332
  619. package/src/util/error-tracking.ts +0 -79
  620. package/src/util/report-builder-unwinds.ts +0 -180
  621. package/src/util/schema-report-builder.ts +0 -448
  622. package/src/util/slow-query-reporter.ts +0 -216
  623. package/src/util/subscription-dependency-context.ts +0 -1096
  624. package/src/util/tokenizer.ts +0 -38
  625. package/src/workers/codex-runner.worker.ts +0 -142
  626. package/start_server.sh +0 -5
  627. package/tests/ai-assistant-corpus-build.ts +0 -484
  628. package/tests/ai-assistant-corpus-replay-e2e.ts +0 -773
  629. package/tests/ai-assistant-data-parity-e2e.ts +0 -2018
  630. package/tests/ai-assistant-eval-triage.ts +0 -831
  631. package/tests/ai-assistant-openai-e2e.ts +0 -1061
  632. package/tests/ai-assistant-openai-git-e2e.ts +0 -155
  633. package/tests/ai-assistant-preflight-matrix.ts +0 -215
  634. package/tests/ai-assistant-routing-eval.test.ts +0 -560
  635. package/tests/ai-assistant-snf-live-eval.ts +0 -921
  636. package/tests/ai-assistant-utils.test.ts +0 -2165
  637. package/tests/error-reporter.test.ts +0 -145
  638. package/tests/report-builder-linking.test.ts +0 -79
  639. package/tests/subscription-connect-race.test.ts +0 -157
  640. package/tests/subscription-dependency-context.test.ts +0 -324
  641. package/tests/subscription-manager-collection-tracking.test.ts +0 -86
  642. package/tests/subscription-manager-invalidation.test.ts +0 -85
  643. package/tsconfig.json +0 -34
  644. /package/{src/private → private}/email-templates/enrollment.html +0 -0
  645. /package/{src/private → private}/email-templates/forgot-password.html +0 -0
  646. /package/{src/private → private}/email-templates/support-ticket-deleted.html +0 -0
  647. /package/{src/private → private}/email-templates/support-ticket-modified.html +0 -0
  648. /package/{src/private → private}/email-templates/support-ticket.html +0 -0
  649. /package/{src/public_api.ts → public_api.d.ts} +0 -0
@@ -0,0 +1,3422 @@
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 = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["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 (g && (g = 0, op[0] && (_ = 0)), _) 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 __values = (this && this.__values) || function(o) {
39
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
40
+ if (m) return m.call(o);
41
+ if (o && typeof o.length === "number") return {
42
+ next: function () {
43
+ if (o && i >= o.length) o = void 0;
44
+ return { value: o && o[i++], done: !o };
45
+ }
46
+ };
47
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
48
+ };
49
+ var __read = (this && this.__read) || function (o, n) {
50
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
51
+ if (!m) return o;
52
+ var i = m.call(o), r, ar = [], e;
53
+ try {
54
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
55
+ }
56
+ catch (error) { e = { error: error }; }
57
+ finally {
58
+ try {
59
+ if (r && !r.done && (m = i["return"])) m.call(i);
60
+ }
61
+ finally { if (e) throw e.error; }
62
+ }
63
+ return ar;
64
+ };
65
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
66
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
67
+ if (ar || !(i in from)) {
68
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
69
+ ar[i] = from[i];
70
+ }
71
+ }
72
+ return to.concat(ar || Array.prototype.slice.call(from));
73
+ };
74
+ Object.defineProperty(exports, "__esModule", { value: true });
75
+ exports.SubscriptionManager = void 0;
76
+ var NodeCache = require("node-cache");
77
+ var msgpackr_1 = require("msgpackr");
78
+ var WebSocket = require("ws");
79
+ var flag_collection_1 = require("../collections/flag.collection");
80
+ var logged_in_users_collection_1 = require("../collections/logged-in-users.collection");
81
+ var app_status_1 = require("../publications/app-status");
82
+ var app_settings_1 = require("../publications/app-settings");
83
+ var ai_terminal_1 = require("../publications/ai-terminal");
84
+ var cron_jobs_1 = require("../publications/cron-jobs");
85
+ var customer_notifications_1 = require("../publications/customer-notifications");
86
+ var files_1 = require("../publications/files");
87
+ var flags_1 = require("../publications/flags");
88
+ var flags_update_1 = require("../publications/flags-update");
89
+ var logs_1 = require("../publications/logs");
90
+ var notifications_1 = require("../publications/notifications");
91
+ var report_builder_dashboard_builders_1 = require("../publications/report-builder-dashboard-builders");
92
+ var report_builder_libraries_1 = require("../publications/report-builder-libraries");
93
+ var report_builder_reports_1 = require("../publications/report-builder-reports");
94
+ var super_admin_1 = require("../publications/super-admin");
95
+ var user_groups_1 = require("../publications/user-groups");
96
+ var user_guides_1 = require("../publications/user-guides");
97
+ var resolveio_server_app_1 = require("../resolveio-server-app");
98
+ var common_1 = require("../util/common");
99
+ var error_reporter_1 = require("../util/error-reporter");
100
+ var error_tracking_1 = require("../util/error-tracking");
101
+ var subscription_dependency_context_1 = require("../util/subscription-dependency-context");
102
+ var v8 = require('v8');
103
+ var sift = require('sift');
104
+ function resolveHeapLimitBytes() {
105
+ var stats = v8.getHeapStatistics();
106
+ if (stats && typeof stats.heap_size_limit === 'number') {
107
+ return stats.heap_size_limit;
108
+ }
109
+ if (stats && typeof stats.total_available_size === 'number') {
110
+ return stats.total_available_size;
111
+ }
112
+ return 0;
113
+ }
114
+ // interface CurrentPerformanceMonitor {
115
+ // _id: number;
116
+ // function: string;
117
+ // publication: string;
118
+ // subscriptionData: any[];
119
+ // date_start: Date;
120
+ // date_end: Date;
121
+ // duration: number;
122
+ // result: string;
123
+ // }
124
+ var SubscriptionManager = /** @class */ (function () {
125
+ function SubscriptionManager() {
126
+ this._publications = {};
127
+ this._subscriptions = [];
128
+ this._loggedInUsers = [];
129
+ this._lastRouteBySocket = new Map();
130
+ this._mongoQueue = [];
131
+ this._mongoQueueId = 0;
132
+ this._oplogMode = 'auto';
133
+ this._useLocalOplog = false;
134
+ this._localOplogResyncIntervalMs = 0;
135
+ this._localOplogResyncTimer = null;
136
+ this._cacheId = 1;
137
+ this._heapSize = resolveHeapLimitBytes();
138
+ this._enableDebug = false;
139
+ this._enableDependencyDebug = false;
140
+ this._connectDebug = false;
141
+ this._debugOplogCollections = [];
142
+ this._debugOplogHits = 0;
143
+ this._debugSubCollections = [];
144
+ this._debugSubHits = 0;
145
+ this._debugUnSubHits = 0;
146
+ this._debugUnSubAllHits = 0;
147
+ this._debugMongoQueueHits = 0;
148
+ this._debugMongoQueueCollections = [];
149
+ this._debugSendQueueHits = 0;
150
+ this._debugRemoveCacheHits = 0;
151
+ this._subSendDebug = false;
152
+ this._subSendLogEnabled = false;
153
+ this._subSendLogThresholdMs = 0;
154
+ this._subSendLogBytes = 0;
155
+ this._subSendLogSampleRate = 1;
156
+ this._aiWorkerDebug = false;
157
+ this._aiSubscriptionDebug = false;
158
+ this._aiSubLastAt = new Map();
159
+ this._oplogRetryCount = 0;
160
+ this._lastResumeToken = null;
161
+ this._lastResumeTokenSaveMs = 0;
162
+ this._resumeTokenSavePromise = null;
163
+ this._resumeTokenSaveForcePending = false;
164
+ this._fullResyncPromise = null;
165
+ this.RESUME_TOKEN_COLLECTION = 'subscription-manager-resume-tokens';
166
+ this.RESUME_TOKEN_SAVE_INTERVAL_MS = 5000;
167
+ this.RESUME_TOKEN_AUTO_HEAL_RETRY_THRESHOLD = 2;
168
+ // Buffer to store throttled latency updates with timestamps
169
+ this.latencyBuffer = new Map();
170
+ this._latencyFlushInProgress = false;
171
+ this._latencyFlushPending = false;
172
+ // Interval to flush latency updates in MongoDB
173
+ this.LATENCY_UPDATE_INTERVAL = 60000;
174
+ // Minimum time difference between two latency updates for the same user
175
+ this.LATENCY_UPDATE_THRESHOLD_MS = 30000;
176
+ this.LATENCY_DEBUG_THRESHOLD_MS = 1000;
177
+ this.PUBLICATION_DEBUG_THRESHOLD_MS = 300;
178
+ // private currentPerfomanceMonitor: CurrentPerformanceMonitor[] = [];
179
+ // private idPerformance: number = 0;
180
+ // private performanceThread;
181
+ this._invalidationDebounceTimers = new Map();
182
+ this._invalidationPendingTimestamps = new Map();
183
+ this._pendingInvalidations = new Map();
184
+ this.DEBOUNCE_DELAY = 100; // 100ms debounce window
185
+ this.MAX_WAIT_TIME = 500; // 500ms maximum delay
186
+ this._publicationWorkerQueueLimit = 0;
187
+ }
188
+ SubscriptionManager.createPublicationRegistry = function (serverConfig) {
189
+ var subscriptionManager = new SubscriptionManager();
190
+ subscriptionManager.initializePublicationRegistry(serverConfig);
191
+ return subscriptionManager;
192
+ };
193
+ SubscriptionManager.create = function (wss, serverConfig, monitorManagerFunction) {
194
+ var _this = this;
195
+ var subscriptionManager = new SubscriptionManager();
196
+ setImmediate(function () { return __awaiter(_this, void 0, void 0, function () {
197
+ return __generator(this, function (_a) {
198
+ switch (_a.label) {
199
+ case 0: return [4 /*yield*/, subscriptionManager.initialize(wss, serverConfig, monitorManagerFunction)];
200
+ case 1:
201
+ _a.sent();
202
+ return [2 /*return*/];
203
+ }
204
+ });
205
+ }); });
206
+ return subscriptionManager;
207
+ };
208
+ SubscriptionManager.prototype.initializePublicationRegistry = function (serverConfig) {
209
+ this.serverConfig = serverConfig || resolveio_server_app_1.ResolveIOServer.getServerConfig();
210
+ this.registerCorePublications();
211
+ };
212
+ SubscriptionManager.prototype.initialize = function (wss, serverConfig, monitorManagerFunction) {
213
+ return __awaiter(this, void 0, void 0, function () {
214
+ var resumeToken, flag, dependencyFlag;
215
+ var _this = this;
216
+ return __generator(this, function (_a) {
217
+ switch (_a.label) {
218
+ case 0:
219
+ this._websocketManager = resolveio_server_app_1.ResolveIOServer.getMainServer().getWebSocketManager();
220
+ this._monitorManagerFunction = monitorManagerFunction;
221
+ this._nodeCache = new NodeCache({ stdTTL: 0, checkperiod: 0 });
222
+ setInterval(function () { return _this.flushThrottledLatencyUpdates(); }, this.LATENCY_UPDATE_INTERVAL);
223
+ // setTimeout(() => {
224
+ // console.log('Setting up performance thread');
225
+ // this.performanceThread = new Worker(path.join(__dirname, './subscription.performance.js'));
226
+ // this.performanceThread.on('exit', code => {
227
+ // console.error(new Date(), 'THREAD EXITED!!!!!!!!!!!!!!!!!!', code);
228
+ // });
229
+ // this.performanceThread.on('error', code => {
230
+ // console.error(new Date(), 'THREAD RECV ERROR !!!!!!!!!!!!!!!!!!', code);
231
+ // });
232
+ // }, 5000);
233
+ // setInterval(() => {
234
+ // console.log('Post thread msg');
235
+ // this.performanceThread.postMessage(this.currentPerfomanceMonitor);
236
+ // this.currentPerfomanceMonitor = [];
237
+ // }, 10000);
238
+ this.serverConfig = serverConfig;
239
+ this._wss = wss;
240
+ this._connectDebug = this.resolveConnectDebug();
241
+ this._subSendDebug = this.resolveSubSendDebug();
242
+ this._subSendLogEnabled = this.resolveSubSendLogEnabled();
243
+ this._subSendLogThresholdMs = this.resolveSubSendLogThresholdMs();
244
+ this._subSendLogBytes = this.resolveSubSendLogBytes();
245
+ this._subSendLogSampleRate = this.resolveSubSendLogSampleRate();
246
+ this._aiWorkerDebug = this.parseDebugFlag(process.env.AI_ASSISTANT_WORKER_DEBUG);
247
+ this._aiSubscriptionDebug = this.resolveAiSubscriptionDebug();
248
+ this._oplogMode = this.resolveOplogMode();
249
+ this._localOplogResyncIntervalMs = this.resolveLocalOplogResyncIntervalMs();
250
+ this.registerCorePublications();
251
+ if (!(this._oplogMode === 'local')) return [3 /*break*/, 2];
252
+ return [4 /*yield*/, this.enableLocalOplogFallback('config')];
253
+ case 1:
254
+ _a.sent();
255
+ return [3 /*break*/, 5];
256
+ case 2: return [4 /*yield*/, this.loadResumeToken()];
257
+ case 3:
258
+ resumeToken = _a.sent();
259
+ return [4 /*yield*/, this.tailOpLog(resumeToken || undefined)];
260
+ case 4:
261
+ _a.sent();
262
+ _a.label = 5;
263
+ case 5:
264
+ setInterval(function () {
265
+ _this._oplogRetryCount = 0;
266
+ }, 15000);
267
+ setInterval(function () {
268
+ if (_this.getEnableDebug()) {
269
+ console.log(new Date(), 'Sub Manager', 'Subs', _this._subscriptions.length);
270
+ console.log(new Date(), 'Sub Manager', 'Logged In Users', _this._loggedInUsers.length);
271
+ console.log(new Date(), 'Sub Manager', 'Mongo Queue', _this._mongoQueue.length);
272
+ console.log(new Date(), 'Sub Manager', 'Mongo Queue Hits', _this._debugMongoQueueHits);
273
+ console.log(new Date(), 'Sub Manager', 'Mongo Queue Collections', JSON.stringify(_this._debugMongoQueueCollections.sort(function (a, b) { return a.collection.localeCompare(b.collection) || a.publication.localeCompare(b.publication); }), null, 2));
274
+ console.log(new Date(), 'Sub Manager', 'Oplog Hits', _this._debugOplogHits);
275
+ console.log(new Date(), 'Sub Manager', 'Oplog Collections', JSON.stringify(_this._debugOplogCollections.sort(function (a, b) { return a.collection.localeCompare(b.collection) || a.type.localeCompare(b.type); }), null, 2));
276
+ console.log(new Date(), 'Sub Manager', 'Send Queue Hits', _this._debugSendQueueHits);
277
+ console.log(new Date(), 'Sub Manager', 'Sub Hits', _this._debugSubHits);
278
+ console.log(new Date(), 'Sub Manager', 'Sub Collections', JSON.stringify(_this._debugSubCollections.sort(function (a, b) { return a.publication.localeCompare(b.publication); }), null, 2));
279
+ console.log(new Date(), 'Sub Manager', 'Unsub Hits', _this._debugUnSubHits);
280
+ console.log(new Date(), 'Sub Manager', 'Unsub All Hits', _this._debugUnSubAllHits);
281
+ console.log(new Date(), 'Sub Manager', 'Cache Cleanup Hits', _this._debugRemoveCacheHits);
282
+ }
283
+ _this._debugOplogHits = 0;
284
+ _this._debugOplogCollections = [];
285
+ _this._debugSubCollections = [];
286
+ _this._debugMongoQueueHits = 0;
287
+ _this._debugMongoQueueCollections = [];
288
+ _this._debugSendQueueHits = 0;
289
+ _this._debugSubHits = 0;
290
+ _this._debugUnSubHits = 0;
291
+ _this._debugUnSubAllHits = 0;
292
+ _this._debugRemoveCacheHits = 0;
293
+ }, 60000);
294
+ setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
295
+ var _a, userCopy, _loop_1, this_1, i, i, sub, _loop_2, this_2, j;
296
+ return __generator(this, function (_b) {
297
+ switch (_b.label) {
298
+ case 0:
299
+ _a = this;
300
+ return [4 /*yield*/, logged_in_users_collection_1.LoggedInUsers.find()];
301
+ case 1:
302
+ _a._loggedInUsers = _b.sent();
303
+ userCopy = (0, common_1.deepCopy)(this._loggedInUsers);
304
+ _loop_1 = function (i) {
305
+ var loggedInUser;
306
+ return __generator(this, function (_c) {
307
+ switch (_c.label) {
308
+ case 0:
309
+ loggedInUser = userCopy[i];
310
+ if (!(!loggedInUser.date || Date.now() - loggedInUser.date.getTime() > 120000)) return [3 /*break*/, 4];
311
+ if (!this_1._websocketManager.getWebSocket(loggedInUser.id_ws)) return [3 /*break*/, 2];
312
+ if (this_1.getEnableDebug()) {
313
+ console.log(new Date(), 'Sub Manager', 'Unsub WS', this_1._websocketManager.getWebSocket(loggedInUser.id_ws)['user'], this_1._websocketManager.getWebSocket(loggedInUser.id_ws)['id_socket'], 2);
314
+ }
315
+ return [4 /*yield*/, this_1.unsubscribeAll(this_1._websocketManager.getWebSocket(loggedInUser.id_ws))];
316
+ case 1:
317
+ _c.sent();
318
+ return [3 /*break*/, 4];
319
+ case 2:
320
+ this_1._subscriptions.forEach(function (sub) {
321
+ for (var j = sub.clients.length - 1; j >= 0; j--) {
322
+ var client = sub.clients[j];
323
+ if (client.id_socket === loggedInUser.id_ws) {
324
+ sub.clients.splice(j, 1);
325
+ }
326
+ }
327
+ });
328
+ return [4 /*yield*/, logged_in_users_collection_1.LoggedInUsers.deleteOne({ _id: loggedInUser._id })];
329
+ case 3:
330
+ _c.sent();
331
+ if (this_1._loggedInUsers.findIndex(function (a) { return a._id === loggedInUser._id; }) >= 0) {
332
+ this_1._loggedInUsers.splice(this_1._loggedInUsers.findIndex(function (a) { return a._id === loggedInUser._id; }), 1);
333
+ }
334
+ _c.label = 4;
335
+ case 4: return [2 /*return*/];
336
+ }
337
+ });
338
+ };
339
+ this_1 = this;
340
+ i = this._loggedInUsers.length - 1;
341
+ _b.label = 2;
342
+ case 2:
343
+ if (!(i >= 0)) return [3 /*break*/, 5];
344
+ return [5 /*yield**/, _loop_1(i)];
345
+ case 3:
346
+ _b.sent();
347
+ _b.label = 4;
348
+ case 4:
349
+ i--;
350
+ return [3 /*break*/, 2];
351
+ case 5:
352
+ for (i = 0; i < this._subscriptions.length; i++) {
353
+ sub = this._subscriptions[i];
354
+ _loop_2 = function (j) {
355
+ var client = sub.clients[j];
356
+ if (!this_2._loggedInUsers.some(function (a) { return a.id_ws === client.id_socket; })) {
357
+ sub.clients.splice(j, 1);
358
+ }
359
+ };
360
+ this_2 = this;
361
+ for (j = sub.clients.length - 1; j >= 0; j--) {
362
+ _loop_2(j);
363
+ }
364
+ }
365
+ return [2 /*return*/];
366
+ }
367
+ });
368
+ }); }, 30000);
369
+ return [4 /*yield*/, flag_collection_1.Flags.findOne({ type: 'Enable Debug' })];
370
+ case 6:
371
+ flag = _a.sent();
372
+ return [4 /*yield*/, flag_collection_1.Flags.findOne({ type: 'Enable Dependency Debug' })];
373
+ case 7:
374
+ dependencyFlag = _a.sent();
375
+ if (flag && flag.value) {
376
+ this._enableDebug = true;
377
+ }
378
+ else {
379
+ this._enableDebug = false;
380
+ }
381
+ if (dependencyFlag && dependencyFlag.value) {
382
+ this._enableDependencyDebug = true;
383
+ }
384
+ else {
385
+ this._enableDependencyDebug = process.env.ENABLE_DEPENDENCY_DEBUG === 'true';
386
+ }
387
+ this.setCacheLimit();
388
+ this.configurePublicationWorkers();
389
+ return [2 /*return*/];
390
+ }
391
+ });
392
+ });
393
+ };
394
+ SubscriptionManager.prototype.registerCorePublications = function () {
395
+ (0, super_admin_1.loadSuperAdminPublications)(this);
396
+ (0, ai_terminal_1.loadAiTerminalPublications)(this);
397
+ (0, app_status_1.loadAppStatusPublications)(this);
398
+ (0, app_settings_1.loadAppSettingsPublications)(this);
399
+ (0, logs_1.loadLogPublications)(this);
400
+ (0, files_1.loadFilePublications)(this);
401
+ (0, cron_jobs_1.loadCronJobPublications)(this);
402
+ (0, flags_update_1.loadFlagsUpdatePublications)(this);
403
+ (0, flags_1.loadFlagsPublications)(this);
404
+ (0, notifications_1.loadNotificationPublications)(this);
405
+ (0, customer_notifications_1.loadCustomerNotificationPublications)(this);
406
+ (0, report_builder_reports_1.loadReportBuilderReportPublications)(this);
407
+ (0, report_builder_libraries_1.loadReportBuilderLibraryPublications)(this);
408
+ (0, user_groups_1.loadUserGroupPublications)(this);
409
+ (0, user_guides_1.loadUserGuidePublications)(this);
410
+ (0, report_builder_dashboard_builders_1.loadReportBuilderDashboardBuilderPublications)(this);
411
+ };
412
+ SubscriptionManager.prototype.setCacheLimit = function () {
413
+ if (process.env.IS_WORKERS_ENABLED === 'true') {
414
+ this._heapLimit = this._heapSize * 0.4;
415
+ }
416
+ else {
417
+ this._heapLimit = this._heapSize * 0.3; // Use 50% of total heap size
418
+ }
419
+ };
420
+ SubscriptionManager.prototype.configurePublicationWorkers = function () {
421
+ var _a;
422
+ var config = this.serverConfig || resolveio_server_app_1.ResolveIOServer.getServerConfig() || {};
423
+ this._publicationWorkerQueueLimit = this.parsePositiveNumber((_a = process.env.SUBSCRIPTION_WORKER_QUEUE_MAX) !== null && _a !== void 0 ? _a : config['SUBSCRIPTION_WORKER_QUEUE_MAX']);
424
+ };
425
+ SubscriptionManager.prototype.parseBoolean = function (value) {
426
+ if (value === true) {
427
+ return true;
428
+ }
429
+ if (value === false || value === null || value === undefined) {
430
+ return false;
431
+ }
432
+ if (typeof value === 'number') {
433
+ return value === 1;
434
+ }
435
+ if (typeof value === 'string') {
436
+ var normalized = value.trim().toLowerCase();
437
+ return ['1', 'true', 'yes', 'y', 'on'].includes(normalized);
438
+ }
439
+ return false;
440
+ };
441
+ SubscriptionManager.prototype.clampNumber = function (value, min, max) {
442
+ if (value < min) {
443
+ return min;
444
+ }
445
+ if (value > max) {
446
+ return max;
447
+ }
448
+ return value;
449
+ };
450
+ SubscriptionManager.prototype.invalidatePubsCache = function (collection, type, documentId, document) {
451
+ return __awaiter(this, void 0, void 0, function () {
452
+ var queue, debounceKey, now, firstInvalidationTime, waitedTooLong;
453
+ var _this = this;
454
+ return __generator(this, function (_a) {
455
+ switch (_a.label) {
456
+ case 0:
457
+ queue = this._pendingInvalidations.get(collection);
458
+ if (!queue) {
459
+ queue = { events: [] };
460
+ this._pendingInvalidations.set(collection, queue);
461
+ }
462
+ queue.events.push({ type: type, documentId: documentId, document: document });
463
+ debounceKey = collection;
464
+ now = Date.now();
465
+ firstInvalidationTime = this._invalidationPendingTimestamps.get(debounceKey) || now;
466
+ this._invalidationPendingTimestamps.set(debounceKey, firstInvalidationTime);
467
+ // Clear any existing timer
468
+ if (this._invalidationDebounceTimers.has(debounceKey)) {
469
+ clearTimeout(this._invalidationDebounceTimers.get(debounceKey));
470
+ }
471
+ waitedTooLong = (now - firstInvalidationTime) >= this.MAX_WAIT_TIME;
472
+ if (!waitedTooLong) return [3 /*break*/, 2];
473
+ // Immediate execution path
474
+ this._invalidationPendingTimestamps.delete(debounceKey);
475
+ return [4 /*yield*/, this._executeInvalidation(collection)];
476
+ case 1:
477
+ _a.sent();
478
+ return [3 /*break*/, 3];
479
+ case 2:
480
+ // Normal debounce path
481
+ this._invalidationDebounceTimers.set(debounceKey, setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
482
+ return __generator(this, function (_a) {
483
+ switch (_a.label) {
484
+ case 0:
485
+ this._invalidationPendingTimestamps.delete(debounceKey);
486
+ return [4 /*yield*/, this._executeInvalidation(collection)];
487
+ case 1:
488
+ _a.sent();
489
+ return [2 /*return*/];
490
+ }
491
+ });
492
+ }); }, this.DEBOUNCE_DELAY));
493
+ _a.label = 3;
494
+ case 3: return [2 /*return*/];
495
+ }
496
+ });
497
+ });
498
+ };
499
+ SubscriptionManager.prototype._executeInvalidation = function (collection) {
500
+ return __awaiter(this, void 0, void 0, function () {
501
+ var queued, events, collSubs, collSubs_1, collSubs_1_1, sub, batchType, _a, _b, client, ws, _c, e_1_1, e_2_1;
502
+ var e_2, _d, e_1, _e;
503
+ var _this = this;
504
+ return __generator(this, function (_f) {
505
+ switch (_f.label) {
506
+ case 0:
507
+ // Clean up any existing timer (defensive)
508
+ if (this._invalidationDebounceTimers.has(collection)) {
509
+ clearTimeout(this._invalidationDebounceTimers.get(collection));
510
+ this._invalidationDebounceTimers.delete(collection);
511
+ }
512
+ queued = this._pendingInvalidations.get(collection);
513
+ this._pendingInvalidations.delete(collection);
514
+ if (!queued || !queued.events.length) {
515
+ return [2 /*return*/];
516
+ }
517
+ events = queued.events;
518
+ // Original invalidation logic
519
+ resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(collection, true);
520
+ collSubs = this._subscriptions.filter(function (sub) { return _this.subscriptionReferencesCollection(sub, collection); });
521
+ _f.label = 1;
522
+ case 1:
523
+ _f.trys.push([1, 18, 19, 20]);
524
+ collSubs_1 = __values(collSubs), collSubs_1_1 = collSubs_1.next();
525
+ _f.label = 2;
526
+ case 2:
527
+ if (!!collSubs_1_1.done) return [3 /*break*/, 17];
528
+ sub = collSubs_1_1.value;
529
+ if (this._enableDebug) {
530
+ console.log(new Date(), 'Invalidate Sub', sub.publication, sub.running, sub.runAgain);
531
+ }
532
+ return [4 /*yield*/, this.shouldInvalidateSubscriptionForEvents(sub, collection, events)];
533
+ case 3:
534
+ if (!(_f.sent())) {
535
+ return [3 /*break*/, 16];
536
+ }
537
+ if (sub.running) {
538
+ sub.runAgain = true;
539
+ this.dependencyDebug('Subscription busy, scheduling rerun', { publication: sub.publication, collection: collection, events: this.summarizeEvents(events) });
540
+ return [3 /*break*/, 16];
541
+ }
542
+ batchType = events.length === 1 ? events[0].type : events.map(function (event) { return event.type; }).join(',');
543
+ if (!this._publications[sub.publication].user_specific) return [3 /*break*/, 14];
544
+ this.dependencyDebug('Triggering user-specific invalidation', { publication: sub.publication, collection: collection, events: this.summarizeEvents(events), clientCount: sub.clients.length });
545
+ _f.label = 4;
546
+ case 4:
547
+ _f.trys.push([4, 11, 12, 13]);
548
+ _a = (e_1 = void 0, __values(sub.clients)), _b = _a.next();
549
+ _f.label = 5;
550
+ case 5:
551
+ if (!!_b.done) return [3 /*break*/, 10];
552
+ client = _b.value;
553
+ ws = this._websocketManager.getWebSocket(client.id_socket);
554
+ if (!((ws === null || ws === void 0 ? void 0 : ws.readyState) === WebSocket.OPEN)) return [3 /*break*/, 9];
555
+ _f.label = 6;
556
+ case 6:
557
+ _f.trys.push([6, 8, , 9]);
558
+ return [4 /*yield*/, this.sendDataToOneWithRetry(ws, client.messageId, sub, collection, batchType)];
559
+ case 7:
560
+ _f.sent();
561
+ return [3 /*break*/, 9];
562
+ case 8:
563
+ _c = _f.sent();
564
+ return [3 /*break*/, 9];
565
+ case 9:
566
+ _b = _a.next();
567
+ return [3 /*break*/, 5];
568
+ case 10: return [3 /*break*/, 13];
569
+ case 11:
570
+ e_1_1 = _f.sent();
571
+ e_1 = { error: e_1_1 };
572
+ return [3 /*break*/, 13];
573
+ case 12:
574
+ try {
575
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
576
+ }
577
+ finally { if (e_1) throw e_1.error; }
578
+ return [7 /*endfinally*/];
579
+ case 13: return [3 /*break*/, 16];
580
+ case 14:
581
+ this.dependencyDebug('Triggering broadcast invalidation', { publication: sub.publication, collection: collection, events: this.summarizeEvents(events), clientCount: sub.clients.length });
582
+ return [4 /*yield*/, this.sendDataToAllWithRetry(sub, collection, batchType)];
583
+ case 15:
584
+ _f.sent();
585
+ _f.label = 16;
586
+ case 16:
587
+ collSubs_1_1 = collSubs_1.next();
588
+ return [3 /*break*/, 2];
589
+ case 17: return [3 /*break*/, 20];
590
+ case 18:
591
+ e_2_1 = _f.sent();
592
+ e_2 = { error: e_2_1 };
593
+ return [3 /*break*/, 20];
594
+ case 19:
595
+ try {
596
+ if (collSubs_1_1 && !collSubs_1_1.done && (_d = collSubs_1.return)) _d.call(collSubs_1);
597
+ }
598
+ finally { if (e_2) throw e_2.error; }
599
+ return [7 /*endfinally*/];
600
+ case 20: return [2 /*return*/];
601
+ }
602
+ });
603
+ });
604
+ };
605
+ SubscriptionManager.prototype.subscriptionReferencesCollection = function (sub, collection) {
606
+ var _a, _b, _c, _d;
607
+ if ((_a = sub.collections) === null || _a === void 0 ? void 0 : _a.includes(collection)) {
608
+ return true;
609
+ }
610
+ this.ensureDependencyContainers(sub);
611
+ var dependencyIds = (_b = sub.collectionDependencies) === null || _b === void 0 ? void 0 : _b.get(collection);
612
+ if (dependencyIds && dependencyIds.size > 0) {
613
+ return true;
614
+ }
615
+ var filters = (_c = sub.collectionFilters) === null || _c === void 0 ? void 0 : _c.get(collection);
616
+ if (filters && filters.length > 0) {
617
+ return true;
618
+ }
619
+ if ((_d = sub.watchAllCollections) === null || _d === void 0 ? void 0 : _d.has(collection)) {
620
+ return true;
621
+ }
622
+ return false;
623
+ };
624
+ SubscriptionManager.prototype.delay = function (ms) {
625
+ return __awaiter(this, void 0, void 0, function () {
626
+ return __generator(this, function (_a) {
627
+ // eslint-disable-next-line no-restricted-syntax
628
+ return [2 /*return*/, new Promise(function (resolve) { return setTimeout(resolve, ms); })];
629
+ });
630
+ });
631
+ };
632
+ SubscriptionManager.prototype.getMessageDateMs = function (messageDate) {
633
+ if (messageDate instanceof Date && !isNaN(messageDate.getTime())) {
634
+ return messageDate.getTime();
635
+ }
636
+ if (typeof messageDate === 'string' || typeof messageDate === 'number') {
637
+ var parsed = new Date(messageDate);
638
+ if (!isNaN(parsed.getTime())) {
639
+ return parsed.getTime();
640
+ }
641
+ }
642
+ return Date.now();
643
+ };
644
+ SubscriptionManager.prototype.isStaleRouteMessage = function (ws, messageRoute, messageDate) {
645
+ var socketId = ws ? ws['id_socket'] : '';
646
+ if (!socketId) {
647
+ return false;
648
+ }
649
+ if (!messageRoute || messageRoute === 'Bypass') {
650
+ return false;
651
+ }
652
+ var messageDateMs = this.getMessageDateMs(messageDate);
653
+ var lastRoute = this._lastRouteBySocket.get(socketId);
654
+ if (lastRoute && messageDateMs < lastRoute.dateMs) {
655
+ this.dependencyDebug('Skip route cleanup due to stale route message', {
656
+ socketId: socketId,
657
+ messageRoute: messageRoute,
658
+ messageDateMs: messageDateMs,
659
+ lastRoute: lastRoute.route,
660
+ lastRouteDateMs: lastRoute.dateMs
661
+ });
662
+ return true;
663
+ }
664
+ this._lastRouteBySocket.set(socketId, { route: messageRoute, dateMs: messageDateMs });
665
+ return false;
666
+ };
667
+ SubscriptionManager.prototype.getPublicationWorkerDispatcher = function () {
668
+ var mainServer = resolveio_server_app_1.ResolveIOServer.getMainServer();
669
+ return mainServer && typeof mainServer.getWorkerDispatcherManager === 'function'
670
+ ? mainServer.getWorkerDispatcherManager()
671
+ : null;
672
+ };
673
+ SubscriptionManager.prototype.shouldUsePublicationWorkers = function () {
674
+ var dispatcher = this.getPublicationWorkerDispatcher();
675
+ if (!dispatcher || !dispatcher.hasWorkers()) {
676
+ return false;
677
+ }
678
+ if (typeof dispatcher.hasWorkersForMethod === 'function' && !dispatcher.hasWorkersForMethod('runPublication')) {
679
+ return false;
680
+ }
681
+ if (this._publicationWorkerQueueLimit > 0) {
682
+ var snapshot = dispatcher.getQueueSnapshot();
683
+ if (snapshot.queueDepth >= this._publicationWorkerQueueLimit) {
684
+ return false;
685
+ }
686
+ }
687
+ return true;
688
+ };
689
+ SubscriptionManager.prototype.runPublicationViaWorker = function (sub, userId) {
690
+ return __awaiter(this, void 0, void 0, function () {
691
+ var dispatcher, response, meta, snapshot, error_1;
692
+ return __generator(this, function (_a) {
693
+ switch (_a.label) {
694
+ case 0:
695
+ if (!this.shouldUsePublicationWorkers()) {
696
+ return [2 /*return*/, null];
697
+ }
698
+ dispatcher = this.getPublicationWorkerDispatcher();
699
+ if (!dispatcher) {
700
+ return [2 /*return*/, null];
701
+ }
702
+ _a.label = 1;
703
+ case 1:
704
+ _a.trys.push([1, 3, , 4]);
705
+ return [4 /*yield*/, dispatcher.sendInternalPromiseRaw('runPublication', [
706
+ sub.publication,
707
+ sub.subscriptionData,
708
+ userId
709
+ ])];
710
+ case 2:
711
+ response = _a.sent();
712
+ if (!response || response.error) {
713
+ throw (response === null || response === void 0 ? void 0 : response.result) || new Error('Publication worker error');
714
+ }
715
+ if (!response.packedResult) {
716
+ throw new Error('Publication worker missing packedResult');
717
+ }
718
+ meta = response.meta || {};
719
+ snapshot = meta.snapshot ? (0, subscription_dependency_context_1.deserializeDependencySnapshot)(meta.snapshot) : undefined;
720
+ return [2 /*return*/, {
721
+ packedResult: response.packedResult,
722
+ snapshot: snapshot,
723
+ encoding: response.encoding || meta.encoding || 'msgpack',
724
+ workerUsed: true
725
+ }];
726
+ case 3:
727
+ error_1 = _a.sent();
728
+ if (this._enableDebug) {
729
+ console.log(new Date(), 'Sub Manager', 'Worker publication failed, falling back', sub.publication, (error_1 === null || error_1 === void 0 ? void 0 : error_1.message) || error_1);
730
+ }
731
+ return [2 /*return*/, null];
732
+ case 4: return [2 /*return*/];
733
+ }
734
+ });
735
+ });
736
+ };
737
+ SubscriptionManager.prototype.runPublicationLocally = function (sub, userId) {
738
+ return __awaiter(this, void 0, void 0, function () {
739
+ var pub, context, metadata, execution, _a;
740
+ return __generator(this, function (_b) {
741
+ switch (_b.label) {
742
+ case 0:
743
+ pub = this._publications[sub.publication];
744
+ if (!pub) {
745
+ throw new Error("Publication not found: ".concat(sub.publication));
746
+ }
747
+ context = Object.assign({}, this, SubscriptionManager.prototype);
748
+ metadata = {
749
+ publication: sub.publication,
750
+ subscriptionData: sub.subscriptionData
751
+ };
752
+ if (!pub.user_specific) return [3 /*break*/, 2];
753
+ return [4 /*yield*/, (0, subscription_dependency_context_1.withDependencyTracking)(function () {
754
+ var _a;
755
+ return (_a = pub.function).call.apply(_a, __spreadArray([context, userId || ''], __read(sub.subscriptionData), false));
756
+ }, Object.assign({}, metadata, { userId: userId }))];
757
+ case 1:
758
+ _a = _b.sent();
759
+ return [3 /*break*/, 4];
760
+ case 2: return [4 /*yield*/, (0, subscription_dependency_context_1.withDependencyTracking)(function () {
761
+ var _a;
762
+ return (_a = pub.function).call.apply(_a, __spreadArray([context], __read(sub.subscriptionData), false));
763
+ }, metadata)];
764
+ case 3:
765
+ _a = _b.sent();
766
+ _b.label = 4;
767
+ case 4:
768
+ execution = _a;
769
+ return [2 /*return*/, {
770
+ result: execution.result,
771
+ packedResult: this.packCachePayload(execution.result),
772
+ snapshot: execution.snapshot,
773
+ encoding: 'msgpack',
774
+ workerUsed: false
775
+ }];
776
+ }
777
+ });
778
+ });
779
+ };
780
+ SubscriptionManager.prototype.runPublicationExecution = function (sub, userId) {
781
+ return __awaiter(this, void 0, void 0, function () {
782
+ var pub, effectiveUserId, workerExecution;
783
+ return __generator(this, function (_a) {
784
+ switch (_a.label) {
785
+ case 0:
786
+ pub = this._publications[sub.publication];
787
+ if (!pub) {
788
+ throw new Error("Publication not found: ".concat(sub.publication));
789
+ }
790
+ effectiveUserId = pub.user_specific ? userId : undefined;
791
+ return [4 /*yield*/, this.runPublicationViaWorker(sub, effectiveUserId)];
792
+ case 1:
793
+ workerExecution = _a.sent();
794
+ if (workerExecution) {
795
+ return [2 /*return*/, workerExecution];
796
+ }
797
+ return [2 /*return*/, this.runPublicationLocally(sub, effectiveUserId)];
798
+ }
799
+ });
800
+ });
801
+ };
802
+ SubscriptionManager.prototype.sendDataToAllWithRetry = function (sub, collection, type) {
803
+ return __awaiter(this, void 0, void 0, function () {
804
+ return __generator(this, function (_a) {
805
+ switch (_a.label) {
806
+ case 0:
807
+ sub.running = true;
808
+ _a.label = 1;
809
+ case 1:
810
+ if (this._enableDebug) {
811
+ console.log(new Date(), 'Running sendDataToAll Sub', sub.publication);
812
+ }
813
+ sub.runAgain = false;
814
+ return [4 /*yield*/, this.sendDataToAll(sub, collection, type)];
815
+ case 2:
816
+ _a.sent();
817
+ if (this._enableDebug) {
818
+ console.log(new Date(), 'Done sendDataToAll Sub', sub.publication, sub.runAgain);
819
+ }
820
+ if (!sub.runAgain) return [3 /*break*/, 4];
821
+ return [4 /*yield*/, this.delay(500)];
822
+ case 3:
823
+ _a.sent(); // delay, adjust as needed
824
+ _a.label = 4;
825
+ case 4:
826
+ if (sub.runAgain) return [3 /*break*/, 1];
827
+ _a.label = 5;
828
+ case 5:
829
+ sub.running = false;
830
+ return [2 /*return*/];
831
+ }
832
+ });
833
+ });
834
+ };
835
+ SubscriptionManager.prototype.sendDataToOneWithRetry = function (ws, messageId, sub, collection, type) {
836
+ return __awaiter(this, void 0, void 0, function () {
837
+ return __generator(this, function (_a) {
838
+ switch (_a.label) {
839
+ case 0:
840
+ sub.running = true;
841
+ _a.label = 1;
842
+ case 1:
843
+ if (this._enableDebug) {
844
+ console.log(new Date(), 'Running sendDataToOne Sub', sub.publication);
845
+ }
846
+ sub.runAgain = false;
847
+ return [4 /*yield*/, this.sendDataToOne(ws, messageId, sub, collection, type)];
848
+ case 2:
849
+ _a.sent();
850
+ if (this._enableDebug) {
851
+ console.log(new Date(), 'Done sendDataToOne Sub', sub.publication, sub.runAgain);
852
+ }
853
+ if (!sub.runAgain) return [3 /*break*/, 4];
854
+ return [4 /*yield*/, this.delay(500)];
855
+ case 3:
856
+ _a.sent(); // delay, adjust as needed
857
+ _a.label = 4;
858
+ case 4:
859
+ if (sub.runAgain) return [3 /*break*/, 1];
860
+ _a.label = 5;
861
+ case 5:
862
+ sub.running = false;
863
+ return [2 /*return*/];
864
+ }
865
+ });
866
+ });
867
+ };
868
+ // Add all files to publications private object
869
+ SubscriptionManager.prototype.publications = function (method) {
870
+ this._publications = Object.assign(this._publications, method);
871
+ };
872
+ SubscriptionManager.prototype.getPublication = function (publication) {
873
+ return this._publications[publication];
874
+ };
875
+ SubscriptionManager.prototype.getPublications = function () {
876
+ return this._publications;
877
+ };
878
+ // Throttled `loggedInLatency` method
879
+ SubscriptionManager.prototype.loggedInLatency = function (ws) {
880
+ var now = new Date();
881
+ var newLatency = ws['latency'];
882
+ var socketId = ws['id_socket'];
883
+ var loggedInUser = this._loggedInUsers.find(function (a) { return a.id_ws === socketId; });
884
+ if (!loggedInUser) {
885
+ return;
886
+ }
887
+ var existingEntry = this.latencyBuffer.get(socketId);
888
+ // Throttle updates: only update if time threshold has passed or latency has significantly changed
889
+ if (!existingEntry ||
890
+ (now.getTime() - existingEntry.lastUpdate.getTime() >= this.LATENCY_UPDATE_THRESHOLD_MS) ||
891
+ (Math.abs(newLatency - existingEntry.latency) > 100) // Optional: log only significant changes
892
+ ) {
893
+ // Update in-memory and buffer
894
+ loggedInUser.date = now;
895
+ this.latencyBuffer.set(socketId, { latency: newLatency, lastUpdate: now });
896
+ }
897
+ };
898
+ // Method to flush buffered latency updates in bulk
899
+ SubscriptionManager.prototype.flushThrottledLatencyUpdates = function () {
900
+ return __awaiter(this, void 0, void 0, function () {
901
+ var pendingEntries, updates, error_2, pendingEntries_1, pendingEntries_1_1, _a, id_ws, payload, current;
902
+ var e_3, _b;
903
+ var _this = this;
904
+ return __generator(this, function (_c) {
905
+ switch (_c.label) {
906
+ case 0:
907
+ if (this.latencyBuffer.size === 0)
908
+ return [2 /*return*/]; // No updates to flush
909
+ if (this._latencyFlushInProgress) {
910
+ this._latencyFlushPending = true;
911
+ return [2 /*return*/];
912
+ }
913
+ this._latencyFlushInProgress = true;
914
+ pendingEntries = Array.from(this.latencyBuffer.entries());
915
+ this.latencyBuffer.clear();
916
+ updates = pendingEntries.map(function (_a) {
917
+ var _b = __read(_a, 2), id_ws = _b[0], _c = _b[1], latency = _c.latency, lastUpdate = _c.lastUpdate;
918
+ return ({
919
+ updateOne: {
920
+ filter: { id_ws: id_ws },
921
+ update: { $set: { latency: latency, date: lastUpdate } }
922
+ }
923
+ });
924
+ });
925
+ _c.label = 1;
926
+ case 1:
927
+ _c.trys.push([1, 3, 4, 5]);
928
+ return [4 /*yield*/, logged_in_users_collection_1.LoggedInUsers.bulkWrite(updates, { ordered: false })];
929
+ case 2:
930
+ _c.sent();
931
+ if (this.getEnableDebug()) {
932
+ console.log(new Date(), 'Sub Manager', 'Throttled latency batch update successful', updates.length);
933
+ }
934
+ return [3 /*break*/, 5];
935
+ case 3:
936
+ error_2 = _c.sent();
937
+ try {
938
+ for (pendingEntries_1 = __values(pendingEntries), pendingEntries_1_1 = pendingEntries_1.next(); !pendingEntries_1_1.done; pendingEntries_1_1 = pendingEntries_1.next()) {
939
+ _a = __read(pendingEntries_1_1.value, 2), id_ws = _a[0], payload = _a[1];
940
+ current = this.latencyBuffer.get(id_ws);
941
+ if (!current || current.lastUpdate < payload.lastUpdate) {
942
+ this.latencyBuffer.set(id_ws, payload);
943
+ }
944
+ }
945
+ }
946
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
947
+ finally {
948
+ try {
949
+ if (pendingEntries_1_1 && !pendingEntries_1_1.done && (_b = pendingEntries_1.return)) _b.call(pendingEntries_1);
950
+ }
951
+ finally { if (e_3) throw e_3.error; }
952
+ }
953
+ console.error(new Date(), 'Sub Manager', 'Throttled latency batch update failed', error_2);
954
+ return [3 /*break*/, 5];
955
+ case 4:
956
+ this._latencyFlushInProgress = false;
957
+ if (this._latencyFlushPending) {
958
+ this._latencyFlushPending = false;
959
+ setImmediate(function () { return _this.flushThrottledLatencyUpdates(); });
960
+ }
961
+ return [7 /*endfinally*/];
962
+ case 5: return [2 /*return*/];
963
+ }
964
+ });
965
+ });
966
+ };
967
+ // Subscribe to publication
968
+ SubscriptionManager.prototype.subscribe = function (messageRoute, messageDate, ws, messageId, publication, subscriptionData) {
969
+ return __awaiter(this, void 0, void 0, function () {
970
+ var pub, valObj, valKeys, rootKeys, i, staleRouteMessage, urlData, urlModule_1, urlNext, otherRouteSubs, normalizedSubscriptionData, subscriptionKey_1, sub, isAiPublication, wsId_1, wsUserId, existingIndex, existingClient, i;
971
+ var _this = this;
972
+ var _a;
973
+ return __generator(this, function (_b) {
974
+ switch (_b.label) {
975
+ case 0:
976
+ this._debugSubHits += 1;
977
+ this.connectDebug('Subscribe request', {
978
+ publication: publication,
979
+ messageRoute: messageRoute,
980
+ messageId: messageId,
981
+ id_socket: ws ? ws['id_socket'] : null,
982
+ user: ws ? ws['user'] : null,
983
+ args: Array.isArray(subscriptionData) ? subscriptionData.length : 0
984
+ });
985
+ if (!this._debugSubCollections.some(function (a) { return a.publication === publication; })) {
986
+ this._debugSubCollections.push({
987
+ publication: publication,
988
+ hits: 1
989
+ });
990
+ }
991
+ else {
992
+ this._debugSubCollections.find(function (a) { return a.publication === publication; }).hits += 1;
993
+ }
994
+ pub = this._publications[publication];
995
+ if (!!pub) return [3 /*break*/, 1];
996
+ console.error(new Date(), 'No Publication: ' + publication);
997
+ this.connectDebug('Missing publication', { publication: publication, messageRoute: messageRoute, messageId: messageId });
998
+ return [2 /*return*/];
999
+ case 1:
1000
+ if (subscriptionData.length > 1 || subscriptionData[0]) {
1001
+ if (!pub.check) {
1002
+ console.error(new Date(), 'No Check Function For Pub ' + publication);
1003
+ this.connectDebug('Missing check for publication', { publication: publication, messageRoute: messageRoute, messageId: messageId });
1004
+ return [2 /*return*/];
1005
+ }
1006
+ else if (!pub.check._schema) {
1007
+ console.error(new Date(), 'No Check Schema For Pub ' + publication);
1008
+ this.connectDebug('Missing check schema for publication', { publication: publication, messageRoute: messageRoute, messageId: messageId });
1009
+ return [2 /*return*/];
1010
+ }
1011
+ else {
1012
+ valObj = {};
1013
+ valKeys = Object.keys(pub.check._schema);
1014
+ rootKeys = valKeys.filter(function (a) { return !a.includes('.'); });
1015
+ for (i = 0; i < subscriptionData.length; i++) {
1016
+ valObj[rootKeys[i]] = subscriptionData[i];
1017
+ }
1018
+ try {
1019
+ pub.check.validate(valObj);
1020
+ }
1021
+ catch (errors) {
1022
+ if (errors) {
1023
+ console.error(new Date(), 'Error in Pub Check (' + publication + ')', errors);
1024
+ this.connectDebug('Publication check failed', {
1025
+ publication: publication,
1026
+ messageRoute: messageRoute,
1027
+ messageId: messageId,
1028
+ args: subscriptionData.length,
1029
+ valObj: valObj,
1030
+ error: (errors === null || errors === void 0 ? void 0 : errors.message) || errors
1031
+ });
1032
+ return [2 /*return*/];
1033
+ }
1034
+ }
1035
+ }
1036
+ }
1037
+ staleRouteMessage = this.isStaleRouteMessage(ws, messageRoute, messageDate);
1038
+ if (messageRoute !== 'Bypass' && messageRoute !== '/' && !staleRouteMessage) {
1039
+ urlData = messageRoute.split('/');
1040
+ urlModule_1 = '';
1041
+ urlNext = urlData[0];
1042
+ if (urlData[0] === '') {
1043
+ urlModule_1 = '/';
1044
+ urlNext = urlData[1];
1045
+ }
1046
+ urlModule_1 += urlNext;
1047
+ if (urlData.length > 1) {
1048
+ urlModule_1 += '/';
1049
+ }
1050
+ otherRouteSubs = this._subscriptions.filter(function (a) { return a.clients.some(function (b) { return b.id_socket === ws['id_socket'] && b.messageRoute !== 'Bypass' && b.messageRoute !== '/' && b.messageRoute !== messageRoute && !b.messageRoute.startsWith(urlModule_1); }); });
1051
+ if (otherRouteSubs.length) {
1052
+ // ResolveIOServer.getMainServer().getMethodManager().sendEmail('dev@resolveio.com', 'SERVER - Detected Undestroyed Subscription - ' + this.serverConfig['CLIENT_NAME'], 'USER: ' + ws['user'] + ' (Socket: ' + ws['id_socket'] + ') ' + ' is on route: ' + messageRoute + ' but has the following subscriptions on other routes:' + JSON.stringify(otherRouteSubs, null, 2));
1053
+ otherRouteSubs.forEach(function (otherSub) {
1054
+ otherSub.clients.filter(function (a) { return a.id_socket === ws['id_socket']; }).forEach(function (client) {
1055
+ _this.unsubscribe(client.messageRoute, new Date(), ws, client.messageId, otherSub.publication, otherSub.subscriptionData);
1056
+ });
1057
+ });
1058
+ }
1059
+ }
1060
+ normalizedSubscriptionData = Array.isArray(subscriptionData) ? subscriptionData : [];
1061
+ subscriptionKey_1 = JSON.stringify(normalizedSubscriptionData);
1062
+ sub = this._subscriptions.find(function (a) { return a.publication === publication && a.subscriptionKey === subscriptionKey_1; });
1063
+ isAiPublication = this.isAiPublication(publication);
1064
+ wsId_1 = ws ? ws['id_socket'] : null;
1065
+ wsUserId = ws ? ws['id_user'] : null;
1066
+ // If sub found (another user watching same data), add client to same sub
1067
+ if (sub) {
1068
+ if (isAiPublication && wsId_1) {
1069
+ existingIndex = sub.clients.findIndex(function (a) { return a.id_socket === wsId_1; });
1070
+ if (existingIndex >= 0) {
1071
+ existingClient = sub.clients[existingIndex];
1072
+ for (i = sub.clients.length - 1; i >= 0; i--) {
1073
+ if (i !== existingIndex && sub.clients[i].id_socket === wsId_1) {
1074
+ sub.clients.splice(i, 1);
1075
+ }
1076
+ }
1077
+ existingClient.id_user = wsUserId;
1078
+ existingClient.messageId = messageId;
1079
+ existingClient.messageRoute = messageRoute;
1080
+ }
1081
+ else {
1082
+ sub.clients.push({
1083
+ id_user: wsUserId,
1084
+ messageId: messageId,
1085
+ id_socket: wsId_1,
1086
+ messageRoute: messageRoute
1087
+ });
1088
+ }
1089
+ }
1090
+ else if (!sub.clients.some(function (a) { return a.id_socket === ws['id_socket'] && a.messageId === messageId; })) {
1091
+ sub.clients.push({
1092
+ id_user: ws['id_user'],
1093
+ messageId: messageId,
1094
+ id_socket: ws['id_socket'],
1095
+ messageRoute: messageRoute
1096
+ });
1097
+ }
1098
+ }
1099
+ // If sub not found, create new sub
1100
+ else {
1101
+ this._subscriptions.push({
1102
+ publication: publication,
1103
+ subscriptionKey: subscriptionKey_1,
1104
+ subscriptionData: normalizedSubscriptionData,
1105
+ collections: this.getPublicationCollections(publication),
1106
+ clients: [{
1107
+ id_user: ws['id_user'],
1108
+ messageId: messageId,
1109
+ id_socket: ws['id_socket'],
1110
+ messageRoute: messageRoute,
1111
+ }],
1112
+ cacheId: 0,
1113
+ running: false,
1114
+ runAgain: false,
1115
+ collectionDependencies: new Map(),
1116
+ collectionFilters: new Map(),
1117
+ watchAllCollections: new Set(),
1118
+ collectionQueryMeta: new Map()
1119
+ });
1120
+ }
1121
+ if (!sub) {
1122
+ sub = this._subscriptions.find(function (a) { return a.publication === publication && a.subscriptionKey === subscriptionKey_1; });
1123
+ }
1124
+ this.ensureDependencyContainers(sub);
1125
+ this.logAiSubscriptionEvent('sub', {
1126
+ publication: publication,
1127
+ subscriptionKey: subscriptionKey_1,
1128
+ messageId: messageId,
1129
+ id_socket: ws ? ws['id_socket'] : null,
1130
+ messageRoute: messageRoute,
1131
+ clients: ((_a = sub === null || sub === void 0 ? void 0 : sub.clients) === null || _a === void 0 ? void 0 : _a.length) || 0,
1132
+ subscriptionData: normalizedSubscriptionData
1133
+ });
1134
+ if (this._enableDebug) {
1135
+ console.log(new Date(), 'New Sub', sub.publication, sub.running, sub.runAgain, sub.clients.length);
1136
+ }
1137
+ // Immediately send data to the new client
1138
+ return [4 /*yield*/, this.processSubscription(sub, ws, messageId)];
1139
+ case 2:
1140
+ // Immediately send data to the new client
1141
+ _b.sent();
1142
+ _b.label = 3;
1143
+ case 3: return [2 /*return*/];
1144
+ }
1145
+ });
1146
+ });
1147
+ };
1148
+ SubscriptionManager.prototype.createLoggedInUser = function (id_ws) {
1149
+ return __awaiter(this, void 0, void 0, function () {
1150
+ var ws, user;
1151
+ return __generator(this, function (_a) {
1152
+ switch (_a.label) {
1153
+ case 0:
1154
+ ws = this._websocketManager.getWebSocket(id_ws);
1155
+ if (!ws) return [3 /*break*/, 2];
1156
+ user = {
1157
+ _id: (0, common_1.objectIdHexString)(),
1158
+ __v: 0,
1159
+ date: new Date(),
1160
+ id_user: ws['id_user'],
1161
+ user: ws['user'],
1162
+ id_ws: ws['id_socket']
1163
+ };
1164
+ this._loggedInUsers.push(user);
1165
+ return [4 /*yield*/, logged_in_users_collection_1.LoggedInUsers.insertOne(user)];
1166
+ case 1:
1167
+ _a.sent();
1168
+ return [2 /*return*/, user];
1169
+ case 2: throw new Error('Error in Create Logged In User: No WS');
1170
+ }
1171
+ });
1172
+ });
1173
+ };
1174
+ // Unsubscribe from publication
1175
+ SubscriptionManager.prototype.unsubscribe = function (messageRoute, messageDate, ws, messageId, publication, subscriptionData) {
1176
+ this._debugUnSubHits += 1;
1177
+ if (!this._publications[publication]) {
1178
+ console.log('No Publication: ' + publication);
1179
+ return;
1180
+ }
1181
+ else {
1182
+ var normalizedSubscriptionData = Array.isArray(subscriptionData) ? subscriptionData : [];
1183
+ var subscriptionKey_2 = JSON.stringify(normalizedSubscriptionData);
1184
+ var sub = this._subscriptions.find(function (a) { return a.publication === publication && a.subscriptionKey === subscriptionKey_2; });
1185
+ if (sub) {
1186
+ var isAiPublication = this.isAiPublication(publication);
1187
+ for (var i = sub.clients.length - 1; i >= 0; i--) {
1188
+ if (isAiPublication && sub.clients[i].id_socket === ws['id_socket']) {
1189
+ sub.clients.splice(i, 1);
1190
+ }
1191
+ else if (sub.clients[i].id_user === ws['id_user'] && sub.clients[i].messageId === messageId && sub.clients[i].id_socket === ws['id_socket']) {
1192
+ sub.clients.splice(i, 1);
1193
+ }
1194
+ }
1195
+ this.logAiSubscriptionEvent('unsub', {
1196
+ publication: publication,
1197
+ subscriptionKey: subscriptionKey_2,
1198
+ messageId: messageId,
1199
+ id_socket: ws ? ws['id_socket'] : null,
1200
+ messageRoute: messageRoute,
1201
+ clients: sub.clients.length,
1202
+ subscriptionData: normalizedSubscriptionData
1203
+ });
1204
+ if (this._enableDebug) {
1205
+ console.log(new Date(), 'Unsub', sub.publication, sub.running, sub.runAgain, sub.clients.length);
1206
+ }
1207
+ }
1208
+ }
1209
+ };
1210
+ // Unsubscribe from publication
1211
+ SubscriptionManager.prototype.unsubscribeAll = function (ws) {
1212
+ return __awaiter(this, void 0, void 0, function () {
1213
+ var userSubs, i, sub, j;
1214
+ return __generator(this, function (_a) {
1215
+ switch (_a.label) {
1216
+ case 0:
1217
+ this._debugUnSubAllHits += 1;
1218
+ if (!ws) return [3 /*break*/, 2];
1219
+ if (ws['id_socket']) {
1220
+ this._lastRouteBySocket.delete(ws['id_socket']);
1221
+ }
1222
+ // Check if WebSocket has already been unsubscribed
1223
+ if (ws['isUnsubscribed']) {
1224
+ return [2 /*return*/]; // Skip if already unsubscribed
1225
+ }
1226
+ // Mark the WebSocket as unsubscribed to prevent further calls
1227
+ ws['isUnsubscribed'] = true;
1228
+ if (this._loggedInUsers.map(function (a) { return a.id_ws; }).indexOf(ws['id_socket']) >= 0) {
1229
+ this._loggedInUsers.splice(this._loggedInUsers.map(function (a) { return a.id_ws; }).indexOf(ws['id_socket']), 1);
1230
+ }
1231
+ return [4 /*yield*/, logged_in_users_collection_1.LoggedInUsers.deleteOne({ id_ws: ws['id_socket'] })];
1232
+ case 1:
1233
+ _a.sent();
1234
+ userSubs = this._subscriptions.filter(function (a) { return a.clients.some(function (b) { return b.id_user === ws['id_user'] && b.id_socket === ws['id_socket']; }); });
1235
+ for (i = userSubs.length - 1; i >= 0; i--) {
1236
+ sub = userSubs[i];
1237
+ for (j = sub.clients.length - 1; j >= 0; j--) {
1238
+ if (sub.clients[j].id_socket === ws['id_socket']) {
1239
+ sub.clients.splice(j, 1);
1240
+ }
1241
+ }
1242
+ }
1243
+ this._websocketManager.removeWebSocket(ws);
1244
+ _a.label = 2;
1245
+ case 2: return [2 /*return*/];
1246
+ }
1247
+ });
1248
+ });
1249
+ };
1250
+ SubscriptionManager.prototype.getActiveSubscriptions = function () {
1251
+ return this._subscriptions;
1252
+ };
1253
+ // Get publication collection
1254
+ SubscriptionManager.prototype.getPublicationCollections = function (publication) {
1255
+ return this._publications[publication].collections;
1256
+ };
1257
+ SubscriptionManager.prototype.getWatchedDatabases = function () {
1258
+ var mongoManager = resolveio_server_app_1.ResolveIOServer.getMongoManager();
1259
+ var managerDatabases = (mongoManager === null || mongoManager === void 0 ? void 0 : mongoManager.getWatchedDatabases()) || [];
1260
+ if (managerDatabases.length) {
1261
+ return managerDatabases;
1262
+ }
1263
+ var config = this.serverConfig || resolveio_server_app_1.ResolveIOServer.getServerConfig();
1264
+ var mainDb = config && typeof config['DATABASE'] === 'string' ? config['DATABASE'] : '';
1265
+ return mainDb ? [mainDb] : [];
1266
+ };
1267
+ SubscriptionManager.prototype.resolveOplogMode = function () {
1268
+ var config = this.serverConfig || resolveio_server_app_1.ResolveIOServer.getServerConfig() || {};
1269
+ var raw = process.env.SUBSCRIPTION_OPLOG_MODE || process.env.OPLOG_MODE || config['SUBSCRIPTION_OPLOG_MODE'] || config['OPLOG_MODE'];
1270
+ if (typeof raw !== 'string' || !raw.trim()) {
1271
+ return 'auto';
1272
+ }
1273
+ var normalized = raw.trim().toLowerCase();
1274
+ if (['local', 'single', 'standalone', 'fallback', 'poll'].includes(normalized)) {
1275
+ return 'local';
1276
+ }
1277
+ if (['change-stream', 'changestream', 'oplog', 'replica', 'replicaset', 'rs'].includes(normalized)) {
1278
+ return 'change-stream';
1279
+ }
1280
+ return 'auto';
1281
+ };
1282
+ SubscriptionManager.prototype.resolveLocalOplogResyncIntervalMs = function () {
1283
+ var config = this.serverConfig || resolveio_server_app_1.ResolveIOServer.getServerConfig() || {};
1284
+ var raw = process.env.OPLOG_FALLBACK_RESYNC_MS
1285
+ || process.env.SUBSCRIPTION_OPLOG_RESYNC_MS
1286
+ || config['OPLOG_FALLBACK_RESYNC_MS']
1287
+ || config['SUBSCRIPTION_OPLOG_RESYNC_MS'];
1288
+ return this.parsePositiveNumber(raw);
1289
+ };
1290
+ SubscriptionManager.prototype.resolveResumeTokenMaxAgeMs = function () {
1291
+ var config = this.serverConfig || resolveio_server_app_1.ResolveIOServer.getServerConfig() || {};
1292
+ var raw = process.env.OPLOG_RESUME_TOKEN_MAX_AGE_MS
1293
+ || process.env.SUBSCRIPTION_OPLOG_RESUME_TOKEN_MAX_AGE_MS
1294
+ || config['OPLOG_RESUME_TOKEN_MAX_AGE_MS']
1295
+ || config['SUBSCRIPTION_OPLOG_RESUME_TOKEN_MAX_AGE_MS'];
1296
+ return this.parsePositiveNumber(raw);
1297
+ };
1298
+ SubscriptionManager.prototype.resolveConnectDebug = function () {
1299
+ var config = this.serverConfig || resolveio_server_app_1.ResolveIOServer.getServerConfig() || {};
1300
+ var raw = process.env.WS_CONNECT_DEBUG
1301
+ || process.env.CONNECT_DEBUG
1302
+ || config['WS_CONNECT_DEBUG']
1303
+ || config['CONNECT_DEBUG'];
1304
+ return this.parseDebugFlag(raw);
1305
+ };
1306
+ SubscriptionManager.prototype.resolveSubSendDebug = function () {
1307
+ var config = this.serverConfig || resolveio_server_app_1.ResolveIOServer.getServerConfig() || {};
1308
+ var raw = process.env.SUB_SEND_DEBUG
1309
+ || process.env.SUBSCRIPTION_SEND_DEBUG
1310
+ || config['SUB_SEND_DEBUG']
1311
+ || config['SUBSCRIPTION_SEND_DEBUG'];
1312
+ return this.parseDebugFlag(raw);
1313
+ };
1314
+ SubscriptionManager.prototype.resolveSubSendLogEnabled = function () {
1315
+ var config = this.serverConfig || resolveio_server_app_1.ResolveIOServer.getServerConfig() || {};
1316
+ var raw = process.env.SUB_SEND_LOG_ENABLED
1317
+ || process.env.SUBSCRIPTION_SEND_LOG_ENABLED
1318
+ || config['SUB_SEND_LOG_ENABLED']
1319
+ || config['SUBSCRIPTION_SEND_LOG_ENABLED'];
1320
+ if (raw === undefined || raw === null || raw === '') {
1321
+ return this._subSendDebug;
1322
+ }
1323
+ return this.parseDebugFlag(raw);
1324
+ };
1325
+ SubscriptionManager.prototype.resolveSubSendLogThresholdMs = function () {
1326
+ var config = this.serverConfig || resolveio_server_app_1.ResolveIOServer.getServerConfig() || {};
1327
+ var raw = process.env.SUB_SEND_LOG_THRESHOLD_MS
1328
+ || process.env.SUBSCRIPTION_SEND_LOG_THRESHOLD_MS
1329
+ || config['SUB_SEND_LOG_THRESHOLD_MS']
1330
+ || config['SUBSCRIPTION_SEND_LOG_THRESHOLD_MS'];
1331
+ return this.parsePositiveNumber(raw);
1332
+ };
1333
+ SubscriptionManager.prototype.resolveSubSendLogBytes = function () {
1334
+ var config = this.serverConfig || resolveio_server_app_1.ResolveIOServer.getServerConfig() || {};
1335
+ var raw = process.env.SUB_SEND_LOG_BYTES
1336
+ || process.env.SUBSCRIPTION_SEND_LOG_BYTES
1337
+ || config['SUB_SEND_LOG_BYTES']
1338
+ || config['SUBSCRIPTION_SEND_LOG_BYTES'];
1339
+ return this.parsePositiveNumber(raw);
1340
+ };
1341
+ SubscriptionManager.prototype.resolveSubSendLogSampleRate = function () {
1342
+ var config = this.serverConfig || resolveio_server_app_1.ResolveIOServer.getServerConfig() || {};
1343
+ var raw = process.env.SUB_SEND_LOG_SAMPLE_RATE
1344
+ || process.env.SUBSCRIPTION_SEND_LOG_SAMPLE_RATE
1345
+ || config['SUB_SEND_LOG_SAMPLE_RATE']
1346
+ || config['SUBSCRIPTION_SEND_LOG_SAMPLE_RATE'];
1347
+ return this.parseSampleRate(raw, this._subSendLogSampleRate);
1348
+ };
1349
+ SubscriptionManager.prototype.resolveAiSubscriptionDebug = function () {
1350
+ var config = this.serverConfig || resolveio_server_app_1.ResolveIOServer.getServerConfig() || {};
1351
+ var raw = process.env.AI_ASSISTANT_SUB_DEBUG
1352
+ || process.env.AI_TERMINAL_SUB_DEBUG
1353
+ || config['AI_ASSISTANT_SUB_DEBUG']
1354
+ || config['AI_TERMINAL_SUB_DEBUG'];
1355
+ return this.parseDebugFlag(raw);
1356
+ };
1357
+ SubscriptionManager.prototype.parseDebugFlag = function (value) {
1358
+ if (value === true) {
1359
+ return true;
1360
+ }
1361
+ if (value === false || value === null || value === undefined) {
1362
+ return false;
1363
+ }
1364
+ if (typeof value === 'number') {
1365
+ return value === 1;
1366
+ }
1367
+ if (typeof value === 'string') {
1368
+ var normalized = value.trim().toLowerCase();
1369
+ return ['1', 'true', 'yes', 'y', 'on'].includes(normalized);
1370
+ }
1371
+ return false;
1372
+ };
1373
+ SubscriptionManager.prototype.parsePositiveNumber = function (value) {
1374
+ if (typeof value === 'number' && Number.isFinite(value) && value > 0) {
1375
+ return value;
1376
+ }
1377
+ if (typeof value === 'string' && value.trim().length) {
1378
+ var parsed = parseInt(value, 10);
1379
+ if (Number.isFinite(parsed) && parsed > 0) {
1380
+ return parsed;
1381
+ }
1382
+ }
1383
+ return 0;
1384
+ };
1385
+ SubscriptionManager.prototype.parseSampleRate = function (value, fallback) {
1386
+ var parsed = parseFloat(value !== null && value !== void 0 ? value : '');
1387
+ if (Number.isFinite(parsed) && parsed > 0 && parsed <= 1) {
1388
+ return parsed;
1389
+ }
1390
+ return fallback;
1391
+ };
1392
+ SubscriptionManager.prototype.shouldLogSubSend = function (durationMs, payloadBytes) {
1393
+ if (!this._subSendLogEnabled) {
1394
+ return false;
1395
+ }
1396
+ if (this._subSendLogSampleRate < 1 && Math.random() > this._subSendLogSampleRate) {
1397
+ return false;
1398
+ }
1399
+ if (this._subSendDebug) {
1400
+ return true;
1401
+ }
1402
+ if (this._subSendLogThresholdMs > 0 && durationMs >= this._subSendLogThresholdMs) {
1403
+ return true;
1404
+ }
1405
+ if (payloadBytes !== null && this._subSendLogBytes > 0 && payloadBytes >= this._subSendLogBytes) {
1406
+ return true;
1407
+ }
1408
+ return false;
1409
+ };
1410
+ SubscriptionManager.prototype.logSubSend = function (sub, details) {
1411
+ if (!details.clients) {
1412
+ return;
1413
+ }
1414
+ if (!this.shouldLogSubSend(details.durationMs, details.payloadBytes)) {
1415
+ return;
1416
+ }
1417
+ console.log(new Date(), '[Sub Send]', JSON.stringify({
1418
+ publication: sub.publication,
1419
+ subscriptionKey: sub.subscriptionKey,
1420
+ clients: details.clients,
1421
+ durationMs: details.durationMs,
1422
+ payloadBytes: details.payloadBytes,
1423
+ collection: details.collection,
1424
+ type: details.type
1425
+ }));
1426
+ };
1427
+ SubscriptionManager.prototype.isAiPublication = function (publication) {
1428
+ return publication === 'aiTerminalMessages' || publication === 'aiTerminalConversations';
1429
+ };
1430
+ SubscriptionManager.prototype.logAiSubSend = function (details) {
1431
+ var _a;
1432
+ if (!this._aiWorkerDebug) {
1433
+ return;
1434
+ }
1435
+ if (!this.isAiPublication(details.publication)) {
1436
+ return;
1437
+ }
1438
+ console.log(new Date(), '[AI Worker Debug] subSend', JSON.stringify({
1439
+ publication: details.publication,
1440
+ subscriptionKey: details.subscriptionKey,
1441
+ messageId: details.messageId,
1442
+ id_socket: details.id_socket,
1443
+ clients: details.clients,
1444
+ payloadBytes: details.payloadBytes,
1445
+ packed: details.packed,
1446
+ cacheHit: (_a = details.cacheHit) !== null && _a !== void 0 ? _a : false,
1447
+ collection: details.collection,
1448
+ type: details.type,
1449
+ subscriptionData: details.subscriptionData
1450
+ }));
1451
+ };
1452
+ SubscriptionManager.prototype.logAiSubscriptionEvent = function (event, details) {
1453
+ if (!this._aiSubscriptionDebug) {
1454
+ return;
1455
+ }
1456
+ if (!this.isAiPublication(details.publication)) {
1457
+ return;
1458
+ }
1459
+ var wsId = details.id_socket || 'unknown';
1460
+ var key = "".concat(event, ":").concat(wsId, ":").concat(details.publication, ":").concat(details.subscriptionKey);
1461
+ var now = Date.now();
1462
+ var lastAt = this._aiSubLastAt.get(key);
1463
+ var deltaMs = lastAt ? now - lastAt : null;
1464
+ this._aiSubLastAt.set(key, now);
1465
+ console.log(new Date(), "[AI Sub Debug] ".concat(event), JSON.stringify({
1466
+ publication: details.publication,
1467
+ subscriptionKey: details.subscriptionKey,
1468
+ messageId: details.messageId,
1469
+ messageRoute: details.messageRoute,
1470
+ id_socket: wsId,
1471
+ clients: details.clients,
1472
+ deltaMs: deltaMs,
1473
+ subscriptionData: details.subscriptionData
1474
+ }));
1475
+ };
1476
+ SubscriptionManager.prototype.connectDebug = function (message, details) {
1477
+ if (!this._connectDebug) {
1478
+ return;
1479
+ }
1480
+ if (details) {
1481
+ console.log(new Date(), '[Connect Debug]', message, JSON.stringify(details));
1482
+ }
1483
+ else {
1484
+ console.log(new Date(), '[Connect Debug]', message);
1485
+ }
1486
+ };
1487
+ SubscriptionManager.prototype.parsePositiveFloat = function (value) {
1488
+ if (typeof value === 'number' && Number.isFinite(value) && value > 0) {
1489
+ return value;
1490
+ }
1491
+ if (typeof value === 'string' && value.trim().length) {
1492
+ var parsed = parseFloat(value);
1493
+ if (Number.isFinite(parsed) && parsed > 0) {
1494
+ return parsed;
1495
+ }
1496
+ }
1497
+ return 0;
1498
+ };
1499
+ SubscriptionManager.prototype.isChangeStreamUnsupported = function (error) {
1500
+ var code = typeof (error === null || error === void 0 ? void 0 : error.code) === 'number' ? error.code : null;
1501
+ var codeName = typeof (error === null || error === void 0 ? void 0 : error.codeName) === 'string' ? error.codeName.toLowerCase() : '';
1502
+ var message = typeof (error === null || error === void 0 ? void 0 : error.message) === 'string' ? error.message.toLowerCase() : '';
1503
+ if (code === 40573) {
1504
+ return true;
1505
+ }
1506
+ if (codeName && codeName.includes('changestream')) {
1507
+ return true;
1508
+ }
1509
+ if (message.includes('change stream') && message.includes('replica')) {
1510
+ return true;
1511
+ }
1512
+ if (message.includes('change stream') && message.includes('not supported')) {
1513
+ return true;
1514
+ }
1515
+ return false;
1516
+ };
1517
+ SubscriptionManager.prototype.isResumeTokenInvalid = function (error) {
1518
+ var code = typeof (error === null || error === void 0 ? void 0 : error.code) === 'number' ? error.code : null;
1519
+ var codeName = typeof (error === null || error === void 0 ? void 0 : error.codeName) === 'string' ? error.codeName.toLowerCase() : '';
1520
+ var message = typeof (error === null || error === void 0 ? void 0 : error.message) === 'string' ? error.message.toLowerCase() : '';
1521
+ if (code === 286) {
1522
+ // ChangeStreamHistoryLost
1523
+ return true;
1524
+ }
1525
+ if (codeName.includes('changestreamhistorylost')) {
1526
+ return true;
1527
+ }
1528
+ if (message.includes('resume token') && (message.includes('not found') ||
1529
+ message.includes('not possible') ||
1530
+ message.includes('no longer') ||
1531
+ message.includes('not in the oplog') ||
1532
+ message.includes('too old'))) {
1533
+ return true;
1534
+ }
1535
+ if (message.includes('resume of change stream') && message.includes('not possible')) {
1536
+ return true;
1537
+ }
1538
+ return false;
1539
+ };
1540
+ SubscriptionManager.prototype.isChangeStreamDocumentTooLarge = function (error) {
1541
+ var code = typeof (error === null || error === void 0 ? void 0 : error.code) === 'number' ? error.code : null;
1542
+ var codeName = typeof (error === null || error === void 0 ? void 0 : error.codeName) === 'string' ? error.codeName.toLowerCase() : '';
1543
+ var message = typeof (error === null || error === void 0 ? void 0 : error.message) === 'string' ? error.message.toLowerCase() : '';
1544
+ if (code === 10334) {
1545
+ return true;
1546
+ }
1547
+ if (codeName.includes('bsonobjecttoolarge')) {
1548
+ return true;
1549
+ }
1550
+ if (message.includes('bsonobj size') && message.includes('16mb')) {
1551
+ return true;
1552
+ }
1553
+ return false;
1554
+ };
1555
+ SubscriptionManager.prototype.enableLocalOplogFallback = function (reason, error) {
1556
+ return __awaiter(this, void 0, void 0, function () {
1557
+ var _a, message;
1558
+ var _this = this;
1559
+ return __generator(this, function (_b) {
1560
+ switch (_b.label) {
1561
+ case 0:
1562
+ if (this._useLocalOplog) {
1563
+ return [2 /*return*/];
1564
+ }
1565
+ this._useLocalOplog = true;
1566
+ this._oplogMode = 'local';
1567
+ this._oplogRetryCount = 0;
1568
+ if (!(this._oplog$ && !this._oplog$.closed)) return [3 /*break*/, 5];
1569
+ _b.label = 1;
1570
+ case 1:
1571
+ _b.trys.push([1, 3, , 4]);
1572
+ this._oplog$.removeAllListeners();
1573
+ return [4 /*yield*/, this._oplog$.close()];
1574
+ case 2:
1575
+ _b.sent();
1576
+ return [3 /*break*/, 4];
1577
+ case 3:
1578
+ _a = _b.sent();
1579
+ return [3 /*break*/, 4];
1580
+ case 4:
1581
+ this._oplog$ = null;
1582
+ _b.label = 5;
1583
+ case 5:
1584
+ message = typeof (error === null || error === void 0 ? void 0 : error.message) === 'string' ? error.message : '';
1585
+ console.log(new Date(), 'oplog fallback enabled', reason, message ? "(".concat(message, ")") : '');
1586
+ this.queueFullResync('oplog-fallback-enabled');
1587
+ if (this._localOplogResyncIntervalMs > 0 && !this._localOplogResyncTimer) {
1588
+ this._localOplogResyncTimer = setInterval(function () {
1589
+ _this.queueFullResync('oplog-fallback-poll');
1590
+ }, this._localOplogResyncIntervalMs);
1591
+ }
1592
+ return [2 /*return*/];
1593
+ }
1594
+ });
1595
+ });
1596
+ };
1597
+ SubscriptionManager.prototype.getUseLocalOplog = function () {
1598
+ return this._useLocalOplog;
1599
+ };
1600
+ SubscriptionManager.prototype.handleMongoReconnect = function (reason) {
1601
+ return __awaiter(this, void 0, void 0, function () {
1602
+ var resumeToken, _a, error_3;
1603
+ return __generator(this, function (_b) {
1604
+ switch (_b.label) {
1605
+ case 0:
1606
+ if (this._useLocalOplog) {
1607
+ this.queueFullResync("mongo-reconnect:".concat(reason));
1608
+ return [2 /*return*/];
1609
+ }
1610
+ _b.label = 1;
1611
+ case 1:
1612
+ _b.trys.push([1, 5, , 6]);
1613
+ _a = this._lastResumeToken;
1614
+ if (_a) return [3 /*break*/, 3];
1615
+ return [4 /*yield*/, this.loadResumeToken()];
1616
+ case 2:
1617
+ _a = (_b.sent());
1618
+ _b.label = 3;
1619
+ case 3:
1620
+ resumeToken = _a;
1621
+ return [4 /*yield*/, this.tailOpLog(resumeToken || undefined)];
1622
+ case 4:
1623
+ _b.sent();
1624
+ return [3 /*break*/, 6];
1625
+ case 5:
1626
+ error_3 = _b.sent();
1627
+ console.log(new Date(), 'Sub Manager', 'Mongo reconnect: tailOpLog restart failed', reason, error_3);
1628
+ return [3 /*break*/, 6];
1629
+ case 6:
1630
+ this.queueFullResync("mongo-reconnect:".concat(reason));
1631
+ return [2 /*return*/];
1632
+ }
1633
+ });
1634
+ });
1635
+ };
1636
+ SubscriptionManager.prototype.notifyLocalOplog = function (collection, type) {
1637
+ var _this = this;
1638
+ if (type === void 0) { type = 'update'; }
1639
+ if (!this._useLocalOplog) {
1640
+ return;
1641
+ }
1642
+ setImmediate(function () { return __awaiter(_this, void 0, void 0, function () {
1643
+ var _a;
1644
+ return __generator(this, function (_b) {
1645
+ switch (_b.label) {
1646
+ case 0:
1647
+ _b.trys.push([0, 2, , 3]);
1648
+ return [4 /*yield*/, this.invalidatePubsCache(collection, type)];
1649
+ case 1:
1650
+ _b.sent();
1651
+ return [3 /*break*/, 3];
1652
+ case 2:
1653
+ _a = _b.sent();
1654
+ return [3 /*break*/, 3];
1655
+ case 3: return [2 /*return*/];
1656
+ }
1657
+ });
1658
+ }); });
1659
+ };
1660
+ SubscriptionManager.prototype.getResumeTokenDocId = function () {
1661
+ if (process.env.NODE_APP_INSTANCE) {
1662
+ return "oplog:".concat(process.env.NODE_APP_INSTANCE);
1663
+ }
1664
+ // Fallback: avoid cross-process contention when NODE_APP_INSTANCE is not set.
1665
+ return "oplog:".concat(process.pid);
1666
+ };
1667
+ SubscriptionManager.prototype.loadResumeToken = function () {
1668
+ return __awaiter(this, void 0, void 0, function () {
1669
+ var db_1, loadToken, mongoManager, _a, error_4;
1670
+ var _this = this;
1671
+ return __generator(this, function (_b) {
1672
+ switch (_b.label) {
1673
+ case 0:
1674
+ _b.trys.push([0, 5, , 6]);
1675
+ db_1 = resolveio_server_app_1.ResolveIOServer.getMainDB();
1676
+ if (!db_1) {
1677
+ return [2 /*return*/, null];
1678
+ }
1679
+ loadToken = function () { return __awaiter(_this, void 0, void 0, function () {
1680
+ var collection, doc, token, maxAgeMs, ageMs;
1681
+ return __generator(this, function (_a) {
1682
+ switch (_a.label) {
1683
+ case 0:
1684
+ collection = db_1.collection(this.RESUME_TOKEN_COLLECTION);
1685
+ return [4 /*yield*/, collection.findOne({ _id: this.getResumeTokenDocId() })];
1686
+ case 1:
1687
+ doc = _a.sent();
1688
+ token = (doc === null || doc === void 0 ? void 0 : doc.token) || null;
1689
+ if (!token) {
1690
+ return [2 /*return*/, null];
1691
+ }
1692
+ maxAgeMs = this.resolveResumeTokenMaxAgeMs();
1693
+ if (!(maxAgeMs > 0 && (doc === null || doc === void 0 ? void 0 : doc.updatedAt) instanceof Date)) return [3 /*break*/, 3];
1694
+ ageMs = Date.now() - doc.updatedAt.getTime();
1695
+ if (!(ageMs > maxAgeMs)) return [3 /*break*/, 3];
1696
+ return [4 /*yield*/, collection.deleteOne({ _id: this.getResumeTokenDocId() })];
1697
+ case 2:
1698
+ _a.sent();
1699
+ console.log(new Date(), 'Sub Manager', 'Resume token expired, clearing', (0, common_1.round)(ageMs / 1000), 's');
1700
+ return [2 /*return*/, null];
1701
+ case 3: return [2 /*return*/, token];
1702
+ }
1703
+ });
1704
+ }); };
1705
+ mongoManager = resolveio_server_app_1.ResolveIOServer.getMongoManager();
1706
+ if (!mongoManager) return [3 /*break*/, 2];
1707
+ return [4 /*yield*/, mongoManager.runWithoutSession(loadToken)];
1708
+ case 1:
1709
+ _a = _b.sent();
1710
+ return [3 /*break*/, 4];
1711
+ case 2: return [4 /*yield*/, loadToken()];
1712
+ case 3:
1713
+ _a = _b.sent();
1714
+ _b.label = 4;
1715
+ case 4: return [2 /*return*/, _a];
1716
+ case 5:
1717
+ error_4 = _b.sent();
1718
+ console.log(new Date(), 'Sub Manager', 'Failed to load oplog resume token', error_4);
1719
+ return [2 /*return*/, null];
1720
+ case 6: return [2 /*return*/];
1721
+ }
1722
+ });
1723
+ });
1724
+ };
1725
+ SubscriptionManager.prototype.saveResumeToken = function (token) {
1726
+ return __awaiter(this, void 0, void 0, function () {
1727
+ var db_2, saveToken, mongoManager, error_5;
1728
+ var _this = this;
1729
+ return __generator(this, function (_a) {
1730
+ switch (_a.label) {
1731
+ case 0:
1732
+ if (!token) {
1733
+ return [2 /*return*/];
1734
+ }
1735
+ _a.label = 1;
1736
+ case 1:
1737
+ _a.trys.push([1, 6, , 7]);
1738
+ db_2 = resolveio_server_app_1.ResolveIOServer.getMainDB();
1739
+ if (!db_2) {
1740
+ return [2 /*return*/];
1741
+ }
1742
+ saveToken = function () { return __awaiter(_this, void 0, void 0, function () {
1743
+ return __generator(this, function (_a) {
1744
+ switch (_a.label) {
1745
+ case 0: return [4 /*yield*/, db_2.collection(this.RESUME_TOKEN_COLLECTION).updateOne({ _id: this.getResumeTokenDocId() }, { $set: { token: token, updatedAt: new Date() } }, { upsert: true })];
1746
+ case 1:
1747
+ _a.sent();
1748
+ return [2 /*return*/];
1749
+ }
1750
+ });
1751
+ }); };
1752
+ mongoManager = resolveio_server_app_1.ResolveIOServer.getMongoManager();
1753
+ if (!mongoManager) return [3 /*break*/, 3];
1754
+ return [4 /*yield*/, mongoManager.runWithoutSession(saveToken)];
1755
+ case 2:
1756
+ _a.sent();
1757
+ return [3 /*break*/, 5];
1758
+ case 3: return [4 /*yield*/, saveToken()];
1759
+ case 4:
1760
+ _a.sent();
1761
+ _a.label = 5;
1762
+ case 5: return [3 /*break*/, 7];
1763
+ case 6:
1764
+ error_5 = _a.sent();
1765
+ console.log(new Date(), 'Sub Manager', 'Failed to persist oplog resume token', error_5);
1766
+ return [3 /*break*/, 7];
1767
+ case 7: return [2 /*return*/];
1768
+ }
1769
+ });
1770
+ });
1771
+ };
1772
+ SubscriptionManager.prototype.clearResumeToken = function () {
1773
+ return __awaiter(this, void 0, void 0, function () {
1774
+ var db_3, clearToken, mongoManager, error_6;
1775
+ var _this = this;
1776
+ return __generator(this, function (_a) {
1777
+ switch (_a.label) {
1778
+ case 0:
1779
+ _a.trys.push([0, 5, , 6]);
1780
+ db_3 = resolveio_server_app_1.ResolveIOServer.getMainDB();
1781
+ if (!db_3) {
1782
+ return [2 /*return*/];
1783
+ }
1784
+ clearToken = function () { return __awaiter(_this, void 0, void 0, function () {
1785
+ return __generator(this, function (_a) {
1786
+ switch (_a.label) {
1787
+ case 0: return [4 /*yield*/, db_3.collection(this.RESUME_TOKEN_COLLECTION)
1788
+ .deleteOne({ _id: this.getResumeTokenDocId() })];
1789
+ case 1:
1790
+ _a.sent();
1791
+ return [2 /*return*/];
1792
+ }
1793
+ });
1794
+ }); };
1795
+ mongoManager = resolveio_server_app_1.ResolveIOServer.getMongoManager();
1796
+ if (!mongoManager) return [3 /*break*/, 2];
1797
+ return [4 /*yield*/, mongoManager.runWithoutSession(clearToken)];
1798
+ case 1:
1799
+ _a.sent();
1800
+ return [3 /*break*/, 4];
1801
+ case 2: return [4 /*yield*/, clearToken()];
1802
+ case 3:
1803
+ _a.sent();
1804
+ _a.label = 4;
1805
+ case 4: return [3 /*break*/, 6];
1806
+ case 5:
1807
+ error_6 = _a.sent();
1808
+ console.log(new Date(), 'Sub Manager', 'Failed to clear oplog resume token', error_6);
1809
+ return [3 /*break*/, 6];
1810
+ case 6: return [2 /*return*/];
1811
+ }
1812
+ });
1813
+ });
1814
+ };
1815
+ SubscriptionManager.prototype.queueResumeTokenSave = function (token, options) {
1816
+ var _this = this;
1817
+ if (!token) {
1818
+ return;
1819
+ }
1820
+ this._lastResumeToken = token;
1821
+ var now = Date.now();
1822
+ var shouldSave = !!(options === null || options === void 0 ? void 0 : options.force) || (now - this._lastResumeTokenSaveMs) >= this.RESUME_TOKEN_SAVE_INTERVAL_MS;
1823
+ if (!shouldSave) {
1824
+ return;
1825
+ }
1826
+ if (this._resumeTokenSavePromise !== null) {
1827
+ if (options === null || options === void 0 ? void 0 : options.force) {
1828
+ this._resumeTokenSaveForcePending = true;
1829
+ }
1830
+ return;
1831
+ }
1832
+ this._lastResumeTokenSaveMs = now;
1833
+ var tokenToSave = this._lastResumeToken;
1834
+ this._resumeTokenSavePromise = (function () { return __awaiter(_this, void 0, void 0, function () {
1835
+ return __generator(this, function (_a) {
1836
+ switch (_a.label) {
1837
+ case 0:
1838
+ _a.trys.push([0, , 2, 3]);
1839
+ return [4 /*yield*/, this.saveResumeToken(tokenToSave)];
1840
+ case 1:
1841
+ _a.sent();
1842
+ return [3 /*break*/, 3];
1843
+ case 2:
1844
+ this._resumeTokenSavePromise = null;
1845
+ if (this._resumeTokenSaveForcePending) {
1846
+ this._resumeTokenSaveForcePending = false;
1847
+ this.queueResumeTokenSave(this._lastResumeToken, { force: true });
1848
+ }
1849
+ return [7 /*endfinally*/];
1850
+ case 3: return [2 /*return*/];
1851
+ }
1852
+ });
1853
+ }); })();
1854
+ };
1855
+ SubscriptionManager.prototype.queueFullResync = function (reason) {
1856
+ var _this = this;
1857
+ if (this._fullResyncPromise !== null) {
1858
+ return;
1859
+ }
1860
+ this._fullResyncPromise = (function () { return __awaiter(_this, void 0, void 0, function () {
1861
+ return __generator(this, function (_a) {
1862
+ switch (_a.label) {
1863
+ case 0:
1864
+ _a.trys.push([0, , 2, 3]);
1865
+ return [4 /*yield*/, this.fullResyncSubscriptions(reason)];
1866
+ case 1:
1867
+ _a.sent();
1868
+ return [3 /*break*/, 3];
1869
+ case 2:
1870
+ this._fullResyncPromise = null;
1871
+ return [7 /*endfinally*/];
1872
+ case 3: return [2 /*return*/];
1873
+ }
1874
+ });
1875
+ }); })();
1876
+ };
1877
+ SubscriptionManager.prototype.fullResyncSubscriptions = function (reason) {
1878
+ return __awaiter(this, void 0, void 0, function () {
1879
+ var subs, subs_1, subs_1_1, sub, pub, _a, _b, client, ws, _c, e_4_1, e_5_1, error_7;
1880
+ var e_5, _d, e_4, _e;
1881
+ var _f, _g, _h, _j, _k;
1882
+ return __generator(this, function (_l) {
1883
+ switch (_l.label) {
1884
+ case 0:
1885
+ _l.trys.push([0, 20, , 21]);
1886
+ if (this._enableDebug) {
1887
+ console.log(new Date(), 'Sub Manager', 'Full subscription resync', reason, this._subscriptions.length);
1888
+ }
1889
+ try {
1890
+ (_g = (_f = resolveio_server_app_1.ResolveIOServer.getMongoManager()) === null || _f === void 0 ? void 0 : _f.clearQueryCache) === null || _g === void 0 ? void 0 : _g.call(_f, reason);
1891
+ }
1892
+ catch (_m) {
1893
+ // ignore cache-clear errors
1894
+ }
1895
+ try {
1896
+ (_j = (_h = this._nodeCache) === null || _h === void 0 ? void 0 : _h.flushAll) === null || _j === void 0 ? void 0 : _j.call(_h);
1897
+ }
1898
+ catch (_o) {
1899
+ // ignore cache-clear errors
1900
+ }
1901
+ subs = this._subscriptions.slice();
1902
+ _l.label = 1;
1903
+ case 1:
1904
+ _l.trys.push([1, 17, 18, 19]);
1905
+ subs_1 = __values(subs), subs_1_1 = subs_1.next();
1906
+ _l.label = 2;
1907
+ case 2:
1908
+ if (!!subs_1_1.done) return [3 /*break*/, 16];
1909
+ sub = subs_1_1.value;
1910
+ if (!((_k = sub === null || sub === void 0 ? void 0 : sub.clients) === null || _k === void 0 ? void 0 : _k.length)) {
1911
+ return [3 /*break*/, 15];
1912
+ }
1913
+ pub = this._publications[sub.publication];
1914
+ if (!pub) {
1915
+ return [3 /*break*/, 15];
1916
+ }
1917
+ if (sub.running) {
1918
+ sub.runAgain = true;
1919
+ return [3 /*break*/, 15];
1920
+ }
1921
+ if (!pub.user_specific) return [3 /*break*/, 13];
1922
+ _l.label = 3;
1923
+ case 3:
1924
+ _l.trys.push([3, 10, 11, 12]);
1925
+ _a = (e_4 = void 0, __values(sub.clients)), _b = _a.next();
1926
+ _l.label = 4;
1927
+ case 4:
1928
+ if (!!_b.done) return [3 /*break*/, 9];
1929
+ client = _b.value;
1930
+ ws = this._websocketManager.getWebSocket(client.id_socket);
1931
+ if (!((ws === null || ws === void 0 ? void 0 : ws.readyState) === WebSocket.OPEN)) return [3 /*break*/, 8];
1932
+ _l.label = 5;
1933
+ case 5:
1934
+ _l.trys.push([5, 7, , 8]);
1935
+ return [4 /*yield*/, this.sendDataToOneWithRetry(ws, client.messageId, sub, '', reason)];
1936
+ case 6:
1937
+ _l.sent();
1938
+ return [3 /*break*/, 8];
1939
+ case 7:
1940
+ _c = _l.sent();
1941
+ return [3 /*break*/, 8];
1942
+ case 8:
1943
+ _b = _a.next();
1944
+ return [3 /*break*/, 4];
1945
+ case 9: return [3 /*break*/, 12];
1946
+ case 10:
1947
+ e_4_1 = _l.sent();
1948
+ e_4 = { error: e_4_1 };
1949
+ return [3 /*break*/, 12];
1950
+ case 11:
1951
+ try {
1952
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
1953
+ }
1954
+ finally { if (e_4) throw e_4.error; }
1955
+ return [7 /*endfinally*/];
1956
+ case 12: return [3 /*break*/, 15];
1957
+ case 13: return [4 /*yield*/, this.sendDataToAllWithRetry(sub, '', reason)];
1958
+ case 14:
1959
+ _l.sent();
1960
+ _l.label = 15;
1961
+ case 15:
1962
+ subs_1_1 = subs_1.next();
1963
+ return [3 /*break*/, 2];
1964
+ case 16: return [3 /*break*/, 19];
1965
+ case 17:
1966
+ e_5_1 = _l.sent();
1967
+ e_5 = { error: e_5_1 };
1968
+ return [3 /*break*/, 19];
1969
+ case 18:
1970
+ try {
1971
+ if (subs_1_1 && !subs_1_1.done && (_d = subs_1.return)) _d.call(subs_1);
1972
+ }
1973
+ finally { if (e_5) throw e_5.error; }
1974
+ return [7 /*endfinally*/];
1975
+ case 19: return [3 /*break*/, 21];
1976
+ case 20:
1977
+ error_7 = _l.sent();
1978
+ console.log(new Date(), 'Sub Manager', 'Full resync failed', reason, error_7);
1979
+ return [3 /*break*/, 21];
1980
+ case 21: return [2 /*return*/];
1981
+ }
1982
+ });
1983
+ });
1984
+ };
1985
+ // Watch (tail) Mongo's operation log on the entire database (all insert/modify/delete will trigger this function)
1986
+ SubscriptionManager.prototype.tailOpLog = function (resumeToken) {
1987
+ return __awaiter(this, void 0, void 0, function () {
1988
+ var watchDatabases, pipeline, lastResumeToken_1, startedWithResumeToken_1, sawChangeEvent_1, resumeTokenInvalidated_1, streamStartedAtMs_1, error_8, innerError_1, error_9;
1989
+ var _this = this;
1990
+ return __generator(this, function (_a) {
1991
+ switch (_a.label) {
1992
+ case 0:
1993
+ if (this._useLocalOplog) {
1994
+ return [2 /*return*/];
1995
+ }
1996
+ if (!(this._oplog$ && !this._oplog$.closed)) return [3 /*break*/, 2];
1997
+ this._oplog$.removeAllListeners();
1998
+ return [4 /*yield*/, this._oplog$.close()];
1999
+ case 1:
2000
+ _a.sent();
2001
+ this._oplog$ = null;
2002
+ _a.label = 2;
2003
+ case 2:
2004
+ // eslint-disable-next-line no-restricted-syntax
2005
+ return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 1000); })];
2006
+ case 3:
2007
+ // eslint-disable-next-line no-restricted-syntax
2008
+ _a.sent();
2009
+ if (!(!this._oplog$ || this._oplog$.closed)) return [3 /*break*/, 27];
2010
+ this._oplogRetryCount += 1;
2011
+ if (this._oplogRetryCount > 5) {
2012
+ console.error('****************** TAIL OPLOG ERROR, RETRYING A BUNCH OF TIMES, KILLING PROCESS **************************');
2013
+ process.exit(1);
2014
+ }
2015
+ watchDatabases = this.getWatchedDatabases();
2016
+ pipeline = [
2017
+ {
2018
+ $match: {
2019
+ $and: __spreadArray(__spreadArray([], __read((watchDatabases.length ? [{ 'ns.db': { $in: watchDatabases } }] : [])), false), [
2020
+ { 'ns.coll': { $nin: [
2021
+ 'log-method-latencies',
2022
+ 'log-subscriptions',
2023
+ 'logs',
2024
+ 'counters',
2025
+ 'cron-job-histories',
2026
+ 'email-histories',
2027
+ 'qb-soap-request-histories',
2028
+ 'qb-soap-request-responses',
2029
+ 'qb-soap-requests',
2030
+ 'qb-soap-retries',
2031
+ this.RESUME_TOKEN_COLLECTION
2032
+ ] } },
2033
+ { 'ns.coll': { $not: /.*\.versions$/ } },
2034
+ { 'ns.coll': { $not: /^monitor-/ } },
2035
+ ], false)
2036
+ },
2037
+ },
2038
+ ];
2039
+ startedWithResumeToken_1 = false;
2040
+ sawChangeEvent_1 = false;
2041
+ resumeTokenInvalidated_1 = false;
2042
+ streamStartedAtMs_1 = Date.now();
2043
+ if (!resumeToken) return [3 /*break*/, 20];
2044
+ lastResumeToken_1 = resumeToken;
2045
+ _a.label = 4;
2046
+ case 4:
2047
+ _a.trys.push([4, 5, , 19]);
2048
+ this._oplog$ = resolveio_server_app_1.ResolveIOServer.getMongoConnection().watch(pipeline, { resumeAfter: resumeToken, fullDocument: 'updateLookup' });
2049
+ startedWithResumeToken_1 = true;
2050
+ return [3 /*break*/, 19];
2051
+ case 5:
2052
+ error_8 = _a.sent();
2053
+ if (!this.isChangeStreamUnsupported(error_8)) return [3 /*break*/, 7];
2054
+ return [4 /*yield*/, this.enableLocalOplogFallback('change-stream-unsupported', error_8)];
2055
+ case 6:
2056
+ _a.sent();
2057
+ return [2 /*return*/];
2058
+ case 7:
2059
+ if (!this.isChangeStreamDocumentTooLarge(error_8)) return [3 /*break*/, 10];
2060
+ return [4 /*yield*/, this.clearResumeToken()];
2061
+ case 8:
2062
+ _a.sent();
2063
+ lastResumeToken_1 = null;
2064
+ this.queueFullResync('oplog-change-stream-document-too-large');
2065
+ return [4 /*yield*/, this.enableLocalOplogFallback('change-stream-document-too-large', error_8)];
2066
+ case 9:
2067
+ _a.sent();
2068
+ return [2 /*return*/];
2069
+ case 10:
2070
+ if (!this._oplog$) return [3 /*break*/, 12];
2071
+ this._oplog$.removeAllListeners();
2072
+ return [4 /*yield*/, this._oplog$.close()];
2073
+ case 11:
2074
+ _a.sent();
2075
+ this._oplog$ = null;
2076
+ _a.label = 12;
2077
+ case 12: return [4 /*yield*/, this.clearResumeToken()];
2078
+ case 13:
2079
+ _a.sent();
2080
+ lastResumeToken_1 = null;
2081
+ console.log(new Date(), 'oplog resumeAfter failed, starting fresh', error_8);
2082
+ _a.label = 14;
2083
+ case 14:
2084
+ _a.trys.push([14, 15, , 18]);
2085
+ this._oplog$ = resolveio_server_app_1.ResolveIOServer.getMongoConnection().watch(pipeline, { fullDocument: 'updateLookup' });
2086
+ startedWithResumeToken_1 = false;
2087
+ return [3 /*break*/, 18];
2088
+ case 15:
2089
+ innerError_1 = _a.sent();
2090
+ if (!this.isChangeStreamUnsupported(innerError_1)) return [3 /*break*/, 17];
2091
+ return [4 /*yield*/, this.enableLocalOplogFallback('change-stream-unsupported', innerError_1)];
2092
+ case 16:
2093
+ _a.sent();
2094
+ return [2 /*return*/];
2095
+ case 17: throw innerError_1;
2096
+ case 18:
2097
+ this.queueFullResync('oplog-resumeAfter-failed');
2098
+ return [3 /*break*/, 19];
2099
+ case 19: return [3 /*break*/, 26];
2100
+ case 20:
2101
+ _a.trys.push([20, 21, , 26]);
2102
+ this._oplog$ = resolveio_server_app_1.ResolveIOServer.getMongoConnection().watch(pipeline, { fullDocument: 'updateLookup' });
2103
+ return [3 /*break*/, 26];
2104
+ case 21:
2105
+ error_9 = _a.sent();
2106
+ if (!this.isChangeStreamUnsupported(error_9)) return [3 /*break*/, 23];
2107
+ return [4 /*yield*/, this.enableLocalOplogFallback('change-stream-unsupported', error_9)];
2108
+ case 22:
2109
+ _a.sent();
2110
+ return [2 /*return*/];
2111
+ case 23:
2112
+ if (!this.isChangeStreamDocumentTooLarge(error_9)) return [3 /*break*/, 25];
2113
+ return [4 /*yield*/, this.enableLocalOplogFallback('change-stream-document-too-large', error_9)];
2114
+ case 24:
2115
+ _a.sent();
2116
+ return [2 /*return*/];
2117
+ case 25: throw error_9;
2118
+ case 26:
2119
+ console.log(new Date(), 'oplog started', startedWithResumeToken_1 ? '(resumeAfter)' : '');
2120
+ this._oplog$.on('change', function (doc) { return __awaiter(_this, void 0, void 0, function () {
2121
+ var collection, fullDocument, docId, flag, dependencyFlag;
2122
+ return __generator(this, function (_a) {
2123
+ switch (_a.label) {
2124
+ case 0:
2125
+ sawChangeEvent_1 = true;
2126
+ this._oplogRetryCount = 0;
2127
+ if (!doc.ns) return [3 /*break*/, 14];
2128
+ if (this._enableDebug) {
2129
+ console.log(new Date(), 'Oplog Hit', doc.ns);
2130
+ }
2131
+ collection = doc.ns.coll;
2132
+ if (!this._debugOplogCollections.some(function (a) { return a.collection === doc.ns.coll && a.type === doc.operationType; })) {
2133
+ this._debugOplogCollections.push({
2134
+ collection: doc.ns.coll,
2135
+ type: doc.operationType,
2136
+ hits: 1
2137
+ });
2138
+ }
2139
+ else {
2140
+ this._debugOplogCollections.find(function (a) { return a.collection === doc.ns.coll && a.type === doc.operationType; }).hits += 1;
2141
+ }
2142
+ if (!collection) return [3 /*break*/, 10];
2143
+ this._debugOplogHits += 1;
2144
+ fullDocument = doc['fullDocument'];
2145
+ docId = doc.documentKey && doc.documentKey['_id'] !== undefined ? doc.documentKey['_id'] : (fullDocument && fullDocument['_id'] !== undefined ? fullDocument['_id'] : null);
2146
+ if (!(doc.operationType === 'insert')) return [3 /*break*/, 5];
2147
+ if (!(collection === 'support-tickets')) return [3 /*break*/, 2];
2148
+ if (!(this.serverConfig['ROOT_URL'] === 'https://resolveio.com')) return [3 /*break*/, 2];
2149
+ resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(), 'sendSupportTicketEmail', doc.documentKey['_id']);
2150
+ return [4 /*yield*/, this.invalidatePubsCache(collection, 'insert', docId, fullDocument)];
2151
+ case 1:
2152
+ _a.sent();
2153
+ _a.label = 2;
2154
+ case 2:
2155
+ if (!(collection !== 'method-responses')) return [3 /*break*/, 4];
2156
+ return [4 /*yield*/, this.invalidatePubsCache(collection, 'insert', docId, fullDocument)];
2157
+ case 3:
2158
+ _a.sent();
2159
+ _a.label = 4;
2160
+ case 4: return [3 /*break*/, 10];
2161
+ case 5:
2162
+ if (!(doc.operationType === 'update' || doc.operationType === 'replace')) return [3 /*break*/, 8];
2163
+ if (!(collection !== 'method-responses')) return [3 /*break*/, 7];
2164
+ return [4 /*yield*/, this.invalidatePubsCache(collection, 'update', docId, fullDocument)];
2165
+ case 6:
2166
+ _a.sent();
2167
+ _a.label = 7;
2168
+ case 7: return [3 /*break*/, 10];
2169
+ case 8:
2170
+ if (!(doc.operationType === 'delete')) return [3 /*break*/, 10];
2171
+ if (!(collection !== 'method-responses')) return [3 /*break*/, 10];
2172
+ return [4 /*yield*/, this.invalidatePubsCache(collection, 'delete', docId)];
2173
+ case 9:
2174
+ _a.sent();
2175
+ _a.label = 10;
2176
+ case 10:
2177
+ if (!(collection === 'flags')) return [3 /*break*/, 13];
2178
+ return [4 /*yield*/, flag_collection_1.Flags.findOne({ type: 'Enable Debug' })];
2179
+ case 11:
2180
+ flag = _a.sent();
2181
+ return [4 /*yield*/, flag_collection_1.Flags.findOne({ type: 'Enable Dependency Debug' })];
2182
+ case 12:
2183
+ dependencyFlag = _a.sent();
2184
+ if (flag && flag.value) {
2185
+ this._enableDebug = true;
2186
+ }
2187
+ else {
2188
+ this._enableDebug = false;
2189
+ }
2190
+ if (dependencyFlag && dependencyFlag.value) {
2191
+ this._enableDependencyDebug = true;
2192
+ }
2193
+ else {
2194
+ this._enableDependencyDebug = process.env.ENABLE_DEPENDENCY_DEBUG === 'true';
2195
+ }
2196
+ _a.label = 13;
2197
+ case 13:
2198
+ lastResumeToken_1 = doc._id;
2199
+ this.queueResumeTokenSave(lastResumeToken_1);
2200
+ if ((!process.env.NODE_APP_INSTANCE || process.env.NODE_APP_INSTANCE === '0') && (process.env.IS_WORKERS_ENABLED === 'false' || (process.env.IS_WORKER_INSTANCE === 'true' && process.env.WORKER_INDEX === '0'))) {
2201
+ }
2202
+ _a.label = 14;
2203
+ case 14: return [2 /*return*/];
2204
+ }
2205
+ });
2206
+ }); });
2207
+ this._oplog$.on('error', function (error) { return __awaiter(_this, void 0, void 0, function () {
2208
+ return __generator(this, function (_a) {
2209
+ switch (_a.label) {
2210
+ case 0:
2211
+ console.log(new Date(), 'oplog error', error);
2212
+ if (!this.isChangeStreamUnsupported(error)) return [3 /*break*/, 2];
2213
+ return [4 /*yield*/, this.enableLocalOplogFallback('change-stream-unsupported', error)];
2214
+ case 1:
2215
+ _a.sent();
2216
+ return [2 /*return*/];
2217
+ case 2:
2218
+ if (!this.isChangeStreamDocumentTooLarge(error)) return [3 /*break*/, 5];
2219
+ return [4 /*yield*/, this.clearResumeToken()];
2220
+ case 3:
2221
+ _a.sent();
2222
+ lastResumeToken_1 = null;
2223
+ this.queueFullResync('oplog-change-stream-document-too-large');
2224
+ return [4 /*yield*/, this.enableLocalOplogFallback('change-stream-document-too-large', error)];
2225
+ case 4:
2226
+ _a.sent();
2227
+ return [2 /*return*/];
2228
+ case 5:
2229
+ if (!this.isResumeTokenInvalid(error)) return [3 /*break*/, 7];
2230
+ resumeTokenInvalidated_1 = true;
2231
+ return [4 /*yield*/, this.clearResumeToken()];
2232
+ case 6:
2233
+ _a.sent();
2234
+ lastResumeToken_1 = null;
2235
+ this.queueFullResync('oplog-resume-token-invalid');
2236
+ _a.label = 7;
2237
+ case 7: return [4 /*yield*/, this._oplog$.close()];
2238
+ case 8:
2239
+ _a.sent();
2240
+ return [2 /*return*/];
2241
+ }
2242
+ });
2243
+ }); });
2244
+ this._oplog$.on('end', function () { return __awaiter(_this, void 0, void 0, function () {
2245
+ return __generator(this, function (_a) {
2246
+ switch (_a.label) {
2247
+ case 0:
2248
+ console.log(new Date(), 'oplog end');
2249
+ return [4 /*yield*/, this._oplog$.close()];
2250
+ case 1:
2251
+ _a.sent();
2252
+ return [2 /*return*/];
2253
+ }
2254
+ });
2255
+ }); });
2256
+ this._oplog$.on('close', function () { return __awaiter(_this, void 0, void 0, function () {
2257
+ var retryThresholdReached, shouldAutoHealResumeToken;
2258
+ return __generator(this, function (_a) {
2259
+ switch (_a.label) {
2260
+ case 0:
2261
+ console.log(new Date(), 'oplog close');
2262
+ retryThresholdReached = this._oplogRetryCount >= this.RESUME_TOKEN_AUTO_HEAL_RETRY_THRESHOLD;
2263
+ shouldAutoHealResumeToken = startedWithResumeToken_1
2264
+ && !!lastResumeToken_1
2265
+ && !sawChangeEvent_1
2266
+ && (resumeTokenInvalidated_1 || retryThresholdReached);
2267
+ if (!shouldAutoHealResumeToken) return [3 /*break*/, 2];
2268
+ return [4 /*yield*/, this.clearResumeToken()];
2269
+ case 1:
2270
+ _a.sent();
2271
+ lastResumeToken_1 = null;
2272
+ if (!resumeTokenInvalidated_1) {
2273
+ this.queueFullResync('oplog-resume-token-auto-heal');
2274
+ }
2275
+ console.log(new Date(), 'Sub Manager', 'Auto-healed stale oplog resume token', { retryCount: this._oplogRetryCount, streamLifetimeMs: Date.now() - streamStartedAtMs_1 });
2276
+ _a.label = 2;
2277
+ case 2:
2278
+ if (lastResumeToken_1) {
2279
+ this.queueResumeTokenSave(lastResumeToken_1, { force: true });
2280
+ }
2281
+ this._oplog$.removeAllListeners();
2282
+ this._oplog$ = null;
2283
+ return [4 /*yield*/, this.tailOpLog(lastResumeToken_1)];
2284
+ case 3:
2285
+ _a.sent();
2286
+ return [2 /*return*/];
2287
+ }
2288
+ });
2289
+ }); });
2290
+ _a.label = 27;
2291
+ case 27: return [2 /*return*/];
2292
+ }
2293
+ });
2294
+ });
2295
+ };
2296
+ SubscriptionManager.prototype.packCachePayload = function (data) {
2297
+ if (data === undefined) {
2298
+ return null;
2299
+ }
2300
+ try {
2301
+ var packed = (0, msgpackr_1.pack)(data);
2302
+ return Buffer.isBuffer(packed) ? packed : Buffer.from(packed);
2303
+ }
2304
+ catch (err) {
2305
+ if (this._enableDebug) {
2306
+ console.log(new Date(), 'Sub Cache', 'Pack Failed', (err === null || err === void 0 ? void 0 : err.message) || err);
2307
+ }
2308
+ return null;
2309
+ }
2310
+ };
2311
+ SubscriptionManager.prototype.decodeCachePayload = function (raw) {
2312
+ if (!raw) {
2313
+ return null;
2314
+ }
2315
+ if (Buffer.isBuffer(raw)) {
2316
+ try {
2317
+ return (0, msgpackr_1.unpack)(raw);
2318
+ }
2319
+ catch (err) {
2320
+ if (this._enableDebug) {
2321
+ console.log(new Date(), 'Sub Cache', 'Unpack Failed', (err === null || err === void 0 ? void 0 : err.message) || err);
2322
+ }
2323
+ return null;
2324
+ }
2325
+ }
2326
+ if (raw instanceof Uint8Array) {
2327
+ try {
2328
+ return (0, msgpackr_1.unpack)(raw);
2329
+ }
2330
+ catch (err) {
2331
+ if (this._enableDebug) {
2332
+ console.log(new Date(), 'Sub Cache', 'Unpack Failed', (err === null || err === void 0 ? void 0 : err.message) || err);
2333
+ }
2334
+ return null;
2335
+ }
2336
+ }
2337
+ if (raw instanceof ArrayBuffer) {
2338
+ try {
2339
+ return (0, msgpackr_1.unpack)(new Uint8Array(raw));
2340
+ }
2341
+ catch (err) {
2342
+ if (this._enableDebug) {
2343
+ console.log(new Date(), 'Sub Cache', 'Unpack Failed', (err === null || err === void 0 ? void 0 : err.message) || err);
2344
+ }
2345
+ return null;
2346
+ }
2347
+ }
2348
+ // Treat legacy string caches as invalid to avoid JSON parsing in the hot path.
2349
+ if (typeof raw === 'string') {
2350
+ return null;
2351
+ }
2352
+ return raw;
2353
+ };
2354
+ SubscriptionManager.prototype.getCacheBuffer = function (raw) {
2355
+ if (!raw) {
2356
+ return null;
2357
+ }
2358
+ if (Buffer.isBuffer(raw)) {
2359
+ return raw;
2360
+ }
2361
+ if (raw instanceof Uint8Array) {
2362
+ return Buffer.from(raw);
2363
+ }
2364
+ if (raw instanceof ArrayBuffer) {
2365
+ return Buffer.from(new Uint8Array(raw));
2366
+ }
2367
+ return null;
2368
+ };
2369
+ SubscriptionManager.prototype.shouldCachePayload = function (sub, payload) {
2370
+ if (!payload || payload.byteLength >= 1000000) {
2371
+ return false;
2372
+ }
2373
+ if (sub.collections.includes('logs')) {
2374
+ return false;
2375
+ }
2376
+ if (sub.collections.find(function (a) { return a.endsWith('.versions'); })) {
2377
+ return false;
2378
+ }
2379
+ if (sub.collections.find(function (a) { return a.startsWith('monitor-'); })) {
2380
+ return false;
2381
+ }
2382
+ return true;
2383
+ };
2384
+ SubscriptionManager.prototype.buffersEqual = function (a, b) {
2385
+ if (!a || !b) {
2386
+ return false;
2387
+ }
2388
+ if (a.byteLength !== b.byteLength) {
2389
+ return false;
2390
+ }
2391
+ var toBuffer = function (value) {
2392
+ if (Buffer.isBuffer(value)) {
2393
+ return value;
2394
+ }
2395
+ var buffer = value.buffer;
2396
+ return Buffer.from(buffer, value.byteOffset, value.byteLength);
2397
+ };
2398
+ var aBuf = toBuffer(a);
2399
+ var bBuf = toBuffer(b);
2400
+ return aBuf.equals(bBuf);
2401
+ };
2402
+ SubscriptionManager.prototype.processSubscription = function (sub, ws, messageId) {
2403
+ return __awaiter(this, void 0, void 0, function () {
2404
+ var cachedRaw, cachedBuffer, cacheData, serverRes, _a;
2405
+ return __generator(this, function (_b) {
2406
+ switch (_b.label) {
2407
+ case 0:
2408
+ if (!!this._publications[sub.publication].user_specific) return [3 /*break*/, 8];
2409
+ if (!sub.cacheId) return [3 /*break*/, 5];
2410
+ _b.label = 1;
2411
+ case 1:
2412
+ _b.trys.push([1, 2, , 4]);
2413
+ cachedRaw = this._nodeCache.get(sub.cacheId);
2414
+ cachedBuffer = this.getCacheBuffer(cachedRaw);
2415
+ if (cachedBuffer && cachedBuffer.byteLength) {
2416
+ if (this._enableDebug) {
2417
+ console.log(new Date(), 'Process Sub, Cache (packed)', sub.publication);
2418
+ }
2419
+ this._websocketManager.sendPackedBuffer(ws, messageId, false, cachedBuffer, 'msgpack');
2420
+ this.logAiSubSend({
2421
+ publication: sub.publication,
2422
+ subscriptionKey: sub.subscriptionKey,
2423
+ messageId: messageId,
2424
+ id_socket: ws['id_socket'],
2425
+ clients: 1,
2426
+ payloadBytes: cachedBuffer.byteLength,
2427
+ packed: true,
2428
+ cacheHit: true,
2429
+ collection: '',
2430
+ type: 'cache',
2431
+ subscriptionData: sub.subscriptionData
2432
+ });
2433
+ }
2434
+ else {
2435
+ cacheData = this.decodeCachePayload(cachedRaw);
2436
+ if (cacheData === null || cacheData === undefined) {
2437
+ throw new Error('cache-miss');
2438
+ }
2439
+ serverRes = {
2440
+ messageId: messageId,
2441
+ hasError: false,
2442
+ data: cacheData
2443
+ };
2444
+ if (this._enableDebug) {
2445
+ console.log(new Date(), 'Process Sub, Cache', sub.publication);
2446
+ }
2447
+ this.sendWS(ws, serverRes);
2448
+ this.logAiSubSend({
2449
+ publication: sub.publication,
2450
+ subscriptionKey: sub.subscriptionKey,
2451
+ messageId: messageId,
2452
+ id_socket: ws['id_socket'],
2453
+ clients: 1,
2454
+ payloadBytes: cacheData ? Buffer.byteLength(JSON.stringify(cacheData)) : null,
2455
+ packed: false,
2456
+ cacheHit: true,
2457
+ collection: '',
2458
+ type: 'cache',
2459
+ subscriptionData: sub.subscriptionData
2460
+ });
2461
+ }
2462
+ return [3 /*break*/, 4];
2463
+ case 2:
2464
+ _a = _b.sent();
2465
+ this._nodeCache.del(sub.cacheId);
2466
+ sub.cacheId = 0;
2467
+ return [4 /*yield*/, this.sendDataToAllWithRetry(sub, '', 'newSub')];
2468
+ case 3:
2469
+ _b.sent();
2470
+ return [3 /*break*/, 4];
2471
+ case 4: return [3 /*break*/, 7];
2472
+ case 5:
2473
+ if (this._enableDebug) {
2474
+ console.log(new Date(), 'Process Sub, Non - Cache', sub.publication, sub.running);
2475
+ }
2476
+ if (sub.running) {
2477
+ return [2 /*return*/];
2478
+ }
2479
+ return [4 /*yield*/, this.sendDataToAllWithRetry(sub, '', 'newSub')];
2480
+ case 6:
2481
+ _b.sent();
2482
+ _b.label = 7;
2483
+ case 7: return [3 /*break*/, 10];
2484
+ case 8:
2485
+ if (this._enableDebug) {
2486
+ console.log(new Date(), 'Process Sub Specific, Non - Cache', sub.publication, sub.running);
2487
+ }
2488
+ if (sub.running) {
2489
+ return [2 /*return*/];
2490
+ }
2491
+ return [4 /*yield*/, this.sendDataToOneWithRetry(ws, messageId, sub, '', 'newSub')];
2492
+ case 9:
2493
+ _b.sent();
2494
+ _b.label = 10;
2495
+ case 10: return [2 /*return*/];
2496
+ }
2497
+ });
2498
+ });
2499
+ };
2500
+ SubscriptionManager.prototype.sendDataToOne = function (ws, messageId, sub, collection, type) {
2501
+ return __awaiter(this, void 0, void 0, function () {
2502
+ var monitor, dependencySnapshot, res, execution, packedRes, payloadBytes, serverRes, err_1, _a, normalizedError, correlationId, serverRes, errorPayload;
2503
+ var _b, _c;
2504
+ return __generator(this, function (_d) {
2505
+ switch (_d.label) {
2506
+ case 0:
2507
+ if (!ws || ws.readyState !== WebSocket.OPEN) {
2508
+ return [2 /*return*/];
2509
+ }
2510
+ monitor = this._monitorManagerFunction.startMonitorFunction('User Specific Publication', sub.publication, '', '', sub.subscriptionData);
2511
+ _d.label = 1;
2512
+ case 1:
2513
+ _d.trys.push([1, 3, 5, 7]);
2514
+ resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(), 'insertSubscriptionLog', type, sub.publication, collection, JSON.stringify(sub.subscriptionData));
2515
+ return [4 /*yield*/, this.runPublicationExecution(sub, ws['id_user'])];
2516
+ case 2:
2517
+ execution = _d.sent();
2518
+ res = execution.result;
2519
+ dependencySnapshot = execution.snapshot;
2520
+ this.updateSubscriptionDependencies(sub, dependencySnapshot);
2521
+ packedRes = (_b = execution.packedResult) !== null && _b !== void 0 ? _b : this.packCachePayload(res);
2522
+ payloadBytes = packedRes ? packedRes.byteLength : null;
2523
+ if (execution.packedResult) {
2524
+ this._websocketManager.sendPackedBuffer(ws, messageId, false, execution.packedResult, execution.encoding || 'msgpack', { passThrough: !!execution.workerUsed });
2525
+ }
2526
+ else {
2527
+ serverRes = {
2528
+ messageId: messageId,
2529
+ hasError: false,
2530
+ data: res
2531
+ };
2532
+ this.sendWS(ws, serverRes);
2533
+ }
2534
+ this.logAiSubSend({
2535
+ publication: sub.publication,
2536
+ subscriptionKey: sub.subscriptionKey,
2537
+ messageId: messageId,
2538
+ id_socket: ws['id_socket'],
2539
+ clients: 1,
2540
+ payloadBytes: payloadBytes,
2541
+ packed: !!execution.packedResult,
2542
+ cacheHit: false,
2543
+ collection: collection,
2544
+ type: type,
2545
+ subscriptionData: sub.subscriptionData
2546
+ });
2547
+ return [3 /*break*/, 7];
2548
+ case 3:
2549
+ err_1 = _d.sent();
2550
+ _a = (0, error_tracking_1.ensureErrorWithCorrelation)(err_1), normalizedError = _a.error, correlationId = _a.correlationId;
2551
+ serverRes = {
2552
+ messageId: messageId,
2553
+ hasError: true,
2554
+ data: Object.assign({}, normalizedError, { correlationId: correlationId })
2555
+ };
2556
+ this.sendWS(ws, serverRes);
2557
+ errorPayload = {
2558
+ publication: sub.publication,
2559
+ subscriptionData: sub.subscriptionData,
2560
+ type: type,
2561
+ collection: collection,
2562
+ messageId: messageId,
2563
+ correlationId: correlationId,
2564
+ error: {
2565
+ name: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.name,
2566
+ message: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.message,
2567
+ stack: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.stack,
2568
+ code: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.code,
2569
+ codeName: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.codeName
2570
+ }
2571
+ };
2572
+ return [4 /*yield*/, error_reporter_1.ErrorReporter.report({
2573
+ sourceApp: 'subscription-manager',
2574
+ message: 'SERVER - Error Detected - ' + this.serverConfig['CLIENT_NAME'],
2575
+ environment: (_c = this.serverConfig) === null || _c === void 0 ? void 0 : _c.ROOT_URL,
2576
+ clientSlug: resolveio_server_app_1.ResolveIOServer.getClientName(),
2577
+ clientName: this.serverConfig['CLIENT_NAME'],
2578
+ stack: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.stack,
2579
+ context: errorPayload,
2580
+ metadata: {
2581
+ context: 'subscription-sendDataToOne',
2582
+ publication: sub.publication,
2583
+ correlationId: correlationId
2584
+ },
2585
+ correlationId: correlationId
2586
+ })];
2587
+ case 4:
2588
+ _d.sent();
2589
+ return [3 /*break*/, 7];
2590
+ case 5: return [4 /*yield*/, this._monitorManagerFunction.finishMonitorFunction(monitor)];
2591
+ case 6:
2592
+ _d.sent();
2593
+ return [7 /*endfinally*/];
2594
+ case 7: return [2 /*return*/];
2595
+ }
2596
+ });
2597
+ });
2598
+ };
2599
+ // Fetch pub once, send to all clients linked to this pub
2600
+ SubscriptionManager.prototype.sendDataToAll = function (sub, collection, type) {
2601
+ return __awaiter(this, void 0, void 0, function () {
2602
+ var isAiPublication, seenSockets_1, activeClients, _a, _b, client, ws, subIndex, monitor, res, dependencySnapshot, execution, packedRes, passThrough, payloadBytes, shouldCache, cachedBuffer, isSame, sendStartMs, sentClients, activeClients_1, activeClients_1_1, entry, client, ws, serverRes, sendStartMs, sentClients, activeClients_2, activeClients_2_1, entry, client, ws, serverRes, nodeCacheSize, deleteCount, subArr, zz, err_2, _c, normalizedError, correlationId, _d, _e, client, ws, serverRes, errorPayload;
2603
+ var e_6, _f, e_7, _g, e_8, _h, e_9, _j;
2604
+ var _k, _l;
2605
+ return __generator(this, function (_m) {
2606
+ switch (_m.label) {
2607
+ case 0:
2608
+ isAiPublication = this.isAiPublication(sub.publication);
2609
+ if (isAiPublication && sub.clients.length > 1) {
2610
+ seenSockets_1 = new Set();
2611
+ sub.clients = sub.clients.filter(function (client) {
2612
+ if (seenSockets_1.has(client.id_socket)) {
2613
+ return false;
2614
+ }
2615
+ seenSockets_1.add(client.id_socket);
2616
+ return true;
2617
+ });
2618
+ }
2619
+ activeClients = [];
2620
+ if (sub.clients.length) {
2621
+ try {
2622
+ for (_a = __values(sub.clients), _b = _a.next(); !_b.done; _b = _a.next()) {
2623
+ client = _b.value;
2624
+ ws = this._websocketManager.getWebSocket(client.id_socket);
2625
+ if (ws && ws.readyState === WebSocket.OPEN) {
2626
+ activeClients.push({ client: client, ws: ws });
2627
+ }
2628
+ }
2629
+ }
2630
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
2631
+ finally {
2632
+ try {
2633
+ if (_b && !_b.done && (_f = _a.return)) _f.call(_a);
2634
+ }
2635
+ finally { if (e_6) throw e_6.error; }
2636
+ }
2637
+ }
2638
+ if (!!activeClients.length) return [3 /*break*/, 1];
2639
+ if (sub.cacheId) {
2640
+ this._nodeCache.del(sub.cacheId);
2641
+ sub.cacheId = 0;
2642
+ }
2643
+ subIndex = this._subscriptions.findIndex(function (a) { return a.publication === sub.publication && a.subscriptionKey === sub.subscriptionKey; });
2644
+ if (subIndex >= 0) {
2645
+ this._subscriptions.splice(subIndex, 1);
2646
+ }
2647
+ return [2 /*return*/];
2648
+ case 1:
2649
+ if (activeClients.length !== sub.clients.length) {
2650
+ sub.clients = activeClients.map(function (entry) { return entry.client; });
2651
+ }
2652
+ monitor = this._monitorManagerFunction.startMonitorFunction('Publication', sub.publication, '', '', sub.subscriptionData);
2653
+ res = void 0;
2654
+ dependencySnapshot = void 0;
2655
+ _m.label = 2;
2656
+ case 2:
2657
+ _m.trys.push([2, 4, 6, 8]);
2658
+ if (sub.publication !== 'superadminAPM' && sub.publication !== 'loggedInUsers') {
2659
+ resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(), 'insertSubscriptionLog', type, sub.publication, collection, JSON.stringify(sub.subscriptionData));
2660
+ }
2661
+ return [4 /*yield*/, this.runPublicationExecution(sub)];
2662
+ case 3:
2663
+ execution = _m.sent();
2664
+ res = execution.result;
2665
+ dependencySnapshot = execution.snapshot;
2666
+ this.updateSubscriptionDependencies(sub, dependencySnapshot);
2667
+ packedRes = (_k = execution.packedResult) !== null && _k !== void 0 ? _k : this.packCachePayload(res);
2668
+ passThrough = !!execution.workerUsed;
2669
+ payloadBytes = packedRes ? packedRes.byteLength : null;
2670
+ shouldCache = this.shouldCachePayload(sub, packedRes);
2671
+ if (sub.cacheId) {
2672
+ cachedBuffer = this.getCacheBuffer(this._nodeCache.get(sub.cacheId));
2673
+ isSame = this.buffersEqual(cachedBuffer, packedRes);
2674
+ if (!isSame) {
2675
+ sendStartMs = Date.now();
2676
+ sentClients = 0;
2677
+ try {
2678
+ for (activeClients_1 = __values(activeClients), activeClients_1_1 = activeClients_1.next(); !activeClients_1_1.done; activeClients_1_1 = activeClients_1.next()) {
2679
+ entry = activeClients_1_1.value;
2680
+ client = entry.client, ws = entry.ws;
2681
+ if (packedRes) {
2682
+ this._websocketManager.sendPackedBuffer(ws, client.messageId, false, packedRes, execution.encoding || 'msgpack', { passThrough: passThrough });
2683
+ }
2684
+ else {
2685
+ serverRes = {
2686
+ messageId: client.messageId,
2687
+ hasError: false,
2688
+ data: res
2689
+ };
2690
+ this.sendWS(ws, serverRes);
2691
+ }
2692
+ if (isAiPublication) {
2693
+ this.logAiSubSend({
2694
+ publication: sub.publication,
2695
+ subscriptionKey: sub.subscriptionKey,
2696
+ messageId: client.messageId,
2697
+ id_socket: ws['id_socket'],
2698
+ clients: activeClients.length,
2699
+ payloadBytes: payloadBytes,
2700
+ packed: !!packedRes,
2701
+ cacheHit: false,
2702
+ collection: collection,
2703
+ type: type,
2704
+ subscriptionData: sub.subscriptionData
2705
+ });
2706
+ }
2707
+ sentClients += 1;
2708
+ }
2709
+ }
2710
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
2711
+ finally {
2712
+ try {
2713
+ if (activeClients_1_1 && !activeClients_1_1.done && (_g = activeClients_1.return)) _g.call(activeClients_1);
2714
+ }
2715
+ finally { if (e_7) throw e_7.error; }
2716
+ }
2717
+ this.logSubSend(sub, {
2718
+ collection: collection,
2719
+ type: type,
2720
+ clients: sentClients,
2721
+ durationMs: Date.now() - sendStartMs,
2722
+ payloadBytes: payloadBytes
2723
+ });
2724
+ this._nodeCache.del(sub.cacheId);
2725
+ if (shouldCache) {
2726
+ this._nodeCache.set(sub.cacheId, packedRes);
2727
+ }
2728
+ else {
2729
+ sub.cacheId = 0;
2730
+ }
2731
+ }
2732
+ else if (!cachedBuffer && shouldCache) {
2733
+ this._nodeCache.set(sub.cacheId, packedRes);
2734
+ }
2735
+ else if (!shouldCache) {
2736
+ this._nodeCache.del(sub.cacheId);
2737
+ sub.cacheId = 0;
2738
+ }
2739
+ }
2740
+ else {
2741
+ sendStartMs = Date.now();
2742
+ sentClients = 0;
2743
+ try {
2744
+ for (activeClients_2 = __values(activeClients), activeClients_2_1 = activeClients_2.next(); !activeClients_2_1.done; activeClients_2_1 = activeClients_2.next()) {
2745
+ entry = activeClients_2_1.value;
2746
+ client = entry.client, ws = entry.ws;
2747
+ if (packedRes) {
2748
+ this._websocketManager.sendPackedBuffer(ws, client.messageId, false, packedRes, execution.encoding || 'msgpack', { passThrough: passThrough });
2749
+ }
2750
+ else {
2751
+ serverRes = {
2752
+ messageId: client.messageId,
2753
+ hasError: false,
2754
+ data: res
2755
+ };
2756
+ this.sendWS(ws, serverRes);
2757
+ }
2758
+ if (isAiPublication) {
2759
+ this.logAiSubSend({
2760
+ publication: sub.publication,
2761
+ subscriptionKey: sub.subscriptionKey,
2762
+ messageId: client.messageId,
2763
+ id_socket: ws['id_socket'],
2764
+ clients: activeClients.length,
2765
+ payloadBytes: payloadBytes,
2766
+ packed: !!packedRes,
2767
+ cacheHit: false,
2768
+ collection: collection,
2769
+ type: type,
2770
+ subscriptionData: sub.subscriptionData
2771
+ });
2772
+ }
2773
+ sentClients += 1;
2774
+ }
2775
+ }
2776
+ catch (e_8_1) { e_8 = { error: e_8_1 }; }
2777
+ finally {
2778
+ try {
2779
+ if (activeClients_2_1 && !activeClients_2_1.done && (_h = activeClients_2.return)) _h.call(activeClients_2);
2780
+ }
2781
+ finally { if (e_8) throw e_8.error; }
2782
+ }
2783
+ this.logSubSend(sub, {
2784
+ collection: collection,
2785
+ type: type,
2786
+ clients: sentClients,
2787
+ durationMs: Date.now() - sendStartMs,
2788
+ payloadBytes: payloadBytes
2789
+ });
2790
+ if (shouldCache) {
2791
+ sub.cacheId = this._cacheId++;
2792
+ this._nodeCache.set(sub.cacheId, packedRes);
2793
+ nodeCacheSize = this._nodeCache.getStats().vsize;
2794
+ if (nodeCacheSize > this._heapLimit) {
2795
+ deleteCount = 0;
2796
+ subArr = this._subscriptions.filter(function (a) { return a.cacheId && !a.clients.length; });
2797
+ for (zz = 0; zz < subArr.length; zz++) {
2798
+ this._debugRemoveCacheHits += 1;
2799
+ this._nodeCache.del(subArr[zz].cacheId);
2800
+ subArr[zz].cacheId = 0;
2801
+ deleteCount += 1;
2802
+ if (this._nodeCache.getStats().vsize < this._heapLimit * 0.75) {
2803
+ break;
2804
+ }
2805
+ }
2806
+ if (this._enableDebug) {
2807
+ console.log('Sub Cache: ' +
2808
+ 'Too Big - ' +
2809
+ sub.publication +
2810
+ ' - Deleted: ' +
2811
+ deleteCount +
2812
+ ' - ' +
2813
+ nodeCacheSize);
2814
+ }
2815
+ }
2816
+ }
2817
+ else {
2818
+ sub.cacheId = 0;
2819
+ }
2820
+ }
2821
+ return [3 /*break*/, 8];
2822
+ case 4:
2823
+ err_2 = _m.sent();
2824
+ _c = (0, error_tracking_1.ensureErrorWithCorrelation)(err_2), normalizedError = _c.error, correlationId = _c.correlationId;
2825
+ try {
2826
+ for (_d = __values(sub.clients), _e = _d.next(); !_e.done; _e = _d.next()) {
2827
+ client = _e.value;
2828
+ ws = this._websocketManager.getWebSocket(client.id_socket);
2829
+ if (ws && ws.readyState === WebSocket.OPEN) {
2830
+ serverRes = {
2831
+ messageId: client.messageId,
2832
+ hasError: true,
2833
+ data: Object.assign({}, normalizedError, { correlationId: correlationId })
2834
+ };
2835
+ this.sendWS(ws, serverRes);
2836
+ }
2837
+ }
2838
+ }
2839
+ catch (e_9_1) { e_9 = { error: e_9_1 }; }
2840
+ finally {
2841
+ try {
2842
+ if (_e && !_e.done && (_j = _d.return)) _j.call(_d);
2843
+ }
2844
+ finally { if (e_9) throw e_9.error; }
2845
+ }
2846
+ errorPayload = {
2847
+ publication: sub.publication,
2848
+ subscriptionData: sub.subscriptionData,
2849
+ type: type,
2850
+ collection: collection,
2851
+ correlationId: correlationId,
2852
+ error: {
2853
+ name: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.name,
2854
+ message: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.message,
2855
+ stack: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.stack,
2856
+ code: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.code,
2857
+ codeName: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.codeName
2858
+ }
2859
+ };
2860
+ return [4 /*yield*/, error_reporter_1.ErrorReporter.report({
2861
+ sourceApp: 'subscription-manager',
2862
+ message: 'SERVER - Error Detected - ' + this.serverConfig['CLIENT_NAME'],
2863
+ environment: (_l = this.serverConfig) === null || _l === void 0 ? void 0 : _l.ROOT_URL,
2864
+ clientSlug: resolveio_server_app_1.ResolveIOServer.getClientName(),
2865
+ clientName: this.serverConfig['CLIENT_NAME'],
2866
+ stack: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.stack,
2867
+ context: errorPayload,
2868
+ metadata: {
2869
+ context: 'subscription-sendDataToAll',
2870
+ publication: sub.publication,
2871
+ correlationId: correlationId
2872
+ },
2873
+ correlationId: correlationId
2874
+ })];
2875
+ case 5:
2876
+ _m.sent();
2877
+ return [3 /*break*/, 8];
2878
+ case 6: return [4 /*yield*/, this._monitorManagerFunction.finishMonitorFunction(monitor)];
2879
+ case 7:
2880
+ _m.sent();
2881
+ return [7 /*endfinally*/];
2882
+ case 8: return [2 /*return*/];
2883
+ }
2884
+ });
2885
+ });
2886
+ };
2887
+ SubscriptionManager.prototype.ensureDependencyContainers = function (sub) {
2888
+ if (!sub.collectionDependencies) {
2889
+ sub.collectionDependencies = new Map();
2890
+ }
2891
+ if (!sub.collectionFilters) {
2892
+ sub.collectionFilters = new Map();
2893
+ }
2894
+ if (!sub.watchAllCollections) {
2895
+ sub.watchAllCollections = new Set();
2896
+ }
2897
+ if (!sub.collectionQueryMeta) {
2898
+ sub.collectionQueryMeta = new Map();
2899
+ }
2900
+ };
2901
+ SubscriptionManager.prototype.updateSubscriptionDependencies = function (sub, snapshot) {
2902
+ this.ensureDependencyContainers(sub);
2903
+ if (!snapshot) {
2904
+ sub.collectionDependencies.clear();
2905
+ sub.collectionFilters.clear();
2906
+ sub.watchAllCollections.clear();
2907
+ sub.collectionQueryMeta.clear();
2908
+ this.dependencyDebug('Cleared dependency snapshot', { publication: sub.publication });
2909
+ return;
2910
+ }
2911
+ sub.collectionDependencies = snapshot.dependencies;
2912
+ sub.collectionFilters = snapshot.filters;
2913
+ sub.watchAllCollections = snapshot.watchAllCollections;
2914
+ sub.collectionQueryMeta = snapshot.queryMetadata;
2915
+ this.logDependencySnapshot(sub, 'Snapshot updated');
2916
+ };
2917
+ SubscriptionManager.prototype.normalizeDocumentId = function (rawId) {
2918
+ if (rawId === null || rawId === undefined) {
2919
+ return null;
2920
+ }
2921
+ if (typeof rawId === 'string') {
2922
+ return rawId;
2923
+ }
2924
+ if (typeof rawId === 'object' && typeof rawId.toHexString === 'function') {
2925
+ return rawId.toHexString();
2926
+ }
2927
+ if (typeof (rawId === null || rawId === void 0 ? void 0 : rawId.toString) === 'function') {
2928
+ var str = rawId.toString();
2929
+ if (str && str !== '[object Object]') {
2930
+ return str;
2931
+ }
2932
+ }
2933
+ return null;
2934
+ };
2935
+ SubscriptionManager.prototype.getDocumentIdQueryCandidates = function (documentId) {
2936
+ var candidates = [];
2937
+ if (documentId === undefined || documentId === null) {
2938
+ return [documentId];
2939
+ }
2940
+ var addCandidate = function (value) {
2941
+ if (value === undefined || value === null) {
2942
+ return;
2943
+ }
2944
+ if (!candidates.includes(value)) {
2945
+ candidates.push(value);
2946
+ }
2947
+ };
2948
+ addCandidate(documentId);
2949
+ if (typeof documentId === 'object') {
2950
+ if (typeof documentId.toHexString === 'function') {
2951
+ addCandidate(documentId.toHexString());
2952
+ }
2953
+ if (typeof documentId.toString === 'function') {
2954
+ var str = documentId.toString();
2955
+ if (str && str !== '[object Object]') {
2956
+ addCandidate(str);
2957
+ }
2958
+ }
2959
+ }
2960
+ else if (typeof documentId !== 'string') {
2961
+ addCandidate(String(documentId));
2962
+ }
2963
+ return candidates;
2964
+ };
2965
+ SubscriptionManager.prototype.isFilterSafeForInMemory = function (filter) {
2966
+ if (!filter || typeof filter !== 'object') {
2967
+ return false;
2968
+ }
2969
+ var allowedOperators = new Set([
2970
+ '$and',
2971
+ '$or',
2972
+ '$nor',
2973
+ '$in',
2974
+ '$nin',
2975
+ '$eq',
2976
+ '$ne',
2977
+ '$gt',
2978
+ '$gte',
2979
+ '$lt',
2980
+ '$lte',
2981
+ '$exists',
2982
+ '$size',
2983
+ '$all',
2984
+ '$elemMatch',
2985
+ '$regex',
2986
+ '$options'
2987
+ ]);
2988
+ var disallowedOperators = new Set([
2989
+ '$where',
2990
+ '$expr',
2991
+ '$text',
2992
+ '$geoWithin',
2993
+ '$geoIntersects',
2994
+ '$near',
2995
+ '$nearSphere',
2996
+ '$jsonSchema',
2997
+ '$mod',
2998
+ '$type',
2999
+ '$bitsAllSet',
3000
+ '$bitsAllClear',
3001
+ '$bitsAnySet',
3002
+ '$bitsAnyClear'
3003
+ ]);
3004
+ var walk = function (value) {
3005
+ var e_10, _a;
3006
+ if (Array.isArray(value)) {
3007
+ return value.every(function (item) { return walk(item); });
3008
+ }
3009
+ if (!value || typeof value !== 'object') {
3010
+ return true;
3011
+ }
3012
+ try {
3013
+ for (var _b = __values(Object.entries(value)), _c = _b.next(); !_c.done; _c = _b.next()) {
3014
+ var _d = __read(_c.value, 2), key = _d[0], entry = _d[1];
3015
+ if (key.startsWith('$')) {
3016
+ if (disallowedOperators.has(key)) {
3017
+ return false;
3018
+ }
3019
+ if (!allowedOperators.has(key)) {
3020
+ return false;
3021
+ }
3022
+ }
3023
+ if (!walk(entry)) {
3024
+ return false;
3025
+ }
3026
+ }
3027
+ }
3028
+ catch (e_10_1) { e_10 = { error: e_10_1 }; }
3029
+ finally {
3030
+ try {
3031
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
3032
+ }
3033
+ finally { if (e_10) throw e_10.error; }
3034
+ }
3035
+ return true;
3036
+ };
3037
+ return walk(filter);
3038
+ };
3039
+ SubscriptionManager.prototype.documentIdMatchesEvent = function (documentId, document) {
3040
+ var e_11, _a;
3041
+ var _this = this;
3042
+ if (!document || documentId === undefined || documentId === null) {
3043
+ return true;
3044
+ }
3045
+ var eventCandidates = this.getDocumentIdQueryCandidates(documentId)
3046
+ .map(function (id) { return _this.normalizeDocumentId(id); })
3047
+ .filter(Boolean);
3048
+ var docCandidates = this.getDocumentIdQueryCandidates(document === null || document === void 0 ? void 0 : document._id)
3049
+ .map(function (id) { return _this.normalizeDocumentId(id); })
3050
+ .filter(Boolean);
3051
+ if (!eventCandidates.length || !docCandidates.length) {
3052
+ return true;
3053
+ }
3054
+ var docSet = new Set(docCandidates);
3055
+ try {
3056
+ for (var eventCandidates_1 = __values(eventCandidates), eventCandidates_1_1 = eventCandidates_1.next(); !eventCandidates_1_1.done; eventCandidates_1_1 = eventCandidates_1.next()) {
3057
+ var candidate = eventCandidates_1_1.value;
3058
+ if (docSet.has(candidate)) {
3059
+ return true;
3060
+ }
3061
+ }
3062
+ }
3063
+ catch (e_11_1) { e_11 = { error: e_11_1 }; }
3064
+ finally {
3065
+ try {
3066
+ if (eventCandidates_1_1 && !eventCandidates_1_1.done && (_a = eventCandidates_1.return)) _a.call(eventCandidates_1);
3067
+ }
3068
+ finally { if (e_11) throw e_11.error; }
3069
+ }
3070
+ return false;
3071
+ };
3072
+ SubscriptionManager.prototype.resolveFilterMatch = function (filter, document, documentId) {
3073
+ if (!document || !filter || typeof filter !== 'object') {
3074
+ return null;
3075
+ }
3076
+ if (!this.isFilterSafeForInMemory(filter)) {
3077
+ return null;
3078
+ }
3079
+ if (!this.documentIdMatchesEvent(documentId, document)) {
3080
+ return false;
3081
+ }
3082
+ try {
3083
+ var matcher = sift(filter);
3084
+ return matcher(document);
3085
+ }
3086
+ catch (_a) {
3087
+ return null;
3088
+ }
3089
+ };
3090
+ SubscriptionManager.prototype.documentMatchesFilter = function (collection, documentId, filter, document) {
3091
+ return __awaiter(this, void 0, void 0, function () {
3092
+ var inMemoryMatch, db, filterCopy, idCandidates, idCandidates_1, idCandidates_1_1, idValue, combinedFilter, doc, e_12_1, _a;
3093
+ var e_12, _b;
3094
+ return __generator(this, function (_c) {
3095
+ switch (_c.label) {
3096
+ case 0:
3097
+ _c.trys.push([0, 9, , 10]);
3098
+ inMemoryMatch = this.resolveFilterMatch(filter, document, documentId);
3099
+ if (inMemoryMatch !== null) {
3100
+ return [2 /*return*/, inMemoryMatch];
3101
+ }
3102
+ db = resolveio_server_app_1.ResolveIOServer.getMainDB();
3103
+ filterCopy = (0, common_1.deepCopy)(filter) || {};
3104
+ idCandidates = this.getDocumentIdQueryCandidates(documentId);
3105
+ _c.label = 1;
3106
+ case 1:
3107
+ _c.trys.push([1, 6, 7, 8]);
3108
+ idCandidates_1 = __values(idCandidates), idCandidates_1_1 = idCandidates_1.next();
3109
+ _c.label = 2;
3110
+ case 2:
3111
+ if (!!idCandidates_1_1.done) return [3 /*break*/, 5];
3112
+ idValue = idCandidates_1_1.value;
3113
+ combinedFilter = {
3114
+ $and: [
3115
+ { _id: idValue },
3116
+ filterCopy
3117
+ ]
3118
+ };
3119
+ return [4 /*yield*/, db.collection(collection).findOne(combinedFilter)];
3120
+ case 3:
3121
+ doc = _c.sent();
3122
+ if (doc) {
3123
+ return [2 /*return*/, true];
3124
+ }
3125
+ _c.label = 4;
3126
+ case 4:
3127
+ idCandidates_1_1 = idCandidates_1.next();
3128
+ return [3 /*break*/, 2];
3129
+ case 5: return [3 /*break*/, 8];
3130
+ case 6:
3131
+ e_12_1 = _c.sent();
3132
+ e_12 = { error: e_12_1 };
3133
+ return [3 /*break*/, 8];
3134
+ case 7:
3135
+ try {
3136
+ if (idCandidates_1_1 && !idCandidates_1_1.done && (_b = idCandidates_1.return)) _b.call(idCandidates_1);
3137
+ }
3138
+ finally { if (e_12) throw e_12.error; }
3139
+ return [7 /*endfinally*/];
3140
+ case 8: return [2 /*return*/, false];
3141
+ case 9:
3142
+ _a = _c.sent();
3143
+ return [2 /*return*/, true];
3144
+ case 10: return [2 /*return*/];
3145
+ }
3146
+ });
3147
+ });
3148
+ };
3149
+ SubscriptionManager.prototype.shouldInvalidateSubscription = function (sub, collection, type, documentId, document) {
3150
+ return __awaiter(this, void 0, void 0, function () {
3151
+ var normalizedDocumentId, trackedIds, filters, hasTrackedIds, hasFilters, hasDependencyData, filters_1, filters_1_1, filter, e_13_1;
3152
+ var e_13, _a;
3153
+ var _b, _c;
3154
+ return __generator(this, function (_d) {
3155
+ switch (_d.label) {
3156
+ case 0:
3157
+ this.ensureDependencyContainers(sub);
3158
+ normalizedDocumentId = this.normalizeDocumentId(documentId);
3159
+ trackedIds = sub.collectionDependencies.get(collection);
3160
+ filters = sub.collectionFilters.get(collection);
3161
+ hasTrackedIds = !!(trackedIds && trackedIds.size);
3162
+ hasFilters = !!(filters && filters.length);
3163
+ hasDependencyData = (((_b = sub.collectionDependencies.get(collection)) === null || _b === void 0 ? void 0 : _b.size) || 0) > 0 ||
3164
+ (((_c = sub.collectionFilters.get(collection)) === null || _c === void 0 ? void 0 : _c.length) || 0) > 0 ||
3165
+ sub.watchAllCollections.has(collection);
3166
+ if (!normalizedDocumentId) {
3167
+ this.dependencyDebug('Invalidate due to missing documentId', { publication: sub.publication, collection: collection, type: type });
3168
+ return [2 /*return*/, true];
3169
+ }
3170
+ if (!hasDependencyData) {
3171
+ this.dependencyDebug('Invalidate due to missing dependency metadata', {
3172
+ publication: sub.publication,
3173
+ collection: collection,
3174
+ type: type
3175
+ });
3176
+ return [2 /*return*/, true];
3177
+ }
3178
+ if (sub.watchAllCollections.has(collection)) {
3179
+ this.dependencyDebug('Invalidate due to watch-all collection', { publication: sub.publication, collection: collection, type: type });
3180
+ return [2 /*return*/, true];
3181
+ }
3182
+ if (trackedIds && trackedIds.has(normalizedDocumentId)) {
3183
+ this.dependencyDebug('Invalidate due to tracked id', { publication: sub.publication, collection: collection, type: type, documentId: normalizedDocumentId });
3184
+ return [2 /*return*/, true];
3185
+ }
3186
+ if (type === 'delete') {
3187
+ if (hasFilters && !hasTrackedIds) {
3188
+ this.dependencyDebug('Invalidate delete due to filter-only dependency without tracked ids', {
3189
+ publication: sub.publication,
3190
+ collection: collection,
3191
+ type: type,
3192
+ documentId: normalizedDocumentId
3193
+ });
3194
+ return [2 /*return*/, true];
3195
+ }
3196
+ this.dependencyDebug('Skip invalidation on delete for unknown id', { publication: sub.publication, collection: collection, type: type, documentId: normalizedDocumentId });
3197
+ return [2 /*return*/, false];
3198
+ }
3199
+ if (type === 'update' && hasFilters && !hasTrackedIds) {
3200
+ this.dependencyDebug('Invalidate update due to filter-only dependency without tracked ids', {
3201
+ publication: sub.publication,
3202
+ collection: collection,
3203
+ type: type,
3204
+ documentId: normalizedDocumentId
3205
+ });
3206
+ return [2 /*return*/, true];
3207
+ }
3208
+ if (!(filters && filters.length)) return [3 /*break*/, 8];
3209
+ _d.label = 1;
3210
+ case 1:
3211
+ _d.trys.push([1, 6, 7, 8]);
3212
+ filters_1 = __values(filters), filters_1_1 = filters_1.next();
3213
+ _d.label = 2;
3214
+ case 2:
3215
+ if (!!filters_1_1.done) return [3 /*break*/, 5];
3216
+ filter = filters_1_1.value;
3217
+ return [4 /*yield*/, this.documentMatchesFilter(collection, documentId, filter, document)];
3218
+ case 3:
3219
+ if (_d.sent()) {
3220
+ this.dependencyDebug('Invalidate due to filter match', { publication: sub.publication, collection: collection, type: type, documentId: normalizedDocumentId, filter: filter });
3221
+ return [2 /*return*/, true];
3222
+ }
3223
+ _d.label = 4;
3224
+ case 4:
3225
+ filters_1_1 = filters_1.next();
3226
+ return [3 /*break*/, 2];
3227
+ case 5: return [3 /*break*/, 8];
3228
+ case 6:
3229
+ e_13_1 = _d.sent();
3230
+ e_13 = { error: e_13_1 };
3231
+ return [3 /*break*/, 8];
3232
+ case 7:
3233
+ try {
3234
+ if (filters_1_1 && !filters_1_1.done && (_a = filters_1.return)) _a.call(filters_1);
3235
+ }
3236
+ finally { if (e_13) throw e_13.error; }
3237
+ return [7 /*endfinally*/];
3238
+ case 8:
3239
+ this.dependencyDebug('Skip invalidation after dependency checks', { publication: sub.publication, collection: collection, type: type, documentId: normalizedDocumentId });
3240
+ return [2 /*return*/, false];
3241
+ }
3242
+ });
3243
+ });
3244
+ };
3245
+ SubscriptionManager.prototype.shouldInvalidateSubscriptionForEvents = function (sub, collection, events) {
3246
+ return __awaiter(this, void 0, void 0, function () {
3247
+ var sawInsert, sawDelete, events_1, events_1_1, event_1, e_14_1, paginationMeta, paginationReasons;
3248
+ var e_14, _a;
3249
+ return __generator(this, function (_b) {
3250
+ switch (_b.label) {
3251
+ case 0:
3252
+ sawInsert = false;
3253
+ sawDelete = false;
3254
+ _b.label = 1;
3255
+ case 1:
3256
+ _b.trys.push([1, 6, 7, 8]);
3257
+ events_1 = __values(events), events_1_1 = events_1.next();
3258
+ _b.label = 2;
3259
+ case 2:
3260
+ if (!!events_1_1.done) return [3 /*break*/, 5];
3261
+ event_1 = events_1_1.value;
3262
+ if (event_1.type === 'insert') {
3263
+ sawInsert = true;
3264
+ }
3265
+ else if (event_1.type === 'delete') {
3266
+ sawDelete = true;
3267
+ }
3268
+ return [4 /*yield*/, this.shouldInvalidateSubscription(sub, collection, event_1.type, event_1.documentId, event_1.document)];
3269
+ case 3:
3270
+ if (_b.sent()) {
3271
+ return [2 /*return*/, true];
3272
+ }
3273
+ _b.label = 4;
3274
+ case 4:
3275
+ events_1_1 = events_1.next();
3276
+ return [3 /*break*/, 2];
3277
+ case 5: return [3 /*break*/, 8];
3278
+ case 6:
3279
+ e_14_1 = _b.sent();
3280
+ e_14 = { error: e_14_1 };
3281
+ return [3 /*break*/, 8];
3282
+ case 7:
3283
+ try {
3284
+ if (events_1_1 && !events_1_1.done && (_a = events_1.return)) _a.call(events_1);
3285
+ }
3286
+ finally { if (e_14) throw e_14.error; }
3287
+ return [7 /*endfinally*/];
3288
+ case 8:
3289
+ paginationMeta = this.getPaginationMeta(sub, collection);
3290
+ paginationReasons = [];
3291
+ if (sawInsert) {
3292
+ paginationReasons.push('insert');
3293
+ }
3294
+ if (sawDelete) {
3295
+ paginationReasons.push('delete');
3296
+ }
3297
+ if ((paginationMeta === null || paginationMeta === void 0 ? void 0 : paginationMeta.length) && paginationReasons.length) {
3298
+ this.dependencyDebug('Invalidate due to pagination metadata', {
3299
+ publication: sub.publication,
3300
+ collection: collection,
3301
+ events: this.summarizeEvents(events),
3302
+ reasons: paginationReasons,
3303
+ queryMeta: paginationMeta.slice(0, 5)
3304
+ });
3305
+ return [2 /*return*/, true];
3306
+ }
3307
+ return [2 /*return*/, false];
3308
+ }
3309
+ });
3310
+ });
3311
+ };
3312
+ SubscriptionManager.prototype.getPaginationMeta = function (sub, collection) {
3313
+ var _a;
3314
+ this.ensureDependencyContainers(sub);
3315
+ var metaList = (_a = sub.collectionQueryMeta) === null || _a === void 0 ? void 0 : _a.get(collection);
3316
+ if (!metaList || !metaList.length) {
3317
+ return null;
3318
+ }
3319
+ var relevantMeta = metaList.filter(function (meta) {
3320
+ if (!meta) {
3321
+ return false;
3322
+ }
3323
+ var limitUsed = typeof meta.limit === 'number' && meta.limit > 0;
3324
+ var skipUsed = typeof meta.skip === 'number' && meta.skip > 0;
3325
+ return limitUsed || skipUsed;
3326
+ });
3327
+ return relevantMeta.length ? relevantMeta : null;
3328
+ };
3329
+ SubscriptionManager.prototype.sendWS = function (ws, data) {
3330
+ this._websocketManager.send(ws, data);
3331
+ };
3332
+ SubscriptionManager.prototype.getEnableDebug = function () {
3333
+ return this._enableDebug;
3334
+ };
3335
+ SubscriptionManager.prototype.dependencyDebug = function (message, details) {
3336
+ if (!this._enableDependencyDebug) {
3337
+ return;
3338
+ }
3339
+ if (details) {
3340
+ console.log(new Date(), '[Dependency Debug]', message, JSON.stringify(details, null, 2));
3341
+ }
3342
+ else {
3343
+ console.log(new Date(), '[Dependency Debug]', message);
3344
+ }
3345
+ };
3346
+ SubscriptionManager.prototype.summarizeEvents = function (events) {
3347
+ var _this = this;
3348
+ return events.map(function (event) { return ({
3349
+ type: event.type,
3350
+ documentId: _this.normalizeDocumentId(event.documentId)
3351
+ }); });
3352
+ };
3353
+ SubscriptionManager.prototype.logDependencySnapshot = function (sub, context) {
3354
+ if (!this._enableDependencyDebug) {
3355
+ return;
3356
+ }
3357
+ var dependencySummary = Array.from(sub.collectionDependencies.entries()).map(function (_a) {
3358
+ var _b = __read(_a, 2), collectionName = _b[0], ids = _b[1];
3359
+ var idList = Array.from(ids || []);
3360
+ return {
3361
+ collection: collectionName,
3362
+ count: idList.length,
3363
+ sample: idList.slice(0, 10)
3364
+ };
3365
+ });
3366
+ var filterSummary = Array.from(sub.collectionFilters.entries()).map(function (_a) {
3367
+ var _b = __read(_a, 2), collectionName = _b[0], filters = _b[1];
3368
+ return ({
3369
+ collection: collectionName,
3370
+ count: filters.length
3371
+ });
3372
+ });
3373
+ var queryMetaSummary = Array.from(sub.collectionQueryMeta.entries()).map(function (_a) {
3374
+ var _b = __read(_a, 2), collectionName = _b[0], metaList = _b[1];
3375
+ return ({
3376
+ collection: collectionName,
3377
+ count: metaList.length,
3378
+ meta: metaList.slice(0, 5)
3379
+ });
3380
+ });
3381
+ var watchAll = Array.from(sub.watchAllCollections || []);
3382
+ this.dependencyDebug('Dependency snapshot updated', {
3383
+ context: context,
3384
+ publication: sub.publication,
3385
+ dependencies: dependencySummary,
3386
+ collectionsWithFilters: filterSummary,
3387
+ collectionsWithMeta: queryMetaSummary,
3388
+ watchAll: watchAll
3389
+ });
3390
+ };
3391
+ SubscriptionManager.prototype.runPublicationOnce = function (publication_1) {
3392
+ return __awaiter(this, arguments, void 0, function (publication, subscriptionData, options) {
3393
+ var pub, context, metadata;
3394
+ if (subscriptionData === void 0) { subscriptionData = []; }
3395
+ return __generator(this, function (_a) {
3396
+ pub = this._publications[publication];
3397
+ if (!pub) {
3398
+ throw new Error("Publication not found: ".concat(publication));
3399
+ }
3400
+ context = Object.assign({}, this, SubscriptionManager.prototype);
3401
+ metadata = {
3402
+ publication: publication,
3403
+ subscriptionData: subscriptionData
3404
+ };
3405
+ if (pub.user_specific) {
3406
+ return [2 /*return*/, (0, subscription_dependency_context_1.withDependencyTracking)(function () {
3407
+ var _a;
3408
+ return (_a = pub.function).call.apply(_a, __spreadArray([context, (options === null || options === void 0 ? void 0 : options.userId) || ''], __read(subscriptionData), false));
3409
+ }, metadata)];
3410
+ }
3411
+ return [2 /*return*/, (0, subscription_dependency_context_1.withDependencyTracking)(function () {
3412
+ var _a;
3413
+ return (_a = pub.function).call.apply(_a, __spreadArray([context], __read(subscriptionData), false));
3414
+ }, metadata)];
3415
+ });
3416
+ });
3417
+ };
3418
+ return SubscriptionManager;
3419
+ }());
3420
+ exports.SubscriptionManager = SubscriptionManager;
3421
+
3422
+ //# sourceMappingURL=subscription.manager.js.map