@resolveio/server-lib 22.3.221 → 22.3.223

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 (745) hide show
  1. package/ai/assistant-core-heuristics.d.ts +11 -0
  2. package/ai/assistant-core-heuristics.js +356 -0
  3. package/ai/assistant-core-heuristics.js.map +1 -0
  4. package/ai/resolveio-platform-intelligence-memory-corpus.d.ts +3 -0
  5. package/ai/resolveio-platform-intelligence-memory-corpus.js +214 -0
  6. package/ai/resolveio-platform-intelligence-memory-corpus.js.map +1 -0
  7. package/ai/resolveio-platform-intelligence-memory.d.ts +20 -0
  8. package/ai/resolveio-platform-intelligence-memory.js +341 -0
  9. package/ai/resolveio-platform-intelligence-memory.js.map +1 -0
  10. package/{src/ai/resolveio-platform-intelligence-types.ts → ai/resolveio-platform-intelligence-types.d.ts} +15 -20
  11. package/ai/resolveio-platform-intelligence-types.js +4 -0
  12. package/ai/resolveio-platform-intelligence-types.js.map +1 -0
  13. package/ai/resolveio-platform-intelligence.d.ts +6 -0
  14. package/ai/resolveio-platform-intelligence.js +463 -0
  15. package/ai/resolveio-platform-intelligence.js.map +1 -0
  16. package/client-server-app.d.ts +1 -0
  17. package/client-server-app.js +68 -0
  18. package/client-server-app.js.map +1 -0
  19. package/collections/ai-run.collection.d.ts +3 -0
  20. package/collections/ai-run.collection.js +170 -0
  21. package/collections/ai-run.collection.js.map +1 -0
  22. package/collections/ai-terminal-conversation.collection.d.ts +2 -0
  23. package/collections/ai-terminal-conversation.collection.js +140 -0
  24. package/collections/ai-terminal-conversation.collection.js.map +1 -0
  25. package/collections/ai-terminal-issue-report.collection.d.ts +2 -0
  26. package/collections/ai-terminal-issue-report.collection.js +148 -0
  27. package/collections/ai-terminal-issue-report.collection.js.map +1 -0
  28. package/collections/ai-terminal-message.collection.d.ts +2 -0
  29. package/collections/ai-terminal-message.collection.js +121 -0
  30. package/collections/ai-terminal-message.collection.js.map +1 -0
  31. package/collections/app-setting.collection.d.ts +3 -0
  32. package/collections/app-setting.collection.js +103 -0
  33. package/collections/app-setting.collection.js.map +1 -0
  34. package/collections/app-status.collection.d.ts +3 -0
  35. package/collections/app-status.collection.js +57 -0
  36. package/collections/app-status.collection.js.map +1 -0
  37. package/collections/communication-metric.collection.d.ts +2 -0
  38. package/collections/communication-metric.collection.js +133 -0
  39. package/collections/communication-metric.collection.js.map +1 -0
  40. package/collections/counter.collection.d.ts +3 -0
  41. package/collections/counter.collection.js +56 -0
  42. package/collections/counter.collection.js.map +1 -0
  43. package/collections/cron-job-history.collection.d.ts +3 -0
  44. package/collections/cron-job-history.collection.js +137 -0
  45. package/collections/cron-job-history.collection.js.map +1 -0
  46. package/collections/cron-job.collection.d.ts +3 -0
  47. package/collections/cron-job.collection.js +92 -0
  48. package/collections/cron-job.collection.js.map +1 -0
  49. package/collections/customer-notification.collection.d.ts +3 -0
  50. package/collections/customer-notification.collection.js +130 -0
  51. package/collections/customer-notification.collection.js.map +1 -0
  52. package/collections/customer-portal-password.collection.d.ts +3 -0
  53. package/collections/customer-portal-password.collection.js +75 -0
  54. package/collections/customer-portal-password.collection.js.map +1 -0
  55. package/collections/email-history.collection.d.ts +3 -0
  56. package/collections/email-history.collection.js +134 -0
  57. package/collections/email-history.collection.js.map +1 -0
  58. package/collections/email-verified.collection.d.ts +3 -0
  59. package/collections/email-verified.collection.js +62 -0
  60. package/collections/email-verified.collection.js.map +1 -0
  61. package/collections/file.collection.d.ts +3 -0
  62. package/collections/file.collection.js +74 -0
  63. package/collections/file.collection.js.map +1 -0
  64. package/collections/flag-update.collection.d.ts +3 -0
  65. package/collections/flag-update.collection.js +57 -0
  66. package/collections/flag-update.collection.js.map +1 -0
  67. package/collections/flag.collection.d.ts +3 -0
  68. package/collections/flag.collection.js +57 -0
  69. package/collections/flag.collection.js.map +1 -0
  70. package/collections/log-method-latency.collection.d.ts +3 -0
  71. package/collections/log-method-latency.collection.js +77 -0
  72. package/collections/log-method-latency.collection.js.map +1 -0
  73. package/collections/log-subscription.collection.d.ts +3 -0
  74. package/collections/log-subscription.collection.js +80 -0
  75. package/collections/log-subscription.collection.js.map +1 -0
  76. package/collections/log.collection.d.ts +3 -0
  77. package/collections/log.collection.js +93 -0
  78. package/collections/log.collection.js.map +1 -0
  79. package/collections/logged-in-users.collection.d.ts +3 -0
  80. package/collections/logged-in-users.collection.js +67 -0
  81. package/collections/logged-in-users.collection.js.map +1 -0
  82. package/collections/monitor-cpu.collection.d.ts +3 -0
  83. package/collections/monitor-cpu.collection.js +65 -0
  84. package/collections/monitor-cpu.collection.js.map +1 -0
  85. package/collections/monitor-function.collection.d.ts +3 -0
  86. package/collections/monitor-function.collection.js +74 -0
  87. package/collections/monitor-function.collection.js.map +1 -0
  88. package/collections/monitor-memory.collection.d.ts +3 -0
  89. package/collections/monitor-memory.collection.js +77 -0
  90. package/collections/monitor-memory.collection.js.map +1 -0
  91. package/collections/monitor-mongo.collection.d.ts +3 -0
  92. package/collections/monitor-mongo.collection.js +71 -0
  93. package/collections/monitor-mongo.collection.js.map +1 -0
  94. package/collections/notification.collection.d.ts +3 -0
  95. package/collections/notification.collection.js +57 -0
  96. package/collections/notification.collection.js.map +1 -0
  97. package/collections/openai-usage-ledger.collection.d.ts +2 -0
  98. package/collections/openai-usage-ledger.collection.js +188 -0
  99. package/collections/openai-usage-ledger.collection.js.map +1 -0
  100. package/collections/report-builder-dashboard-builder.collection.d.ts +3 -0
  101. package/collections/report-builder-dashboard-builder.collection.js +109 -0
  102. package/collections/report-builder-dashboard-builder.collection.js.map +1 -0
  103. package/collections/report-builder-library.collection.d.ts +3 -0
  104. package/collections/report-builder-library.collection.js +87 -0
  105. package/collections/report-builder-library.collection.js.map +1 -0
  106. package/collections/report-builder-report.collection.d.ts +4 -0
  107. package/collections/report-builder-report.collection.js +184 -0
  108. package/collections/report-builder-report.collection.js.map +1 -0
  109. package/collections/user-group.collection.d.ts +4 -0
  110. package/collections/user-group.collection.js +89 -0
  111. package/collections/user-group.collection.js.map +1 -0
  112. package/collections/user-guide.collection.d.ts +3 -0
  113. package/collections/user-guide.collection.js +57 -0
  114. package/collections/user-guide.collection.js.map +1 -0
  115. package/collections/user.collection.d.ts +4 -0
  116. package/collections/user.collection.js +180 -0
  117. package/collections/user.collection.js.map +1 -0
  118. package/cron/cron.d.ts +14 -0
  119. package/cron/cron.js +216 -0
  120. package/cron/cron.js.map +1 -0
  121. package/fixtures/cron-jobs.d.ts +1 -0
  122. package/fixtures/cron-jobs.js +150 -0
  123. package/fixtures/cron-jobs.js.map +1 -0
  124. package/fixtures/init.d.ts +1 -0
  125. package/fixtures/init.js +91 -0
  126. package/fixtures/init.js.map +1 -0
  127. package/http/auth.d.ts +2 -0
  128. package/http/auth.js +951 -0
  129. package/http/auth.js.map +1 -0
  130. package/http/health.d.ts +1 -0
  131. package/http/health.js +11 -0
  132. package/http/health.js.map +1 -0
  133. package/http/home.d.ts +1 -0
  134. package/http/home.js +134 -0
  135. package/http/home.js.map +1 -0
  136. package/http/slow-query-publication.d.ts +2 -0
  137. package/http/slow-query-publication.js +99 -0
  138. package/http/slow-query-publication.js.map +1 -0
  139. package/index.d.ts +1 -0
  140. package/index.js +19 -0
  141. package/index.js.map +1 -0
  142. package/managers/ai-assistant-codex-manager.manager.d.ts +67 -0
  143. package/managers/ai-assistant-codex-manager.manager.js +1113 -0
  144. package/managers/ai-assistant-codex-manager.manager.js.map +1 -0
  145. package/managers/ai-run-evidence.manager.d.ts +36 -0
  146. package/managers/ai-run-evidence.manager.js +377 -0
  147. package/managers/ai-run-evidence.manager.js.map +1 -0
  148. package/managers/communication-metric.manager.d.ts +16 -0
  149. package/managers/communication-metric.manager.js +134 -0
  150. package/managers/communication-metric.manager.js.map +1 -0
  151. package/managers/cron.manager.d.ts +20 -0
  152. package/managers/cron.manager.js +534 -0
  153. package/managers/cron.manager.js.map +1 -0
  154. package/managers/customer-notification-content.manager.d.ts +55 -0
  155. package/managers/customer-notification-content.manager.js +158 -0
  156. package/managers/customer-notification-content.manager.js.map +1 -0
  157. package/managers/diagnostic-manager-bootstrap.d.ts +9 -0
  158. package/managers/diagnostic-manager-bootstrap.js +260 -0
  159. package/managers/diagnostic-manager-bootstrap.js.map +1 -0
  160. package/managers/error-auto-fix.manager.d.ts +149 -0
  161. package/managers/error-auto-fix.manager.js +3064 -0
  162. package/managers/error-auto-fix.manager.js.map +1 -0
  163. package/managers/local-log.manager.d.ts +18 -0
  164. package/managers/local-log.manager.js +88 -0
  165. package/managers/local-log.manager.js.map +1 -0
  166. package/managers/method.manager.d.ts +84 -0
  167. package/managers/method.manager.js +1964 -0
  168. package/managers/method.manager.js.map +1 -0
  169. package/managers/mongo.manager.d.ts +224 -0
  170. package/managers/mongo.manager.js +5000 -0
  171. package/managers/mongo.manager.js.map +1 -0
  172. package/managers/monitor.manager.d.ts +70 -0
  173. package/managers/monitor.manager.js +550 -0
  174. package/managers/monitor.manager.js.map +1 -0
  175. package/managers/openai-usage-ledger.manager.d.ts +30 -0
  176. package/managers/openai-usage-ledger.manager.js +142 -0
  177. package/managers/openai-usage-ledger.manager.js.map +1 -0
  178. package/managers/slow-query-verifier.manager.d.ts +144 -0
  179. package/managers/slow-query-verifier.manager.js +3857 -0
  180. package/managers/slow-query-verifier.manager.js.map +1 -0
  181. package/managers/slow-query.manager.d.ts +28 -0
  182. package/managers/slow-query.manager.js +468 -0
  183. package/managers/slow-query.manager.js.map +1 -0
  184. package/managers/subscription.manager.d.ts +169 -0
  185. package/managers/subscription.manager.js +3434 -0
  186. package/managers/subscription.manager.js.map +1 -0
  187. package/managers/websocket.manager.d.ts +73 -0
  188. package/managers/websocket.manager.js +673 -0
  189. package/managers/websocket.manager.js.map +1 -0
  190. package/managers/worker-dispatcher.manager.d.ts +120 -0
  191. package/managers/worker-dispatcher.manager.js +1266 -0
  192. package/managers/worker-dispatcher.manager.js.map +1 -0
  193. package/managers/worker-server.manager.d.ts +35 -0
  194. package/managers/worker-server.manager.js +582 -0
  195. package/managers/worker-server.manager.js.map +1 -0
  196. package/methods/accounts.d.ts +2 -0
  197. package/methods/accounts.js +624 -0
  198. package/methods/accounts.js.map +1 -0
  199. package/methods/ai-terminal.d.ts +458 -0
  200. package/methods/ai-terminal.js +27991 -0
  201. package/methods/ai-terminal.js.map +1 -0
  202. package/methods/app-settings.d.ts +2 -0
  203. package/methods/app-settings.js +169 -0
  204. package/methods/app-settings.js.map +1 -0
  205. package/methods/aws.d.ts +2 -0
  206. package/methods/aws.js +877 -0
  207. package/methods/aws.js.map +1 -0
  208. package/methods/collections.d.ts +2 -0
  209. package/methods/collections.js +719 -0
  210. package/methods/collections.js.map +1 -0
  211. package/methods/counters.d.ts +2 -0
  212. package/methods/counters.js +113 -0
  213. package/methods/counters.js.map +1 -0
  214. package/methods/cron-jobs.d.ts +2 -0
  215. package/methods/cron-jobs.js +2475 -0
  216. package/methods/cron-jobs.js.map +1 -0
  217. package/methods/customer-notifications.d.ts +2 -0
  218. package/methods/customer-notifications.js +528 -0
  219. package/methods/customer-notifications.js.map +1 -0
  220. package/methods/diagnostics.d.ts +2 -0
  221. package/methods/diagnostics.js +703 -0
  222. package/methods/diagnostics.js.map +1 -0
  223. package/methods/flag-updates.d.ts +2 -0
  224. package/methods/flag-updates.js +8 -0
  225. package/methods/flag-updates.js.map +1 -0
  226. package/methods/flags.d.ts +2 -0
  227. package/methods/flags.js +8 -0
  228. package/methods/flags.js.map +1 -0
  229. package/methods/logs.d.ts +2 -0
  230. package/methods/logs.js +751 -0
  231. package/methods/logs.js.map +1 -0
  232. package/methods/mongo-explorer.d.ts +2 -0
  233. package/methods/mongo-explorer.js +1808 -0
  234. package/methods/mongo-explorer.js.map +1 -0
  235. package/methods/monitor.d.ts +2 -0
  236. package/methods/monitor.js +543 -0
  237. package/methods/monitor.js.map +1 -0
  238. package/methods/pdf.d.ts +2 -0
  239. package/methods/pdf.js +1216 -0
  240. package/methods/pdf.js.map +1 -0
  241. package/methods/publications.d.ts +1 -0
  242. package/methods/publications.js +183 -0
  243. package/methods/publications.js.map +1 -0
  244. package/methods/report-builder.d.ts +2 -0
  245. package/methods/report-builder.js +3094 -0
  246. package/methods/report-builder.js.map +1 -0
  247. package/methods/support.d.ts +2 -0
  248. package/methods/support.js +430 -0
  249. package/methods/support.js.map +1 -0
  250. package/models/ai-run.model.d.ts +19 -0
  251. package/models/ai-run.model.js +4 -0
  252. package/models/ai-run.model.js.map +1 -0
  253. package/models/ai-terminal-conversation.model.d.ts +17 -0
  254. package/models/ai-terminal-conversation.model.js +4 -0
  255. package/models/ai-terminal-conversation.model.js.map +1 -0
  256. package/models/ai-terminal-issue-report.model.d.ts +19 -0
  257. package/models/ai-terminal-issue-report.model.js +4 -0
  258. package/models/ai-terminal-issue-report.model.js.map +1 -0
  259. package/models/ai-terminal-message.model.d.ts +22 -0
  260. package/models/ai-terminal-message.model.js +4 -0
  261. package/models/ai-terminal-message.model.js.map +1 -0
  262. package/models/app-setting.model.d.ts +16 -0
  263. package/models/app-setting.model.js +4 -0
  264. package/models/app-setting.model.js.map +1 -0
  265. package/{src/models/app-status.model.ts → models/app-status.model.d.ts} +2 -3
  266. package/models/app-status.model.js +4 -0
  267. package/models/app-status.model.js.map +1 -0
  268. package/{src/models/billing-logged-in-users.model.ts → models/billing-logged-in-users.model.d.ts} +4 -5
  269. package/models/billing-logged-in-users.model.js +4 -0
  270. package/models/billing-logged-in-users.model.js.map +1 -0
  271. package/models/collection-document.model.d.ts +21 -0
  272. package/models/collection-document.model.js +4 -0
  273. package/models/collection-document.model.js.map +1 -0
  274. package/models/communication-metric.model.d.ts +20 -0
  275. package/models/communication-metric.model.js +4 -0
  276. package/models/communication-metric.model.js.map +1 -0
  277. package/{src/models/counter.model.ts → models/counter.model.d.ts} +3 -4
  278. package/models/counter.model.js +4 -0
  279. package/models/counter.model.js.map +1 -0
  280. package/models/cron-job-history.model.d.ts +15 -0
  281. package/models/cron-job-history.model.js +4 -0
  282. package/models/cron-job-history.model.js.map +1 -0
  283. package/models/cron-job.model.d.ts +14 -0
  284. package/models/cron-job.model.js +4 -0
  285. package/models/cron-job.model.js.map +1 -0
  286. package/models/customer-notification.model.d.ts +26 -0
  287. package/models/customer-notification.model.js +4 -0
  288. package/models/customer-notification.model.js.map +1 -0
  289. package/models/customer-portal-password.model.d.ts +11 -0
  290. package/models/customer-portal-password.model.js +4 -0
  291. package/models/customer-portal-password.model.js.map +1 -0
  292. package/models/dialog.model.d.ts +23 -0
  293. package/models/dialog.model.js +4 -0
  294. package/models/dialog.model.js.map +1 -0
  295. package/models/email-history.model.d.ts +32 -0
  296. package/{src/models/email-history.model.ts → models/email-history.model.js} +4 -36
  297. package/models/email-history.model.js.map +1 -0
  298. package/{src/models/email-verified.model.ts → models/email-verified.model.d.ts} +5 -6
  299. package/models/email-verified.model.js +4 -0
  300. package/models/email-verified.model.js.map +1 -0
  301. package/{src/models/file.model.ts → models/file.model.d.ts} +7 -8
  302. package/models/file.model.js +4 -0
  303. package/models/file.model.js.map +1 -0
  304. package/{src/models/flag-update.model.ts → models/flag-update.model.d.ts} +3 -4
  305. package/models/flag-update.model.js +4 -0
  306. package/models/flag-update.model.js.map +1 -0
  307. package/{src/models/flag.model.ts → models/flag.model.d.ts} +3 -4
  308. package/models/flag.model.js +4 -0
  309. package/models/flag.model.js.map +1 -0
  310. package/models/log-method-latency.model.d.ts +10 -0
  311. package/models/log-method-latency.model.js +4 -0
  312. package/models/log-method-latency.model.js.map +1 -0
  313. package/{src/models/log-subscription.model.ts → models/log-subscription.model.d.ts} +9 -11
  314. package/models/log-subscription.model.js +4 -0
  315. package/models/log-subscription.model.js.map +1 -0
  316. package/models/log.model.d.ts +17 -0
  317. package/models/log.model.js +4 -0
  318. package/models/log.model.js.map +1 -0
  319. package/{src/models/logged-in-users.model.ts → models/logged-in-users.model.d.ts} +5 -6
  320. package/models/logged-in-users.model.js +4 -0
  321. package/models/logged-in-users.model.js.map +1 -0
  322. package/{src/models/method-response.model.ts → models/method-response.model.d.ts} +6 -7
  323. package/models/method-response.model.js +4 -0
  324. package/models/method-response.model.js.map +1 -0
  325. package/models/method.model.d.ts +26 -0
  326. package/models/method.model.js +4 -0
  327. package/models/method.model.js.map +1 -0
  328. package/{src/models/monitor-cpu.model.ts → models/monitor-cpu.model.d.ts} +7 -9
  329. package/models/monitor-cpu.model.js +4 -0
  330. package/models/monitor-cpu.model.js.map +1 -0
  331. package/models/monitor-function.model.d.ts +14 -0
  332. package/models/monitor-function.model.js +4 -0
  333. package/models/monitor-function.model.js.map +1 -0
  334. package/models/monitor-memory.model.d.ts +15 -0
  335. package/models/monitor-memory.model.js +4 -0
  336. package/models/monitor-memory.model.js.map +1 -0
  337. package/models/monitor-mongo.model.d.ts +13 -0
  338. package/models/monitor-mongo.model.js +4 -0
  339. package/models/monitor-mongo.model.js.map +1 -0
  340. package/{src/models/notification.model.ts → models/notification.model.d.ts} +4 -6
  341. package/models/notification.model.js +4 -0
  342. package/models/notification.model.js.map +1 -0
  343. package/models/openai-usage-ledger.model.d.ts +30 -0
  344. package/models/openai-usage-ledger.model.js +4 -0
  345. package/models/openai-usage-ledger.model.js.map +1 -0
  346. package/models/pagination.model.d.ts +11 -0
  347. package/models/pagination.model.js +28 -0
  348. package/models/pagination.model.js.map +1 -0
  349. package/models/permission.model.d.ts +12 -0
  350. package/models/permission.model.js +4 -0
  351. package/models/permission.model.js.map +1 -0
  352. package/models/report-builder-dashboard-builder.model.d.ts +25 -0
  353. package/models/report-builder-dashboard-builder.model.js +4 -0
  354. package/models/report-builder-dashboard-builder.model.js.map +1 -0
  355. package/models/report-builder-library.model.d.ts +17 -0
  356. package/models/report-builder-library.model.js +4 -0
  357. package/models/report-builder-library.model.js.map +1 -0
  358. package/models/report-builder-report.model.d.ts +121 -0
  359. package/models/report-builder-report.model.js +4 -0
  360. package/models/report-builder-report.model.js.map +1 -0
  361. package/models/report-builder.model.d.ts +61 -0
  362. package/models/report-builder.model.js +4 -0
  363. package/models/report-builder.model.js.map +1 -0
  364. package/models/select-data-label.model.d.ts +9 -0
  365. package/models/select-data-label.model.js +4 -0
  366. package/models/select-data-label.model.js.map +1 -0
  367. package/models/server-message.model.d.ts +32 -0
  368. package/models/server-message.model.js +4 -0
  369. package/models/server-message.model.js.map +1 -0
  370. package/models/slow-query-report.model.d.ts +23 -0
  371. package/models/slow-query-report.model.js +4 -0
  372. package/models/slow-query-report.model.js.map +1 -0
  373. package/models/subscription.model.d.ts +31 -0
  374. package/models/subscription.model.js +4 -0
  375. package/models/subscription.model.js.map +1 -0
  376. package/models/support-ticket.model.d.ts +87 -0
  377. package/models/support-ticket.model.js +4 -0
  378. package/models/support-ticket.model.js.map +1 -0
  379. package/models/user-group.model.d.ts +20 -0
  380. package/models/user-group.model.js +4 -0
  381. package/models/user-group.model.js.map +1 -0
  382. package/{src/models/user-guide.model.ts → models/user-guide.model.d.ts} +4 -5
  383. package/models/user-guide.model.js +4 -0
  384. package/models/user-guide.model.js.map +1 -0
  385. package/models/user.model.d.ts +84 -0
  386. package/models/user.model.js +4 -0
  387. package/models/user.model.js.map +1 -0
  388. package/package.json +1 -1
  389. package/private/images/ResolveIO.png +0 -0
  390. package/public_api.js +127 -0
  391. package/public_api.js.map +1 -0
  392. package/publications/ai-terminal.d.ts +1 -0
  393. package/publications/ai-terminal.js +122 -0
  394. package/publications/ai-terminal.js.map +1 -0
  395. package/publications/app-settings.d.ts +2 -0
  396. package/publications/app-settings.js +28 -0
  397. package/publications/app-settings.js.map +1 -0
  398. package/publications/app-status.d.ts +2 -0
  399. package/publications/app-status.js +16 -0
  400. package/publications/app-status.js.map +1 -0
  401. package/publications/cron-jobs.d.ts +2 -0
  402. package/publications/cron-jobs.js +88 -0
  403. package/publications/cron-jobs.js.map +1 -0
  404. package/publications/customer-notifications.d.ts +2 -0
  405. package/publications/customer-notifications.js +161 -0
  406. package/publications/customer-notifications.js.map +1 -0
  407. package/publications/files.d.ts +2 -0
  408. package/publications/files.js +36 -0
  409. package/publications/files.js.map +1 -0
  410. package/publications/flags-update.d.ts +2 -0
  411. package/publications/flags-update.js +22 -0
  412. package/publications/flags-update.js.map +1 -0
  413. package/publications/flags.d.ts +2 -0
  414. package/publications/flags.js +22 -0
  415. package/publications/flags.js.map +1 -0
  416. package/publications/logs.d.ts +2 -0
  417. package/publications/logs.js +164 -0
  418. package/publications/logs.js.map +1 -0
  419. package/publications/notifications.d.ts +2 -0
  420. package/publications/notifications.js +16 -0
  421. package/publications/notifications.js.map +1 -0
  422. package/publications/report-builder-dashboard-builders.d.ts +2 -0
  423. package/publications/report-builder-dashboard-builders.js +42 -0
  424. package/publications/report-builder-dashboard-builders.js.map +1 -0
  425. package/publications/report-builder-libraries.d.ts +2 -0
  426. package/publications/report-builder-libraries.js +90 -0
  427. package/publications/report-builder-libraries.js.map +1 -0
  428. package/publications/report-builder-reports.d.ts +2 -0
  429. package/publications/report-builder-reports.js +50 -0
  430. package/publications/report-builder-reports.js.map +1 -0
  431. package/publications/super-admin.d.ts +2 -0
  432. package/publications/super-admin.js +16 -0
  433. package/publications/super-admin.js.map +1 -0
  434. package/publications/user-groups.d.ts +1 -0
  435. package/publications/user-groups.js +16 -0
  436. package/publications/user-groups.js.map +1 -0
  437. package/publications/user-guides.d.ts +1 -0
  438. package/publications/user-guides.js +16 -0
  439. package/publications/user-guides.js.map +1 -0
  440. package/resolveio-server-app.d.ts +70 -0
  441. package/resolveio-server-app.js +801 -0
  442. package/resolveio-server-app.js.map +1 -0
  443. package/server-app.d.ts +228 -0
  444. package/server-app.js +3566 -0
  445. package/server-app.js.map +1 -0
  446. package/services/codex-client.d.ts +128 -0
  447. package/services/codex-client.js +1629 -0
  448. package/services/codex-client.js.map +1 -0
  449. package/services/openai-client.d.ts +46 -0
  450. package/services/openai-client.js +318 -0
  451. package/services/openai-client.js.map +1 -0
  452. package/types/error-report.d.ts +25 -0
  453. package/types/error-report.js +4 -0
  454. package/types/error-report.js.map +1 -0
  455. package/types/slow-query-report.d.ts +27 -0
  456. package/types/slow-query-report.js +6 -0
  457. package/types/slow-query-report.js.map +1 -0
  458. package/util/ai-qa-policy.d.ts +124 -0
  459. package/util/ai-qa-policy.js +736 -0
  460. package/util/ai-qa-policy.js.map +1 -0
  461. package/util/ai-run-evidence-adapters.d.ts +109 -0
  462. package/util/ai-run-evidence-adapters.js +7234 -0
  463. package/util/ai-run-evidence-adapters.js.map +1 -0
  464. package/util/ai-run-evidence-dashboard.d.ts +88 -0
  465. package/util/ai-run-evidence-dashboard.js +343 -0
  466. package/util/ai-run-evidence-dashboard.js.map +1 -0
  467. package/util/ai-run-evidence-eval.d.ts +86 -0
  468. package/util/ai-run-evidence-eval.js +1018 -0
  469. package/util/ai-run-evidence-eval.js.map +1 -0
  470. package/util/ai-run-evidence.d.ts +244 -0
  471. package/util/ai-run-evidence.js +1096 -0
  472. package/util/ai-run-evidence.js.map +1 -0
  473. package/util/ai-runner-artifacts.d.ts +82 -0
  474. package/util/ai-runner-artifacts.js +713 -0
  475. package/util/ai-runner-artifacts.js.map +1 -0
  476. package/util/ai-runner-manager-autopilot.d.ts +210 -0
  477. package/util/ai-runner-manager-autopilot.js +642 -0
  478. package/util/ai-runner-manager-autopilot.js.map +1 -0
  479. package/util/ai-runner-manager-policy.d.ts +807 -0
  480. package/util/ai-runner-manager-policy.js +3501 -0
  481. package/util/ai-runner-manager-policy.js.map +1 -0
  482. package/util/ai-runner-qa-auth.d.ts +5 -0
  483. package/util/ai-runner-qa-auth.js +839 -0
  484. package/util/ai-runner-qa-auth.js.map +1 -0
  485. package/util/ai-runner-qa-tools.d.ts +26 -0
  486. package/util/ai-runner-qa-tools.js +3520 -0
  487. package/util/ai-runner-qa-tools.js.map +1 -0
  488. package/util/aicoder-runner-v6.d.ts +426 -0
  489. package/util/aicoder-runner-v6.js +2464 -0
  490. package/util/aicoder-runner-v6.js.map +1 -0
  491. package/util/common.d.ts +31 -0
  492. package/util/common.js +683 -0
  493. package/util/common.js.map +1 -0
  494. package/util/customer-portal-password.d.ts +13 -0
  495. package/util/customer-portal-password.js +209 -0
  496. package/util/customer-portal-password.js.map +1 -0
  497. package/util/error-reporter.d.ts +52 -0
  498. package/util/error-reporter.js +326 -0
  499. package/util/error-reporter.js.map +1 -0
  500. package/util/error-tracking.d.ts +13 -0
  501. package/util/error-tracking.js +120 -0
  502. package/util/error-tracking.js.map +1 -0
  503. package/util/openai-usage-cost.d.ts +6 -0
  504. package/util/openai-usage-cost.js +103 -0
  505. package/util/openai-usage-cost.js.map +1 -0
  506. package/util/report-builder-unwinds.d.ts +15 -0
  507. package/util/report-builder-unwinds.js +156 -0
  508. package/util/report-builder-unwinds.js.map +1 -0
  509. package/util/runner-process-janitor.d.ts +27 -0
  510. package/util/runner-process-janitor.js +208 -0
  511. package/util/runner-process-janitor.js.map +1 -0
  512. package/util/schema-report-builder.d.ts +6 -0
  513. package/util/schema-report-builder.js +481 -0
  514. package/util/schema-report-builder.js.map +1 -0
  515. package/util/slow-query-reporter.d.ts +28 -0
  516. package/util/slow-query-reporter.js +226 -0
  517. package/util/slow-query-reporter.js.map +1 -0
  518. package/util/subscription-dependency-context.d.ts +34 -0
  519. package/util/subscription-dependency-context.js +1283 -0
  520. package/util/subscription-dependency-context.js.map +1 -0
  521. package/util/support-runner-v5.d.ts +1426 -0
  522. package/util/support-runner-v5.js +7647 -0
  523. package/util/support-runner-v5.js.map +1 -0
  524. package/util/tokenizer.d.ts +5 -0
  525. package/util/tokenizer.js +41 -0
  526. package/util/tokenizer.js.map +1 -0
  527. package/workers/codex-runner.worker.d.ts +1 -0
  528. package/workers/codex-runner.worker.js +192 -0
  529. package/workers/codex-runner.worker.js.map +1 -0
  530. package/.nodemon.json +0 -5
  531. package/.vscode/settings.json +0 -21
  532. package/AGENTS.md +0 -195
  533. package/README.md +0 -22
  534. package/build_package.sh +0 -5
  535. package/compileDTS.pl +0 -64
  536. package/docs/ai-assistant-nightly-eval.md +0 -65
  537. package/docs/ai-assistant-preflight-checklist.md +0 -23
  538. package/docs/ai-assistant-report-builder-bridge-playbook.md +0 -115
  539. package/eslint-plugin-custom/index.js +0 -7
  540. package/eslint-plugin-custom/rules/no-filter-zero-index.js +0 -44
  541. package/eslint.config.js +0 -103
  542. package/gulpfile.js +0 -216
  543. package/methodAndPublicationListGenerator.py +0 -375
  544. package/mongodbensurers.js +0 -2
  545. package/mongostop.js +0 -3
  546. package/scripts/cleanup-bypassed-callmethod-logs.js +0 -616
  547. package/settings.development.json +0 -25
  548. package/settings.development.redacted.json +0 -25
  549. package/src/.env +0 -12
  550. package/src/ai/assistant-core-heuristics.ts +0 -379
  551. package/src/ai/resolveio-platform-intelligence-memory-corpus.ts +0 -185
  552. package/src/ai/resolveio-platform-intelligence-memory.ts +0 -325
  553. package/src/ai/resolveio-platform-intelligence.ts +0 -462
  554. package/src/client-server-app.ts +0 -12
  555. package/src/collections/ai-run.collection.ts +0 -117
  556. package/src/collections/ai-terminal-conversation.collection.ts +0 -91
  557. package/src/collections/ai-terminal-issue-report.collection.ts +0 -99
  558. package/src/collections/ai-terminal-message.collection.ts +0 -77
  559. package/src/collections/app-setting.collection.ts +0 -104
  560. package/src/collections/app-status.collection.ts +0 -58
  561. package/src/collections/communication-metric.collection.ts +0 -84
  562. package/src/collections/counter.collection.ts +0 -56
  563. package/src/collections/cron-job-history.collection.ts +0 -94
  564. package/src/collections/cron-job.collection.ts +0 -92
  565. package/src/collections/customer-notification.collection.ts +0 -131
  566. package/src/collections/customer-portal-password.collection.ts +0 -76
  567. package/src/collections/email-history.collection.ts +0 -134
  568. package/src/collections/email-verified.collection.ts +0 -62
  569. package/src/collections/file.collection.ts +0 -74
  570. package/src/collections/flag-update.collection.ts +0 -57
  571. package/src/collections/flag.collection.ts +0 -57
  572. package/src/collections/log-method-latency.collection.ts +0 -77
  573. package/src/collections/log-subscription.collection.ts +0 -80
  574. package/src/collections/log.collection.ts +0 -93
  575. package/src/collections/logged-in-users.collection.ts +0 -67
  576. package/src/collections/monitor-cpu.collection.ts +0 -65
  577. package/src/collections/monitor-function.collection.ts +0 -74
  578. package/src/collections/monitor-memory.collection.ts +0 -77
  579. package/src/collections/monitor-mongo.collection.ts +0 -71
  580. package/src/collections/notification.collection.ts +0 -57
  581. package/src/collections/openai-usage-ledger.collection.ts +0 -131
  582. package/src/collections/report-builder-dashboard-builder.collection.ts +0 -109
  583. package/src/collections/report-builder-library.collection.ts +0 -89
  584. package/src/collections/report-builder-report.collection.ts +0 -184
  585. package/src/collections/user-group.collection.ts +0 -89
  586. package/src/collections/user-guide.collection.ts +0 -57
  587. package/src/collections/user.collection.ts +0 -181
  588. package/src/cron/cron.ts +0 -117
  589. package/src/fixtures/cron-jobs.ts +0 -95
  590. package/src/fixtures/init.ts +0 -35
  591. package/src/http/auth.ts +0 -818
  592. package/src/http/health.ts +0 -7
  593. package/src/http/home.ts +0 -90
  594. package/src/http/slow-query-publication.ts +0 -49
  595. package/src/index.ts +0 -1
  596. package/src/managers/ai-assistant-codex-manager.manager.ts +0 -1131
  597. package/src/managers/ai-run-evidence.manager.ts +0 -264
  598. package/src/managers/communication-metric.manager.ts +0 -82
  599. package/src/managers/cron.manager.ts +0 -333
  600. package/src/managers/customer-notification-content.manager.ts +0 -236
  601. package/src/managers/diagnostic-manager-bootstrap.ts +0 -165
  602. package/src/managers/error-auto-fix.manager.ts +0 -2767
  603. package/src/managers/local-log.manager.ts +0 -113
  604. package/src/managers/method.manager.ts +0 -1857
  605. package/src/managers/mongo.manager.ts +0 -4575
  606. package/src/managers/monitor.manager.ts +0 -507
  607. package/src/managers/openai-usage-ledger.manager.ts +0 -112
  608. package/src/managers/slow-query-verifier.manager.ts +0 -3590
  609. package/src/managers/slow-query.manager.ts +0 -519
  610. package/src/managers/subscription.manager.ts +0 -3128
  611. package/src/managers/websocket.manager.ts +0 -746
  612. package/src/managers/worker-dispatcher.manager.ts +0 -1360
  613. package/src/managers/worker-server.manager.ts +0 -536
  614. package/src/methods/accounts.ts +0 -532
  615. package/src/methods/ai-terminal.ts +0 -29070
  616. package/src/methods/app-settings.ts +0 -114
  617. package/src/methods/aws.ts +0 -649
  618. package/src/methods/collections.ts +0 -641
  619. package/src/methods/counters.ts +0 -69
  620. package/src/methods/cron-jobs.ts +0 -2614
  621. package/src/methods/customer-notifications.ts +0 -458
  622. package/src/methods/diagnostics.ts +0 -616
  623. package/src/methods/flag-updates.ts +0 -7
  624. package/src/methods/flags.ts +0 -7
  625. package/src/methods/logs.ts +0 -657
  626. package/src/methods/mongo-explorer.ts +0 -1880
  627. package/src/methods/monitor.ts +0 -540
  628. package/src/methods/pdf.ts +0 -1236
  629. package/src/methods/publications.ts +0 -129
  630. package/src/methods/report-builder.ts +0 -3300
  631. package/src/methods/support.ts +0 -335
  632. package/src/models/ai-run.model.ts +0 -27
  633. package/src/models/ai-terminal-conversation.model.ts +0 -19
  634. package/src/models/ai-terminal-issue-report.model.ts +0 -21
  635. package/src/models/ai-terminal-message.model.ts +0 -24
  636. package/src/models/app-setting.model.ts +0 -17
  637. package/src/models/collection-document.model.ts +0 -24
  638. package/src/models/communication-metric.model.ts +0 -23
  639. package/src/models/cron-job-history.model.ts +0 -16
  640. package/src/models/cron-job.model.ts +0 -15
  641. package/src/models/customer-notification.model.ts +0 -28
  642. package/src/models/customer-portal-password.model.ts +0 -12
  643. package/src/models/dialog.model.ts +0 -25
  644. package/src/models/log-method-latency.model.ts +0 -11
  645. package/src/models/log.model.ts +0 -19
  646. package/src/models/method.model.ts +0 -25
  647. package/src/models/monitor-function.model.ts +0 -16
  648. package/src/models/monitor-memory.model.ts +0 -17
  649. package/src/models/monitor-mongo.model.ts +0 -15
  650. package/src/models/openai-usage-ledger.model.ts +0 -56
  651. package/src/models/pagination.model.ts +0 -35
  652. package/src/models/permission.model.ts +0 -14
  653. package/src/models/report-builder-dashboard-builder.model.ts +0 -29
  654. package/src/models/report-builder-library.model.ts +0 -20
  655. package/src/models/report-builder-report.model.ts +0 -136
  656. package/src/models/report-builder.model.ts +0 -68
  657. package/src/models/select-data-label.model.ts +0 -9
  658. package/src/models/server-message.model.ts +0 -31
  659. package/src/models/slow-query-report.model.ts +0 -23
  660. package/src/models/subscription.model.ts +0 -73
  661. package/src/models/support-ticket.model.ts +0 -104
  662. package/src/models/user-group.model.ts +0 -24
  663. package/src/models/user.model.ts +0 -96
  664. package/src/private/images/ResolveIO.png +0 -0
  665. package/src/publications/ai-terminal.ts +0 -73
  666. package/src/publications/app-settings.ts +0 -25
  667. package/src/publications/app-status.ts +0 -13
  668. package/src/publications/cron-jobs.ts +0 -40
  669. package/src/publications/customer-notifications.ts +0 -101
  670. package/src/publications/files.ts +0 -33
  671. package/src/publications/flags-update.ts +0 -19
  672. package/src/publications/flags.ts +0 -19
  673. package/src/publications/logs.ts +0 -163
  674. package/src/publications/notifications.ts +0 -13
  675. package/src/publications/report-builder-dashboard-builders.ts +0 -39
  676. package/src/publications/report-builder-libraries.ts +0 -41
  677. package/src/publications/report-builder-reports.ts +0 -47
  678. package/src/publications/super-admin.ts +0 -13
  679. package/src/publications/user-groups.ts +0 -12
  680. package/src/publications/user-guides.ts +0 -12
  681. package/src/resolveio-server-app.ts +0 -617
  682. package/src/server-app.ts +0 -3354
  683. package/src/services/codex-client.ts +0 -1231
  684. package/src/services/openai-client.ts +0 -265
  685. package/src/types/error-report.ts +0 -26
  686. package/src/types/js-tiktoken.d.ts +0 -11
  687. package/src/types/slow-query-report.ts +0 -28
  688. package/src/util/ai-qa-policy.ts +0 -925
  689. package/src/util/ai-run-evidence-adapters.ts +0 -8347
  690. package/src/util/ai-run-evidence-dashboard.ts +0 -323
  691. package/src/util/ai-run-evidence-eval.ts +0 -1057
  692. package/src/util/ai-run-evidence.ts +0 -1430
  693. package/src/util/ai-runner-artifacts.ts +0 -586
  694. package/src/util/ai-runner-manager-autopilot.ts +0 -961
  695. package/src/util/ai-runner-manager-policy.ts +0 -5011
  696. package/src/util/ai-runner-qa-auth.ts +0 -838
  697. package/src/util/ai-runner-qa-tools.ts +0 -3536
  698. package/src/util/aicoder-runner-v6.ts +0 -3121
  699. package/src/util/common.ts +0 -649
  700. package/src/util/customer-portal-password.ts +0 -183
  701. package/src/util/error-reporter.ts +0 -332
  702. package/src/util/error-tracking.ts +0 -79
  703. package/src/util/openai-usage-cost.ts +0 -114
  704. package/src/util/report-builder-unwinds.ts +0 -180
  705. package/src/util/runner-process-janitor.ts +0 -219
  706. package/src/util/schema-report-builder.ts +0 -448
  707. package/src/util/slow-query-reporter.ts +0 -216
  708. package/src/util/subscription-dependency-context.ts +0 -1096
  709. package/src/util/support-runner-v5.ts +0 -10040
  710. package/src/util/tokenizer.ts +0 -38
  711. package/src/workers/codex-runner.worker.ts +0 -142
  712. package/start_server.sh +0 -5
  713. package/tests/ai-assistant-corpus-build.ts +0 -484
  714. package/tests/ai-assistant-corpus-replay-e2e.ts +0 -774
  715. package/tests/ai-assistant-data-parity-e2e.ts +0 -1989
  716. package/tests/ai-assistant-eval-triage.ts +0 -831
  717. package/tests/ai-assistant-openai-e2e.ts +0 -1061
  718. package/tests/ai-assistant-openai-git-e2e.ts +0 -155
  719. package/tests/ai-assistant-preflight-matrix.ts +0 -215
  720. package/tests/ai-assistant-routing-eval.test.ts +0 -585
  721. package/tests/ai-assistant-snf-live-eval.ts +0 -975
  722. package/tests/ai-assistant-utils.test.ts +0 -4834
  723. package/tests/ai-manager-autopilot-snapshot.test.ts +0 -193
  724. package/tests/ai-manager-recovery-checkpoint.test.ts +0 -1383
  725. package/tests/ai-run-eval.test.ts +0 -132
  726. package/tests/ai-run-evidence.test.ts +0 -3773
  727. package/tests/ai-runner-contract.test.ts +0 -515
  728. package/tests/aicoder-runner-v6.test.ts +0 -822
  729. package/tests/error-reporter.test.ts +0 -145
  730. package/tests/method-publication-generator.test.ts +0 -46
  731. package/tests/report-builder-linking.test.ts +0 -79
  732. package/tests/resolveio-platform-intelligence.test.ts +0 -352
  733. package/tests/server-app-cron-owner.test.ts +0 -127
  734. package/tests/subscription-connect-race.test.ts +0 -158
  735. package/tests/subscription-dependency-context.test.ts +0 -324
  736. package/tests/subscription-manager-collection-tracking.test.ts +0 -86
  737. package/tests/subscription-manager-invalidation.test.ts +0 -86
  738. package/tests/support-runner-v5.test.ts +0 -3201
  739. package/tsconfig.json +0 -34
  740. /package/{src/private → private}/email-templates/enrollment.html +0 -0
  741. /package/{src/private → private}/email-templates/forgot-password.html +0 -0
  742. /package/{src/private → private}/email-templates/support-ticket-deleted.html +0 -0
  743. /package/{src/private → private}/email-templates/support-ticket-modified.html +0 -0
  744. /package/{src/private → private}/email-templates/support-ticket.html +0 -0
  745. /package/{src/public_api.ts → public_api.d.ts} +0 -0
