@resolveio/server-lib 22.3.220 → 22.3.221

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/.nodemon.json +5 -0
  2. package/.vscode/settings.json +21 -0
  3. package/AGENTS.md +195 -0
  4. package/README.md +22 -0
  5. package/build_package.sh +5 -0
  6. package/compileDTS.pl +64 -0
  7. package/docs/ai-assistant-nightly-eval.md +65 -0
  8. package/docs/ai-assistant-preflight-checklist.md +23 -0
  9. package/docs/ai-assistant-report-builder-bridge-playbook.md +115 -0
  10. package/eslint-plugin-custom/index.js +7 -0
  11. package/eslint-plugin-custom/rules/no-filter-zero-index.js +44 -0
  12. package/eslint.config.js +103 -0
  13. package/gulpfile.js +216 -0
  14. package/methodAndPublicationListGenerator.py +375 -0
  15. package/mongodbensurers.js +2 -0
  16. package/mongostop.js +3 -0
  17. package/package.json +1 -1
  18. package/scripts/cleanup-bypassed-callmethod-logs.js +616 -0
  19. package/settings.development.json +25 -0
  20. package/settings.development.redacted.json +25 -0
  21. package/src/.env +12 -0
  22. package/src/ai/assistant-core-heuristics.ts +379 -0
  23. package/src/ai/resolveio-platform-intelligence-memory-corpus.ts +185 -0
  24. package/src/ai/resolveio-platform-intelligence-memory.ts +325 -0
  25. package/{ai/resolveio-platform-intelligence-types.d.ts → src/ai/resolveio-platform-intelligence-types.ts} +20 -15
  26. package/src/ai/resolveio-platform-intelligence.ts +462 -0
  27. package/src/client-server-app.ts +12 -0
  28. package/src/collections/ai-run.collection.ts +117 -0
  29. package/src/collections/ai-terminal-conversation.collection.ts +91 -0
  30. package/src/collections/ai-terminal-issue-report.collection.ts +99 -0
  31. package/src/collections/ai-terminal-message.collection.ts +77 -0
  32. package/src/collections/app-setting.collection.ts +104 -0
  33. package/src/collections/app-status.collection.ts +58 -0
  34. package/src/collections/communication-metric.collection.ts +84 -0
  35. package/src/collections/counter.collection.ts +56 -0
  36. package/src/collections/cron-job-history.collection.ts +94 -0
  37. package/src/collections/cron-job.collection.ts +92 -0
  38. package/src/collections/customer-notification.collection.ts +131 -0
  39. package/src/collections/customer-portal-password.collection.ts +76 -0
  40. package/src/collections/email-history.collection.ts +134 -0
  41. package/src/collections/email-verified.collection.ts +62 -0
  42. package/src/collections/file.collection.ts +74 -0
  43. package/src/collections/flag-update.collection.ts +57 -0
  44. package/src/collections/flag.collection.ts +57 -0
  45. package/src/collections/log-method-latency.collection.ts +77 -0
  46. package/src/collections/log-subscription.collection.ts +80 -0
  47. package/src/collections/log.collection.ts +93 -0
  48. package/src/collections/logged-in-users.collection.ts +67 -0
  49. package/src/collections/monitor-cpu.collection.ts +65 -0
  50. package/src/collections/monitor-function.collection.ts +74 -0
  51. package/src/collections/monitor-memory.collection.ts +77 -0
  52. package/src/collections/monitor-mongo.collection.ts +71 -0
  53. package/src/collections/notification.collection.ts +57 -0
  54. package/src/collections/openai-usage-ledger.collection.ts +131 -0
  55. package/src/collections/report-builder-dashboard-builder.collection.ts +109 -0
  56. package/src/collections/report-builder-library.collection.ts +89 -0
  57. package/src/collections/report-builder-report.collection.ts +184 -0
  58. package/src/collections/user-group.collection.ts +89 -0
  59. package/src/collections/user-guide.collection.ts +57 -0
  60. package/src/collections/user.collection.ts +181 -0
  61. package/src/cron/cron.ts +117 -0
  62. package/src/fixtures/cron-jobs.ts +95 -0
  63. package/src/fixtures/init.ts +35 -0
  64. package/src/http/auth.ts +818 -0
  65. package/src/http/health.ts +7 -0
  66. package/src/http/home.ts +90 -0
  67. package/src/http/slow-query-publication.ts +49 -0
  68. package/src/index.ts +1 -0
  69. package/src/managers/ai-assistant-codex-manager.manager.ts +1131 -0
  70. package/src/managers/ai-run-evidence.manager.ts +264 -0
  71. package/src/managers/communication-metric.manager.ts +82 -0
  72. package/src/managers/cron.manager.ts +333 -0
  73. package/src/managers/customer-notification-content.manager.ts +236 -0
  74. package/src/managers/diagnostic-manager-bootstrap.ts +165 -0
  75. package/src/managers/error-auto-fix.manager.ts +2767 -0
  76. package/src/managers/local-log.manager.ts +113 -0
  77. package/src/managers/method.manager.ts +1857 -0
  78. package/src/managers/mongo.manager.ts +4575 -0
  79. package/src/managers/monitor.manager.ts +507 -0
  80. package/src/managers/openai-usage-ledger.manager.ts +112 -0
  81. package/src/managers/slow-query-verifier.manager.ts +3590 -0
  82. package/src/managers/slow-query.manager.ts +519 -0
  83. package/src/managers/subscription.manager.ts +3128 -0
  84. package/src/managers/websocket.manager.ts +746 -0
  85. package/src/managers/worker-dispatcher.manager.ts +1360 -0
  86. package/src/managers/worker-server.manager.ts +536 -0
  87. package/src/methods/accounts.ts +532 -0
  88. package/src/methods/ai-terminal.ts +29070 -0
  89. package/src/methods/app-settings.ts +114 -0
  90. package/src/methods/aws.ts +649 -0
  91. package/src/methods/collections.ts +641 -0
  92. package/src/methods/counters.ts +69 -0
  93. package/src/methods/cron-jobs.ts +2614 -0
  94. package/src/methods/customer-notifications.ts +458 -0
  95. package/src/methods/diagnostics.ts +616 -0
  96. package/src/methods/flag-updates.ts +7 -0
  97. package/src/methods/flags.ts +7 -0
  98. package/src/methods/logs.ts +657 -0
  99. package/src/methods/mongo-explorer.ts +1880 -0
  100. package/src/methods/monitor.ts +540 -0
  101. package/src/methods/pdf.ts +1236 -0
  102. package/src/methods/publications.ts +129 -0
  103. package/src/methods/report-builder.ts +3300 -0
  104. package/src/methods/support.ts +335 -0
  105. package/src/models/ai-run.model.ts +27 -0
  106. package/src/models/ai-terminal-conversation.model.ts +19 -0
  107. package/src/models/ai-terminal-issue-report.model.ts +21 -0
  108. package/src/models/ai-terminal-message.model.ts +24 -0
  109. package/src/models/app-setting.model.ts +17 -0
  110. package/{models/app-status.model.d.ts → src/models/app-status.model.ts} +3 -2
  111. package/{models/billing-logged-in-users.model.d.ts → src/models/billing-logged-in-users.model.ts} +5 -4
  112. package/src/models/collection-document.model.ts +24 -0
  113. package/src/models/communication-metric.model.ts +23 -0
  114. package/{models/counter.model.d.ts → src/models/counter.model.ts} +4 -3
  115. package/src/models/cron-job-history.model.ts +16 -0
  116. package/src/models/cron-job.model.ts +15 -0
  117. package/src/models/customer-notification.model.ts +28 -0
  118. package/src/models/customer-portal-password.model.ts +12 -0
  119. package/src/models/dialog.model.ts +25 -0
  120. package/{models/email-history.model.js → src/models/email-history.model.ts} +36 -4
  121. package/{models/email-verified.model.d.ts → src/models/email-verified.model.ts} +6 -5
  122. package/{models/file.model.d.ts → src/models/file.model.ts} +8 -7
  123. package/{models/flag-update.model.d.ts → src/models/flag-update.model.ts} +4 -3
  124. package/{models/flag.model.d.ts → src/models/flag.model.ts} +4 -3
  125. package/src/models/log-method-latency.model.ts +11 -0
  126. package/{models/log-subscription.model.d.ts → src/models/log-subscription.model.ts} +11 -9
  127. package/src/models/log.model.ts +19 -0
  128. package/{models/logged-in-users.model.d.ts → src/models/logged-in-users.model.ts} +6 -5
  129. package/{models/method-response.model.d.ts → src/models/method-response.model.ts} +7 -6
  130. package/src/models/method.model.ts +25 -0
  131. package/{models/monitor-cpu.model.d.ts → src/models/monitor-cpu.model.ts} +9 -7
  132. package/src/models/monitor-function.model.ts +16 -0
  133. package/src/models/monitor-memory.model.ts +17 -0
  134. package/src/models/monitor-mongo.model.ts +15 -0
  135. package/{models/notification.model.d.ts → src/models/notification.model.ts} +6 -4
  136. package/src/models/openai-usage-ledger.model.ts +56 -0
  137. package/src/models/pagination.model.ts +35 -0
  138. package/src/models/permission.model.ts +14 -0
  139. package/src/models/report-builder-dashboard-builder.model.ts +29 -0
  140. package/src/models/report-builder-library.model.ts +20 -0
  141. package/src/models/report-builder-report.model.ts +136 -0
  142. package/src/models/report-builder.model.ts +68 -0
  143. package/src/models/select-data-label.model.ts +9 -0
  144. package/src/models/server-message.model.ts +31 -0
  145. package/src/models/slow-query-report.model.ts +23 -0
  146. package/src/models/subscription.model.ts +73 -0
  147. package/src/models/support-ticket.model.ts +104 -0
  148. package/src/models/user-group.model.ts +24 -0
  149. package/{models/user-guide.model.d.ts → src/models/user-guide.model.ts} +5 -4
  150. package/src/models/user.model.ts +96 -0
  151. package/src/private/images/ResolveIO.png +0 -0
  152. package/src/publications/ai-terminal.ts +73 -0
  153. package/src/publications/app-settings.ts +25 -0
  154. package/src/publications/app-status.ts +13 -0
  155. package/src/publications/cron-jobs.ts +40 -0
  156. package/src/publications/customer-notifications.ts +101 -0
  157. package/src/publications/files.ts +33 -0
  158. package/src/publications/flags-update.ts +19 -0
  159. package/src/publications/flags.ts +19 -0
  160. package/src/publications/logs.ts +163 -0
  161. package/src/publications/notifications.ts +13 -0
  162. package/src/publications/report-builder-dashboard-builders.ts +39 -0
  163. package/src/publications/report-builder-libraries.ts +41 -0
  164. package/src/publications/report-builder-reports.ts +47 -0
  165. package/src/publications/super-admin.ts +13 -0
  166. package/src/publications/user-groups.ts +12 -0
  167. package/src/publications/user-guides.ts +12 -0
  168. package/src/resolveio-server-app.ts +617 -0
  169. package/src/server-app.ts +3354 -0
  170. package/src/services/codex-client.ts +1231 -0
  171. package/src/services/openai-client.ts +265 -0
  172. package/src/types/error-report.ts +26 -0
  173. package/src/types/js-tiktoken.d.ts +11 -0
  174. package/src/types/slow-query-report.ts +28 -0
  175. package/src/util/ai-qa-policy.ts +925 -0
  176. package/src/util/ai-run-evidence-adapters.ts +8347 -0
  177. package/src/util/ai-run-evidence-dashboard.ts +323 -0
  178. package/src/util/ai-run-evidence-eval.ts +1057 -0
  179. package/src/util/ai-run-evidence.ts +1430 -0
  180. package/src/util/ai-runner-artifacts.ts +586 -0
  181. package/src/util/ai-runner-manager-autopilot.ts +961 -0
  182. package/src/util/ai-runner-manager-policy.ts +5011 -0
  183. package/src/util/ai-runner-qa-auth.ts +838 -0
  184. package/src/util/ai-runner-qa-tools.ts +3536 -0
  185. package/src/util/aicoder-runner-v6.ts +3121 -0
  186. package/src/util/common.ts +649 -0
  187. package/src/util/customer-portal-password.ts +183 -0
  188. package/src/util/error-reporter.ts +332 -0
  189. package/src/util/error-tracking.ts +79 -0
  190. package/src/util/openai-usage-cost.ts +114 -0
  191. package/src/util/report-builder-unwinds.ts +180 -0
  192. package/src/util/runner-process-janitor.ts +219 -0
  193. package/src/util/schema-report-builder.ts +448 -0
  194. package/src/util/slow-query-reporter.ts +216 -0
  195. package/src/util/subscription-dependency-context.ts +1096 -0
  196. package/src/util/support-runner-v5.ts +10040 -0
  197. package/src/util/tokenizer.ts +38 -0
  198. package/src/workers/codex-runner.worker.ts +142 -0
  199. package/start_server.sh +5 -0
  200. package/tests/ai-assistant-corpus-build.ts +484 -0
  201. package/tests/ai-assistant-corpus-replay-e2e.ts +774 -0
  202. package/tests/ai-assistant-data-parity-e2e.ts +1989 -0
  203. package/tests/ai-assistant-eval-triage.ts +831 -0
  204. package/tests/ai-assistant-openai-e2e.ts +1061 -0
  205. package/tests/ai-assistant-openai-git-e2e.ts +155 -0
  206. package/tests/ai-assistant-preflight-matrix.ts +215 -0
  207. package/tests/ai-assistant-routing-eval.test.ts +585 -0
  208. package/tests/ai-assistant-snf-live-eval.ts +975 -0
  209. package/tests/ai-assistant-utils.test.ts +4834 -0
  210. package/tests/ai-manager-autopilot-snapshot.test.ts +193 -0
  211. package/tests/ai-manager-recovery-checkpoint.test.ts +1383 -0
  212. package/tests/ai-run-eval.test.ts +132 -0
  213. package/tests/ai-run-evidence.test.ts +3773 -0
  214. package/tests/ai-runner-contract.test.ts +515 -0
  215. package/tests/aicoder-runner-v6.test.ts +822 -0
  216. package/tests/error-reporter.test.ts +145 -0
  217. package/tests/method-publication-generator.test.ts +46 -0
  218. package/tests/report-builder-linking.test.ts +79 -0
  219. package/tests/resolveio-platform-intelligence.test.ts +352 -0
  220. package/tests/server-app-cron-owner.test.ts +127 -0
  221. package/tests/subscription-connect-race.test.ts +158 -0
  222. package/tests/subscription-dependency-context.test.ts +324 -0
  223. package/tests/subscription-manager-collection-tracking.test.ts +86 -0
  224. package/tests/subscription-manager-invalidation.test.ts +86 -0
  225. package/tests/support-runner-v5.test.ts +3201 -0
  226. package/tsconfig.json +34 -0
  227. package/ai/assistant-core-heuristics.d.ts +0 -11
  228. package/ai/assistant-core-heuristics.js +0 -356
  229. package/ai/assistant-core-heuristics.js.map +0 -1
  230. package/ai/resolveio-platform-intelligence-memory-corpus.d.ts +0 -3
  231. package/ai/resolveio-platform-intelligence-memory-corpus.js +0 -214
  232. package/ai/resolveio-platform-intelligence-memory-corpus.js.map +0 -1
  233. package/ai/resolveio-platform-intelligence-memory.d.ts +0 -20
  234. package/ai/resolveio-platform-intelligence-memory.js +0 -341
  235. package/ai/resolveio-platform-intelligence-memory.js.map +0 -1
  236. package/ai/resolveio-platform-intelligence-types.js +0 -4
  237. package/ai/resolveio-platform-intelligence-types.js.map +0 -1
  238. package/ai/resolveio-platform-intelligence.d.ts +0 -6
  239. package/ai/resolveio-platform-intelligence.js +0 -463
  240. package/ai/resolveio-platform-intelligence.js.map +0 -1
  241. package/client-server-app.d.ts +0 -1
  242. package/client-server-app.js +0 -68
  243. package/client-server-app.js.map +0 -1
  244. package/collections/ai-run.collection.d.ts +0 -3
  245. package/collections/ai-run.collection.js +0 -170
  246. package/collections/ai-run.collection.js.map +0 -1
  247. package/collections/ai-terminal-conversation.collection.d.ts +0 -2
  248. package/collections/ai-terminal-conversation.collection.js +0 -140
  249. package/collections/ai-terminal-conversation.collection.js.map +0 -1
  250. package/collections/ai-terminal-issue-report.collection.d.ts +0 -2
  251. package/collections/ai-terminal-issue-report.collection.js +0 -148
  252. package/collections/ai-terminal-issue-report.collection.js.map +0 -1
  253. package/collections/ai-terminal-message.collection.d.ts +0 -2
  254. package/collections/ai-terminal-message.collection.js +0 -121
  255. package/collections/ai-terminal-message.collection.js.map +0 -1
  256. package/collections/app-setting.collection.d.ts +0 -3
  257. package/collections/app-setting.collection.js +0 -103
  258. package/collections/app-setting.collection.js.map +0 -1
  259. package/collections/app-status.collection.d.ts +0 -3
  260. package/collections/app-status.collection.js +0 -57
  261. package/collections/app-status.collection.js.map +0 -1
  262. package/collections/communication-metric.collection.d.ts +0 -2
  263. package/collections/communication-metric.collection.js +0 -133
  264. package/collections/communication-metric.collection.js.map +0 -1
  265. package/collections/counter.collection.d.ts +0 -3
  266. package/collections/counter.collection.js +0 -56
  267. package/collections/counter.collection.js.map +0 -1
  268. package/collections/cron-job-history.collection.d.ts +0 -3
  269. package/collections/cron-job-history.collection.js +0 -137
  270. package/collections/cron-job-history.collection.js.map +0 -1
  271. package/collections/cron-job.collection.d.ts +0 -3
  272. package/collections/cron-job.collection.js +0 -92
  273. package/collections/cron-job.collection.js.map +0 -1
  274. package/collections/customer-notification.collection.d.ts +0 -3
  275. package/collections/customer-notification.collection.js +0 -130
  276. package/collections/customer-notification.collection.js.map +0 -1
  277. package/collections/customer-portal-password.collection.d.ts +0 -3
  278. package/collections/customer-portal-password.collection.js +0 -75
  279. package/collections/customer-portal-password.collection.js.map +0 -1
  280. package/collections/email-history.collection.d.ts +0 -3
  281. package/collections/email-history.collection.js +0 -134
  282. package/collections/email-history.collection.js.map +0 -1
  283. package/collections/email-verified.collection.d.ts +0 -3
  284. package/collections/email-verified.collection.js +0 -62
  285. package/collections/email-verified.collection.js.map +0 -1
  286. package/collections/file.collection.d.ts +0 -3
  287. package/collections/file.collection.js +0 -74
  288. package/collections/file.collection.js.map +0 -1
  289. package/collections/flag-update.collection.d.ts +0 -3
  290. package/collections/flag-update.collection.js +0 -57
  291. package/collections/flag-update.collection.js.map +0 -1
  292. package/collections/flag.collection.d.ts +0 -3
  293. package/collections/flag.collection.js +0 -57
  294. package/collections/flag.collection.js.map +0 -1
  295. package/collections/log-method-latency.collection.d.ts +0 -3
  296. package/collections/log-method-latency.collection.js +0 -77
  297. package/collections/log-method-latency.collection.js.map +0 -1
  298. package/collections/log-subscription.collection.d.ts +0 -3
  299. package/collections/log-subscription.collection.js +0 -80
  300. package/collections/log-subscription.collection.js.map +0 -1
  301. package/collections/log.collection.d.ts +0 -3
  302. package/collections/log.collection.js +0 -93
  303. package/collections/log.collection.js.map +0 -1
  304. package/collections/logged-in-users.collection.d.ts +0 -3
  305. package/collections/logged-in-users.collection.js +0 -67
  306. package/collections/logged-in-users.collection.js.map +0 -1
  307. package/collections/monitor-cpu.collection.d.ts +0 -3
  308. package/collections/monitor-cpu.collection.js +0 -65
  309. package/collections/monitor-cpu.collection.js.map +0 -1
  310. package/collections/monitor-function.collection.d.ts +0 -3
  311. package/collections/monitor-function.collection.js +0 -74
  312. package/collections/monitor-function.collection.js.map +0 -1
  313. package/collections/monitor-memory.collection.d.ts +0 -3
  314. package/collections/monitor-memory.collection.js +0 -77
  315. package/collections/monitor-memory.collection.js.map +0 -1
  316. package/collections/monitor-mongo.collection.d.ts +0 -3
  317. package/collections/monitor-mongo.collection.js +0 -71
  318. package/collections/monitor-mongo.collection.js.map +0 -1
  319. package/collections/notification.collection.d.ts +0 -3
  320. package/collections/notification.collection.js +0 -57
  321. package/collections/notification.collection.js.map +0 -1
  322. package/collections/openai-usage-ledger.collection.d.ts +0 -2
  323. package/collections/openai-usage-ledger.collection.js +0 -188
  324. package/collections/openai-usage-ledger.collection.js.map +0 -1
  325. package/collections/report-builder-dashboard-builder.collection.d.ts +0 -3
  326. package/collections/report-builder-dashboard-builder.collection.js +0 -109
  327. package/collections/report-builder-dashboard-builder.collection.js.map +0 -1
  328. package/collections/report-builder-library.collection.d.ts +0 -3
  329. package/collections/report-builder-library.collection.js +0 -87
  330. package/collections/report-builder-library.collection.js.map +0 -1
  331. package/collections/report-builder-report.collection.d.ts +0 -4
  332. package/collections/report-builder-report.collection.js +0 -184
  333. package/collections/report-builder-report.collection.js.map +0 -1
  334. package/collections/user-group.collection.d.ts +0 -4
  335. package/collections/user-group.collection.js +0 -89
  336. package/collections/user-group.collection.js.map +0 -1
  337. package/collections/user-guide.collection.d.ts +0 -3
  338. package/collections/user-guide.collection.js +0 -57
  339. package/collections/user-guide.collection.js.map +0 -1
  340. package/collections/user.collection.d.ts +0 -4
  341. package/collections/user.collection.js +0 -180
  342. package/collections/user.collection.js.map +0 -1
  343. package/cron/cron.d.ts +0 -14
  344. package/cron/cron.js +0 -216
  345. package/cron/cron.js.map +0 -1
  346. package/fixtures/cron-jobs.d.ts +0 -1
  347. package/fixtures/cron-jobs.js +0 -150
  348. package/fixtures/cron-jobs.js.map +0 -1
  349. package/fixtures/init.d.ts +0 -1
  350. package/fixtures/init.js +0 -91
  351. package/fixtures/init.js.map +0 -1
  352. package/http/auth.d.ts +0 -2
  353. package/http/auth.js +0 -951
  354. package/http/auth.js.map +0 -1
  355. package/http/health.d.ts +0 -1
  356. package/http/health.js +0 -11
  357. package/http/health.js.map +0 -1
  358. package/http/home.d.ts +0 -1
  359. package/http/home.js +0 -134
  360. package/http/home.js.map +0 -1
  361. package/http/slow-query-publication.d.ts +0 -2
  362. package/http/slow-query-publication.js +0 -99
  363. package/http/slow-query-publication.js.map +0 -1
  364. package/index.d.ts +0 -1
  365. package/index.js +0 -19
  366. package/index.js.map +0 -1
  367. package/managers/ai-assistant-codex-manager.manager.d.ts +0 -67
  368. package/managers/ai-assistant-codex-manager.manager.js +0 -1113
  369. package/managers/ai-assistant-codex-manager.manager.js.map +0 -1
  370. package/managers/ai-run-evidence.manager.d.ts +0 -36
  371. package/managers/ai-run-evidence.manager.js +0 -377
  372. package/managers/ai-run-evidence.manager.js.map +0 -1
  373. package/managers/communication-metric.manager.d.ts +0 -16
  374. package/managers/communication-metric.manager.js +0 -134
  375. package/managers/communication-metric.manager.js.map +0 -1
  376. package/managers/cron.manager.d.ts +0 -20
  377. package/managers/cron.manager.js +0 -534
  378. package/managers/cron.manager.js.map +0 -1
  379. package/managers/customer-notification-content.manager.d.ts +0 -55
  380. package/managers/customer-notification-content.manager.js +0 -158
  381. package/managers/customer-notification-content.manager.js.map +0 -1
  382. package/managers/diagnostic-manager-bootstrap.d.ts +0 -9
  383. package/managers/diagnostic-manager-bootstrap.js +0 -260
  384. package/managers/diagnostic-manager-bootstrap.js.map +0 -1
  385. package/managers/error-auto-fix.manager.d.ts +0 -149
  386. package/managers/error-auto-fix.manager.js +0 -3064
  387. package/managers/error-auto-fix.manager.js.map +0 -1
  388. package/managers/local-log.manager.d.ts +0 -18
  389. package/managers/local-log.manager.js +0 -88
  390. package/managers/local-log.manager.js.map +0 -1
  391. package/managers/method.manager.d.ts +0 -84
  392. package/managers/method.manager.js +0 -1964
  393. package/managers/method.manager.js.map +0 -1
  394. package/managers/mongo.manager.d.ts +0 -224
  395. package/managers/mongo.manager.js +0 -5000
  396. package/managers/mongo.manager.js.map +0 -1
  397. package/managers/monitor.manager.d.ts +0 -70
  398. package/managers/monitor.manager.js +0 -550
  399. package/managers/monitor.manager.js.map +0 -1
  400. package/managers/openai-usage-ledger.manager.d.ts +0 -30
  401. package/managers/openai-usage-ledger.manager.js +0 -142
  402. package/managers/openai-usage-ledger.manager.js.map +0 -1
  403. package/managers/slow-query-verifier.manager.d.ts +0 -144
  404. package/managers/slow-query-verifier.manager.js +0 -3857
  405. package/managers/slow-query-verifier.manager.js.map +0 -1
  406. package/managers/slow-query.manager.d.ts +0 -28
  407. package/managers/slow-query.manager.js +0 -468
  408. package/managers/slow-query.manager.js.map +0 -1
  409. package/managers/subscription.manager.d.ts +0 -169
  410. package/managers/subscription.manager.js +0 -3434
  411. package/managers/subscription.manager.js.map +0 -1
  412. package/managers/websocket.manager.d.ts +0 -73
  413. package/managers/websocket.manager.js +0 -673
  414. package/managers/websocket.manager.js.map +0 -1
  415. package/managers/worker-dispatcher.manager.d.ts +0 -120
  416. package/managers/worker-dispatcher.manager.js +0 -1266
  417. package/managers/worker-dispatcher.manager.js.map +0 -1
  418. package/managers/worker-server.manager.d.ts +0 -35
  419. package/managers/worker-server.manager.js +0 -582
  420. package/managers/worker-server.manager.js.map +0 -1
  421. package/methods/accounts.d.ts +0 -2
  422. package/methods/accounts.js +0 -624
  423. package/methods/accounts.js.map +0 -1
  424. package/methods/ai-terminal.d.ts +0 -458
  425. package/methods/ai-terminal.js +0 -27991
  426. package/methods/ai-terminal.js.map +0 -1
  427. package/methods/app-settings.d.ts +0 -2
  428. package/methods/app-settings.js +0 -169
  429. package/methods/app-settings.js.map +0 -1
  430. package/methods/aws.d.ts +0 -2
  431. package/methods/aws.js +0 -877
  432. package/methods/aws.js.map +0 -1
  433. package/methods/collections.d.ts +0 -2
  434. package/methods/collections.js +0 -719
  435. package/methods/collections.js.map +0 -1
  436. package/methods/counters.d.ts +0 -2
  437. package/methods/counters.js +0 -113
  438. package/methods/counters.js.map +0 -1
  439. package/methods/cron-jobs.d.ts +0 -2
  440. package/methods/cron-jobs.js +0 -2475
  441. package/methods/cron-jobs.js.map +0 -1
  442. package/methods/customer-notifications.d.ts +0 -2
  443. package/methods/customer-notifications.js +0 -528
  444. package/methods/customer-notifications.js.map +0 -1
  445. package/methods/diagnostics.d.ts +0 -2
  446. package/methods/diagnostics.js +0 -703
  447. package/methods/diagnostics.js.map +0 -1
  448. package/methods/flag-updates.d.ts +0 -2
  449. package/methods/flag-updates.js +0 -8
  450. package/methods/flag-updates.js.map +0 -1
  451. package/methods/flags.d.ts +0 -2
  452. package/methods/flags.js +0 -8
  453. package/methods/flags.js.map +0 -1
  454. package/methods/logs.d.ts +0 -2
  455. package/methods/logs.js +0 -751
  456. package/methods/logs.js.map +0 -1
  457. package/methods/mongo-explorer.d.ts +0 -2
  458. package/methods/mongo-explorer.js +0 -1808
  459. package/methods/mongo-explorer.js.map +0 -1
  460. package/methods/monitor.d.ts +0 -2
  461. package/methods/monitor.js +0 -543
  462. package/methods/monitor.js.map +0 -1
  463. package/methods/pdf.d.ts +0 -2
  464. package/methods/pdf.js +0 -1216
  465. package/methods/pdf.js.map +0 -1
  466. package/methods/publications.d.ts +0 -1
  467. package/methods/publications.js +0 -183
  468. package/methods/publications.js.map +0 -1
  469. package/methods/report-builder.d.ts +0 -2
  470. package/methods/report-builder.js +0 -3094
  471. package/methods/report-builder.js.map +0 -1
  472. package/methods/support.d.ts +0 -2
  473. package/methods/support.js +0 -430
  474. package/methods/support.js.map +0 -1
  475. package/models/ai-run.model.d.ts +0 -19
  476. package/models/ai-run.model.js +0 -4
  477. package/models/ai-run.model.js.map +0 -1
  478. package/models/ai-terminal-conversation.model.d.ts +0 -17
  479. package/models/ai-terminal-conversation.model.js +0 -4
  480. package/models/ai-terminal-conversation.model.js.map +0 -1
  481. package/models/ai-terminal-issue-report.model.d.ts +0 -19
  482. package/models/ai-terminal-issue-report.model.js +0 -4
  483. package/models/ai-terminal-issue-report.model.js.map +0 -1
  484. package/models/ai-terminal-message.model.d.ts +0 -22
  485. package/models/ai-terminal-message.model.js +0 -4
  486. package/models/ai-terminal-message.model.js.map +0 -1
  487. package/models/app-setting.model.d.ts +0 -16
  488. package/models/app-setting.model.js +0 -4
  489. package/models/app-setting.model.js.map +0 -1
  490. package/models/app-status.model.js +0 -4
  491. package/models/app-status.model.js.map +0 -1
  492. package/models/billing-logged-in-users.model.js +0 -4
  493. package/models/billing-logged-in-users.model.js.map +0 -1
  494. package/models/collection-document.model.d.ts +0 -21
  495. package/models/collection-document.model.js +0 -4
  496. package/models/collection-document.model.js.map +0 -1
  497. package/models/communication-metric.model.d.ts +0 -20
  498. package/models/communication-metric.model.js +0 -4
  499. package/models/communication-metric.model.js.map +0 -1
  500. package/models/counter.model.js +0 -4
  501. package/models/counter.model.js.map +0 -1
  502. package/models/cron-job-history.model.d.ts +0 -15
  503. package/models/cron-job-history.model.js +0 -4
  504. package/models/cron-job-history.model.js.map +0 -1
  505. package/models/cron-job.model.d.ts +0 -14
  506. package/models/cron-job.model.js +0 -4
  507. package/models/cron-job.model.js.map +0 -1
  508. package/models/customer-notification.model.d.ts +0 -26
  509. package/models/customer-notification.model.js +0 -4
  510. package/models/customer-notification.model.js.map +0 -1
  511. package/models/customer-portal-password.model.d.ts +0 -11
  512. package/models/customer-portal-password.model.js +0 -4
  513. package/models/customer-portal-password.model.js.map +0 -1
  514. package/models/dialog.model.d.ts +0 -23
  515. package/models/dialog.model.js +0 -4
  516. package/models/dialog.model.js.map +0 -1
  517. package/models/email-history.model.d.ts +0 -32
  518. package/models/email-history.model.js.map +0 -1
  519. package/models/email-verified.model.js +0 -4
  520. package/models/email-verified.model.js.map +0 -1
  521. package/models/file.model.js +0 -4
  522. package/models/file.model.js.map +0 -1
  523. package/models/flag-update.model.js +0 -4
  524. package/models/flag-update.model.js.map +0 -1
  525. package/models/flag.model.js +0 -4
  526. package/models/flag.model.js.map +0 -1
  527. package/models/log-method-latency.model.d.ts +0 -10
  528. package/models/log-method-latency.model.js +0 -4
  529. package/models/log-method-latency.model.js.map +0 -1
  530. package/models/log-subscription.model.js +0 -4
  531. package/models/log-subscription.model.js.map +0 -1
  532. package/models/log.model.d.ts +0 -17
  533. package/models/log.model.js +0 -4
  534. package/models/log.model.js.map +0 -1
  535. package/models/logged-in-users.model.js +0 -4
  536. package/models/logged-in-users.model.js.map +0 -1
  537. package/models/method-response.model.js +0 -4
  538. package/models/method-response.model.js.map +0 -1
  539. package/models/method.model.d.ts +0 -26
  540. package/models/method.model.js +0 -4
  541. package/models/method.model.js.map +0 -1
  542. package/models/monitor-cpu.model.js +0 -4
  543. package/models/monitor-cpu.model.js.map +0 -1
  544. package/models/monitor-function.model.d.ts +0 -14
  545. package/models/monitor-function.model.js +0 -4
  546. package/models/monitor-function.model.js.map +0 -1
  547. package/models/monitor-memory.model.d.ts +0 -15
  548. package/models/monitor-memory.model.js +0 -4
  549. package/models/monitor-memory.model.js.map +0 -1
  550. package/models/monitor-mongo.model.d.ts +0 -13
  551. package/models/monitor-mongo.model.js +0 -4
  552. package/models/monitor-mongo.model.js.map +0 -1
  553. package/models/notification.model.js +0 -4
  554. package/models/notification.model.js.map +0 -1
  555. package/models/openai-usage-ledger.model.d.ts +0 -30
  556. package/models/openai-usage-ledger.model.js +0 -4
  557. package/models/openai-usage-ledger.model.js.map +0 -1
  558. package/models/pagination.model.d.ts +0 -11
  559. package/models/pagination.model.js +0 -28
  560. package/models/pagination.model.js.map +0 -1
  561. package/models/permission.model.d.ts +0 -12
  562. package/models/permission.model.js +0 -4
  563. package/models/permission.model.js.map +0 -1
  564. package/models/report-builder-dashboard-builder.model.d.ts +0 -25
  565. package/models/report-builder-dashboard-builder.model.js +0 -4
  566. package/models/report-builder-dashboard-builder.model.js.map +0 -1
  567. package/models/report-builder-library.model.d.ts +0 -17
  568. package/models/report-builder-library.model.js +0 -4
  569. package/models/report-builder-library.model.js.map +0 -1
  570. package/models/report-builder-report.model.d.ts +0 -121
  571. package/models/report-builder-report.model.js +0 -4
  572. package/models/report-builder-report.model.js.map +0 -1
  573. package/models/report-builder.model.d.ts +0 -61
  574. package/models/report-builder.model.js +0 -4
  575. package/models/report-builder.model.js.map +0 -1
  576. package/models/select-data-label.model.d.ts +0 -9
  577. package/models/select-data-label.model.js +0 -4
  578. package/models/select-data-label.model.js.map +0 -1
  579. package/models/server-message.model.d.ts +0 -32
  580. package/models/server-message.model.js +0 -4
  581. package/models/server-message.model.js.map +0 -1
  582. package/models/slow-query-report.model.d.ts +0 -23
  583. package/models/slow-query-report.model.js +0 -4
  584. package/models/slow-query-report.model.js.map +0 -1
  585. package/models/subscription.model.d.ts +0 -31
  586. package/models/subscription.model.js +0 -4
  587. package/models/subscription.model.js.map +0 -1
  588. package/models/support-ticket.model.d.ts +0 -87
  589. package/models/support-ticket.model.js +0 -4
  590. package/models/support-ticket.model.js.map +0 -1
  591. package/models/user-group.model.d.ts +0 -20
  592. package/models/user-group.model.js +0 -4
  593. package/models/user-group.model.js.map +0 -1
  594. package/models/user-guide.model.js +0 -4
  595. package/models/user-guide.model.js.map +0 -1
  596. package/models/user.model.d.ts +0 -84
  597. package/models/user.model.js +0 -4
  598. package/models/user.model.js.map +0 -1
  599. package/private/images/ResolveIO.png +0 -0
  600. package/public_api.js +0 -127
  601. package/public_api.js.map +0 -1
  602. package/publications/ai-terminal.d.ts +0 -1
  603. package/publications/ai-terminal.js +0 -122
  604. package/publications/ai-terminal.js.map +0 -1
  605. package/publications/app-settings.d.ts +0 -2
  606. package/publications/app-settings.js +0 -28
  607. package/publications/app-settings.js.map +0 -1
  608. package/publications/app-status.d.ts +0 -2
  609. package/publications/app-status.js +0 -16
  610. package/publications/app-status.js.map +0 -1
  611. package/publications/cron-jobs.d.ts +0 -2
  612. package/publications/cron-jobs.js +0 -88
  613. package/publications/cron-jobs.js.map +0 -1
  614. package/publications/customer-notifications.d.ts +0 -2
  615. package/publications/customer-notifications.js +0 -161
  616. package/publications/customer-notifications.js.map +0 -1
  617. package/publications/files.d.ts +0 -2
  618. package/publications/files.js +0 -36
  619. package/publications/files.js.map +0 -1
  620. package/publications/flags-update.d.ts +0 -2
  621. package/publications/flags-update.js +0 -22
  622. package/publications/flags-update.js.map +0 -1
  623. package/publications/flags.d.ts +0 -2
  624. package/publications/flags.js +0 -22
  625. package/publications/flags.js.map +0 -1
  626. package/publications/logs.d.ts +0 -2
  627. package/publications/logs.js +0 -164
  628. package/publications/logs.js.map +0 -1
  629. package/publications/notifications.d.ts +0 -2
  630. package/publications/notifications.js +0 -16
  631. package/publications/notifications.js.map +0 -1
  632. package/publications/report-builder-dashboard-builders.d.ts +0 -2
  633. package/publications/report-builder-dashboard-builders.js +0 -42
  634. package/publications/report-builder-dashboard-builders.js.map +0 -1
  635. package/publications/report-builder-libraries.d.ts +0 -2
  636. package/publications/report-builder-libraries.js +0 -90
  637. package/publications/report-builder-libraries.js.map +0 -1
  638. package/publications/report-builder-reports.d.ts +0 -2
  639. package/publications/report-builder-reports.js +0 -50
  640. package/publications/report-builder-reports.js.map +0 -1
  641. package/publications/super-admin.d.ts +0 -2
  642. package/publications/super-admin.js +0 -16
  643. package/publications/super-admin.js.map +0 -1
  644. package/publications/user-groups.d.ts +0 -1
  645. package/publications/user-groups.js +0 -16
  646. package/publications/user-groups.js.map +0 -1
  647. package/publications/user-guides.d.ts +0 -1
  648. package/publications/user-guides.js +0 -16
  649. package/publications/user-guides.js.map +0 -1
  650. package/resolveio-server-app.d.ts +0 -70
  651. package/resolveio-server-app.js +0 -801
  652. package/resolveio-server-app.js.map +0 -1
  653. package/server-app.d.ts +0 -228
  654. package/server-app.js +0 -3566
  655. package/server-app.js.map +0 -1
  656. package/services/codex-client.d.ts +0 -128
  657. package/services/codex-client.js +0 -1629
  658. package/services/codex-client.js.map +0 -1
  659. package/services/openai-client.d.ts +0 -46
  660. package/services/openai-client.js +0 -318
  661. package/services/openai-client.js.map +0 -1
  662. package/types/error-report.d.ts +0 -25
  663. package/types/error-report.js +0 -4
  664. package/types/error-report.js.map +0 -1
  665. package/types/slow-query-report.d.ts +0 -27
  666. package/types/slow-query-report.js +0 -6
  667. package/types/slow-query-report.js.map +0 -1
  668. package/util/ai-qa-policy.d.ts +0 -124
  669. package/util/ai-qa-policy.js +0 -736
  670. package/util/ai-qa-policy.js.map +0 -1
  671. package/util/ai-run-evidence-adapters.d.ts +0 -109
  672. package/util/ai-run-evidence-adapters.js +0 -7234
  673. package/util/ai-run-evidence-adapters.js.map +0 -1
  674. package/util/ai-run-evidence-dashboard.d.ts +0 -88
  675. package/util/ai-run-evidence-dashboard.js +0 -343
  676. package/util/ai-run-evidence-dashboard.js.map +0 -1
  677. package/util/ai-run-evidence-eval.d.ts +0 -86
  678. package/util/ai-run-evidence-eval.js +0 -1018
  679. package/util/ai-run-evidence-eval.js.map +0 -1
  680. package/util/ai-run-evidence.d.ts +0 -244
  681. package/util/ai-run-evidence.js +0 -1096
  682. package/util/ai-run-evidence.js.map +0 -1
  683. package/util/ai-runner-artifacts.d.ts +0 -82
  684. package/util/ai-runner-artifacts.js +0 -713
  685. package/util/ai-runner-artifacts.js.map +0 -1
  686. package/util/ai-runner-manager-autopilot.d.ts +0 -210
  687. package/util/ai-runner-manager-autopilot.js +0 -642
  688. package/util/ai-runner-manager-autopilot.js.map +0 -1
  689. package/util/ai-runner-manager-policy.d.ts +0 -807
  690. package/util/ai-runner-manager-policy.js +0 -3501
  691. package/util/ai-runner-manager-policy.js.map +0 -1
  692. package/util/ai-runner-qa-auth.d.ts +0 -5
  693. package/util/ai-runner-qa-auth.js +0 -839
  694. package/util/ai-runner-qa-auth.js.map +0 -1
  695. package/util/ai-runner-qa-tools.d.ts +0 -26
  696. package/util/ai-runner-qa-tools.js +0 -3520
  697. package/util/ai-runner-qa-tools.js.map +0 -1
  698. package/util/aicoder-runner-v6.d.ts +0 -426
  699. package/util/aicoder-runner-v6.js +0 -2464
  700. package/util/aicoder-runner-v6.js.map +0 -1
  701. package/util/common.d.ts +0 -31
  702. package/util/common.js +0 -683
  703. package/util/common.js.map +0 -1
  704. package/util/customer-portal-password.d.ts +0 -13
  705. package/util/customer-portal-password.js +0 -209
  706. package/util/customer-portal-password.js.map +0 -1
  707. package/util/error-reporter.d.ts +0 -52
  708. package/util/error-reporter.js +0 -326
  709. package/util/error-reporter.js.map +0 -1
  710. package/util/error-tracking.d.ts +0 -13
  711. package/util/error-tracking.js +0 -120
  712. package/util/error-tracking.js.map +0 -1
  713. package/util/openai-usage-cost.d.ts +0 -6
  714. package/util/openai-usage-cost.js +0 -103
  715. package/util/openai-usage-cost.js.map +0 -1
  716. package/util/report-builder-unwinds.d.ts +0 -15
  717. package/util/report-builder-unwinds.js +0 -156
  718. package/util/report-builder-unwinds.js.map +0 -1
  719. package/util/runner-process-janitor.d.ts +0 -27
  720. package/util/runner-process-janitor.js +0 -208
  721. package/util/runner-process-janitor.js.map +0 -1
  722. package/util/schema-report-builder.d.ts +0 -6
  723. package/util/schema-report-builder.js +0 -481
  724. package/util/schema-report-builder.js.map +0 -1
  725. package/util/slow-query-reporter.d.ts +0 -28
  726. package/util/slow-query-reporter.js +0 -226
  727. package/util/slow-query-reporter.js.map +0 -1
  728. package/util/subscription-dependency-context.d.ts +0 -34
  729. package/util/subscription-dependency-context.js +0 -1283
  730. package/util/subscription-dependency-context.js.map +0 -1
  731. package/util/support-runner-v5.d.ts +0 -1426
  732. package/util/support-runner-v5.js +0 -7631
  733. package/util/support-runner-v5.js.map +0 -1
  734. package/util/tokenizer.d.ts +0 -5
  735. package/util/tokenizer.js +0 -41
  736. package/util/tokenizer.js.map +0 -1
  737. package/workers/codex-runner.worker.d.ts +0 -1
  738. package/workers/codex-runner.worker.js +0 -192
  739. package/workers/codex-runner.worker.js.map +0 -1
  740. /package/{private → src/private}/email-templates/enrollment.html +0 -0
  741. /package/{private → src/private}/email-templates/forgot-password.html +0 -0
  742. /package/{private → src/private}/email-templates/support-ticket-deleted.html +0 -0
  743. /package/{private → src/private}/email-templates/support-ticket-modified.html +0 -0
  744. /package/{private → src/private}/email-templates/support-ticket.html +0 -0
  745. /package/{public_api.d.ts → src/public_api.ts} +0 -0
@@ -1,3434 +0,0 @@
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