@@ -0,0 +1,3434 @@
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
+ if (code === 13 && message.includes('allchangesforcluster')) {
1516
+ return true;
1517
+ }
1518
+ if (code === 13 && message.includes('not authorized') && message.includes('$db: "admin"')) {
1519
+ return true;
1520
+ }
1521
+ return false;
1522
+ };
1523
+ SubscriptionManager.prototype.isResumeTokenInvalid = function (error) {
1524
+ var code = typeof (error === null || error === void 0 ? void 0 : error.code) === 'number' ? error.code : null;
1525
+ var codeName = typeof (error === null || error === void 0 ? void 0 : error.codeName) === 'string' ? error.codeName.toLowerCase() : '';
1526
+ var message = typeof (error === null || error === void 0 ? void 0 : error.message) === 'string' ? error.message.toLowerCase() : '';
1527
+ if (code === 286) {
1528
+ // ChangeStreamHistoryLost
1529
+ return true;
1530
+ }
1531
+ if (codeName.includes('changestreamhistorylost')) {
1532
+ return true;
1533
+ }
1534
+ if (message.includes('resume token') && (message.includes('not found') ||
1535
+ message.includes('not possible') ||
1536
+ message.includes('no longer') ||
1537
+ message.includes('not in the oplog') ||
1538
+ message.includes('too old'))) {
1539
+ return true;
1540
+ }
1541
+ if (message.includes('resume of change stream') && message.includes('not possible')) {
1542
+ return true;
1543
+ }
1544
+ return false;
1545
+ };
1546
+ SubscriptionManager.prototype.isChangeStreamDocumentTooLarge = function (error) {
1547
+ var code = typeof (error === null || error === void 0 ? void 0 : error.code) === 'number' ? error.code : null;
1548
+ var codeName = typeof (error === null || error === void 0 ? void 0 : error.codeName) === 'string' ? error.codeName.toLowerCase() : '';
1549
+ var message = typeof (error === null || error === void 0 ? void 0 : error.message) === 'string' ? error.message.toLowerCase() : '';
1550
+ if (code === 10334) {
1551
+ return true;
1552
+ }
1553
+ if (codeName.includes('bsonobjecttoolarge')) {
1554
+ return true;
1555
+ }
1556
+ if (message.includes('bsonobj size') && message.includes('16mb')) {
1557
+ return true;
1558
+ }
1559
+ return false;
1560
+ };
1561
+ SubscriptionManager.prototype.enableLocalOplogFallback = function (reason, error) {
1562
+ return __awaiter(this, void 0, void 0, function () {
1563
+ var _a, message;
1564
+ var _this = this;
1565
+ return __generator(this, function (_b) {
1566
+ switch (_b.label) {
1567
+ case 0:
1568
+ if (this._useLocalOplog) {
1569
+ return [2 /*return*/];
1570
+ }
1571
+ this._useLocalOplog = true;
1572
+ this._oplogMode = 'local';
1573
+ this._oplogRetryCount = 0;
1574
+ if (!(this._oplog$ && !this._oplog$.closed)) return [3 /*break*/, 5];
1575
+ _b.label = 1;
1576
+ case 1:
1577
+ _b.trys.push([1, 3, , 4]);
1578
+ this._oplog$.removeAllListeners();
1579
+ return [4 /*yield*/, this._oplog$.close()];
1580
+ case 2:
1581
+ _b.sent();
1582
+ return [3 /*break*/, 4];
1583
+ case 3:
1584
+ _a = _b.sent();
1585
+ return [3 /*break*/, 4];
1586
+ case 4:
1587
+ this._oplog$ = null;
1588
+ _b.label = 5;
1589
+ case 5:
1590
+ message = typeof (error === null || error === void 0 ? void 0 : error.message) === 'string' ? error.message : '';
1591
+ console.log(new Date(), 'oplog fallback enabled', reason, message ? "(".concat(message, ")") : '');
1592
+ this.queueFullResync('oplog-fallback-enabled');
1593
+ if (this._localOplogResyncIntervalMs > 0 && !this._localOplogResyncTimer) {
1594
+ this._localOplogResyncTimer = setInterval(function () {
1595
+ _this.queueFullResync('oplog-fallback-poll');
1596
+ }, this._localOplogResyncIntervalMs);
1597
+ }
1598
+ return [2 /*return*/];
1599
+ }
1600
+ });
1601
+ });
1602
+ };
1603
+ SubscriptionManager.prototype.getUseLocalOplog = function () {
1604
+ return this._useLocalOplog;
1605
+ };
1606
+ SubscriptionManager.prototype.handleMongoReconnect = function (reason) {
1607
+ return __awaiter(this, void 0, void 0, function () {
1608
+ var resumeToken, _a, error_3;
1609
+ return __generator(this, function (_b) {
1610
+ switch (_b.label) {
1611
+ case 0:
1612
+ if (this._useLocalOplog) {
1613
+ this.queueFullResync("mongo-reconnect:".concat(reason));
1614
+ return [2 /*return*/];
1615
+ }
1616
+ _b.label = 1;
1617
+ case 1:
1618
+ _b.trys.push([1, 5, , 6]);
1619
+ _a = this._lastResumeToken;
1620
+ if (_a) return [3 /*break*/, 3];
1621
+ return [4 /*yield*/, this.loadResumeToken()];
1622
+ case 2:
1623
+ _a = (_b.sent());
1624
+ _b.label = 3;
1625
+ case 3:
1626
+ resumeToken = _a;
1627
+ return [4 /*yield*/, this.tailOpLog(resumeToken || undefined)];
1628
+ case 4:
1629
+ _b.sent();
1630
+ return [3 /*break*/, 6];
1631
+ case 5:
1632
+ error_3 = _b.sent();
1633
+ console.log(new Date(), 'Sub Manager', 'Mongo reconnect: tailOpLog restart failed', reason, error_3);
1634
+ return [3 /*break*/, 6];
1635
+ case 6:
1636
+ this.queueFullResync("mongo-reconnect:".concat(reason));
1637
+ return [2 /*return*/];
1638
+ }
1639
+ });
1640
+ });
1641
+ };
1642
+ SubscriptionManager.prototype.notifyLocalOplog = function (collection, type) {
1643
+ var _this = this;
1644
+ if (type === void 0) { type = 'update'; }
1645
+ if (!this._useLocalOplog) {
1646
+ return;
1647
+ }
1648
+ setImmediate(function () { return __awaiter(_this, void 0, void 0, function () {
1649
+ var _a;
1650
+ return __generator(this, function (_b) {
1651
+ switch (_b.label) {
1652
+ case 0:
1653
+ _b.trys.push([0, 2, , 3]);
1654
+ return [4 /*yield*/, this.invalidatePubsCache(collection, type)];
1655
+ case 1:
1656
+ _b.sent();
1657
+ return [3 /*break*/, 3];
1658
+ case 2:
1659
+ _a = _b.sent();
1660
+ return [3 /*break*/, 3];
1661
+ case 3: return [2 /*return*/];
1662
+ }
1663
+ });
1664
+ }); });
1665
+ };
1666
+ SubscriptionManager.prototype.getResumeTokenDocId = function () {
1667
+ if (process.env.NODE_APP_INSTANCE) {
1668
+ return "oplog:".concat(process.env.NODE_APP_INSTANCE);
1669
+ }
1670
+ // Fallback: avoid cross-process contention when NODE_APP_INSTANCE is not set.
1671
+ return "oplog:".concat(process.pid);
1672
+ };
1673
+ SubscriptionManager.prototype.loadResumeToken = function () {
1674
+ return __awaiter(this, void 0, void 0, function () {
1675
+ var db_1, loadToken, mongoManager, _a, error_4;
1676
+ var _this = this;
1677
+ return __generator(this, function (_b) {
1678
+ switch (_b.label) {
1679
+ case 0:
1680
+ _b.trys.push([0, 5, , 6]);
1681
+ db_1 = resolveio_server_app_1.ResolveIOServer.getMainDB();
1682
+ if (!db_1) {
1683
+ return [2 /*return*/, null];
1684
+ }
1685
+ loadToken = function () { return __awaiter(_this, void 0, void 0, function () {
1686
+ var collection, doc, token, maxAgeMs, ageMs;
1687
+ return __generator(this, function (_a) {
1688
+ switch (_a.label) {
1689
+ case 0:
1690
+ collection = db_1.collection(this.RESUME_TOKEN_COLLECTION);
1691
+ return [4 /*yield*/, collection.findOne({ _id: this.getResumeTokenDocId() })];
1692
+ case 1:
1693
+ doc = _a.sent();
1694
+ token = (doc === null || doc === void 0 ? void 0 : doc.token) || null;
1695
+ if (!token) {
1696
+ return [2 /*return*/, null];
1697
+ }
1698
+ maxAgeMs = this.resolveResumeTokenMaxAgeMs();
1699
+ if (!(maxAgeMs > 0 && (doc === null || doc === void 0 ? void 0 : doc.updatedAt) instanceof Date)) return [3 /*break*/, 3];
1700
+ ageMs = Date.now() - doc.updatedAt.getTime();
1701
+ if (!(ageMs > maxAgeMs)) return [3 /*break*/, 3];
1702
+ return [4 /*yield*/, collection.deleteOne({ _id: this.getResumeTokenDocId() })];
1703
+ case 2:
1704
+ _a.sent();
1705
+ console.log(new Date(), 'Sub Manager', 'Resume token expired, clearing', (0, common_1.round)(ageMs / 1000), 's');
1706
+ return [2 /*return*/, null];
1707
+ case 3: return [2 /*return*/, token];
1708
+ }
1709
+ });
1710
+ }); };
1711
+ mongoManager = resolveio_server_app_1.ResolveIOServer.getMongoManager();
1712
+ if (!mongoManager) return [3 /*break*/, 2];
1713
+ return [4 /*yield*/, mongoManager.runWithoutSession(loadToken)];
1714
+ case 1:
1715
+ _a = _b.sent();
1716
+ return [3 /*break*/, 4];
1717
+ case 2: return [4 /*yield*/, loadToken()];
1718
+ case 3:
1719
+ _a = _b.sent();
1720
+ _b.label = 4;
1721
+ case 4: return [2 /*return*/, _a];
1722
+ case 5:
1723
+ error_4 = _b.sent();
1724
+ console.log(new Date(), 'Sub Manager', 'Failed to load oplog resume token', error_4);
1725
+ return [2 /*return*/, null];
1726
+ case 6: return [2 /*return*/];
1727
+ }
1728
+ });
1729
+ });
1730
+ };
1731
+ SubscriptionManager.prototype.saveResumeToken = function (token) {
1732
+ return __awaiter(this, void 0, void 0, function () {
1733
+ var db_2, saveToken, mongoManager, error_5;
1734
+ var _this = this;
1735
+ return __generator(this, function (_a) {
1736
+ switch (_a.label) {
1737
+ case 0:
1738
+ if (!token) {
1739
+ return [2 /*return*/];
1740
+ }
1741
+ _a.label = 1;
1742
+ case 1:
1743
+ _a.trys.push([1, 6, , 7]);
1744
+ db_2 = resolveio_server_app_1.ResolveIOServer.getMainDB();
1745
+ if (!db_2) {
1746
+ return [2 /*return*/];
1747
+ }
1748
+ saveToken = function () { return __awaiter(_this, void 0, void 0, function () {
1749
+ return __generator(this, function (_a) {
1750
+ switch (_a.label) {
1751
+ case 0: return [4 /*yield*/, db_2.collection(this.RESUME_TOKEN_COLLECTION).updateOne({ _id: this.getResumeTokenDocId() }, { $set: { token: token, updatedAt: new Date() } }, { upsert: true })];
1752
+ case 1:
1753
+ _a.sent();
1754
+ return [2 /*return*/];
1755
+ }
1756
+ });
1757
+ }); };
1758
+ mongoManager = resolveio_server_app_1.ResolveIOServer.getMongoManager();
1759
+ if (!mongoManager) return [3 /*break*/, 3];
1760
+ return [4 /*yield*/, mongoManager.runWithoutSession(saveToken)];
1761
+ case 2:
1762
+ _a.sent();
1763
+ return [3 /*break*/, 5];
1764
+ case 3: return [4 /*yield*/, saveToken()];
1765
+ case 4:
1766
+ _a.sent();
1767
+ _a.label = 5;
1768
+ case 5: return [3 /*break*/, 7];
1769
+ case 6:
1770
+ error_5 = _a.sent();
1771
+ console.log(new Date(), 'Sub Manager', 'Failed to persist oplog resume token', error_5);
1772
+ return [3 /*break*/, 7];
1773
+ case 7: return [2 /*return*/];
1774
+ }
1775
+ });
1776
+ });
1777
+ };
1778
+ SubscriptionManager.prototype.clearResumeToken = function () {
1779
+ return __awaiter(this, void 0, void 0, function () {
1780
+ var db_3, clearToken, mongoManager, error_6;
1781
+ var _this = this;
1782
+ return __generator(this, function (_a) {
1783
+ switch (_a.label) {
1784
+ case 0:
1785
+ _a.trys.push([0, 5, , 6]);
1786
+ db_3 = resolveio_server_app_1.ResolveIOServer.getMainDB();
1787
+ if (!db_3) {
1788
+ return [2 /*return*/];
1789
+ }
1790
+ clearToken = function () { return __awaiter(_this, void 0, void 0, function () {
1791
+ return __generator(this, function (_a) {
1792
+ switch (_a.label) {
1793
+ case 0: return [4 /*yield*/, db_3.collection(this.RESUME_TOKEN_COLLECTION)
1794
+ .deleteOne({ _id: this.getResumeTokenDocId() })];
1795
+ case 1:
1796
+ _a.sent();
1797
+ return [2 /*return*/];
1798
+ }
1799
+ });
1800
+ }); };
1801
+ mongoManager = resolveio_server_app_1.ResolveIOServer.getMongoManager();
1802
+ if (!mongoManager) return [3 /*break*/, 2];
1803
+ return [4 /*yield*/, mongoManager.runWithoutSession(clearToken)];
1804
+ case 1:
1805
+ _a.sent();
1806
+ return [3 /*break*/, 4];
1807
+ case 2: return [4 /*yield*/, clearToken()];
1808
+ case 3:
1809
+ _a.sent();
1810
+ _a.label = 4;
1811
+ case 4: return [3 /*break*/, 6];
1812
+ case 5:
1813
+ error_6 = _a.sent();
1814
+ console.log(new Date(), 'Sub Manager', 'Failed to clear oplog resume token', error_6);
1815
+ return [3 /*break*/, 6];
1816
+ case 6: return [2 /*return*/];
1817
+ }
1818
+ });
1819
+ });
1820
+ };
1821
+ SubscriptionManager.prototype.queueResumeTokenSave = function (token, options) {
1822
+ var _this = this;
1823
+ if (!token) {
1824
+ return;
1825
+ }
1826
+ this._lastResumeToken = token;
1827
+ var now = Date.now();
1828
+ var shouldSave = !!(options === null || options === void 0 ? void 0 : options.force) || (now - this._lastResumeTokenSaveMs) >= this.RESUME_TOKEN_SAVE_INTERVAL_MS;
1829
+ if (!shouldSave) {
1830
+ return;
1831
+ }
1832
+ if (this._resumeTokenSavePromise !== null) {
1833
+ if (options === null || options === void 0 ? void 0 : options.force) {
1834
+ this._resumeTokenSaveForcePending = true;
1835
+ }
1836
+ return;
1837
+ }
1838
+ this._lastResumeTokenSaveMs = now;
1839
+ var tokenToSave = this._lastResumeToken;
1840
+ this._resumeTokenSavePromise = (function () { return __awaiter(_this, void 0, void 0, function () {
1841
+ return __generator(this, function (_a) {
1842
+ switch (_a.label) {
1843
+ case 0:
1844
+ _a.trys.push([0, , 2, 3]);
1845
+ return [4 /*yield*/, this.saveResumeToken(tokenToSave)];
1846
+ case 1:
1847
+ _a.sent();
1848
+ return [3 /*break*/, 3];
1849
+ case 2:
1850
+ this._resumeTokenSavePromise = null;
1851
+ if (this._resumeTokenSaveForcePending) {
1852
+ this._resumeTokenSaveForcePending = false;
1853
+ this.queueResumeTokenSave(this._lastResumeToken, { force: true });
1854
+ }
1855
+ return [7 /*endfinally*/];
1856
+ case 3: return [2 /*return*/];
1857
+ }
1858
+ });
1859
+ }); })();
1860
+ };
1861
+ SubscriptionManager.prototype.queueFullResync = function (reason) {
1862
+ var _this = this;
1863
+ if (this._fullResyncPromise !== null) {
1864
+ return;
1865
+ }
1866
+ this._fullResyncPromise = (function () { return __awaiter(_this, void 0, void 0, function () {
1867
+ return __generator(this, function (_a) {
1868
+ switch (_a.label) {
1869
+ case 0:
1870
+ _a.trys.push([0, , 2, 3]);
1871
+ return [4 /*yield*/, this.fullResyncSubscriptions(reason)];
1872
+ case 1:
1873
+ _a.sent();
1874
+ return [3 /*break*/, 3];
1875
+ case 2:
1876
+ this._fullResyncPromise = null;
1877
+ return [7 /*endfinally*/];
1878
+ case 3: return [2 /*return*/];
1879
+ }
1880
+ });
1881
+ }); })();
1882
+ };
1883
+ SubscriptionManager.prototype.fullResyncSubscriptions = function (reason) {
1884
+ return __awaiter(this, void 0, void 0, function () {
1885
+ var subs, subs_1, subs_1_1, sub, pub, _a, _b, client, ws, _c, e_4_1, e_5_1, error_7;
1886
+ var e_5, _d, e_4, _e;
1887
+ var _f, _g, _h, _j, _k;
1888
+ return __generator(this, function (_l) {
1889
+ switch (_l.label) {
1890
+ case 0:
1891
+ _l.trys.push([0, 20, , 21]);
1892
+ if (this._enableDebug) {
1893
+ console.log(new Date(), 'Sub Manager', 'Full subscription resync', reason, this._subscriptions.length);
1894
+ }
1895
+ try {
1896
+ (_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);
1897
+ }
1898
+ catch (_m) {
1899
+ // ignore cache-clear errors
1900
+ }
1901
+ try {
1902
+ (_j = (_h = this._nodeCache) === null || _h === void 0 ? void 0 : _h.flushAll) === null || _j === void 0 ? void 0 : _j.call(_h);
1903
+ }
1904
+ catch (_o) {
1905
+ // ignore cache-clear errors
1906
+ }
1907
+ subs = this._subscriptions.slice();
1908
+ _l.label = 1;
1909
+ case 1:
1910
+ _l.trys.push([1, 17, 18, 19]);
1911
+ subs_1 = __values(subs), subs_1_1 = subs_1.next();
1912
+ _l.label = 2;
1913
+ case 2:
1914
+ if (!!subs_1_1.done) return [3 /*break*/, 16];
1915
+ sub = subs_1_1.value;
1916
+ if (!((_k = sub === null || sub === void 0 ? void 0 : sub.clients) === null || _k === void 0 ? void 0 : _k.length)) {
1917
+ return [3 /*break*/, 15];
1918
+ }
1919
+ pub = this._publications[sub.publication];
1920
+ if (!pub) {
1921
+ return [3 /*break*/, 15];
1922
+ }
1923
+ if (sub.running) {
1924
+ sub.runAgain = true;
1925
+ return [3 /*break*/, 15];
1926
+ }
1927
+ if (!pub.user_specific) return [3 /*break*/, 13];
1928
+ _l.label = 3;
1929
+ case 3:
1930
+ _l.trys.push([3, 10, 11, 12]);
1931
+ _a = (e_4 = void 0, __values(sub.clients)), _b = _a.next();
1932
+ _l.label = 4;
1933
+ case 4:
1934
+ if (!!_b.done) return [3 /*break*/, 9];
1935
+ client = _b.value;
1936
+ ws = this._websocketManager.getWebSocket(client.id_socket);
1937
+ if (!((ws === null || ws === void 0 ? void 0 : ws.readyState) === WebSocket.OPEN)) return [3 /*break*/, 8];
1938
+ _l.label = 5;
1939
+ case 5:
1940
+ _l.trys.push([5, 7, , 8]);
1941
+ return [4 /*yield*/, this.sendDataToOneWithRetry(ws, client.messageId, sub, '', reason)];
1942
+ case 6:
1943
+ _l.sent();
1944
+ return [3 /*break*/, 8];
1945
+ case 7:
1946
+ _c = _l.sent();
1947
+ return [3 /*break*/, 8];
1948
+ case 8:
1949
+ _b = _a.next();
1950
+ return [3 /*break*/, 4];
1951
+ case 9: return [3 /*break*/, 12];
1952
+ case 10:
1953
+ e_4_1 = _l.sent();
1954
+ e_4 = { error: e_4_1 };
1955
+ return [3 /*break*/, 12];
1956
+ case 11:
1957
+ try {
1958
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
1959
+ }
1960
+ finally { if (e_4) throw e_4.error; }
1961
+ return [7 /*endfinally*/];
1962
+ case 12: return [3 /*break*/, 15];
1963
+ case 13: return [4 /*yield*/, this.sendDataToAllWithRetry(sub, '', reason)];
1964
+ case 14:
1965
+ _l.sent();
1966
+ _l.label = 15;
1967
+ case 15:
1968
+ subs_1_1 = subs_1.next();
1969
+ return [3 /*break*/, 2];
1970
+ case 16: return [3 /*break*/, 19];
1971
+ case 17:
1972
+ e_5_1 = _l.sent();
1973
+ e_5 = { error: e_5_1 };
1974
+ return [3 /*break*/, 19];
1975
+ case 18:
1976
+ try {
1977
+ if (subs_1_1 && !subs_1_1.done && (_d = subs_1.return)) _d.call(subs_1);
1978
+ }
1979
+ finally { if (e_5) throw e_5.error; }
1980
+ return [7 /*endfinally*/];
1981
+ case 19: return [3 /*break*/, 21];
1982
+ case 20:
1983
+ error_7 = _l.sent();
1984
+ console.log(new Date(), 'Sub Manager', 'Full resync failed', reason, error_7);
1985
+ return [3 /*break*/, 21];
1986
+ case 21: return [2 /*return*/];
1987
+ }
1988
+ });
1989
+ });
1990
+ };
1991
+ // Watch (tail) Mongo's operation log on the entire database (all insert/modify/delete will trigger this function)
1992
+ SubscriptionManager.prototype.tailOpLog = function (resumeToken) {
1993
+ return __awaiter(this, void 0, void 0, function () {
1994
+ var watchDatabases, pipeline, lastResumeToken_1, startedWithResumeToken_1, sawChangeEvent_1, resumeTokenInvalidated_1, streamStartedAtMs_1, error_8, innerError_1, error_9;
1995
+ var _this = this;
1996
+ return __generator(this, function (_a) {
1997
+ switch (_a.label) {
1998
+ case 0:
1999
+ if (this._useLocalOplog) {
2000
+ return [2 /*return*/];
2001
+ }
2002
+ if (!(this._oplog$ && !this._oplog$.closed)) return [3 /*break*/, 2];
2003
+ this._oplog$.removeAllListeners();
2004
+ return [4 /*yield*/, this._oplog$.close()];
2005
+ case 1:
2006
+ _a.sent();
2007
+ this._oplog$ = null;
2008
+ _a.label = 2;
2009
+ case 2:
2010
+ // eslint-disable-next-line no-restricted-syntax
2011
+ return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 1000); })];
2012
+ case 3:
2013
+ // eslint-disable-next-line no-restricted-syntax
2014
+ _a.sent();
2015
+ if (!(!this._oplog$ || this._oplog$.closed)) return [3 /*break*/, 27];
2016
+ this._oplogRetryCount += 1;
2017
+ if (this._oplogRetryCount > 5) {
2018
+ console.error('****************** TAIL OPLOG ERROR, RETRYING A BUNCH OF TIMES, KILLING PROCESS **************************');
2019
+ process.exit(1);
2020
+ }
2021
+ watchDatabases = this.getWatchedDatabases();
2022
+ pipeline = [
2023
+ {
2024
+ $match: {
2025
+ $and: __spreadArray(__spreadArray([], __read((watchDatabases.length ? [{ 'ns.db': { $in: watchDatabases } }] : [])), false), [
2026
+ { 'ns.coll': { $nin: [
2027
+ 'log-method-latencies',
2028
+ 'log-subscriptions',
2029
+ 'logs',
2030
+ 'counters',
2031
+ 'cron-job-histories',
2032
+ 'email-histories',
2033
+ 'qb-soap-request-histories',
2034
+ 'qb-soap-request-responses',
2035
+ 'qb-soap-requests',
2036
+ 'qb-soap-retries',
2037
+ this.RESUME_TOKEN_COLLECTION
2038
+ ] } },
2039
+ { 'ns.coll': { $not: /.*\.versions$/ } },
2040
+ { 'ns.coll': { $not: /^monitor-/ } },
2041
+ ], false)
2042
+ },
2043
+ },
2044
+ ];
2045
+ startedWithResumeToken_1 = false;
2046
+ sawChangeEvent_1 = false;
2047
+ resumeTokenInvalidated_1 = false;
2048
+ streamStartedAtMs_1 = Date.now();
2049
+ if (!resumeToken) return [3 /*break*/, 20];
2050
+ lastResumeToken_1 = resumeToken;
2051
+ _a.label = 4;
2052
+ case 4:
2053
+ _a.trys.push([4, 5, , 19]);
2054
+ this._oplog$ = resolveio_server_app_1.ResolveIOServer.getMongoConnection().watch(pipeline, { resumeAfter: resumeToken, fullDocument: 'updateLookup' });
2055
+ startedWithResumeToken_1 = true;
2056
+ return [3 /*break*/, 19];
2057
+ case 5:
2058
+ error_8 = _a.sent();
2059
+ if (!this.isChangeStreamUnsupported(error_8)) return [3 /*break*/, 7];
2060
+ return [4 /*yield*/, this.enableLocalOplogFallback('change-stream-unsupported', error_8)];
2061
+ case 6:
2062
+ _a.sent();
2063
+ return [2 /*return*/];
2064
+ case 7:
2065
+ if (!this.isChangeStreamDocumentTooLarge(error_8)) return [3 /*break*/, 10];
2066
+ return [4 /*yield*/, this.clearResumeToken()];
2067
+ case 8:
2068
+ _a.sent();
2069
+ lastResumeToken_1 = null;
2070
+ this.queueFullResync('oplog-change-stream-document-too-large');
2071
+ return [4 /*yield*/, this.enableLocalOplogFallback('change-stream-document-too-large', error_8)];
2072
+ case 9:
2073
+ _a.sent();
2074
+ return [2 /*return*/];
2075
+ case 10:
2076
+ if (!this._oplog$) return [3 /*break*/, 12];
2077
+ this._oplog$.removeAllListeners();
2078
+ return [4 /*yield*/, this._oplog$.close()];
2079
+ case 11:
2080
+ _a.sent();
2081
+ this._oplog$ = null;
2082
+ _a.label = 12;
2083
+ case 12: return [4 /*yield*/, this.clearResumeToken()];
2084
+ case 13:
2085
+ _a.sent();
2086
+ lastResumeToken_1 = null;
2087
+ console.log(new Date(), 'oplog resumeAfter failed, starting fresh', error_8);
2088
+ _a.label = 14;
2089
+ case 14:
2090
+ _a.trys.push([14, 15, , 18]);
2091
+ this._oplog$ = resolveio_server_app_1.ResolveIOServer.getMongoConnection().watch(pipeline, { fullDocument: 'updateLookup' });
2092
+ startedWithResumeToken_1 = false;
2093
+ return [3 /*break*/, 18];
2094
+ case 15:
2095
+ innerError_1 = _a.sent();
2096
+ if (!this.isChangeStreamUnsupported(innerError_1)) return [3 /*break*/, 17];
2097
+ return [4 /*yield*/, this.enableLocalOplogFallback('change-stream-unsupported', innerError_1)];
2098
+ case 16:
2099
+ _a.sent();
2100
+ return [2 /*return*/];
2101
+ case 17: throw innerError_1;
2102
+ case 18:
2103
+ this.queueFullResync('oplog-resumeAfter-failed');
2104
+ return [3 /*break*/, 19];
2105
+ case 19: return [3 /*break*/, 26];
2106
+ case 20:
2107
+ _a.trys.push([20, 21, , 26]);
2108
+ this._oplog$ = resolveio_server_app_1.ResolveIOServer.getMongoConnection().watch(pipeline, { fullDocument: 'updateLookup' });
2109
+ return [3 /*break*/, 26];
2110
+ case 21:
2111
+ error_9 = _a.sent();
2112
+ if (!this.isChangeStreamUnsupported(error_9)) return [3 /*break*/, 23];
2113
+ return [4 /*yield*/, this.enableLocalOplogFallback('change-stream-unsupported', error_9)];
2114
+ case 22:
2115
+ _a.sent();
2116
+ return [2 /*return*/];
2117
+ case 23:
2118
+ if (!this.isChangeStreamDocumentTooLarge(error_9)) return [3 /*break*/, 25];
2119
+ return [4 /*yield*/, this.enableLocalOplogFallback('change-stream-document-too-large', error_9)];
2120
+ case 24:
2121
+ _a.sent();
2122
+ return [2 /*return*/];
2123
+ case 25: throw error_9;
2124
+ case 26:
2125
+ console.log(new Date(), 'oplog started', startedWithResumeToken_1 ? '(resumeAfter)' : '');
2126
+ this._oplog$.on('change', function (doc) { return __awaiter(_this, void 0, void 0, function () {
2127
+ var collection, fullDocument, docId, flag, dependencyFlag;
2128
+ return __generator(this, function (_a) {
2129
+ switch (_a.label) {
2130
+ case 0:
2131
+ sawChangeEvent_1 = true;
2132
+ this._oplogRetryCount = 0;
2133
+ if (!doc.ns) return [3 /*break*/, 15];
2134
+ if (this._enableDebug) {
2135
+ console.log(new Date(), 'Oplog Hit', doc.ns);
2136
+ }
2137
+ collection = doc.ns.coll;
2138
+ if (!this._debugOplogCollections.some(function (a) { return a.collection === doc.ns.coll && a.type === doc.operationType; })) {
2139
+ this._debugOplogCollections.push({
2140
+ collection: doc.ns.coll,
2141
+ type: doc.operationType,
2142
+ hits: 1
2143
+ });
2144
+ }
2145
+ else {
2146
+ this._debugOplogCollections.find(function (a) { return a.collection === doc.ns.coll && a.type === doc.operationType; }).hits += 1;
2147
+ }
2148
+ if (!collection) return [3 /*break*/, 11];
2149
+ this._debugOplogHits += 1;
2150
+ fullDocument = doc['fullDocument'];
2151
+ docId = doc.documentKey && doc.documentKey['_id'] !== undefined ? doc.documentKey['_id'] : (fullDocument && fullDocument['_id'] !== undefined ? fullDocument['_id'] : null);
2152
+ if (!(doc.operationType === 'insert')) return [3 /*break*/, 6];
2153
+ if (!(collection === 'support-tickets')) return [3 /*break*/, 3];
2154
+ if (!(this.serverConfig['ROOT_URL'] === 'https://resolveio.com')) return [3 /*break*/, 3];
2155
+ return [4 /*yield*/, resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(), 'sendSupportTicketEmail', doc.documentKey['_id'])];
2156
+ case 1:
2157
+ _a.sent();
2158
+ return [4 /*yield*/, this.invalidatePubsCache(collection, 'insert', docId, fullDocument)];
2159
+ case 2:
2160
+ _a.sent();
2161
+ _a.label = 3;
2162
+ case 3:
2163
+ if (!(collection !== 'method-responses')) return [3 /*break*/, 5];
2164
+ return [4 /*yield*/, this.invalidatePubsCache(collection, 'insert', docId, fullDocument)];
2165
+ case 4:
2166
+ _a.sent();
2167
+ _a.label = 5;
2168
+ case 5: return [3 /*break*/, 11];
2169
+ case 6:
2170
+ if (!(doc.operationType === 'update' || doc.operationType === 'replace')) return [3 /*break*/, 9];
2171
+ if (!(collection !== 'method-responses')) return [3 /*break*/, 8];
2172
+ return [4 /*yield*/, this.invalidatePubsCache(collection, 'update', docId, fullDocument)];
2173
+ case 7:
2174
+ _a.sent();
2175
+ _a.label = 8;
2176
+ case 8: return [3 /*break*/, 11];
2177
+ case 9:
2178
+ if (!(doc.operationType === 'delete')) return [3 /*break*/, 11];
2179
+ if (!(collection !== 'method-responses')) return [3 /*break*/, 11];
2180
+ return [4 /*yield*/, this.invalidatePubsCache(collection, 'delete', docId)];
2181
+ case 10:
2182
+ _a.sent();
2183
+ _a.label = 11;
2184
+ case 11:
2185
+ if (!(collection === 'flags')) return [3 /*break*/, 14];
2186
+ return [4 /*yield*/, flag_collection_1.Flags.findOne({ type: 'Enable Debug' })];
2187
+ case 12:
2188
+ flag = _a.sent();
2189
+ return [4 /*yield*/, flag_collection_1.Flags.findOne({ type: 'Enable Dependency Debug' })];
2190
+ case 13:
2191
+ dependencyFlag = _a.sent();
2192
+ if (flag && flag.value) {
2193
+ this._enableDebug = true;
2194
+ }
2195
+ else {
2196
+ this._enableDebug = false;
2197
+ }
2198
+ if (dependencyFlag && dependencyFlag.value) {
2199
+ this._enableDependencyDebug = true;
2200
+ }
2201
+ else {
2202
+ this._enableDependencyDebug = process.env.ENABLE_DEPENDENCY_DEBUG === 'true';
2203
+ }
2204
+ _a.label = 14;
2205
+ case 14:
2206
+ lastResumeToken_1 = doc._id;
2207
+ this.queueResumeTokenSave(lastResumeToken_1);
2208
+ 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'))) {
2209
+ }
2210
+ _a.label = 15;
2211
+ case 15: return [2 /*return*/];
2212
+ }
2213
+ });
2214
+ }); });
2215
+ this._oplog$.on('error', function (error) { return __awaiter(_this, void 0, void 0, function () {
2216
+ return __generator(this, function (_a) {
2217
+ switch (_a.label) {
2218
+ case 0:
2219
+ console.log(new Date(), 'oplog error', error);
2220
+ if (!this.isChangeStreamUnsupported(error)) return [3 /*break*/, 2];
2221
+ return [4 /*yield*/, this.enableLocalOplogFallback('change-stream-unsupported', error)];
2222
+ case 1:
2223
+ _a.sent();
2224
+ return [2 /*return*/];
2225
+ case 2:
2226
+ if (!this.isChangeStreamDocumentTooLarge(error)) return [3 /*break*/, 5];
2227
+ return [4 /*yield*/, this.clearResumeToken()];
2228
+ case 3:
2229
+ _a.sent();
2230
+ lastResumeToken_1 = null;
2231
+ this.queueFullResync('oplog-change-stream-document-too-large');
2232
+ return [4 /*yield*/, this.enableLocalOplogFallback('change-stream-document-too-large', error)];
2233
+ case 4:
2234
+ _a.sent();
2235
+ return [2 /*return*/];
2236
+ case 5:
2237
+ if (!this.isResumeTokenInvalid(error)) return [3 /*break*/, 7];
2238
+ resumeTokenInvalidated_1 = true;
2239
+ return [4 /*yield*/, this.clearResumeToken()];
2240
+ case 6:
2241
+ _a.sent();
2242
+ lastResumeToken_1 = null;
2243
+ this.queueFullResync('oplog-resume-token-invalid');
2244
+ _a.label = 7;
2245
+ case 7: return [4 /*yield*/, this._oplog$.close()];
2246
+ case 8:
2247
+ _a.sent();
2248
+ return [2 /*return*/];
2249
+ }
2250
+ });
2251
+ }); });
2252
+ this._oplog$.on('end', function () { return __awaiter(_this, void 0, void 0, function () {
2253
+ return __generator(this, function (_a) {
2254
+ switch (_a.label) {
2255
+ case 0:
2256
+ console.log(new Date(), 'oplog end');
2257
+ return [4 /*yield*/, this._oplog$.close()];
2258
+ case 1:
2259
+ _a.sent();
2260
+ return [2 /*return*/];
2261
+ }
2262
+ });
2263
+ }); });
2264
+ this._oplog$.on('close', function () { return __awaiter(_this, void 0, void 0, function () {
2265
+ var retryThresholdReached, shouldAutoHealResumeToken;
2266
+ return __generator(this, function (_a) {
2267
+ switch (_a.label) {
2268
+ case 0:
2269
+ console.log(new Date(), 'oplog close');
2270
+ retryThresholdReached = this._oplogRetryCount >= this.RESUME_TOKEN_AUTO_HEAL_RETRY_THRESHOLD;
2271
+ shouldAutoHealResumeToken = startedWithResumeToken_1
2272
+ && !!lastResumeToken_1
2273
+ && !sawChangeEvent_1
2274
+ && (resumeTokenInvalidated_1 || retryThresholdReached);
2275
+ if (!shouldAutoHealResumeToken) return [3 /*break*/, 2];
2276
+ return [4 /*yield*/, this.clearResumeToken()];
2277
+ case 1:
2278
+ _a.sent();
2279
+ lastResumeToken_1 = null;
2280
+ if (!resumeTokenInvalidated_1) {
2281
+ this.queueFullResync('oplog-resume-token-auto-heal');
2282
+ }
2283
+ console.log(new Date(), 'Sub Manager', 'Auto-healed stale oplog resume token', { retryCount: this._oplogRetryCount, streamLifetimeMs: Date.now() - streamStartedAtMs_1 });
2284
+ _a.label = 2;
2285
+ case 2:
2286
+ if (lastResumeToken_1) {
2287
+ this.queueResumeTokenSave(lastResumeToken_1, { force: true });
2288
+ }
2289
+ this._oplog$.removeAllListeners();
2290
+ this._oplog$ = null;
2291
+ return [4 /*yield*/, this.tailOpLog(lastResumeToken_1)];
2292
+ case 3:
2293
+ _a.sent();
2294
+ return [2 /*return*/];
2295
+ }
2296
+ });
2297
+ }); });
2298
+ _a.label = 27;
2299
+ case 27: return [2 /*return*/];
2300
+ }
2301
+ });
2302
+ });
2303
+ };
2304
+ SubscriptionManager.prototype.packCachePayload = function (data) {
2305
+ if (data === undefined) {
2306
+ return null;
2307
+ }
2308
+ try {
2309
+ var packed = (0, msgpackr_1.pack)(data);
2310
+ return Buffer.isBuffer(packed) ? packed : Buffer.from(packed);
2311
+ }
2312
+ catch (err) {
2313
+ if (this._enableDebug) {
2314
+ console.log(new Date(), 'Sub Cache', 'Pack Failed', (err === null || err === void 0 ? void 0 : err.message) || err);
2315
+ }
2316
+ return null;
2317
+ }
2318
+ };
2319
+ SubscriptionManager.prototype.decodeCachePayload = function (raw) {
2320
+ if (!raw) {
2321
+ return null;
2322
+ }
2323
+ if (Buffer.isBuffer(raw)) {
2324
+ try {
2325
+ return (0, msgpackr_1.unpack)(raw);
2326
+ }
2327
+ catch (err) {
2328
+ if (this._enableDebug) {
2329
+ console.log(new Date(), 'Sub Cache', 'Unpack Failed', (err === null || err === void 0 ? void 0 : err.message) || err);
2330
+ }
2331
+ return null;
2332
+ }
2333
+ }
2334
+ if (raw instanceof Uint8Array) {
2335
+ try {
2336
+ return (0, msgpackr_1.unpack)(raw);
2337
+ }
2338
+ catch (err) {
2339
+ if (this._enableDebug) {
2340
+ console.log(new Date(), 'Sub Cache', 'Unpack Failed', (err === null || err === void 0 ? void 0 : err.message) || err);
2341
+ }
2342
+ return null;
2343
+ }
2344
+ }
2345
+ if (raw instanceof ArrayBuffer) {
2346
+ try {
2347
+ return (0, msgpackr_1.unpack)(new Uint8Array(raw));
2348
+ }
2349
+ catch (err) {
2350
+ if (this._enableDebug) {
2351
+ console.log(new Date(), 'Sub Cache', 'Unpack Failed', (err === null || err === void 0 ? void 0 : err.message) || err);
2352
+ }
2353
+ return null;
2354
+ }
2355
+ }
2356
+ // Treat legacy string caches as invalid to avoid JSON parsing in the hot path.
2357
+ if (typeof raw === 'string') {
2358
+ return null;
2359
+ }
2360
+ return raw;
2361
+ };
2362
+ SubscriptionManager.prototype.getCacheBuffer = function (raw) {
2363
+ if (!raw) {
2364
+ return null;
2365
+ }
2366
+ if (Buffer.isBuffer(raw)) {
2367
+ return raw;
2368
+ }
2369
+ if (raw instanceof Uint8Array) {
2370
+ return Buffer.from(raw);
2371
+ }
2372
+ if (raw instanceof ArrayBuffer) {
2373
+ return Buffer.from(new Uint8Array(raw));
2374
+ }
2375
+ return null;
2376
+ };
2377
+ SubscriptionManager.prototype.shouldCachePayload = function (sub, payload) {
2378
+ if (!payload || payload.byteLength >= 1000000) {
2379
+ return false;
2380
+ }
2381
+ if (sub.collections.includes('logs')) {
2382
+ return false;
2383
+ }
2384
+ if (sub.collections.find(function (a) { return a.endsWith('.versions'); })) {
2385
+ return false;
2386
+ }
2387
+ if (sub.collections.find(function (a) { return a.startsWith('monitor-'); })) {
2388
+ return false;
2389
+ }
2390
+ return true;
2391
+ };
2392
+ SubscriptionManager.prototype.buffersEqual = function (a, b) {
2393
+ if (!a || !b) {
2394
+ return false;
2395
+ }
2396
+ if (a.byteLength !== b.byteLength) {
2397
+ return false;
2398
+ }
2399
+ var toBuffer = function (value) {
2400
+ if (Buffer.isBuffer(value)) {
2401
+ return value;
2402
+ }
2403
+ var buffer = value.buffer;
2404
+ return Buffer.from(buffer, value.byteOffset, value.byteLength);
2405
+ };
2406
+ var aBuf = toBuffer(a);
2407
+ var bBuf = toBuffer(b);
2408
+ return aBuf.equals(bBuf);
2409
+ };
2410
+ SubscriptionManager.prototype.processSubscription = function (sub, ws, messageId) {
2411
+ return __awaiter(this, void 0, void 0, function () {
2412
+ var cachedRaw, cachedBuffer, cacheData, serverRes, _a;
2413
+ return __generator(this, function (_b) {
2414
+ switch (_b.label) {
2415
+ case 0:
2416
+ if (!!this._publications[sub.publication].user_specific) return [3 /*break*/, 8];
2417
+ if (!sub.cacheId) return [3 /*break*/, 5];
2418
+ _b.label = 1;
2419
+ case 1:
2420
+ _b.trys.push([1, 2, , 4]);
2421
+ cachedRaw = this._nodeCache.get(sub.cacheId);
2422
+ cachedBuffer = this.getCacheBuffer(cachedRaw);
2423
+ if (cachedBuffer && cachedBuffer.byteLength) {
2424
+ if (this._enableDebug) {
2425
+ console.log(new Date(), 'Process Sub, Cache (packed)', sub.publication);
2426
+ }
2427
+ this._websocketManager.sendPackedBuffer(ws, messageId, false, cachedBuffer, 'msgpack');
2428
+ this.logAiSubSend({
2429
+ publication: sub.publication,
2430
+ subscriptionKey: sub.subscriptionKey,
2431
+ messageId: messageId,
2432
+ id_socket: ws['id_socket'],
2433
+ clients: 1,
2434
+ payloadBytes: cachedBuffer.byteLength,
2435
+ packed: true,
2436
+ cacheHit: true,
2437
+ collection: '',
2438
+ type: 'cache',
2439
+ subscriptionData: sub.subscriptionData
2440
+ });
2441
+ }
2442
+ else {
2443
+ cacheData = this.decodeCachePayload(cachedRaw);
2444
+ if (cacheData === null || cacheData === undefined) {
2445
+ throw new Error('cache-miss');
2446
+ }
2447
+ serverRes = {
2448
+ messageId: messageId,
2449
+ hasError: false,
2450
+ data: cacheData
2451
+ };
2452
+ if (this._enableDebug) {
2453
+ console.log(new Date(), 'Process Sub, Cache', sub.publication);
2454
+ }
2455
+ this.sendWS(ws, serverRes);
2456
+ this.logAiSubSend({
2457
+ publication: sub.publication,
2458
+ subscriptionKey: sub.subscriptionKey,
2459
+ messageId: messageId,
2460
+ id_socket: ws['id_socket'],
2461
+ clients: 1,
2462
+ payloadBytes: cacheData ? Buffer.byteLength(JSON.stringify(cacheData)) : null,
2463
+ packed: false,
2464
+ cacheHit: true,
2465
+ collection: '',
2466
+ type: 'cache',
2467
+ subscriptionData: sub.subscriptionData
2468
+ });
2469
+ }
2470
+ return [3 /*break*/, 4];
2471
+ case 2:
2472
+ _a = _b.sent();
2473
+ this._nodeCache.del(sub.cacheId);
2474
+ sub.cacheId = 0;
2475
+ return [4 /*yield*/, this.sendDataToAllWithRetry(sub, '', 'newSub')];
2476
+ case 3:
2477
+ _b.sent();
2478
+ return [3 /*break*/, 4];
2479
+ case 4: return [3 /*break*/, 7];
2480
+ case 5:
2481
+ if (this._enableDebug) {
2482
+ console.log(new Date(), 'Process Sub, Non - Cache', sub.publication, sub.running);
2483
+ }
2484
+ if (sub.running) {
2485
+ return [2 /*return*/];
2486
+ }
2487
+ return [4 /*yield*/, this.sendDataToAllWithRetry(sub, '', 'newSub')];
2488
+ case 6:
2489
+ _b.sent();
2490
+ _b.label = 7;
2491
+ case 7: return [3 /*break*/, 10];
2492
+ case 8:
2493
+ if (this._enableDebug) {
2494
+ console.log(new Date(), 'Process Sub Specific, Non - Cache', sub.publication, sub.running);
2495
+ }
2496
+ if (sub.running) {
2497
+ return [2 /*return*/];
2498
+ }
2499
+ return [4 /*yield*/, this.sendDataToOneWithRetry(ws, messageId, sub, '', 'newSub')];
2500
+ case 9:
2501
+ _b.sent();
2502
+ _b.label = 10;
2503
+ case 10: return [2 /*return*/];
2504
+ }
2505
+ });
2506
+ });
2507
+ };
2508
+ SubscriptionManager.prototype.sendDataToOne = function (ws, messageId, sub, collection, type) {
2509
+ return __awaiter(this, void 0, void 0, function () {
2510
+ var monitor, dependencySnapshot, res, execution, packedRes, payloadBytes, serverRes, err_1, _a, normalizedError, correlationId, serverRes, errorPayload;
2511
+ var _b, _c;
2512
+ return __generator(this, function (_d) {
2513
+ switch (_d.label) {
2514
+ case 0:
2515
+ if (!ws || ws.readyState !== WebSocket.OPEN) {
2516
+ return [2 /*return*/];
2517
+ }
2518
+ monitor = this._monitorManagerFunction.startMonitorFunction('User Specific Publication', sub.publication, '', '', sub.subscriptionData);
2519
+ _d.label = 1;
2520
+ case 1:
2521
+ _d.trys.push([1, 4, 6, 8]);
2522
+ return [4 /*yield*/, 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))];
2523
+ case 2:
2524
+ _d.sent();
2525
+ return [4 /*yield*/, this.runPublicationExecution(sub, ws['id_user'])];
2526
+ case 3:
2527
+ execution = _d.sent();
2528
+ res = execution.result;
2529
+ dependencySnapshot = execution.snapshot;
2530
+ this.updateSubscriptionDependencies(sub, dependencySnapshot);
2531
+ packedRes = (_b = execution.packedResult) !== null && _b !== void 0 ? _b : this.packCachePayload(res);
2532
+ payloadBytes = packedRes ? packedRes.byteLength : null;
2533
+ if (execution.packedResult) {
2534
+ this._websocketManager.sendPackedBuffer(ws, messageId, false, execution.packedResult, execution.encoding || 'msgpack', { passThrough: !!execution.workerUsed });
2535
+ }
2536
+ else {
2537
+ serverRes = {
2538
+ messageId: messageId,
2539
+ hasError: false,
2540
+ data: res
2541
+ };
2542
+ this.sendWS(ws, serverRes);
2543
+ }
2544
+ this.logAiSubSend({
2545
+ publication: sub.publication,
2546
+ subscriptionKey: sub.subscriptionKey,
2547
+ messageId: messageId,
2548
+ id_socket: ws['id_socket'],
2549
+ clients: 1,
2550
+ payloadBytes: payloadBytes,
2551
+ packed: !!execution.packedResult,
2552
+ cacheHit: false,
2553
+ collection: collection,
2554
+ type: type,
2555
+ subscriptionData: sub.subscriptionData
2556
+ });
2557
+ return [3 /*break*/, 8];
2558
+ case 4:
2559
+ err_1 = _d.sent();
2560
+ _a = (0, error_tracking_1.ensureErrorWithCorrelation)(err_1), normalizedError = _a.error, correlationId = _a.correlationId;
2561
+ serverRes = {
2562
+ messageId: messageId,
2563
+ hasError: true,
2564
+ data: Object.assign({}, normalizedError, { correlationId: correlationId })
2565
+ };
2566
+ this.sendWS(ws, serverRes);
2567
+ errorPayload = {
2568
+ publication: sub.publication,
2569
+ subscriptionData: sub.subscriptionData,
2570
+ type: type,
2571
+ collection: collection,
2572
+ messageId: messageId,
2573
+ correlationId: correlationId,
2574
+ error: {
2575
+ name: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.name,
2576
+ message: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.message,
2577
+ stack: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.stack,
2578
+ code: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.code,
2579
+ codeName: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.codeName
2580
+ }
2581
+ };
2582
+ return [4 /*yield*/, error_reporter_1.ErrorReporter.report({
2583
+ sourceApp: 'subscription-manager',
2584
+ message: 'SERVER - Error Detected - ' + this.serverConfig['CLIENT_NAME'],
2585
+ environment: (_c = this.serverConfig) === null || _c === void 0 ? void 0 : _c.ROOT_URL,
2586
+ clientSlug: resolveio_server_app_1.ResolveIOServer.getClientName(),
2587
+ clientName: this.serverConfig['CLIENT_NAME'],
2588
+ stack: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.stack,
2589
+ context: errorPayload,
2590
+ metadata: {
2591
+ context: 'subscription-sendDataToOne',
2592
+ publication: sub.publication,
2593
+ correlationId: correlationId
2594
+ },
2595
+ correlationId: correlationId
2596
+ })];
2597
+ case 5:
2598
+ _d.sent();
2599
+ return [3 /*break*/, 8];
2600
+ case 6: return [4 /*yield*/, this._monitorManagerFunction.finishMonitorFunction(monitor)];
2601
+ case 7:
2602
+ _d.sent();
2603
+ return [7 /*endfinally*/];
2604
+ case 8: return [2 /*return*/];
2605
+ }
2606
+ });
2607
+ });
2608
+ };
2609
+ // Fetch pub once, send to all clients linked to this pub
2610
+ SubscriptionManager.prototype.sendDataToAll = function (sub, collection, type) {
2611
+ return __awaiter(this, void 0, void 0, function () {
2612
+ 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;
2613
+ var e_6, _f, e_7, _g, e_8, _h, e_9, _j;
2614
+ var _k, _l;
2615
+ return __generator(this, function (_m) {
2616
+ switch (_m.label) {
2617
+ case 0:
2618
+ isAiPublication = this.isAiPublication(sub.publication);
2619
+ if (isAiPublication && sub.clients.length > 1) {
2620
+ seenSockets_1 = new Set();
2621
+ sub.clients = sub.clients.filter(function (client) {
2622
+ if (seenSockets_1.has(client.id_socket)) {
2623
+ return false;
2624
+ }
2625
+ seenSockets_1.add(client.id_socket);
2626
+ return true;
2627
+ });
2628
+ }
2629
+ activeClients = [];
2630
+ if (sub.clients.length) {
2631
+ try {
2632
+ for (_a = __values(sub.clients), _b = _a.next(); !_b.done; _b = _a.next()) {
2633
+ client = _b.value;
2634
+ ws = this._websocketManager.getWebSocket(client.id_socket);
2635
+ if (ws && ws.readyState === WebSocket.OPEN) {
2636
+ activeClients.push({ client: client, ws: ws });
2637
+ }
2638
+ }
2639
+ }
2640
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
2641
+ finally {
2642
+ try {
2643
+ if (_b && !_b.done && (_f = _a.return)) _f.call(_a);
2644
+ }
2645
+ finally { if (e_6) throw e_6.error; }
2646
+ }
2647
+ }
2648
+ if (!!activeClients.length) return [3 /*break*/, 1];
2649
+ if (sub.cacheId) {
2650
+ this._nodeCache.del(sub.cacheId);
2651
+ sub.cacheId = 0;
2652
+ }
2653
+ subIndex = this._subscriptions.findIndex(function (a) { return a.publication === sub.publication && a.subscriptionKey === sub.subscriptionKey; });
2654
+ if (subIndex >= 0) {
2655
+ this._subscriptions.splice(subIndex, 1);
2656
+ }
2657
+ return [2 /*return*/];
2658
+ case 1:
2659
+ if (activeClients.length !== sub.clients.length) {
2660
+ sub.clients = activeClients.map(function (entry) { return entry.client; });
2661
+ }
2662
+ monitor = this._monitorManagerFunction.startMonitorFunction('Publication', sub.publication, '', '', sub.subscriptionData);
2663
+ res = void 0;
2664
+ dependencySnapshot = void 0;
2665
+ _m.label = 2;
2666
+ case 2:
2667
+ _m.trys.push([2, 6, 8, 10]);
2668
+ if (!(sub.publication !== 'superadminAPM' && sub.publication !== 'loggedInUsers')) return [3 /*break*/, 4];
2669
+ return [4 /*yield*/, 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))];
2670
+ case 3:
2671
+ _m.sent();
2672
+ _m.label = 4;
2673
+ case 4: return [4 /*yield*/, this.runPublicationExecution(sub)];
2674
+ case 5:
2675
+ execution = _m.sent();
2676
+ res = execution.result;
2677
+ dependencySnapshot = execution.snapshot;
2678
+ this.updateSubscriptionDependencies(sub, dependencySnapshot);
2679
+ packedRes = (_k = execution.packedResult) !== null && _k !== void 0 ? _k : this.packCachePayload(res);
2680
+ passThrough = !!execution.workerUsed;
2681
+ payloadBytes = packedRes ? packedRes.byteLength : null;
2682
+ shouldCache = this.shouldCachePayload(sub, packedRes);
2683
+ if (sub.cacheId) {
2684
+ cachedBuffer = this.getCacheBuffer(this._nodeCache.get(sub.cacheId));
2685
+ isSame = this.buffersEqual(cachedBuffer, packedRes);
2686
+ if (!isSame) {
2687
+ sendStartMs = Date.now();
2688
+ sentClients = 0;
2689
+ try {
2690
+ for (activeClients_1 = __values(activeClients), activeClients_1_1 = activeClients_1.next(); !activeClients_1_1.done; activeClients_1_1 = activeClients_1.next()) {
2691
+ entry = activeClients_1_1.value;
2692
+ client = entry.client, ws = entry.ws;
2693
+ if (packedRes) {
2694
+ this._websocketManager.sendPackedBuffer(ws, client.messageId, false, packedRes, execution.encoding || 'msgpack', { passThrough: passThrough });
2695
+ }
2696
+ else {
2697
+ serverRes = {
2698
+ messageId: client.messageId,
2699
+ hasError: false,
2700
+ data: res
2701
+ };
2702
+ this.sendWS(ws, serverRes);
2703
+ }
2704
+ if (isAiPublication) {
2705
+ this.logAiSubSend({
2706
+ publication: sub.publication,
2707
+ subscriptionKey: sub.subscriptionKey,
2708
+ messageId: client.messageId,
2709
+ id_socket: ws['id_socket'],
2710
+ clients: activeClients.length,
2711
+ payloadBytes: payloadBytes,
2712
+ packed: !!packedRes,
2713
+ cacheHit: false,
2714
+ collection: collection,
2715
+ type: type,
2716
+ subscriptionData: sub.subscriptionData
2717
+ });
2718
+ }
2719
+ sentClients += 1;
2720
+ }
2721
+ }
2722
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
2723
+ finally {
2724
+ try {
2725
+ if (activeClients_1_1 && !activeClients_1_1.done && (_g = activeClients_1.return)) _g.call(activeClients_1);
2726
+ }
2727
+ finally { if (e_7) throw e_7.error; }
2728
+ }
2729
+ this.logSubSend(sub, {
2730
+ collection: collection,
2731
+ type: type,
2732
+ clients: sentClients,
2733
+ durationMs: Date.now() - sendStartMs,
2734
+ payloadBytes: payloadBytes
2735
+ });
2736
+ this._nodeCache.del(sub.cacheId);
2737
+ if (shouldCache) {
2738
+ this._nodeCache.set(sub.cacheId, packedRes);
2739
+ }
2740
+ else {
2741
+ sub.cacheId = 0;
2742
+ }
2743
+ }
2744
+ else if (!cachedBuffer && shouldCache) {
2745
+ this._nodeCache.set(sub.cacheId, packedRes);
2746
+ }
2747
+ else if (!shouldCache) {
2748
+ this._nodeCache.del(sub.cacheId);
2749
+ sub.cacheId = 0;
2750
+ }
2751
+ }
2752
+ else {
2753
+ sendStartMs = Date.now();
2754
+ sentClients = 0;
2755
+ try {
2756
+ for (activeClients_2 = __values(activeClients), activeClients_2_1 = activeClients_2.next(); !activeClients_2_1.done; activeClients_2_1 = activeClients_2.next()) {
2757
+ entry = activeClients_2_1.value;
2758
+ client = entry.client, ws = entry.ws;
2759
+ if (packedRes) {
2760
+ this._websocketManager.sendPackedBuffer(ws, client.messageId, false, packedRes, execution.encoding || 'msgpack', { passThrough: passThrough });
2761
+ }
2762
+ else {
2763
+ serverRes = {
2764
+ messageId: client.messageId,
2765
+ hasError: false,
2766
+ data: res
2767
+ };
2768
+ this.sendWS(ws, serverRes);
2769
+ }
2770
+ if (isAiPublication) {
2771
+ this.logAiSubSend({
2772
+ publication: sub.publication,
2773
+ subscriptionKey: sub.subscriptionKey,
2774
+ messageId: client.messageId,
2775
+ id_socket: ws['id_socket'],
2776
+ clients: activeClients.length,
2777
+ payloadBytes: payloadBytes,
2778
+ packed: !!packedRes,
2779
+ cacheHit: false,
2780
+ collection: collection,
2781
+ type: type,
2782
+ subscriptionData: sub.subscriptionData
2783
+ });
2784
+ }
2785
+ sentClients += 1;
2786
+ }
2787
+ }
2788
+ catch (e_8_1) { e_8 = { error: e_8_1 }; }
2789
+ finally {
2790
+ try {
2791
+ if (activeClients_2_1 && !activeClients_2_1.done && (_h = activeClients_2.return)) _h.call(activeClients_2);
2792
+ }
2793
+ finally { if (e_8) throw e_8.error; }
2794
+ }
2795
+ this.logSubSend(sub, {
2796
+ collection: collection,
2797
+ type: type,
2798
+ clients: sentClients,
2799
+ durationMs: Date.now() - sendStartMs,
2800
+ payloadBytes: payloadBytes
2801
+ });
2802
+ if (shouldCache) {
2803
+ sub.cacheId = this._cacheId++;
2804
+ this._nodeCache.set(sub.cacheId, packedRes);
2805
+ nodeCacheSize = this._nodeCache.getStats().vsize;
2806
+ if (nodeCacheSize > this._heapLimit) {
2807
+ deleteCount = 0;
2808
+ subArr = this._subscriptions.filter(function (a) { return a.cacheId && !a.clients.length; });
2809
+ for (zz = 0; zz < subArr.length; zz++) {
2810
+ this._debugRemoveCacheHits += 1;
2811
+ this._nodeCache.del(subArr[zz].cacheId);
2812
+ subArr[zz].cacheId = 0;
2813
+ deleteCount += 1;
2814
+ if (this._nodeCache.getStats().vsize < this._heapLimit * 0.75) {
2815
+ break;
2816
+ }
2817
+ }
2818
+ if (this._enableDebug) {
2819
+ console.log('Sub Cache: ' +
2820
+ 'Too Big - ' +
2821
+ sub.publication +
2822
+ ' - Deleted: ' +
2823
+ deleteCount +
2824
+ ' - ' +
2825
+ nodeCacheSize);
2826
+ }
2827
+ }
2828
+ }
2829
+ else {
2830
+ sub.cacheId = 0;
2831
+ }
2832
+ }
2833
+ return [3 /*break*/, 10];
2834
+ case 6:
2835
+ err_2 = _m.sent();
2836
+ _c = (0, error_tracking_1.ensureErrorWithCorrelation)(err_2), normalizedError = _c.error, correlationId = _c.correlationId;
2837
+ try {
2838
+ for (_d = __values(sub.clients), _e = _d.next(); !_e.done; _e = _d.next()) {
2839
+ client = _e.value;
2840
+ ws = this._websocketManager.getWebSocket(client.id_socket);
2841
+ if (ws && ws.readyState === WebSocket.OPEN) {
2842
+ serverRes = {
2843
+ messageId: client.messageId,
2844
+ hasError: true,
2845
+ data: Object.assign({}, normalizedError, { correlationId: correlationId })
2846
+ };
2847
+ this.sendWS(ws, serverRes);
2848
+ }
2849
+ }
2850
+ }
2851
+ catch (e_9_1) { e_9 = { error: e_9_1 }; }
2852
+ finally {
2853
+ try {
2854
+ if (_e && !_e.done && (_j = _d.return)) _j.call(_d);
2855
+ }
2856
+ finally { if (e_9) throw e_9.error; }
2857
+ }
2858
+ errorPayload = {
2859
+ publication: sub.publication,
2860
+ subscriptionData: sub.subscriptionData,
2861
+ type: type,
2862
+ collection: collection,
2863
+ correlationId: correlationId,
2864
+ error: {
2865
+ name: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.name,
2866
+ message: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.message,
2867
+ stack: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.stack,
2868
+ code: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.code,
2869
+ codeName: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.codeName
2870
+ }
2871
+ };
2872
+ return [4 /*yield*/, error_reporter_1.ErrorReporter.report({
2873
+ sourceApp: 'subscription-manager',
2874
+ message: 'SERVER - Error Detected - ' + this.serverConfig['CLIENT_NAME'],
2875
+ environment: (_l = this.serverConfig) === null || _l === void 0 ? void 0 : _l.ROOT_URL,
2876
+ clientSlug: resolveio_server_app_1.ResolveIOServer.getClientName(),
2877
+ clientName: this.serverConfig['CLIENT_NAME'],
2878
+ stack: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.stack,
2879
+ context: errorPayload,
2880
+ metadata: {
2881
+ context: 'subscription-sendDataToAll',
2882
+ publication: sub.publication,
2883
+ correlationId: correlationId
2884
+ },
2885
+ correlationId: correlationId
2886
+ })];
2887
+ case 7:
2888
+ _m.sent();
2889
+ return [3 /*break*/, 10];
2890
+ case 8: return [4 /*yield*/, this._monitorManagerFunction.finishMonitorFunction(monitor)];
2891
+ case 9:
2892
+ _m.sent();
2893
+ return [7 /*endfinally*/];
2894
+ case 10: return [2 /*return*/];
2895
+ }
2896
+ });
2897
+ });
2898
+ };
2899
+ SubscriptionManager.prototype.ensureDependencyContainers = function (sub) {
2900
+ if (!sub.collectionDependencies) {
2901
+ sub.collectionDependencies = new Map();
2902
+ }
2903
+ if (!sub.collectionFilters) {
2904
+ sub.collectionFilters = new Map();
2905
+ }
2906
+ if (!sub.watchAllCollections) {
2907
+ sub.watchAllCollections = new Set();
2908
+ }
2909
+ if (!sub.collectionQueryMeta) {
2910
+ sub.collectionQueryMeta = new Map();
2911
+ }
2912
+ };
2913
+ SubscriptionManager.prototype.updateSubscriptionDependencies = function (sub, snapshot) {
2914
+ this.ensureDependencyContainers(sub);
2915
+ if (!snapshot) {
2916
+ sub.collectionDependencies.clear();
2917
+ sub.collectionFilters.clear();
2918
+ sub.watchAllCollections.clear();
2919
+ sub.collectionQueryMeta.clear();
2920
+ this.dependencyDebug('Cleared dependency snapshot', { publication: sub.publication });
2921
+ return;
2922
+ }
2923
+ sub.collectionDependencies = snapshot.dependencies;
2924
+ sub.collectionFilters = snapshot.filters;
2925
+ sub.watchAllCollections = snapshot.watchAllCollections;
2926
+ sub.collectionQueryMeta = snapshot.queryMetadata;
2927
+ this.logDependencySnapshot(sub, 'Snapshot updated');
2928
+ };
2929
+ SubscriptionManager.prototype.normalizeDocumentId = function (rawId) {
2930
+ if (rawId === null || rawId === undefined) {
2931
+ return null;
2932
+ }
2933
+ if (typeof rawId === 'string') {
2934
+ return rawId;
2935
+ }
2936
+ if (typeof rawId === 'object' && typeof rawId.toHexString === 'function') {
2937
+ return rawId.toHexString();
2938
+ }
2939
+ if (typeof (rawId === null || rawId === void 0 ? void 0 : rawId.toString) === 'function') {
2940
+ var str = rawId.toString();
2941
+ if (str && str !== '[object Object]') {
2942
+ return str;
2943
+ }
2944
+ }
2945
+ return null;
2946
+ };
2947
+ SubscriptionManager.prototype.getDocumentIdQueryCandidates = function (documentId) {
2948
+ var candidates = [];
2949
+ if (documentId === undefined || documentId === null) {
2950
+ return [documentId];
2951
+ }
2952
+ var addCandidate = function (value) {
2953
+ if (value === undefined || value === null) {
2954
+ return;
2955
+ }
2956
+ if (!candidates.includes(value)) {
2957
+ candidates.push(value);
2958
+ }
2959
+ };
2960
+ addCandidate(documentId);
2961
+ if (typeof documentId === 'object') {
2962
+ if (typeof documentId.toHexString === 'function') {
2963
+ addCandidate(documentId.toHexString());
2964
+ }
2965
+ if (typeof documentId.toString === 'function') {
2966
+ var str = documentId.toString();
2967
+ if (str && str !== '[object Object]') {
2968
+ addCandidate(str);
2969
+ }
2970
+ }
2971
+ }
2972
+ else if (typeof documentId !== 'string') {
2973
+ addCandidate(String(documentId));
2974
+ }
2975
+ return candidates;
2976
+ };
2977
+ SubscriptionManager.prototype.isFilterSafeForInMemory = function (filter) {
2978
+ if (!filter || typeof filter !== 'object') {
2979
+ return false;
2980
+ }
2981
+ var allowedOperators = new Set([
2982
+ '$and',
2983
+ '$or',
2984
+ '$nor',
2985
+ '$in',
2986
+ '$nin',
2987
+ '$eq',
2988
+ '$ne',
2989
+ '$gt',
2990
+ '$gte',
2991
+ '$lt',
2992
+ '$lte',
2993
+ '$exists',
2994
+ '$size',
2995
+ '$all',
2996
+ '$elemMatch',
2997
+ '$regex',
2998
+ '$options'
2999
+ ]);
3000
+ var disallowedOperators = new Set([
3001
+ '$where',
3002
+ '$expr',
3003
+ '$text',
3004
+ '$geoWithin',
3005
+ '$geoIntersects',
3006
+ '$near',
3007
+ '$nearSphere',
3008
+ '$jsonSchema',
3009
+ '$mod',
3010
+ '$type',
3011
+ '$bitsAllSet',
3012
+ '$bitsAllClear',
3013
+ '$bitsAnySet',
3014
+ '$bitsAnyClear'
3015
+ ]);
3016
+ var walk = function (value) {
3017
+ var e_10, _a;
3018
+ if (Array.isArray(value)) {
3019
+ return value.every(function (item) { return walk(item); });
3020
+ }
3021
+ if (!value || typeof value !== 'object') {
3022
+ return true;
3023
+ }
3024
+ try {
3025
+ for (var _b = __values(Object.entries(value)), _c = _b.next(); !_c.done; _c = _b.next()) {
3026
+ var _d = __read(_c.value, 2), key = _d[0], entry = _d[1];
3027
+ if (key.startsWith('$')) {
3028
+ if (disallowedOperators.has(key)) {
3029
+ return false;
3030
+ }
3031
+ if (!allowedOperators.has(key)) {
3032
+ return false;
3033
+ }
3034
+ }
3035
+ if (!walk(entry)) {
3036
+ return false;
3037
+ }
3038
+ }
3039
+ }
3040
+ catch (e_10_1) { e_10 = { error: e_10_1 }; }
3041
+ finally {
3042
+ try {
3043
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
3044
+ }
3045
+ finally { if (e_10) throw e_10.error; }
3046
+ }
3047
+ return true;
3048
+ };
3049
+ return walk(filter);
3050
+ };
3051
+ SubscriptionManager.prototype.documentIdMatchesEvent = function (documentId, document) {
3052
+ var e_11, _a;
3053
+ var _this = this;
3054
+ if (!document || documentId === undefined || documentId === null) {
3055
+ return true;
3056
+ }
3057
+ var eventCandidates = this.getDocumentIdQueryCandidates(documentId)
3058
+ .map(function (id) { return _this.normalizeDocumentId(id); })
3059
+ .filter(Boolean);
3060
+ var docCandidates = this.getDocumentIdQueryCandidates(document === null || document === void 0 ? void 0 : document._id)
3061
+ .map(function (id) { return _this.normalizeDocumentId(id); })
3062
+ .filter(Boolean);
3063
+ if (!eventCandidates.length || !docCandidates.length) {
3064
+ return true;
3065
+ }
3066
+ var docSet = new Set(docCandidates);
3067
+ try {
3068
+ for (var eventCandidates_1 = __values(eventCandidates), eventCandidates_1_1 = eventCandidates_1.next(); !eventCandidates_1_1.done; eventCandidates_1_1 = eventCandidates_1.next()) {
3069
+ var candidate = eventCandidates_1_1.value;
3070
+ if (docSet.has(candidate)) {
3071
+ return true;
3072
+ }
3073
+ }
3074
+ }
3075
+ catch (e_11_1) { e_11 = { error: e_11_1 }; }
3076
+ finally {
3077
+ try {
3078
+ if (eventCandidates_1_1 && !eventCandidates_1_1.done && (_a = eventCandidates_1.return)) _a.call(eventCandidates_1);
3079
+ }
3080
+ finally { if (e_11) throw e_11.error; }
3081
+ }
3082
+ return false;
3083
+ };
3084
+ SubscriptionManager.prototype.resolveFilterMatch = function (filter, document, documentId) {
3085
+ if (!document || !filter || typeof filter !== 'object') {
3086
+ return null;
3087
+ }
3088
+ if (!this.isFilterSafeForInMemory(filter)) {
3089
+ return null;
3090
+ }
3091
+ if (!this.documentIdMatchesEvent(documentId, document)) {
3092
+ return false;
3093
+ }
3094
+ try {
3095
+ var matcher = sift(filter);
3096
+ return matcher(document);
3097
+ }
3098
+ catch (_a) {
3099
+ return null;
3100
+ }
3101
+ };
3102
+ SubscriptionManager.prototype.documentMatchesFilter = function (collection, documentId, filter, document) {
3103
+ return __awaiter(this, void 0, void 0, function () {
3104
+ var inMemoryMatch, db, filterCopy, idCandidates, idCandidates_1, idCandidates_1_1, idValue, combinedFilter, doc, e_12_1, _a;
3105
+ var e_12, _b;
3106
+ return __generator(this, function (_c) {
3107
+ switch (_c.label) {
3108
+ case 0:
3109
+ _c.trys.push([0, 9, , 10]);
3110
+ inMemoryMatch = this.resolveFilterMatch(filter, document, documentId);
3111
+ if (inMemoryMatch !== null) {
3112
+ return [2 /*return*/, inMemoryMatch];
3113
+ }
3114
+ db = resolveio_server_app_1.ResolveIOServer.getMainDB();
3115
+ filterCopy = (0, common_1.deepCopy)(filter) || {};
3116
+ idCandidates = this.getDocumentIdQueryCandidates(documentId);
3117
+ _c.label = 1;
3118
+ case 1:
3119
+ _c.trys.push([1, 6, 7, 8]);
3120
+ idCandidates_1 = __values(idCandidates), idCandidates_1_1 = idCandidates_1.next();
3121
+ _c.label = 2;
3122
+ case 2:
3123
+ if (!!idCandidates_1_1.done) return [3 /*break*/, 5];
3124
+ idValue = idCandidates_1_1.value;
3125
+ combinedFilter = {
3126
+ $and: [
3127
+ { _id: idValue },
3128
+ filterCopy
3129
+ ]
3130
+ };
3131
+ return [4 /*yield*/, db.collection(collection).findOne(combinedFilter)];
3132
+ case 3:
3133
+ doc = _c.sent();
3134
+ if (doc) {
3135
+ return [2 /*return*/, true];
3136
+ }
3137
+ _c.label = 4;
3138
+ case 4:
3139
+ idCandidates_1_1 = idCandidates_1.next();
3140
+ return [3 /*break*/, 2];
3141
+ case 5: return [3 /*break*/, 8];
3142
+ case 6:
3143
+ e_12_1 = _c.sent();
3144
+ e_12 = { error: e_12_1 };
3145
+ return [3 /*break*/, 8];
3146
+ case 7:
3147
+ try {
3148
+ if (idCandidates_1_1 && !idCandidates_1_1.done && (_b = idCandidates_1.return)) _b.call(idCandidates_1);
3149
+ }
3150
+ finally { if (e_12) throw e_12.error; }
3151
+ return [7 /*endfinally*/];
3152
+ case 8: return [2 /*return*/, false];
3153
+ case 9:
3154
+ _a = _c.sent();
3155
+ return [2 /*return*/, true];
3156
+ case 10: return [2 /*return*/];
3157
+ }
3158
+ });
3159
+ });
3160
+ };
3161
+ SubscriptionManager.prototype.shouldInvalidateSubscription = function (sub, collection, type, documentId, document) {
3162
+ return __awaiter(this, void 0, void 0, function () {
3163
+ var normalizedDocumentId, trackedIds, filters, hasTrackedIds, hasFilters, hasDependencyData, filters_1, filters_1_1, filter, e_13_1;
3164
+ var e_13, _a;
3165
+ var _b, _c;
3166
+ return __generator(this, function (_d) {
3167
+ switch (_d.label) {
3168
+ case 0:
3169
+ this.ensureDependencyContainers(sub);
3170
+ normalizedDocumentId = this.normalizeDocumentId(documentId);
3171
+ trackedIds = sub.collectionDependencies.get(collection);
3172
+ filters = sub.collectionFilters.get(collection);
3173
+ hasTrackedIds = !!(trackedIds && trackedIds.size);
3174
+ hasFilters = !!(filters && filters.length);
3175
+ hasDependencyData = (((_b = sub.collectionDependencies.get(collection)) === null || _b === void 0 ? void 0 : _b.size) || 0) > 0 ||
3176
+ (((_c = sub.collectionFilters.get(collection)) === null || _c === void 0 ? void 0 : _c.length) || 0) > 0 ||
3177
+ sub.watchAllCollections.has(collection);
3178
+ if (!normalizedDocumentId) {
3179
+ this.dependencyDebug('Invalidate due to missing documentId', { publication: sub.publication, collection: collection, type: type });
3180
+ return [2 /*return*/, true];
3181
+ }
3182
+ if (!hasDependencyData) {
3183
+ this.dependencyDebug('Invalidate due to missing dependency metadata', {
3184
+ publication: sub.publication,
3185
+ collection: collection,
3186
+ type: type
3187
+ });
3188
+ return [2 /*return*/, true];
3189
+ }
3190
+ if (sub.watchAllCollections.has(collection)) {
3191
+ this.dependencyDebug('Invalidate due to watch-all collection', { publication: sub.publication, collection: collection, type: type });
3192
+ return [2 /*return*/, true];
3193
+ }
3194
+ if (trackedIds && trackedIds.has(normalizedDocumentId)) {
3195
+ this.dependencyDebug('Invalidate due to tracked id', { publication: sub.publication, collection: collection, type: type, documentId: normalizedDocumentId });
3196
+ return [2 /*return*/, true];
3197
+ }
3198
+ if (type === 'delete') {
3199
+ if (hasFilters && !hasTrackedIds) {
3200
+ this.dependencyDebug('Invalidate delete 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
+ this.dependencyDebug('Skip invalidation on delete for unknown id', { publication: sub.publication, collection: collection, type: type, documentId: normalizedDocumentId });
3209
+ return [2 /*return*/, false];
3210
+ }
3211
+ if (type === 'update' && hasFilters && !hasTrackedIds) {
3212
+ this.dependencyDebug('Invalidate update due to filter-only dependency without tracked ids', {
3213
+ publication: sub.publication,
3214
+ collection: collection,
3215
+ type: type,
3216
+ documentId: normalizedDocumentId
3217
+ });
3218
+ return [2 /*return*/, true];
3219
+ }
3220
+ if (!(filters && filters.length)) return [3 /*break*/, 8];
3221
+ _d.label = 1;
3222
+ case 1:
3223
+ _d.trys.push([1, 6, 7, 8]);
3224
+ filters_1 = __values(filters), filters_1_1 = filters_1.next();
3225
+ _d.label = 2;
3226
+ case 2:
3227
+ if (!!filters_1_1.done) return [3 /*break*/, 5];
3228
+ filter = filters_1_1.value;
3229
+ return [4 /*yield*/, this.documentMatchesFilter(collection, documentId, filter, document)];
3230
+ case 3:
3231
+ if (_d.sent()) {
3232
+ this.dependencyDebug('Invalidate due to filter match', { publication: sub.publication, collection: collection, type: type, documentId: normalizedDocumentId, filter: filter });
3233
+ return [2 /*return*/, true];
3234
+ }
3235
+ _d.label = 4;
3236
+ case 4:
3237
+ filters_1_1 = filters_1.next();
3238
+ return [3 /*break*/, 2];
3239
+ case 5: return [3 /*break*/, 8];
3240
+ case 6:
3241
+ e_13_1 = _d.sent();
3242
+ e_13 = { error: e_13_1 };
3243
+ return [3 /*break*/, 8];
3244
+ case 7:
3245
+ try {
3246
+ if (filters_1_1 && !filters_1_1.done && (_a = filters_1.return)) _a.call(filters_1);
3247
+ }
3248
+ finally { if (e_13) throw e_13.error; }
3249
+ return [7 /*endfinally*/];
3250
+ case 8:
3251
+ this.dependencyDebug('Skip invalidation after dependency checks', { publication: sub.publication, collection: collection, type: type, documentId: normalizedDocumentId });
3252
+ return [2 /*return*/, false];
3253
+ }
3254
+ });
3255
+ });
3256
+ };
3257
+ SubscriptionManager.prototype.shouldInvalidateSubscriptionForEvents = function (sub, collection, events) {
3258
+ return __awaiter(this, void 0, void 0, function () {
3259
+ var sawInsert, sawDelete, events_1, events_1_1, event_1, e_14_1, paginationMeta, paginationReasons;
3260
+ var e_14, _a;
3261
+ return __generator(this, function (_b) {
3262
+ switch (_b.label) {
3263
+ case 0:
3264
+ sawInsert = false;
3265
+ sawDelete = false;
3266
+ _b.label = 1;
3267
+ case 1:
3268
+ _b.trys.push([1, 6, 7, 8]);
3269
+ events_1 = __values(events), events_1_1 = events_1.next();
3270
+ _b.label = 2;
3271
+ case 2:
3272
+ if (!!events_1_1.done) return [3 /*break*/, 5];
3273
+ event_1 = events_1_1.value;
3274
+ if (event_1.type === 'insert') {
3275
+ sawInsert = true;
3276
+ }
3277
+ else if (event_1.type === 'delete') {
3278
+ sawDelete = true;
3279
+ }
3280
+ return [4 /*yield*/, this.shouldInvalidateSubscription(sub, collection, event_1.type, event_1.documentId, event_1.document)];
3281
+ case 3:
3282
+ if (_b.sent()) {
3283
+ return [2 /*return*/, true];
3284
+ }
3285
+ _b.label = 4;
3286
+ case 4:
3287
+ events_1_1 = events_1.next();
3288
+ return [3 /*break*/, 2];
3289
+ case 5: return [3 /*break*/, 8];
3290
+ case 6:
3291
+ e_14_1 = _b.sent();
3292
+ e_14 = { error: e_14_1 };
3293
+ return [3 /*break*/, 8];
3294
+ case 7:
3295
+ try {
3296
+ if (events_1_1 && !events_1_1.done && (_a = events_1.return)) _a.call(events_1);
3297
+ }
3298
+ finally { if (e_14) throw e_14.error; }
3299
+ return [7 /*endfinally*/];
3300
+ case 8:
3301
+ paginationMeta = this.getPaginationMeta(sub, collection);
3302
+ paginationReasons = [];
3303
+ if (sawInsert) {
3304
+ paginationReasons.push('insert');
3305
+ }
3306
+ if (sawDelete) {
3307
+ paginationReasons.push('delete');
3308
+ }
3309
+ if ((paginationMeta === null || paginationMeta === void 0 ? void 0 : paginationMeta.length) && paginationReasons.length) {
3310
+ this.dependencyDebug('Invalidate due to pagination metadata', {
3311
+ publication: sub.publication,
3312
+ collection: collection,
3313
+ events: this.summarizeEvents(events),
3314
+ reasons: paginationReasons,
3315
+ queryMeta: paginationMeta.slice(0, 5)
3316
+ });
3317
+ return [2 /*return*/, true];
3318
+ }
3319
+ return [2 /*return*/, false];
3320
+ }
3321
+ });
3322
+ });
3323
+ };
3324
+ SubscriptionManager.prototype.getPaginationMeta = function (sub, collection) {
3325
+ var _a;
3326
+ this.ensureDependencyContainers(sub);
3327
+ var metaList = (_a = sub.collectionQueryMeta) === null || _a === void 0 ? void 0 : _a.get(collection);
3328
+ if (!metaList || !metaList.length) {
3329
+ return null;
3330
+ }
3331
+ var relevantMeta = metaList.filter(function (meta) {
3332
+ if (!meta) {
3333
+ return false;
3334
+ }
3335
+ var limitUsed = typeof meta.limit === 'number' && meta.limit > 0;
3336
+ var skipUsed = typeof meta.skip === 'number' && meta.skip > 0;
3337
+ return limitUsed || skipUsed;
3338
+ });
3339
+ return relevantMeta.length ? relevantMeta : null;
3340
+ };
3341
+ SubscriptionManager.prototype.sendWS = function (ws, data) {
3342
+ this._websocketManager.send(ws, data);
3343
+ };
3344
+ SubscriptionManager.prototype.getEnableDebug = function () {
3345
+ return this._enableDebug;
3346
+ };
3347
+ SubscriptionManager.prototype.dependencyDebug = function (message, details) {
3348
+ if (!this._enableDependencyDebug) {
3349
+ return;
3350
+ }
3351
+ if (details) {
3352
+ console.log(new Date(), '[Dependency Debug]', message, JSON.stringify(details, null, 2));
3353
+ }
3354
+ else {
3355
+ console.log(new Date(), '[Dependency Debug]', message);
3356
+ }
3357
+ };
3358
+ SubscriptionManager.prototype.summarizeEvents = function (events) {
3359
+ var _this = this;
3360
+ return events.map(function (event) { return ({
3361
+ type: event.type,
3362
+ documentId: _this.normalizeDocumentId(event.documentId)
3363
+ }); });
3364
+ };
3365
+ SubscriptionManager.prototype.logDependencySnapshot = function (sub, context) {
3366
+ if (!this._enableDependencyDebug) {
3367
+ return;
3368
+ }
3369
+ var dependencySummary = Array.from(sub.collectionDependencies.entries()).map(function (_a) {
3370
+ var _b = __read(_a, 2), collectionName = _b[0], ids = _b[1];
3371
+ var idList = Array.from(ids || []);
3372
+ return {
3373
+ collection: collectionName,
3374
+ count: idList.length,
3375
+ sample: idList.slice(0, 10)
3376
+ };
3377
+ });
3378
+ var filterSummary = Array.from(sub.collectionFilters.entries()).map(function (_a) {
3379
+ var _b = __read(_a, 2), collectionName = _b[0], filters = _b[1];
3380
+ return ({
3381
+ collection: collectionName,
3382
+ count: filters.length
3383
+ });
3384
+ });
3385
+ var queryMetaSummary = Array.from(sub.collectionQueryMeta.entries()).map(function (_a) {
3386
+ var _b = __read(_a, 2), collectionName = _b[0], metaList = _b[1];
3387
+ return ({
3388
+ collection: collectionName,
3389
+ count: metaList.length,
3390
+ meta: metaList.slice(0, 5)
3391
+ });
3392
+ });
3393
+ var watchAll = Array.from(sub.watchAllCollections || []);
3394
+ this.dependencyDebug('Dependency snapshot updated', {
3395
+ context: context,
3396
+ publication: sub.publication,
3397
+ dependencies: dependencySummary,
3398
+ collectionsWithFilters: filterSummary,
3399
+ collectionsWithMeta: queryMetaSummary,
3400
+ watchAll: watchAll
3401
+ });
3402
+ };
3403
+ SubscriptionManager.prototype.runPublicationOnce = function (publication_1) {
3404
+ return __awaiter(this, arguments, void 0, function (publication, subscriptionData, options) {
3405
+ var pub, context, metadata;
3406
+ if (subscriptionData === void 0) { subscriptionData = []; }
3407
+ return __generator(this, function (_a) {
3408
+ pub = this._publications[publication];
3409
+ if (!pub) {
3410
+ throw new Error("Publication not found: ".concat(publication));
3411
+ }
3412
+ context = Object.assign({}, this, SubscriptionManager.prototype);
3413
+ metadata = {
3414
+ publication: publication,
3415
+ subscriptionData: subscriptionData
3416
+ };
3417
+ if (pub.user_specific) {
3418
+ return [2 /*return*/, (0, subscription_dependency_context_1.withDependencyTracking)(function () {
3419
+ var _a;
3420
+ return (_a = pub.function).call.apply(_a, __spreadArray([context, (options === null || options === void 0 ? void 0 : options.userId) || ''], __read(subscriptionData), false));
3421
+ }, metadata)];
3422
+ }
3423
+ return [2 /*return*/, (0, subscription_dependency_context_1.withDependencyTracking)(function () {
3424
+ var _a;
3425
+ return (_a = pub.function).call.apply(_a, __spreadArray([context], __read(subscriptionData), false));
3426
+ }, metadata)];
3427
+ });
3428
+ });
3429
+ };
3430
+ return SubscriptionManager;
3431
+ }());
3432
+ exports.SubscriptionManager = SubscriptionManager;
3433
+
3434
+ //# sourceMappingURL=subscription.manager.js.map