@futdevpro/nts-dynamo 1.15.74 → 1.15.78

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 (366) hide show
  1. package/.c8rc.json +26 -26
  2. package/.copilot/patterns.json +7 -7
  3. package/.cursor/rules/__assistant_guide.mdc +30 -30
  4. package/.cursor/rules/_ag_backend-structure.mdc +85 -85
  5. package/.cursor/rules/_ag_backend.mdc +16 -16
  6. package/.cursor/rules/_ag_frontend-structure.mdc +86 -86
  7. package/.cursor/rules/_ag_frontend.mdc +39 -39
  8. package/.cursor/rules/_ag_import-rules.mdc +44 -44
  9. package/.cursor/rules/_ag_naming.mdc +115 -115
  10. package/.cursor/rules/_ag_should-be.mdc +6 -6
  11. package/.cursor/rules/ai_development_guide.md +60 -60
  12. package/.cursor/rules/cursor-rules.md +160 -160
  13. package/.cursor/rules/default-command.mdc +464 -464
  14. package/.cursor/rules/error_code_pattern.md +39 -39
  15. package/.cursor/rules/saved rule mcp server use.md +15 -15
  16. package/.dynamo/logs/cicd-pipeline/output.log +2783 -0
  17. package/.dynamo/logs/cicd-pipeline/status.json +94 -0
  18. package/.vscode/settings.json +10 -10
  19. package/HOWTO.md +15 -15
  20. package/LICENSE +21 -21
  21. package/__documentations/nts-integration-tests-2026-03-17.md +26 -26
  22. package/__documentations/plans/BEDROCK-HYPERPLAN.md +95 -95
  23. package/_specifications/BACKLOG.md +92 -92
  24. package/_specifications/TODO.md +15 -15
  25. package/_specifications/agent.md +138 -138
  26. package/build/_services/core/global.service.d.ts +7 -0
  27. package/build/_services/core/global.service.d.ts.map +1 -1
  28. package/build/_services/core/global.service.js +10 -0
  29. package/build/_services/core/global.service.js.map +1 -1
  30. package/build/_services/server/app.server.d.ts +45 -0
  31. package/build/_services/server/app.server.d.ts.map +1 -1
  32. package/build/_services/server/app.server.js +85 -173
  33. package/build/_services/server/app.server.js.map +1 -1
  34. package/eslint.config.js +3 -3
  35. package/nodemon.json +24 -24
  36. package/package.json +2 -2
  37. package/pnpm-workspace.yaml +5 -5
  38. package/scripts/run-coverage-tests.js +28 -28
  39. package/spec/support/helpers/spec-reporter-loader.js +359 -359
  40. package/spec/support/helpers/ts-node-helper.js +93 -93
  41. package/spec/support/jasmine.coverage.json +24 -24
  42. package/spec/support/jasmine.json +24 -24
  43. package/src/_collections/archive.util.spec.ts +57 -57
  44. package/src/_collections/archive.util.ts +18 -18
  45. package/src/_collections/atlas-default-db-options.const.ts +9 -9
  46. package/src/_collections/default-fallback-cache-max-age.const.spec.ts +11 -11
  47. package/src/_collections/default-fallback-cache-max-age.const.ts +2 -2
  48. package/src/_collections/default-not-found-page.const.spec.ts +19 -19
  49. package/src/_collections/default-not-found-page.const.ts +22 -22
  50. package/src/_collections/default-socket-path.const.spec.ts +12 -12
  51. package/src/_collections/default-socket-path.const.ts +2 -2
  52. package/src/_collections/get-environment-settings.util.spec.ts +210 -210
  53. package/src/_collections/get-environment-settings.util.ts +48 -48
  54. package/src/_collections/global-settings.const.ts +97 -97
  55. package/src/_collections/sample.env +21 -21
  56. package/src/_collections/star.controller.spec.ts +224 -224
  57. package/src/_collections/star.controller.ts +129 -129
  58. package/src/_enums/data-model-type.enum.ts +14 -14
  59. package/src/_enums/data-service-function.enum.ts +24 -24
  60. package/src/_enums/predefined-data-types.enum.ts +16 -16
  61. package/src/_enums/route-security.enum.ts +12 -12
  62. package/src/_models/control-models/api-call-params.control-model.spec.ts +152 -152
  63. package/src/_models/control-models/api-call-params.control-model.ts +142 -142
  64. package/src/_models/control-models/app-ext-system-controls.control-model.spec.ts +52 -52
  65. package/src/_models/control-models/app-ext-system-controls.control-model.ts +9 -9
  66. package/src/_models/control-models/app-params.control-model.spec.ts +225 -225
  67. package/src/_models/control-models/app-params.control-model.ts +136 -136
  68. package/src/_models/control-models/app-system-controls.control-model.spec.ts +31 -31
  69. package/src/_models/control-models/app-system-controls.control-model.ts +9 -9
  70. package/src/_models/control-models/endpoint-params.control-model.spec.ts +627 -627
  71. package/src/_models/control-models/endpoint-params.control-model.ts +627 -627
  72. package/src/_models/control-models/http-settings.control-model.spec.ts +77 -77
  73. package/src/_models/control-models/http-settings.control-model.ts +37 -37
  74. package/src/_models/control-models/system-control.control-model.spec.ts +27 -27
  75. package/src/_models/control-models/system-control.control-model.ts +12 -12
  76. package/src/_models/interfaces/certification-settings.interface.ts +7 -7
  77. package/src/_models/interfaces/environment-settings.interface.ts +59 -59
  78. package/src/_models/interfaces/global-log-settings.interface.ts +171 -171
  79. package/src/_models/interfaces/global-service-settings.interface.ts +47 -47
  80. package/src/_models/interfaces/global-settings.interface.ts +216 -216
  81. package/src/_models/interfaces/routing-module-settings.interface.ts +21 -21
  82. package/src/_models/interfaces/static-client-settings.interface.spec.ts +29 -29
  83. package/src/_models/interfaces/static-client-settings.interface.ts +28 -28
  84. package/src/_models/types/db-update.type.ts +100 -100
  85. package/src/_modules/ai/_models/ai-input-interfaces.ts +117 -117
  86. package/src/_modules/ai/_models/ai-test-generation-result.interface.ts +16 -16
  87. package/src/_modules/ai/_modules/anthropic/_services/aai-user-key.control-service.ts +138 -138
  88. package/src/_modules/ai/_modules/anthropic/index.ts +5 -5
  89. package/src/_modules/ai/_modules/document-ai/_collections/dai-chunking.util.spec.ts +242 -242
  90. package/src/_modules/ai/_modules/document-ai/_collections/dai-chunking.util.ts +639 -639
  91. package/src/_modules/ai/_modules/document-ai/_collections/dai-code-chunking.util.spec.ts +295 -295
  92. package/src/_modules/ai/_modules/document-ai/_collections/dai-code-chunking.util.ts +518 -518
  93. package/src/_modules/ai/_modules/document-ai/_collections/dai-document.util.spec.ts +209 -209
  94. package/src/_modules/ai/_modules/document-ai/_collections/dai-document.util.ts +85 -85
  95. package/src/_modules/ai/_modules/document-ai/_enums/dai-compare-result-type.enum.ts +7 -7
  96. package/src/_modules/ai/_modules/document-ai/_models/data-models/dai-doc-chunk.data-model.ts +146 -146
  97. package/src/_modules/ai/_modules/document-ai/_models/data-models/dai-doc-page.data-model.ts +162 -162
  98. package/src/_modules/ai/_modules/document-ai/_models/data-models/dai-document.data-model.ts +99 -99
  99. package/src/_modules/ai/_modules/document-ai/_models/interfaces/dai-code-chunk.interface.ts +68 -68
  100. package/src/_modules/ai/_modules/document-ai/_models/interfaces/dai-doc-chunk-compare-result.interface.ts +18 -18
  101. package/src/_modules/ai/_modules/document-ai/_models/interfaces/dai-doc-page-compare-result.interface.ts +19 -19
  102. package/src/_modules/ai/_modules/document-ai/_models/interfaces/dai-document-compare-result.interface.ts +25 -25
  103. package/src/_modules/ai/_modules/document-ai/index.ts +30 -30
  104. package/src/_modules/ai/_modules/fdp-ai/_services/fdpai-user-key.control-service.ts +189 -189
  105. package/src/_modules/ai/_modules/fdp-ai/index.ts +5 -5
  106. package/src/_modules/ai/_modules/open-ai/_collections/oai-global-settings.const.ts +9 -9
  107. package/src/_modules/ai/_modules/open-ai/_collections/oai-llm-predefined-requests-hu.conts.ts +82 -82
  108. package/src/_modules/ai/_modules/open-ai/_collections/oai-llm-predefined-requests.conts.ts +75 -75
  109. package/src/_modules/ai/_modules/open-ai/_enums/oai-gpt-message-role.enum.ts +45 -45
  110. package/src/_modules/ai/_modules/open-ai/_models/interfaces/oai-global-settings.interface.ts +7 -7
  111. package/src/_modules/ai/_modules/open-ai/_models/interfaces/oai-gpt-message.interface.ts +7 -7
  112. package/src/_modules/ai/_modules/open-ai/_models/interfaces/oai-llm-predefined-requests.interface.ts +57 -57
  113. package/src/_modules/ai/_modules/open-ai/_services/data-services/oai-doc-chunk-data.service.ts +292 -292
  114. package/src/_modules/ai/_modules/open-ai/_services/data-services/oai-document.data-service.spec.ts +342 -342
  115. package/src/_modules/ai/_modules/open-ai/_services/data-services/oai-vector-data.service.spec.ts +550 -550
  116. package/src/_modules/ai/_modules/open-ai/_services/data-services/oai-vector-data.service.ts +630 -630
  117. package/src/_modules/ai/_modules/open-ai/_services/oai-embedding.control-service.spec.ts +332 -332
  118. package/src/_modules/ai/_modules/open-ai/_services/oai-llm-chat.service-base.spec.ts +462 -462
  119. package/src/_modules/ai/_modules/open-ai/_services/oai-llm-chat.service-base.ts +634 -634
  120. package/src/_modules/ai/_modules/open-ai/_services/oai-llm.service-base.spec.ts +489 -489
  121. package/src/_modules/ai/_modules/open-ai/_services/oai-llm.service-base.tools.spec.ts +173 -173
  122. package/src/_modules/ai/_modules/open-ai/_services/oai-llm.service-base.ts +1033 -1033
  123. package/src/_modules/ai/_modules/open-ai/_services/oai-user-key.control-service.ts +157 -157
  124. package/src/_modules/ai/_services/ai-embedding-mock.service.spec.ts +115 -115
  125. package/src/_modules/ai/_services/ai-embedding-mock.service.ts +212 -212
  126. package/src/_modules/ai/_services/ai-embedding-provider.registry.spec.ts +110 -110
  127. package/src/_modules/ai/_services/ai-embedding-provider.registry.ts +110 -110
  128. package/src/_modules/ai/_services/ai-embedding.service-base.spec.ts +98 -98
  129. package/src/_modules/ai/_services/ai-embedding.service-base.ts +48 -48
  130. package/src/_modules/ai/_services/ai-llm-chat.service-base.spec.ts +229 -229
  131. package/src/_modules/ai/_services/ai-llm-chat.service-base.ts +68 -68
  132. package/src/_modules/ai/_services/ai-llm.service-base.spec.ts +250 -250
  133. package/src/_modules/ai/_services/ai-llm.service-base.ts +519 -519
  134. package/src/_modules/ai/_services/ai-provider.service-base.spec.ts +158 -158
  135. package/src/_modules/ai/_services/ai-user-key.service-base.ts +59 -59
  136. package/src/_modules/ai/_services/lmstudio-embedding.control-service.spec.ts +197 -197
  137. package/src/_modules/ai/_services/lmstudio-embedding.control-service.ts +371 -371
  138. package/src/_modules/ai/index.ts +23 -23
  139. package/src/_modules/assistant/_collections/ass-global-settings.const.ts +13 -13
  140. package/src/_modules/assistant/_collections/ass.util.spec.ts +176 -176
  141. package/src/_modules/assistant/_collections/ass.util.ts +50 -50
  142. package/src/_modules/assistant/_models/ass-global-settings.interface.ts +15 -15
  143. package/src/_modules/assistant/_services/ass-io.control-service.spec.ts +140 -140
  144. package/src/_modules/assistant/_services/ass-main.control-service.spec.ts +192 -192
  145. package/src/_modules/assistant/_services/ass-main.control-service.ts +107 -107
  146. package/src/_modules/bot/_collections/bot-default-commands.const.ts +12 -12
  147. package/src/_modules/bot/_collections/bot-global-settings.const.ts +39 -39
  148. package/src/_modules/bot/_models/bot-channel-wrapper.interface.ts +62 -62
  149. package/src/_modules/bot/_models/bot-command.interface.ts +8 -8
  150. package/src/_modules/bot/_models/bot-global-settings.interface.ts +96 -96
  151. package/src/_modules/bot/_models/bot-last-mention-date.interface.ts +6 -6
  152. package/src/_modules/bot/_models/bot-last-message-date.interface.ts +5 -5
  153. package/src/_modules/bot/_models/bot-user-wrapper.interface.ts +41 -41
  154. package/src/_modules/bot/_modules/discord-bot/_models/dib-platform.types.ts +9 -9
  155. package/src/_modules/bot/_modules/discord-bot/_services/dib-messaging-provider.control-service.spec.ts +431 -431
  156. package/src/_modules/bot/_modules/dynamo-bot/_collections/dyb-operations.util.spec.ts +160 -160
  157. package/src/_modules/bot/_modules/dynamo-bot/_collections/dyb-operations.util.ts +55 -55
  158. package/src/_modules/bot/_modules/dynamo-bot/_models/dyb-platform.types.ts +15 -15
  159. package/src/_modules/bot/_modules/dynamo-bot/_services/dyb-messaging-provider.control-service.spec.ts +374 -374
  160. package/src/_modules/bot/_modules/dynamo-bot/_services/dyb-messaging-provider.control-service.ts +447 -447
  161. package/src/_modules/bot/_modules/dynamo-bot/index.ts +15 -15
  162. package/src/_modules/bot/_modules/slack-bot/_models/slb-platform.types.ts +9 -9
  163. package/src/_modules/bot/_modules/slack-bot/_services/slb-messaging-provider.control-service.spec.ts +344 -344
  164. package/src/_modules/bot/_modules/slack-bot/_services/slb-messaging-provider.control-service.ts +197 -197
  165. package/src/_modules/bot/_modules/teams-bot/_models/teb-platform.types.ts +9 -9
  166. package/src/_modules/bot/_modules/teams-bot/_services/teb-messaging-provider.control-service.spec.ts +345 -345
  167. package/src/_modules/bot/_modules/teams-bot/_services/teb-messaging-provider.control-service.ts +197 -197
  168. package/src/_modules/bot/_services/bot-commands.control-service.spec.ts +116 -116
  169. package/src/_modules/bot/_services/bot-io.control-service.spec.ts +285 -285
  170. package/src/_modules/bot/_services/bot-main.control-service.spec.ts +208 -208
  171. package/src/_modules/bot/_services/bot-messaging-provider.service-base.spec.ts +349 -349
  172. package/src/_modules/bot/_services/bot-routines.control-service.spec.ts +111 -111
  173. package/src/_modules/custom-data/custom-data.controller.spec.ts +49 -49
  174. package/src/_modules/custom-data/custom-data.controller.ts +67 -67
  175. package/src/_modules/custom-data/custom-data.data-service.spec.ts +54 -54
  176. package/src/_modules/custom-data/custom-data.data-service.ts +21 -21
  177. package/src/_modules/custom-data/get-custom-data-routing-module.util.spec.ts +28 -28
  178. package/src/_modules/custom-data/get-custom-data-routing-module.util.ts +24 -24
  179. package/src/_modules/custom-data/index.ts +9 -9
  180. package/src/_modules/data-readers/_collections/dynts-sqlite-reader.util.spec.ts +161 -161
  181. package/src/_modules/data-readers/_collections/dynts-sqlite-reader.util.ts +203 -203
  182. package/src/_modules/data-readers/_models/interfaces/dynts-sqlite-reader.interface.ts +33 -33
  183. package/src/_modules/data-readers/index.ts +11 -11
  184. package/src/_modules/defaults/_collections/default-endpoints.util.ts +487 -487
  185. package/src/_modules/defaults/_models/default-user.data-model.ts +72 -72
  186. package/src/_modules/defaults/_services/default-auth.service.spec.ts +269 -269
  187. package/src/_modules/defaults/_services/default-auth.service.ts +177 -177
  188. package/src/_modules/defaults/_services/default-socket-events.service.spec.ts +42 -42
  189. package/src/_modules/defaults/_services/default-socket-events.service.ts +61 -61
  190. package/src/_modules/defaults/_services/default-user.data-service.spec.ts +187 -187
  191. package/src/_modules/defaults/_services/default-user.data-service.ts +98 -98
  192. package/src/_modules/defaults/index.ts +17 -17
  193. package/src/_modules/discord-assistant/_collections/dias-global-settings.const.ts +19 -19
  194. package/src/_modules/discord-assistant/_collections/dias.util.spec.ts +366 -366
  195. package/src/_modules/discord-assistant/_collections/dias.util.ts +132 -132
  196. package/src/_modules/discord-assistant/_models/dias-global-settings.interface.ts +19 -19
  197. package/src/_modules/discord-assistant/_models/dias-knowledge.data-model.ts +52 -52
  198. package/src/_modules/discord-assistant/_services/dias-chunk.data-service.ts +177 -177
  199. package/src/_modules/discord-assistant/_services/dias-io.control-service.spec.ts +108 -108
  200. package/src/_modules/discord-assistant/_services/dias-io.control-service.ts +69 -69
  201. package/src/_modules/discord-assistant/_services/dias-main.control-service.spec.ts +22 -22
  202. package/src/_modules/discord-assistant/_services/dias-main.control-service.ts +27 -27
  203. package/src/_modules/discord-assistant/_services/dias.service-base.spec.ts +195 -195
  204. package/src/_modules/discord-assistant/_services/dias.service-base.ts +76 -76
  205. package/src/_modules/discord-assistant/index.ts +38 -38
  206. package/src/_modules/discord-assistant-voiced/_services/dias-discord-bot.control-service.spec.ts +34 -34
  207. package/src/_modules/discord-assistant-voiced/_services/dias-discord-bot.control-service.ts +11 -11
  208. package/src/_modules/discord-assistant-voiced/index.ts +36 -36
  209. package/src/_modules/discord-bot/_collections/dibo-default-commands.const.ts +16 -16
  210. package/src/_modules/discord-bot/_collections/dibo-global-settings.conts.ts +55 -55
  211. package/src/_modules/discord-bot/_collections/dibo-operations.util.spec.ts +214 -214
  212. package/src/_modules/discord-bot/_collections/dibo-operations.util.ts +387 -387
  213. package/src/_modules/discord-bot/_models/dibo-command.interface.ts +12 -12
  214. package/src/_modules/discord-bot/_models/dibo-global-settings.interface.ts +98 -98
  215. package/src/_modules/discord-bot/_models/dibo-last-mention-date.inteface.ts +7 -7
  216. package/src/_modules/discord-bot/_models/dibo-last-message-date.interface.ts +6 -6
  217. package/src/_modules/discord-bot/_services/dibo-commands.control-service.spec.ts +154 -154
  218. package/src/_modules/discord-bot/_services/dibo-commands.control-service.ts +153 -153
  219. package/src/_modules/discord-bot/_services/dibo-io.control-service.spec.ts +264 -264
  220. package/src/_modules/discord-bot/_services/dibo-io.control-service.ts +306 -306
  221. package/src/_modules/discord-bot/_services/dibo-main.control-service.spec.ts +408 -408
  222. package/src/_modules/discord-bot/_services/dibo-main.control-service.ts +487 -487
  223. package/src/_modules/discord-bot/_services/dibo-routines.control-service.spec.ts +105 -105
  224. package/src/_modules/discord-bot/index.ts +36 -36
  225. package/src/_modules/local-vector-search/_enums/lvs-search-mode.enum.ts +35 -35
  226. package/src/_modules/local-vector-search/_models/data-models/lvs-vector-persist.data-model.ts +59 -59
  227. package/src/_modules/local-vector-search/_models/lvs-search-result.interface.ts +17 -17
  228. package/src/_modules/local-vector-search/_services/lvs-doc-chunk-data.service.spec.ts +418 -418
  229. package/src/_modules/local-vector-search/_services/lvs-doc-chunk-data.service.ts +276 -276
  230. package/src/_modules/local-vector-search/_services/lvs-local-vector-search.data-service.spec.ts +480 -480
  231. package/src/_modules/local-vector-search/_services/lvs-local-vector-search.data-service.ts +416 -416
  232. package/src/_modules/local-vector-search/_services/lvs-persistent-vector-pool.control-service.spec.ts +198 -198
  233. package/src/_modules/local-vector-search/_services/lvs-persistent-vector-pool.control-service.ts +146 -146
  234. package/src/_modules/local-vector-search/_services/lvs-vector-persist.data-service.spec.ts +167 -167
  235. package/src/_modules/local-vector-search/_services/lvs-vector-persist.data-service.ts +106 -106
  236. package/src/_modules/local-vector-search/_services/lvs-vector-pool.control-service.spec.ts +507 -507
  237. package/src/_modules/local-vector-search/_services/lvs-vector-pool.control-service.ts +272 -272
  238. package/src/_modules/local-vector-search/index.ts +16 -16
  239. package/src/_modules/logs/index.ts +11 -11
  240. package/src/_modules/mcp/_models/interfaces/dynts-mcp.interface.ts +111 -111
  241. package/src/_modules/mcp/_services/dynts-mcp-server.service-base.spec.ts +142 -142
  242. package/src/_modules/mcp/_services/dynts-mcp-server.service-base.ts +120 -120
  243. package/src/_modules/mcp/_services/dynts-mcp.adapter.ts +168 -168
  244. package/src/_modules/mcp/index.ts +13 -13
  245. package/src/_modules/messaging/README.md +354 -354
  246. package/src/_modules/messaging/_collections/get-messaging-routing-module.util.ts +26 -26
  247. package/src/_modules/messaging/_collections/msg-global-settings.const.ts +22 -22
  248. package/src/_modules/messaging/_collections/msg.util.spec.ts +226 -226
  249. package/src/_modules/messaging/_models/msg-global-settings.interface.ts +37 -37
  250. package/src/_modules/messaging/_services/msg-conversation.data-service.ts +146 -146
  251. package/src/_modules/messaging/_services/msg-events.service.spec.ts +219 -219
  252. package/src/_modules/messaging/_services/msg-events.service.ts +267 -267
  253. package/src/_modules/messaging/_services/msg-integration.control-service.ts +179 -179
  254. package/src/_modules/messaging/_services/msg-main.control-service.spec.ts +147 -147
  255. package/src/_modules/messaging/_services/msg-main.control-service.ts +571 -571
  256. package/src/_modules/messaging/_services/msg-message.data-service.ts +129 -129
  257. package/src/_modules/messaging/_services/msg.controller.spec.ts +201 -201
  258. package/src/_modules/messaging/index.ts +30 -30
  259. package/src/_modules/mock/app-extended-server.mock.ts +201 -201
  260. package/src/_modules/mock/app-integration-test.mock.ts +51 -51
  261. package/src/_modules/mock/app-params.mock.spec.ts +21 -21
  262. package/src/_modules/mock/app-params.mock.ts +9 -9
  263. package/src/_modules/mock/app-server.mock.ts +188 -188
  264. package/src/_modules/mock/auth-service.mock.spec.ts +47 -47
  265. package/src/_modules/mock/auth-service.mock.ts +28 -28
  266. package/src/_modules/mock/controller.mock.spec.ts +26 -26
  267. package/src/_modules/mock/controller.mock.ts +16 -16
  268. package/src/_modules/mock/data-model.mock.spec.ts +111 -111
  269. package/src/_modules/mock/data-model.mock.ts +82 -82
  270. package/src/_modules/mock/email-service-collection.mock.spec.ts +24 -24
  271. package/src/_modules/mock/email-service-collection.mock.ts +15 -15
  272. package/src/_modules/mock/email-service.mock.spec.ts +17 -17
  273. package/src/_modules/mock/email-service.mock.ts +20 -20
  274. package/src/_modules/mock/email-template.mock.html +14 -14
  275. package/src/_modules/mock/endpoint.mock.ts +91 -91
  276. package/src/_modules/mock/socket-client.mock.spec.ts +40 -40
  277. package/src/_modules/mock/socket-client.mock.ts +45 -45
  278. package/src/_modules/mock/socket-server.mock.spec.ts +44 -44
  279. package/src/_modules/mock/socket-server.mock.ts +46 -46
  280. package/src/_modules/oauth2/_routes/oauth2.controller.spec.ts +107 -107
  281. package/src/_modules/oauth2/_routes/oauth2.controller.ts +98 -98
  282. package/src/_modules/oauth2/_services/oauth2.auth-service.spec.ts +254 -254
  283. package/src/_modules/oauth2/_services/oauth2.auth-service.ts +232 -232
  284. package/src/_modules/oauth2/_services/oauth2.control-service.spec.ts +585 -585
  285. package/src/_modules/oauth2/_services/oauth2.control-service.ts +653 -653
  286. package/src/_modules/oauth2/index.ts +17 -17
  287. package/src/_modules/scoped-config/_enums/dynts-scoped-config-level.enum.ts +22 -22
  288. package/src/_modules/scoped-config/_models/data-models/dynts-scoped-config.data-model.ts +81 -81
  289. package/src/_modules/scoped-config/_models/interfaces/dynts-scoped-config.interface.ts +107 -107
  290. package/src/_modules/scoped-config/_services/dynts-scoped-config.control-service.spec.ts +306 -306
  291. package/src/_modules/scoped-config/_services/dynts-scoped-config.control-service.ts +295 -295
  292. package/src/_modules/scoped-config/_services/dynts-scoped-config.data-service.spec.ts +118 -118
  293. package/src/_modules/scoped-config/_services/dynts-scoped-config.data-service.ts +105 -105
  294. package/src/_modules/scoped-config/index.ts +17 -17
  295. package/src/_modules/server/errors/errors.control-service.spec.ts +238 -238
  296. package/src/_modules/server/errors/errors.control-service.ts +100 -100
  297. package/src/_modules/server/errors/errors.controller.spec.ts +241 -241
  298. package/src/_modules/server/errors/errors.controller.ts +489 -489
  299. package/src/_modules/server/errors/errors.data-service.spec.ts +480 -480
  300. package/src/_modules/server/index.ts +30 -30
  301. package/src/_modules/server/server-status/server-status-snapshot.control-service.spec.ts +70 -70
  302. package/src/_modules/server/server-status/server-status-snapshot.control-service.ts +17 -17
  303. package/src/_modules/server/server-status/server-status-snapshot.data-service.spec.ts +77 -77
  304. package/src/_modules/server/server-status/server-status-snapshot.data-service.ts +37 -37
  305. package/src/_modules/server/server-status/server-status.control-service.spec.ts +576 -576
  306. package/src/_modules/server/server-status/server-status.control-service.ts +396 -396
  307. package/src/_modules/server/server-status/server-status.controller.spec.ts +240 -240
  308. package/src/_modules/server/server-status/server-status.controller.ts +253 -253
  309. package/src/_modules/socket/_enums/socket-security.enum.ts +11 -11
  310. package/src/_modules/socket/_models/socket-client-service-params.control-model.spec.ts +32 -32
  311. package/src/_modules/socket/_models/socket-client-service-params.control-model.ts +22 -22
  312. package/src/_modules/socket/_models/socket-presence.control-model.spec.ts +164 -164
  313. package/src/_modules/socket/_models/socket-presence.control-model.ts +210 -210
  314. package/src/_modules/socket/_models/socket-server-service-params.control-model.spec.ts +46 -46
  315. package/src/_modules/socket/_models/socket-server-service-params.control-model.ts +22 -22
  316. package/src/_modules/socket/_services/socket-client.service.spec.ts +15 -15
  317. package/src/_modules/socket/_services/socket-client.service.ts +260 -260
  318. package/src/_modules/socket/_services/socket-server.service.spec.ts +11 -11
  319. package/src/_modules/socket/app-extended.integration.spec.ts +85 -85
  320. package/src/_modules/socket/app-extended.server.ts +630 -630
  321. package/src/_modules/socket/index.ts +42 -42
  322. package/src/_modules/test/get-test-routing-module.util.spec.ts +28 -28
  323. package/src/_modules/test/get-test-routing-module.util.ts +23 -23
  324. package/src/_modules/test/index.ts +11 -11
  325. package/src/_modules/test/test.controller.spec.ts +72 -72
  326. package/src/_modules/test/test.controller.ts +115 -115
  327. package/src/_modules/usage/get-usage-routing-module.util.ts +22 -22
  328. package/src/_modules/usage/index.ts +15 -15
  329. package/src/_modules/usage/usage.controller.spec.ts +81 -81
  330. package/src/_modules/usage/usage.controller.ts +126 -126
  331. package/src/_modules/usage/usage.data-service.spec.ts +332 -332
  332. package/src/_modules/usage/usage.data-service.ts +185 -185
  333. package/src/_services/base/api.service-base.spec.ts +125 -125
  334. package/src/_services/base/api.service-base.ts +74 -74
  335. package/src/_services/base/archive-data.service.spec.ts +196 -196
  336. package/src/_services/base/archive-data.service.ts +216 -216
  337. package/src/_services/base/data.service.spec.ts +674 -674
  338. package/src/_services/base/data.service.ts +2719 -2719
  339. package/src/_services/base/db.service.spec.ts +73 -73
  340. package/src/_services/base/db.service.ts +1575 -1575
  341. package/src/_services/base/singleton.service-base.spec.ts +28 -28
  342. package/src/_services/base/singleton.service-base.ts +24 -24
  343. package/src/_services/base/singleton.service.spec.ts +114 -114
  344. package/src/_services/base/singleton.service.ts +38 -38
  345. package/src/_services/core/api.service.spec.ts +140 -140
  346. package/src/_services/core/auth.service.spec.ts +159 -159
  347. package/src/_services/core/auth.service.ts +174 -174
  348. package/src/_services/core/email.service.spec.ts +85 -85
  349. package/src/_services/core/email.service.ts +742 -742
  350. package/src/_services/core/global.service.spec.ts +292 -292
  351. package/src/_services/core/global.service.ts +487 -475
  352. package/src/_services/core/memory-guard.service.spec.ts +245 -245
  353. package/src/_services/core/memory-guard.service.ts +481 -481
  354. package/src/_services/core/service-collection.service.spec.ts +46 -46
  355. package/src/_services/core/service-collection.service.ts +6 -6
  356. package/src/_services/route/controller.service.spec.ts +53 -53
  357. package/src/_services/route/controller.service.ts +148 -148
  358. package/src/_services/route/routing-module.service.spec.ts +98 -98
  359. package/src/_services/route/routing-module.service.ts +330 -330
  360. package/src/_services/server/app.server-retention.spec.ts +106 -0
  361. package/src/_services/server/app.server.ts +1869 -1747
  362. package/src/_services/shared.static-service.spec.ts +99 -99
  363. package/src/_services/shared.static-service.ts +78 -78
  364. package/src/index.ts +96 -96
  365. package/tsconfig.app.json +12 -12
  366. package/tsconfig.json +42 -42
@@ -1,1747 +1,1869 @@
1
-
2
- import Mongoose = require('mongoose');
3
- import Express = require('express');
4
- /* import Mongoose from 'mongoose';
5
- import Express from 'express'; */
6
-
7
- import * as BodyParser from 'body-parser';
8
- import * as FileSystem from 'fs';
9
- import * as Http from 'http';
10
- import * as Https from 'https';
11
- import * as Path from 'path';
12
- import * as dotenv from 'dotenv'
13
-
14
- /* import { version } from '../../../package.json'; */
15
-
16
- import {
17
- DyFM_AnyError,
18
- DyFM_Array,
19
- DyFM_Async,
20
- DyFM_Error,
21
- DyFM_error_defaults,
22
- DyFM_Error_Settings,
23
- DyFM_ErrorLevel,
24
- DyFM_Log,
25
- megabyte,
26
- second
27
- } from '@futdevpro/fsm-dynamo';
28
-
29
- import { DyNTS_defaultFallbackCacheMaxAge } from '../../_collections/default-fallback-cache-max-age.const';
30
- import { DyNTS_defaultNotFoundPageHtml } from '../../_collections/default-not-found-page.const';
31
- import { DyNTS_global_settings } from '../../_collections/global-settings.const';
32
- import { startMongoReconnectGuard } from '../../_collections/mongo-reconnect-guard.util';
33
- import { DyNTS_RouteSecurity } from '../../_enums/route-security.enum';
34
- import { DyNTS_App_Params } from '../../_models/control-models/app-params.control-model';
35
- import {
36
- DyNTS_AppSystemControls
37
- } from '../../_models/control-models/app-system-controls.control-model';
38
- import {
39
- DyNTS_Endpoint_Params
40
- } from '../../_models/control-models/endpoint-params.control-model';
41
- import { DyNTS_Http_Settings } from '../../_models/control-models/http-settings.control-model';
42
- import {
43
- DyNTS_Certification_Settings
44
- } from '../../_models/interfaces/certification-settings.interface';
45
- import {
46
- DyNTS_GlobalService_Settings
47
- } from '../../_models/interfaces/global-service-settings.interface';
48
- import {
49
- DyNTS_StaticClient_Settings
50
- } from '../../_models/interfaces/static-client-settings.interface';
51
- import {
52
- DyNTS_Cors_Settings
53
- } from '../../_models/interfaces/cors-settings.interface';
54
- import { DyNTS_SingletonService } from '../base/singleton.service';
55
- import { DyNTS_GlobalService } from '../core/global.service';
56
- import { DyNTS_MemoryGuard } from '../core/memory-guard.service';
57
- import { DyNTS_RoutingModule } from '../route/routing-module.service';
58
- import { DyNTS_getStarRoute } from '../../_collections/star.controller';
59
-
60
- /**
61
- *
62
- * function MyDecorator(config: any) {
63
- return function (target: Function) {
64
- // attach metadata or modify target
65
- target.prototype.myMeta = config;
66
- };
67
- }
68
-
69
- @MyDecorator({ role: 'admin' })
70
- class User {
71
- printRole() {
72
- console.log((this as any).myMeta.role); // → "admin"
73
- }
74
- }
75
-
76
- */
77
-
78
- /**
79
- * This will be the MAIN service of our server project,
80
- * follow the types and type instructions while setting up your project
81
- *
82
- * In this service, there are abstract functions that you will need to implement,
83
- * where you need to set up the main params for your application.
84
- *
85
- * (after the example, you can find the list of services you can/should setup)
86
- *
87
- * @example
88
- * export class App extends DyNTS_AppExtended {
89
- *
90
- * ...
91
- *
92
- * // Setting up App params, and preparing project global settings
93
- * setupAppParams(): void {
94
- * this.params = new DyNTS_AppParams({
95
- * name: 'Warbots Server',
96
- * title: warbotsTitleLog,
97
- * version: version,
98
- * dbName: 'warbots',
99
- * });
100
- *
101
- * // dynamoNTS_GlobalSettings.logRequestsContent = false;
102
- * }
103
- *
104
- * ...
105
- *
106
- * // Setting up DBServices
107
- * setGlobalServiceCollection(): void {
108
- * DyNTS_GlobalService.setServices({
109
- * authService: AuthService.getInstance(),
110
- * emailServiceCollection: EmailServiceCollectionService.getInstance(),
111
- * dbModels: [
112
- * userModelParams,
113
- * userDataModelParams,
114
- * userOptionsModelParams,
115
- * userStatisticsModelParams,
116
- * userAchievementsModelParams,
117
- * userNotificationsModelParams,
118
- *
119
- * matchStatisticsModelParams,
120
- * matchDataModelParams,
121
-
122
- * DyFM_usageSession_dataParams,
123
- * DyFM_customData_dataParams,
124
- * ]
125
- * });
126
- * }
127
- *
128
- * ...
129
- *
130
- * // Setting up Routes
131
- * setupRoutingModules(): void {
132
- * this.httpPort = env.port;
133
-
134
- * this.routingModules = [
135
- * new DyNTS_RoutingModule({
136
- * route: '/user',
137
- * controllers: [
138
- * UserController.getInstance(),
139
- * UserDataController.getInstance(),
140
- * UserOptionsController.getInstance(),
141
- * UserStatisticsController.getInstance(),
142
- * UserAchievementsController.getInstance(),
143
- * UserNotificationsController.getInstance()
144
- * ]
145
- * }),
146
- * new DyNTS_RoutingModule({
147
- * route: '/match',
148
- * controllers: [
149
- * MatchController.getInstance(),
150
- * MatchDistributionController.getInstance(),
151
- * MatchStatisticsController.getInstance(),
152
- * ]
153
- * }),
154
- * new DyNTS_RoutingModule({
155
- * route: '/server',
156
- * controllers: [
157
- * ServerController.getInstance(),
158
- * ]
159
- * }),
160
-
161
- * getTestRoutingModule(),
162
- * getUsageRoutingModule()
163
- * ];
164
- * }
165
- * }
166
- *
167
- * //
168
- * // The Services available
169
- * //
170
- * // Authentication Service
171
- * // A commonly used basic service,
172
- * // which is necessary fur certain functions (such as registering call issuers)
173
- * //
174
- * // This will handle Authentication Token checking/refreshing,
175
- * // checking issuer's identifier and routeParams,
176
- * // handling JWT Token, or maybe with OAuth2 or other commonly used security procedures
177
- * //
178
- * // You can create one with this Dynamo Object:
179
- * //
180
- *
181
- * @example
182
- * // follow the instructions on the abstract class (DyNTS_AuthService)
183
- * export class AuthService extends DyNTS_AuthService {}
184
- *
185
- *
186
- *
187
- * //
188
- */
189
-
190
- /**
191
- * This will be the MAIN service of our server project,
192
- * follow the types and type instructions while setting up your project
193
- *
194
- * In this service, there are abstract functions that you will need to implement,
195
- * where you need to set up the main params for your application.
196
- *
197
- * (after the example, you can find the list of services you can/should setup)
198
- *
199
- * You need to setup the following functions:
200
- * ```ts
201
- * // this is where you set up the main params for your application
202
- * getAppParams(): DyNTS_AppParams
203
- *
204
- * // this is where you connect your main services
205
- * getGlobalServiceSettings(): DyNTS_GlobalService_Settings
206
- *
207
- * // this is where you set up your ports
208
- * getPorts(): DyNTS_PortSettings
209
- *
210
- * // this is where you set up your routes
211
- * getRoutingModules(): DyNTS_RoutingModule[]
212
- *
213
- *
214
- *
215
- * ```
216
- * optionally you can setup the following functions:
217
- * ```ts
218
- * // this is where you set up your certifications
219
- * getCertificationSettings(): DyNTS_CertificationSettings
220
- *
221
- * // this is where you set up additional root services
222
- * getRootServices(): DyNTS_SingletonService[]
223
- *
224
- * // this is where you set up your initial db entries
225
- * createEntries(): void
226
- *
227
- * // this is where you can define post setup processes
228
- * postProcess(): void
229
- *
230
- *
231
- *
232
- * ```
233
- *
234
- */
235
- export abstract class DyNTS_App extends DyNTS_SingletonService {
236
-
237
- protected systemControls: DyNTS_AppSystemControls = new DyNTS_AppSystemControls();
238
- get started(): boolean {
239
- return this.systemControls.app.started;
240
- }
241
- protected get superStarted(): boolean {
242
- return this.systemControls.app.started;
243
- }
244
- protected constructErrors: (Error | DyFM_Error)[] = [];
245
-
246
- /* removed since cant use version from package.json
247
- private readonly _ntsVersion: string = 'v01.07.18';
248
- protected get ntsVersion(): string {
249
- return this._ntsVersion;
250
- } */
251
-
252
- get serverName(): string {
253
- return this.params.name;
254
- }
255
-
256
- private _params: DyNTS_App_Params;
257
- protected get params(): DyNTS_App_Params {
258
- return this._params;
259
- }
260
-
261
- protected mongoose = Mongoose;
262
-
263
- private _security: DyNTS_RouteSecurity;
264
- protected get security(): DyNTS_RouteSecurity {
265
- return this._security;
266
- }
267
-
268
- protected _portSettings: DyNTS_Http_Settings = new DyNTS_Http_Settings();
269
- protected get portSettings(): DyNTS_Http_Settings {
270
- return this._portSettings;
271
- }
272
-
273
- private _cert?: DyNTS_Certification_Settings;
274
- protected get cert(): DyNTS_Certification_Settings {
275
- return this._cert;
276
- }
277
-
278
- protected openExpress: Express.Application;
279
- protected secureExpress: Express.Application;
280
- protected httpsServer: Https.Server;
281
- protected httpServer: Http.Server;
282
-
283
- private globalService: DyNTS_GlobalService;
284
- private _rootServices: DyNTS_SingletonService[] = [];
285
-
286
- private _routingModules: DyNTS_RoutingModule[] = [];
287
-
288
- protected readonly defaultReadyTimeout: number = 30 * second;
289
-
290
- override readonly defaultErrorUserMsg =
291
- `We encountered an unhandled Server Error, ` +
292
- `\nplease contact the responsible development team.` +
293
- `\n(Internal Server error)`;
294
-
295
- get logSetup(): boolean {
296
- return DyNTS_global_settings.log_settings.setup;
297
- }
298
- get deepLog(): boolean {
299
- return DyNTS_global_settings.log_settings.deep;
300
- }
301
- get fnLogs(): boolean {
302
- return DyNTS_global_settings.log_settings.functions;
303
- }
304
- debugLog: boolean = DyNTS_global_settings.log_settings.server_debug;
305
-
306
- constructor(/* extended?: boolean */){
307
- super();
308
-
309
- /* dotenv.config() */
310
-
311
- process.on(
312
- 'unhandledRejection',
313
- (reason_theError: object, p_passWhatIsThis_maybeThePromise: any): void => {
314
- if (reason_theError instanceof DyFM_Error) {
315
- reason_theError.logSimple('Unhandled Rejection');
316
- } else {
317
- DyFM_Log.H_error(
318
- 'Unhandled Rejection:', (p_passWhatIsThis_maybeThePromise as Promise<any>)?.toString(),
319
- '\n Rejection reason:', (reason_theError as Error)?.stack?.split('at')?.[0],
320
- /* '\n ErrorCode:', (reason as any)?.code, */
321
- '\n\n Stack:',
322
- (reason_theError as Error)?.stack?.replaceAll?.('\n at', '\n at'),
323
- );
324
- }
325
-
326
- try {
327
- DyNTS_GlobalService.globalErrorHandler?.(
328
- new DyFM_Error({
329
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-BASE-UR`,
330
- message:
331
- `Unhandled Rejection!: "${(reason_theError as Error)?.stack?.split('at')?.[0]}"`,
332
- userMessage: this.defaultErrorUserMsg,
333
- addECToUserMsg: true,
334
- error: reason_theError as Error,
335
- additionalContent: {
336
- reason: reason_theError,
337
- rejectedPromise: p_passWhatIsThis_maybeThePromise,
338
- },
339
- systemVersion: DyNTS_global_settings.systemVersion,
340
- level: DyFM_ErrorLevel.critical,
341
- })
342
- );
343
- } catch (error) {
344
- DyFM_Log.error('globalErrorHandler (MULTILEVEL) ERROR:', error);
345
- }
346
- }
347
- );
348
-
349
- this.asyncConstruct(/* extended */).catch((error: any): void => {
350
- if (error instanceof DyFM_Error) {
351
- if (error.additionalContent?.constructErrors?.length) {
352
- error.additionalContent.constructErrors.forEach((errorItem: DyFM_Error): void => {
353
- DyFM_Error.logSimple('(constructor asyncConstruct.catch) error:', errorItem);
354
- /* if (errorItem instanceof DyFM_Error) {
355
- errorItem.logSimple(`(constructor asyncConstruct.catch) error:\n`);
356
- } else {
357
- DyFM_Log.H_warn(
358
- '(constructor asyncConstruct.catch) additional error content:\n',
359
- errorItem
360
- );
361
- } */
362
- });
363
- } else {
364
- DyFM_Log.H_warn(
365
- '(constructor asyncConstruct.catch) additional error content:\n',
366
- error?.additionalContent
367
- );
368
- }
369
- }
370
-
371
- DyFM_Error.logSimple('(constructor asyncConstruct.catch) error:', error);
372
- /* if (
373
- !DyNTS_global_settings.log_settings.highDetailedLogs &&
374
- (error instanceof DyFM_Error)
375
- ) {
376
- error.logSimple(
377
- `Application: "${this.params?.name}" start failed. (constructor asyncConstruct.catch)` +
378
- '\n all error messages (from this stack):\n\n"' +
379
- error._messages.join('"\n\n"') + '"\n\n'
380
- );
381
- } else if (error instanceof DyFM_Error) {
382
- DyFM_Log.H_error(
383
- `Application: "${this.params?.name}" start failed. (constructor asyncConstruct.catch)`,
384
- `\n ERROR:`, error
385
- );
386
- } */
387
-
388
- const message: string =
389
- (error as DyFM_Error)?.additionalContent?.constructErrors?.flatMap(
390
- (errorItem: DyFM_Error): string[] => {
391
- return errorItem?._messages ?? [];
392
- }
393
- )?.join?.('\n') ??
394
- (error as DyFM_Error)?.errors?.flatMap(
395
- (errorItem: DyFM_AnyError): string[] =>
396
- (errorItem as DyFM_Error)?._messages ?? [ (errorItem as Error)?.message ]
397
- )?.join?.('\n') ??
398
- error?.message ??
399
- 'UNKNOWN';
400
-
401
- DyFM_Log.testError('Application start failed:\n', message);
402
-
403
- process.exit(1);
404
- });
405
- }
406
-
407
- protected async asyncConstruct(extended?: boolean): Promise<void> {
408
- if (this.fnLogs && this.deepLog) DyFM_Log.log('\nfn:. asyncConstruct');
409
-
410
- try {
411
- this.systemControls.app.init = true;
412
- this._params = this.getAppParams();
413
- DyFM_Log.log(
414
- `\n\n\n\n\n\n\n\n\n\n` +
415
- `Starting ${this._params?.name}... ` +
416
- /* `v${version}` + */
417
- `\n\n\n\n\n\n\n\n\n\n`
418
- );
419
-
420
- if (!this._params) {
421
- throw new Error('getAppParams() must return a DyNTS_AppParams object!');
422
- }
423
-
424
- if (this.params.systemShortCodeName) {
425
- DyNTS_global_settings.systemShortCodeName = this.params.systemShortCodeName;
426
- }
427
-
428
- if (this.params.systemName) {
429
- DyNTS_global_settings.systemName = this.params.systemName;
430
- DyFM_error_defaults.issuerSystem = this.params.systemName;
431
- }
432
-
433
- if (this.params.version) {
434
- DyNTS_global_settings.systemVersion = this.params.version;
435
- DyFM_error_defaults.systemVersion = this.params.version;
436
- }
437
-
438
- process.stdout.write(
439
- String.fromCharCode(27) + ']0;' +
440
- this._params?.name +
441
- String.fromCharCode(7)
442
- );
443
-
444
- DyFM_error_defaults.issuerSystem = this._params.systemName;
445
-
446
- this.overrideDynamoNTSGlobalSettings?.();
447
-
448
- if (DyNTS_global_settings.log_settings.setup) {
449
- DyFM_Log.S_info(`env settings;\n`, {
450
- systemName: DyNTS_global_settings.systemName,
451
- systemShortCodeName: DyNTS_global_settings.systemShortCodeName,
452
- systemVersion: DyNTS_global_settings.systemVersion,
453
- environment: DyNTS_global_settings.env_settings.environment,
454
- /* log_settings: DyNTS_global_settings.log_settings,
455
- env_settings: DyNTS_global_settings.env_settings, */
456
- });
457
- }
458
-
459
- this.globalService = DyNTS_GlobalService.getInstance();
460
- await DyNTS_GlobalService.setServices(this.getGlobalServiceCollection());
461
- DyNTS_GlobalService.setParams(this.params);
462
-
463
- if (this.getPortSettings) {
464
- this._portSettings = this.getPortSettings();
465
- }
466
-
467
- if (this.getCertificationSettings) {
468
- this._cert = this.getCertificationSettings();
469
- }
470
-
471
- if (this.getApiBasePath) {
472
- DyNTS_global_settings.baseUrl = this.getApiBasePath();
473
- }
474
-
475
- if (this.getRoutingModules) {
476
- this._routingModules = this.getRoutingModules();
477
-
478
- // ezt egyelőre csak tesztelem, nem kerül be
479
- /* if (
480
- !DyNTS_global_settings.dontCreateDefaultRoute &&
481
- !this._routingModules.some((routingModule: DyNTS_RoutingModule): boolean => routingModule.route === '/*')
482
- ) {
483
- this._routingModules.push(
484
- DyNTS_getStarRoute()
485
- );
486
- } */
487
- }
488
-
489
- if (this._params.dbUri) {
490
- await this.startDB();
491
-
492
- // createEntries csak akkor fut, ha a DB tényleg fel-jött.
493
- // Ha a startDB jelezte a hibát, de továbbment, akkor a started flag false marad,
494
- // és nem próbálunk meg írni egy nem létező kapcsolatra.
495
- if (this.createEntries && this.systemControls.mongoose.started) {
496
- await this.createEntries();
497
- }
498
- } else {
499
- DyFM_Log.log(
500
- `\nNo database connection created.`,
501
- );
502
- }
503
-
504
- if (this._routingModules?.length) {
505
- if (this.logSetup) DyFM_Log.log('\nsetting up express routes...');
506
-
507
- this.setSecurity();
508
-
509
- await this.initExpresses();
510
- await this.startExpresses();
511
-
512
- if (this._security !== DyNTS_RouteSecurity.secure) {
513
- await this.mountOpenRoutes();
514
- }
515
-
516
- if (this._security !== DyNTS_RouteSecurity.open && this._cert) {
517
- await this.mountSecureRoutes();
518
- }
519
-
520
- // Generikus, auth-agnosztikus extension-point: custom middleware az API
521
- // route-ok UTÁN, de a SPA static catch-all (mountStaticClient) ELŐTT.
522
- await this.mountCustomMiddleware();
523
-
524
- await this.mountStaticClient();
525
-
526
- if (this.logSetup) {
527
- DyFM_Log.log(`\nRoutes mounted.... server using security: ${this._security}`);
528
- }
529
- } else {
530
- DyFM_Log.warn(
531
- `\nNo routes mounted!`,
532
- );
533
- }
534
-
535
- if (this.getRootServices) {
536
- this._rootServices = await this.getRootServices();
537
- }
538
-
539
- if (this.postProcess) {
540
- await this.postProcess().catch((error: any): void => {
541
- DyFM_Error.logSimple(`"${this._params.name}" postProcess failed:`, error);
542
- DyNTS_GlobalService.globalErrorHandler?.(error);
543
- });
544
- }
545
-
546
- // FR-193 — bedrock OOM korai-figyelmeztetés: feltelepítjük a heap-watchdogot, ha
547
- // engedélyezve (DyNTS_global_settings.memoryGuard.enabled, default true). Biztonságos:
548
- // a guard egy könnyű setInterval, ami SOHA nem dob; csak near-OOM küszöböknél hagy
549
- // tartós nyomot az error-sinkbe, mielőtt a fatal heap-OOM megölné a process-t.
550
- try {
551
- if (DyNTS_global_settings.memoryGuard?.enabled) {
552
- DyNTS_MemoryGuard.getInstance().install();
553
- }
554
- } catch (memoryGuardError: unknown) {
555
- DyFM_Log.warn('[DyNTS_MemoryGuard] auto-install skipped (non-fatal):', memoryGuardError);
556
- }
557
-
558
- if (!extended) {
559
- await this.ready();
560
-
561
- if (this.params.title) {
562
- DyFM_Log.success(this.params.title);
563
- }
564
- DyFM_Log.info(`Version: ${this.params.version}`);
565
- /* DyFM_Log.info(`NTS Version: ${this.ntsVersion}`); */
566
- DyFM_Log.H_success(`${this.params.name} started successfully.`);
567
- }
568
- } catch (error) {
569
- this.constructErrors.push(error);
570
-
571
- if (this.deepLog) {
572
- if (DyNTS_global_settings.log_settings.highDetailedLogs) {
573
- DyFM_Log.H_error(
574
- `"${this._params.name}" start failed (in asyncConstruct (highDetailedLog)). `,
575
- `\n\n construct ERRORS:`, this.constructErrors,
576
- '\n\nlast error:', error
577
- );
578
- } else {
579
- DyFM_Log.H_error(
580
- `"${this._params.name}" start failed (in asyncConstruct). `,
581
- `\n\n construct ERRORS:`, this.getSimplifiedConstructErrors(),
582
- '\n\nlast error:', error instanceof DyFM_Error ? error.getErrorSimplified() : error
583
- );
584
- }
585
- }
586
-
587
- throw new DyFM_Error({
588
- ...this._getDefaultErrorSettings('asyncConstruct', error),
589
-
590
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-001`,
591
- additionalContent: {
592
- constructErrors: this.constructErrors,
593
- systemControls: this.systemControls,
594
- systemReadies: {
595
- app: this.systemControls.app.getIsReady(),
596
- mongoose: this.systemControls.mongoose.getIsReady(),
597
- httpServer: this.systemControls.httpServer.getIsReady(),
598
- httpsServer: this.systemControls.httpsServer.getIsReady(),
599
- },
600
- },
601
- });
602
- }
603
- }
604
-
605
- async ready(timeout: number = this.defaultReadyTimeout): Promise<void> {
606
- try {
607
- if (this.fnLogs) DyFM_Log.log('\nfn:. ready');
608
-
609
- await DyFM_Async.delay(100);
610
-
611
- let ready: boolean = false;
612
- const start: number = +new Date();
613
-
614
- if (this.constructErrors.length) {
615
- if (this.deepLog) {
616
- if (DyNTS_global_settings.log_settings.highDetailedLogs) {
617
- DyFM_Log.H_error(
618
- `"${this._params.name}" start failed. (ready; constructErrors check 1)`,
619
- `\n construct ERRORS:`, this.constructErrors
620
- );
621
- } else {
622
- DyFM_Log.H_error(
623
- `"${this._params.name}" start failed. (ready; constructErrors check 1)`,
624
- `\n construct ERRORS:`, this.getSimplifiedConstructErrors(),
625
- );
626
- }
627
- }
628
-
629
- throw new DyFM_Error({
630
- ...this._getDefaultErrorSettings(
631
- 'ready',
632
- new Error(`"${this._params.name}" start failed.`)
633
- ),
634
-
635
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-R01`,
636
- additionalContent:
637
- this.constructErrors.length === 1 ?
638
- { error: this.constructErrors[0] } :
639
- { errors: this.constructErrors },
640
- });
641
- }
642
-
643
- while (!ready && +new Date() - start < timeout) {
644
- if (this.systemControls.app.init) {
645
- ready = (
646
- this.systemControls.mongoose.getIsReady() &&
647
- this.systemControls.httpServer.getIsReady() &&
648
- this.systemControls.httpsServer.getIsReady()
649
- );
650
- } else {
651
- DyFM_Log.error(`"${this._params.name}" APP NOT INITIALIZED while trying to get ready.`);
652
- }
653
-
654
- if (!ready) {
655
- await DyFM_Async.wait(100);
656
- }
657
- }
658
-
659
- if (timeout < +new Date() - start) {
660
- if (this.deepLog) {
661
- if (DyNTS_global_settings.log_settings.highDetailedLogs) {
662
- DyFM_Log.H_error(
663
- `"${this._params.name}" start failed. (ready; TIMEOUT check)`,
664
- `\n construct ERRORS:`, this.constructErrors
665
- );
666
- } else {
667
- DyFM_Log.H_error(
668
- `"${this._params.name}" start failed. (ready; TIMEOUT check)`,
669
- `\n construct ERRORS:`, this.getSimplifiedConstructErrors(),
670
- );
671
- }
672
- }
673
-
674
- throw new DyFM_Error({
675
- ...this._getDefaultErrorSettings(
676
- 'ready',
677
- new Error(`"${this._params.name}" start failed. TIMEOUT`)
678
- ),
679
-
680
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-R02`,
681
- additionalContent: {
682
- constructErrors: this.constructErrors,
683
- systemControls: this.systemControls,
684
- systemReadies: {
685
- mongoose: this.systemControls.mongoose.getIsReady(),
686
- httpServer: this.systemControls.httpServer.getIsReady(),
687
- httpsServer: this.systemControls.httpsServer.getIsReady(),
688
- },
689
- },
690
- });
691
- }
692
-
693
- if (this.constructErrors.length) {
694
- if (this.deepLog) {
695
- if (DyNTS_global_settings.log_settings.highDetailedLogs) {
696
- DyFM_Log.H_error(
697
- `"${this._params.name}" start failed. (ready; constructErrors check 2)`,
698
- `\n construct ERRORS:`, this.constructErrors
699
- );
700
- } else {
701
- DyFM_Log.H_error(
702
- `"${this._params.name}" start failed. (ready; constructErrors check 2)`,
703
- `\n construct ERRORS:`, this.getSimplifiedConstructErrors(),
704
- );
705
- }
706
- }
707
-
708
- throw new DyFM_Error({
709
- ...this._getDefaultErrorSettings(
710
- 'ready',
711
- new Error(`"${this._params.name}" start failed.`)
712
- ),
713
-
714
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-R03`,
715
- additionalContent: this.constructErrors,
716
- });
717
- }
718
-
719
- if (ready) {
720
- this.systemControls.app.started = true;
721
-
722
- if (this.fnLogs && this.deepLog) DyFM_Log.log('\nfn:. ready: return');
723
-
724
- return;
725
- }
726
-
727
- this.systemControls.app.started = false;
728
-
729
- let msg: string = `"${this._params.name}" start failed. UNKNOWN`;
730
-
731
- if (this.systemControls.mongoose.init && !this.systemControls.mongoose.started) {
732
- msg += `\nMongoose start failed.`;
733
- }
734
-
735
- if (this.systemControls.httpServer.init && !this.systemControls.httpServer.started) {
736
- msg += `\nHTTP Server start failed.`;
737
- }
738
-
739
- if (this.systemControls.httpsServer.init && !this.systemControls.httpsServer.started) {
740
- msg += `\nHTTPS Server start failed.`;
741
- }
742
-
743
- DyFM_Log.error(msg, this.constructErrors);
744
-
745
- throw new DyFM_Error({
746
- ...this._getDefaultErrorSettings('ready', new Error(msg)),
747
-
748
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-R04`,
749
- additionalContent: {
750
- constructErrors: this.constructErrors,
751
- systemControls: this.systemControls,
752
- systemReadies: {
753
- app: this.systemControls.app.getIsReady(),
754
- mongoose: this.systemControls.mongoose.getIsReady(),
755
- httpServer: this.systemControls.httpServer.getIsReady(),
756
- httpsServer: this.systemControls.httpsServer.getIsReady(),
757
- },
758
- },
759
- error: this.constructErrors?.[0] ?? new Error(),
760
- });
761
- } catch (error) {
762
- throw new DyFM_Error({
763
- ...this._getDefaultErrorSettings('ready', error),
764
-
765
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-READY0`,
766
- });
767
- }
768
- }
769
-
770
- protected getSimplifiedConstructErrors(): string[] {
771
- return this.constructErrors.map((error: any): any => {
772
- if (error instanceof DyFM_Error) {
773
- return error.getErrorSimplified();
774
- } else {
775
- return error;
776
- }
777
- });
778
- }
779
-
780
- async stop(dontLog?: boolean): Promise<void> {
781
- try {
782
- DyFM_Log.info('\nstopping server...\n');
783
-
784
- await this.ready();
785
-
786
- if (this.started) {
787
-
788
- if (this.systemControls.mongoose.init) {
789
- DyFM_Log.info(`\nstopping Mongoose....`);
790
-
791
- let tryCount: number = 0;
792
-
793
- while (
794
- !this.systemControls.mongoose.started &&
795
- !this.constructErrors.length &&
796
- tryCount++ < 10
797
- ) {
798
- DyFM_Log.warn(`Mongoose not even started yet....`);
799
- await DyFM_Async.wait(second);
800
- }
801
- this.systemControls.mongoose.started = false;
802
-
803
- if (this.mongoose) {
804
- await DyFM_Array.asyncForEach(
805
- Object.keys(this.mongoose.models),
806
- async (modelName): Promise<void> => {
807
- this.mongoose.deleteModel(modelName);
808
- }
809
- );
810
-
811
- const disconnect: Promise<void> = new Promise((resolve): void => {
812
- this.mongoose.connection.on('disconnecting', (): void => {
813
- resolve();
814
- });
815
- });
816
-
817
- await this.mongoose.disconnect();
818
- await this.mongoose.connection.close();
819
- await disconnect;
820
-
821
- this.mongoose.connection.removeAllListeners();
822
-
823
- while (
824
- this.mongoose.connection.readyState !== 0 &&
825
- !this.constructErrors.length
826
- ) {
827
- DyFM_Log.warn(`\nMongoose still not disconnected....`);
828
- await DyFM_Async.wait(second);
829
- }
830
- } else {
831
- DyFM_Log.error(`\nMongoose not found.`);
832
- }
833
- this.systemControls.mongoose.init = false;
834
- }
835
-
836
- if (this.systemControls.httpServer.init) {
837
- this.systemControls.httpServer.started = false;
838
-
839
- if (this.httpServer) {
840
- await new Promise((resolve): void => {
841
- this.httpServer.close(resolve);
842
- });
843
- } else {
844
- DyFM_Log.error(`\nHTTP Server not found.`);
845
- }
846
- this.systemControls.httpServer.init = false;
847
- }
848
-
849
- if (this.systemControls.httpsServer.init) {
850
- this.systemControls.httpsServer.started = false;
851
-
852
- if (this.httpsServer) {
853
- await new Promise((resolve): void => {
854
- this.httpsServer.close(resolve);
855
- });
856
- } else {
857
- DyFM_Log.error(`\nHTTPS Server not found.`);
858
- }
859
- this.systemControls.httpsServer.init = false;
860
- }
861
-
862
- await DyFM_Async.wait(second);
863
-
864
- if (!dontLog) {
865
- DyFM_Log.H_log(`"${this._params.name}" stopped successfully.`);
866
- }
867
- }
868
- } catch (error) {
869
- throw new DyFM_Error({
870
- ...this._getDefaultErrorSettings('stop', error),
871
-
872
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-STOP0`,
873
- });
874
- }
875
- }
876
-
877
- /**
878
- *
879
- */
880
- private async startDB(): Promise<void> {
881
- if (this.fnLogs && this.deepLog) DyFM_Log.log('\nfn:. startDB');
882
- else if (this.logSetup) DyFM_Log.log('\nstarting DB connection...');
883
-
884
- if (!this._params.dbUri) {
885
- throw new DyFM_Error({
886
- ...this._getDefaultErrorSettings('startDB', new Error('DB URI is not set.')),
887
-
888
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SDB0`,
889
- });
890
- }
891
-
892
- if (!this._params.dbOptions) {
893
- throw new DyFM_Error({
894
- ...this._getDefaultErrorSettings('startDB', new Error('DB OPTIONS are not set.')),
895
-
896
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SDB0`,
897
- });
898
- }
899
-
900
- if (this.systemControls.mongoose.init || this.systemControls.mongoose.started) {
901
- throw new DyFM_Error({
902
- ...this._getDefaultErrorSettings('startDB', new Error('Mongoose is already initialized.')),
903
-
904
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SDB0`,
905
- });
906
- }
907
-
908
- try {
909
- await new Promise<void>(
910
- (resolve, reject): void => {
911
- this.systemControls.mongoose.init = true;
912
-
913
- this.mongoose.connection
914
- .once('open', (): void => {
915
- this.systemControls.mongoose.started = true;
916
- DyFM_Log.success(`\nConnected to MongoDB (${this._params.dbUri})\n`);
917
-
918
- resolve();
919
- })
920
- .on('error', (error): void => {
921
- if (!this.systemControls.mongoose.started) {
922
- // Initial DB-csatlakozás sikertelen:
923
- // jelezzük a hibát (log + globalErrorHandler), de NEM szakítjuk meg az
924
- // App startup-ot (nincs constructErrors push, nincs reject).
925
- // A mongoose.init-et resetteljük, hogy a ready() ne várjon a DB-re.
926
- // Ha a mongoose később mégis tudna csatlakozni, az 'open' event
927
- // beállítja a started flag-et, így a runtime DB-műveletek elindulnak.
928
- this.systemControls.mongoose.init = false;
929
-
930
- const d_error: DyFM_Error = new DyFM_Error({
931
- ...this._getDefaultErrorSettings('startDB', error),
932
-
933
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SDB1`,
934
- message: `Unable to connect to MongoDB server (${this._params.dbUri}), ` +
935
- `ERROR: ${error}`,
936
- level: DyFM_ErrorLevel.serious,
937
- });
938
-
939
- DyFM_Log.H_error(
940
- `\nUnable to connect to MongoDB server (${this._params.dbUri}).` +
941
- `\nServer will continue WITHOUT DB connection.` +
942
- `\nDB-using endpoints will fail at runtime until connection recovers.`
943
- );
944
-
945
- if (this.debugLog) DyFM_Log.S_error(
946
- `\nMongoDB connect ERROR: `,
947
- error
948
- );
949
-
950
- DyNTS_GlobalService.globalErrorHandler?.(d_error);
951
-
952
- resolve();
953
-
954
- } else {
955
- if (this.debugLog) DyFM_Log.error('\nMongoDB ERROR: ', error);
956
-
957
- const d_error: DyFM_Error = new DyFM_Error({
958
- ...this._getDefaultErrorSettings('mongoose.connection.on(error)', error),
959
-
960
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SDB2`,
961
- message: `MongoDB ERROR: ${error}`,
962
- level: DyFM_ErrorLevel.critical,
963
- });
964
-
965
- DyNTS_GlobalService.globalErrorHandler?.(d_error);
966
- }
967
- });
968
-
969
- try {
970
- this.mongoose.connect(
971
- this._params.dbUri,
972
- this._params.dbOptions
973
- /* {
974
- directConnection: true,
975
- } */
976
- );
977
- } catch (error) {
978
- throw new DyFM_Error({
979
- ...this._getDefaultErrorSettings('startDB', error),
980
-
981
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SDB3`,
982
- });
983
- }
984
- }
985
- );
986
- } catch (error) {
987
- throw new DyFM_Error({
988
- ...this._getDefaultErrorSettings('startDB', error),
989
-
990
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SDB0`,
991
- });
992
- }
993
-
994
- // 2026-06-20 — Mongo reconnect-guard. A MongoDB-driver a hostnevet connect-kor EGYSZER
995
- // resolválja + cache-eli az IP-t; ha a Mongo-konténert recreate-elik (új docker-network IP),
996
- // a driver a HALOTT IP-n ragad → ECONNREFUSED + buffering-timeout, és a service magától SOHA
997
- // nem áll vissza. A guard sustained-disconnect (readyState !== 1) esetén — a driver saját
998
- // grace-e UTÁN — TELJES disconnect()+connect()-et csinál → ÚJ MongoClient → ÚJ DNS-resolve →
999
- // új IP → reconnect. Best-effort, always-on, healthy connection-t (===1) SOHA nem bánt; csak
1000
- // konténer-IP-frissítés, nem ír adatot. Lásd: mongo-reconnect-guard.util.ts.
1001
- try {
1002
- startMongoReconnectGuard({
1003
- getReadyState: (): number => this.mongoose.connection.readyState,
1004
- reconnect: async (): Promise<void> => {
1005
- await this.mongoose.disconnect().catch((): void => undefined);
1006
- await this.mongoose.connect(this._params.dbUri, this._params.dbOptions);
1007
- },
1008
- log: (msg: string): void => DyFM_Log.warn(msg),
1009
- });
1010
- } catch (guardErr) {
1011
- DyFM_Log.warn(`[mongo-reconnect-guard] failed to start (non-fatal): ${guardErr instanceof Error ? guardErr.message : String(guardErr)}`);
1012
- }
1013
- }
1014
-
1015
- /**
1016
- *
1017
- */
1018
- private async initExpresses(): Promise<void> {
1019
- if (this.fnLogs && this.deepLog) DyFM_Log.log('\nfn:. initExpresses');
1020
-
1021
- try {
1022
- if (this._security && this._security !== DyNTS_RouteSecurity.secure) {
1023
- if (this._portSettings.httpPort === undefined || this._portSettings.httpPort === null) {
1024
- let errorMsg: string =
1025
- `\nYou have open routes, but httpPort is not set!` +
1026
- `\nThere are ${this._routingModules.filter(
1027
- m => m.security != DyNTS_RouteSecurity.secure
1028
- ).length} open/both routes` +
1029
- `\nroot security: ${this._security}` +
1030
- `\nset httpPort in DynamoBEServer - setupRoutingModules() to enable secure routes.`;
1031
-
1032
- errorMsg += '\n\nThe routes setted to use open server:';
1033
- this._routingModules.forEach((module: DyNTS_RoutingModule): void => {
1034
- if (module.security != DyNTS_RouteSecurity.secure) {
1035
- errorMsg += `\n ${module.route} (security: ${module.security})`;
1036
- errorMsg += `\n subroutes using open sever:`;
1037
- module.endpoints.forEach((endpoint: DyNTS_Endpoint_Params): void => {
1038
- if (endpoint.security != DyNTS_RouteSecurity.secure) {
1039
- errorMsg += `\n ${endpoint.endpoint} (security: ${endpoint.security})`;
1040
- }
1041
- });
1042
- }
1043
- });
1044
-
1045
- const error = new Error(`Open routes cannot be established!\n${errorMsg}`);
1046
- const errorStack: string[] = error.stack.split('\n');
1047
-
1048
- errorStack.splice(1, 2);
1049
- error.stack = errorStack.join('\n');
1050
-
1051
- DyFM_Log.error(errorMsg);
1052
-
1053
- throw error;
1054
- }
1055
-
1056
- await this.initOpenExpress();
1057
- }
1058
-
1059
- if (this._security && this._security !== DyNTS_RouteSecurity.open) {
1060
- if (!this._cert || !this._portSettings.httpsPort) {
1061
- let errorMsg: string =
1062
- `\nYou have secure routes, but the certification paths or httpsPort are not set!` +
1063
- `\nThere are ${this._routingModules.filter(
1064
- m => m.security && m.security !== DyNTS_RouteSecurity.open
1065
- ).length} secure routes` +
1066
- `\nroot security: ${this._security}` +
1067
- `\nset...` +
1068
- `\n(missing exact howto...)` +
1069
- /* `\n httpsPort and` +
1070
- `\n cert: {` +
1071
- `\n keyPath: FileSystem.PathLike,` +
1072
- `\n certPath: FileSystem.PathLike,` +
1073
- `\n }` + */
1074
- `\nin DynamoBEServer - getRoutingModules() to enable secure routes.`;
1075
-
1076
- errorMsg += '\n\nThe routes setted to use secure server:';
1077
- this._routingModules.forEach((module: DyNTS_RoutingModule): void => {
1078
- if (module.security && module.security !== DyNTS_RouteSecurity.open) {
1079
- errorMsg += `\n ${module.route} (security: ${module.security})`;
1080
- errorMsg += `\n location: ${module.stackLocation}`;
1081
- errorMsg += `\n subroutes using secure sever:`;
1082
- module.endpoints.forEach((endpoint: DyNTS_Endpoint_Params): void => {
1083
- if (endpoint.security && endpoint.security !== DyNTS_RouteSecurity.open) {
1084
- errorMsg += `\n ${endpoint.endpoint} (security: ${endpoint.security})`;
1085
- errorMsg += `\n location: ${endpoint.stackLocation}`;
1086
- }
1087
- });
1088
- }
1089
- });
1090
-
1091
- const error = new Error(`Secure routes cannot be established!\n${errorMsg}`);
1092
- const errorStack: string[] = error.stack.split('\n');
1093
-
1094
- errorStack.splice(1, 2);
1095
- error.stack = errorStack.join('\n');
1096
-
1097
- DyFM_Log.error(errorMsg);
1098
-
1099
- throw error;
1100
- }
1101
-
1102
- await this.initSecureExpress();
1103
- }
1104
- } catch (error) {
1105
- throw new DyFM_Error({
1106
- ...this._getDefaultErrorSettings('initExpresses', error),
1107
-
1108
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-IE0`,
1109
- });
1110
- }
1111
- }
1112
-
1113
- /**
1114
- *
1115
- */
1116
- protected async initOpenExpress(): Promise<void> {
1117
- if (this.fnLogs) DyFM_Log.log('\nfn:. initOpenExpress');
1118
- this.openExpress = Express();
1119
- this.openExpress.set('maxHeaderSize', 10 * megabyte); // 1024 * 1024 * 10, // 10MB
1120
- this.openExpress.use(BodyParser.urlencoded(this._portSettings.httpUrlencoded));
1121
- this.openExpress.use(BodyParser.json(this._portSettings.httpJson));
1122
- // FR-041 — CORS allowlist enforcement. No-op if getCorsSettings() is not overridden.
1123
- this.mountCors(this.openExpress);
1124
- }
1125
-
1126
- /**
1127
- *
1128
- */
1129
- protected async initSecureExpress(): Promise<void> {
1130
- if (this.fnLogs) DyFM_Log.log('\nfn:. initSecureExpress');
1131
- this.secureExpress = Express();
1132
- this.secureExpress.use(BodyParser.urlencoded(this._portSettings.httpsUrlencoded));
1133
- this.secureExpress.use(BodyParser.json(this._portSettings.httpsJson));
1134
- // FR-041 — CORS allowlist enforcement (same as open express).
1135
- this.mountCors(this.secureExpress);
1136
-
1137
- const options = {
1138
- key: FileSystem.readFileSync(this._cert.keyPath),
1139
- cert: FileSystem.readFileSync(this._cert.certPath),
1140
- maxHeaderSize: 10 * megabyte, // 1024 * 1024 * 10, // 10MB
1141
- };
1142
-
1143
- this.httpsServer = Https.createServer(options, this.secureExpress);
1144
- }
1145
-
1146
- /**
1147
- *
1148
- */
1149
- private async startExpresses(): Promise<void> {
1150
- if (this.fnLogs && this.deepLog) DyFM_Log.log('\nfn:. startExpresses');
1151
-
1152
- try {
1153
- if (this._security && this._security !== DyNTS_RouteSecurity.open) {
1154
- await new Promise<void>((resolve, reject): void => {
1155
- this.systemControls.httpsServer.init = true;
1156
- this.httpsServer
1157
- .listen(
1158
- this._portSettings.httpsPort,
1159
- this.params.secureHost,
1160
- this.params.expressBacklog,
1161
- (): void => {
1162
- this.systemControls.httpsServer.started = true;
1163
- DyFM_Log.success(
1164
- `\nHTTPS (secure) server is listening on port: ` +
1165
- `${this.params.secureHost}:${this._portSettings.httpsPort}`
1166
- );
1167
-
1168
- resolve();
1169
- })
1170
- .on('error', (error): void => {
1171
- if (this.debugLog) DyFM_Log.error(`\nHTTPS (secure) server ERROR`, error);
1172
-
1173
- if (!this.systemControls.httpsServer.started) {
1174
- const d_error: DyFM_Error = new DyFM_Error({
1175
- ...this._getDefaultErrorSettings('startExpresses', error),
1176
-
1177
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SE1`,
1178
- message: `HTTPS (secure) start server ERROR`,
1179
- });
1180
-
1181
- this.constructErrors.push(d_error);
1182
- DyNTS_GlobalService.globalErrorHandler?.(d_error);
1183
-
1184
- reject(d_error);
1185
-
1186
- } else {
1187
- const d_error: DyFM_Error = new DyFM_Error({
1188
- ...this._getDefaultErrorSettings('httpsServer.on(error)', error),
1189
-
1190
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SE2`,
1191
- message: `HTTPS (secure) server ERROR`,
1192
- level: DyFM_ErrorLevel.serious,
1193
- });
1194
-
1195
- DyNTS_GlobalService.globalErrorHandler?.(d_error);
1196
- }
1197
- })
1198
- .on('uncaughtException', (exception): void => {
1199
- const d_error: DyFM_Error = new DyFM_Error({
1200
- ...this._getDefaultErrorSettings('httpsServer.on(uncaughtException)', exception),
1201
-
1202
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SE3`,
1203
- message: `HTTPS (secure) server uncaughtException`,
1204
- level: DyFM_ErrorLevel.critical,
1205
- });
1206
-
1207
- if (this.debugLog) DyFM_Log.warn(
1208
- `\nHTTPS (secure) server uncaughtException`,
1209
- d_error
1210
- );
1211
-
1212
- DyNTS_GlobalService.globalErrorHandler?.(d_error);
1213
- });
1214
- });
1215
- }
1216
-
1217
- if (this._security && this._security !== DyNTS_RouteSecurity.secure) {
1218
- this.systemControls.httpServer.init = true;
1219
- await new Promise<void>((resolve, reject): void => {
1220
- this.httpServer = this.openExpress
1221
- .listen(
1222
- this._portSettings.httpPort,
1223
- this.params.openHost,
1224
- this.params.expressBacklog,
1225
- (): void => {
1226
- this.systemControls.httpServer.started = true;
1227
-
1228
- const resolvedPort: number = this.httpServer?.address?.()?.['port'] ?? this._portSettings.httpPort;
1229
-
1230
- if (this._portSettings.httpPort === 0) {
1231
- DyFM_Log.H_warn(
1232
- `\nHTTP (open) server is using a randomly selected port by the OS: ${resolvedPort}` +
1233
- `\n (httpPort was set to 0 — this is intended for testing only)`
1234
- );
1235
- }
1236
-
1237
- DyFM_Log.success(
1238
- `\nHTTP (open) server is listening on port: ` +
1239
- `${this.params.openHost}:${resolvedPort}`
1240
- );
1241
-
1242
- resolve();
1243
- }
1244
- )
1245
- .on('error', (error): void => {
1246
- if (this.debugLog) DyFM_Log.error(`\nHTTP (open) server ERROR`, error);
1247
-
1248
- if (!this.systemControls.httpServer.started) {
1249
- const d_error: DyFM_Error = new DyFM_Error({
1250
- ...this._getDefaultErrorSettings('startExpresses', error),
1251
-
1252
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SE3`,
1253
- message: `HTTP (open) start server ERROR`,
1254
- });
1255
-
1256
- this.constructErrors.push(d_error);
1257
- DyNTS_GlobalService.globalErrorHandler?.(d_error);
1258
-
1259
- reject(d_error);
1260
-
1261
- } else {
1262
- const d_error: DyFM_Error = new DyFM_Error({
1263
- ...this._getDefaultErrorSettings('httpServer.on(error)', error),
1264
-
1265
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SE4`,
1266
- message: `HTTP (open) server ERROR`,
1267
- level: DyFM_ErrorLevel.serious,
1268
- });
1269
-
1270
- DyNTS_GlobalService.globalErrorHandler?.(d_error);
1271
- }
1272
- })
1273
- .on('uncaughtException', (exception): void => {
1274
- const d_error: DyFM_Error = new DyFM_Error({
1275
- ...this._getDefaultErrorSettings('httpServer.on(uncaughtException)', exception),
1276
-
1277
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SE5`,
1278
- message: `HTTP (open) server uncaughtException`,
1279
- level: DyFM_ErrorLevel.critical,
1280
- });
1281
-
1282
- if (this.debugLog) {
1283
- DyFM_Log.warn(`\nHTTP (open) server uncaughtException`, d_error);
1284
- }
1285
-
1286
- DyNTS_GlobalService.globalErrorHandler?.(d_error);
1287
- });
1288
- });
1289
- }
1290
- } catch (error) {
1291
- throw new DyFM_Error({
1292
- ...this._getDefaultErrorSettings('startExpresses', error),
1293
-
1294
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SE0`,
1295
- });
1296
- }
1297
- }
1298
-
1299
- /**
1300
- *
1301
- */
1302
- private async expressErrorHandling(error, req, res, next): Promise<void> {
1303
- try {
1304
- if (error) {
1305
- const d_error: DyFM_Error = new DyFM_Error({
1306
- ...this._getDefaultErrorSettings('expressErrorHandling', error),
1307
-
1308
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-EEH1`,
1309
- message: `Express ERROR`,
1310
- additionalContent: {
1311
- req,
1312
- res,
1313
- },
1314
- level: DyFM_ErrorLevel.error,
1315
- });
1316
-
1317
- await DyNTS_GlobalService.globalErrorHandler?.(d_error, req, res);
1318
-
1319
- res.send(error)
1320
- } else {
1321
- DyFM_Log.H_error(
1322
- 'WTF??? express error; without error?...' +
1323
- '\nerr:', error,
1324
- '\nreq:', req,
1325
- '\nres:', res
1326
- );
1327
- }
1328
- } catch (error) {
1329
- DyFM_Log.H_error(
1330
- 'MULTILEVEL ERROR (expressErrorHandling)....' +
1331
- '\n', error
1332
- );
1333
- }
1334
- }
1335
-
1336
- /**
1337
- *
1338
- */
1339
- private async mountSecureRoutes (): Promise<void> {
1340
- try {
1341
- if (this.fnLogs && this.deepLog) DyFM_Log.log('\nfn:. mountSecureRoutes');
1342
-
1343
- if (!this.secureExpress) {
1344
- throw new Error(
1345
- 'secureExpress was not initialized. ' +
1346
- 'Secure routes require getCertificationSettings and httpsPort, and cert files must exist.' +
1347
- '\n\nYou need to set security to secure or both in any route and set httpsPort in getPortSettings.' +
1348
- '\n\nYou need to set cert files in getCertificationSettings.'
1349
- );
1350
- }
1351
-
1352
- this.secureExpress.use(
1353
- (error, req, res, next): Promise<void> => this.expressErrorHandling(error, req, res, next)
1354
- );
1355
-
1356
- await DyFM_Array.asyncForEach(
1357
- this._routingModules,
1358
- async (module: DyNTS_RoutingModule): Promise<void> => {
1359
- if (module.security !== DyNTS_RouteSecurity.open) {
1360
- if (this.logSetup) {
1361
- DyFM_Log.log(`route mount (secure): ${module.route}`);
1362
- }
1363
-
1364
- const existingRoutes: DyNTS_RoutingModule[] = this._routingModules.filter(
1365
- (mod: DyNTS_RoutingModule): boolean => mod.route === module.route
1366
- );
1367
-
1368
- if (1 < existingRoutes.length) {
1369
- const error: Error = new Error(`ROUTE DUPLICATION: ${module.route}`);
1370
- /* const errorStack: string[] = error.stack.split('\n');
1371
-
1372
- errorStack.splice(1, 4);
1373
- error.stack = errorStack.join('\n'); */
1374
- error.stack = module.stackLocation;
1375
-
1376
- DyFM_Log.S_error(`ROUTE DUPLICATION: ${module.route}`, error);
1377
-
1378
- throw new DyFM_Error({
1379
- ...this._getDefaultErrorSettings('mountSecureRoutes', error),
1380
-
1381
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-MSR1`,
1382
- message: `ROUTE DUPLICATION: ${module.route}`,
1383
- });
1384
- }
1385
-
1386
- this.secureExpress.use(module.route, module.secureRouter);
1387
- }
1388
- }
1389
- );
1390
- } catch (error) {
1391
- throw new DyFM_Error({
1392
- ...this._getDefaultErrorSettings('mountSecureRoutes', error),
1393
-
1394
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-MSR0`,
1395
- });
1396
- }
1397
- }
1398
-
1399
- /**
1400
- *
1401
- */
1402
- private async mountOpenRoutes(): Promise<void> {
1403
- try {
1404
- if (this.fnLogs && this.deepLog) DyFM_Log.log('\nfn:. mountOpenRoutes');
1405
-
1406
- if (!this.openExpress) {
1407
- throw new Error(
1408
- 'openExpress was not initialized. Open routes require getOpenExpress and httpPort.' +
1409
- '\n\nYou need to set security to open or both in any route and set httpPort in getPortSettings.'
1410
- );
1411
- }
1412
-
1413
- this.openExpress.use(
1414
- (error, req, res, next): Promise<void> => this.expressErrorHandling(error, req, res, next)
1415
- );
1416
-
1417
- await DyFM_Array.asyncForEach(
1418
- this._routingModules,
1419
- async (module: DyNTS_RoutingModule): Promise<void> => {
1420
- if (module.security !== DyNTS_RouteSecurity.secure) {
1421
- if (this.logSetup) {
1422
- DyFM_Log.log(`route mount (open): ${module.route}`);
1423
- }
1424
-
1425
- const existingRoutes: DyNTS_RoutingModule[] = this._routingModules.filter(
1426
- (mod: DyNTS_RoutingModule): boolean => mod.route === module.route
1427
- );
1428
-
1429
- if (1 < existingRoutes.length) {
1430
- const error: Error = new Error(`ROUTE DUPLICATION: ${module.route}`);
1431
- /* const errorStack: string[] = error.stack.split('\n');
1432
-
1433
- errorStack.splice(1, 4);
1434
- error.stack = errorStack.join('\n'); */
1435
- error.stack = module.stackLocation;
1436
-
1437
- DyFM_Log.S_error(`ROUTE DUPLICATION: ${module.route}`, error);
1438
-
1439
- throw new DyFM_Error({
1440
- ...this._getDefaultErrorSettings('mountOpenRoutes', error),
1441
-
1442
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-MOR1`,
1443
- message: `ROUTE DUPLICATION: ${module.route}`,
1444
- });
1445
- }
1446
-
1447
- this.openExpress.use(module.route, module.openRouter);
1448
- }
1449
- }
1450
- );
1451
- } catch (error) {
1452
- throw new DyFM_Error({
1453
- ...this._getDefaultErrorSettings('mountOpenRoutes', error),
1454
-
1455
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-MOR0`,
1456
- });
1457
- }
1458
- }
1459
-
1460
- /**
1461
- * FR-041 CORS middleware. Echoes a matching `Access-Control-Allow-Origin`
1462
- * for any request whose Origin header is in `getCorsSettings().allowedOrigins`,
1463
- * adds the canonical `Access-Control-*` companion headers, and short-circuits
1464
- * the OPTIONS preflight with a 204.
1465
- *
1466
- * No-op when the subclass does NOT override `getCorsSettings()` — preserves
1467
- * back-compat for apps that have always been same-origin and never needed
1468
- * CORS (the typical pre-FR-041 case).
1469
- *
1470
- * Why hand-rolled instead of the `cors` npm package:
1471
- * - ~30 lines, zero new dependency.
1472
- * - Full control over Vary/credentials/preflight semantics.
1473
- * - Aligns with FDP "no unnecessary deps" philosophy.
1474
- */
1475
- protected mountCors(express: Express.Application): void {
1476
- const settings: DyNTS_Cors_Settings | undefined = this.getCorsSettings?.();
1477
- if (!settings) {
1478
- return;
1479
- }
1480
-
1481
- const allowCreds: boolean = settings.allowCredentials !== false;
1482
- const methods: string[] = settings.allowedMethods ?? [
1483
- 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS',
1484
- ];
1485
- const headers: string[] = settings.allowedHeaders ?? [
1486
- 'Authorization', 'Content-Type', 'X-Admin-Key', 'X-Requested-With',
1487
- ];
1488
- const exposed: string[] = settings.exposedHeaders ?? [
1489
- 'Content-Length', 'Content-Type',
1490
- ];
1491
- const maxAge: number = settings.maxAgeSeconds ?? 86400;
1492
-
1493
- const isAllowed: (origin: string) => boolean = (origin: string): boolean => {
1494
- if (settings.allowedOrigins === '*') return true;
1495
- if (typeof settings.allowedOrigins === 'function') {
1496
- return settings.allowedOrigins(origin);
1497
- }
1498
- return settings.allowedOrigins.includes(origin);
1499
- };
1500
-
1501
- express.use((req: Express.Request, res: Express.Response, next: Express.NextFunction): void => {
1502
- const origin: string | undefined = req.headers.origin;
1503
- if (origin && isAllowed(origin)) {
1504
- // Echo the matching origin (NOT wildcard) because credentials require it.
1505
- res.setHeader('Access-Control-Allow-Origin', origin);
1506
- if (allowCreds) {
1507
- res.setHeader('Access-Control-Allow-Credentials', 'true');
1508
- }
1509
- res.setHeader('Access-Control-Allow-Methods', methods.join(', '));
1510
- res.setHeader('Access-Control-Allow-Headers', headers.join(', '));
1511
- res.setHeader('Access-Control-Expose-Headers', exposed.join(', '));
1512
- res.setHeader('Access-Control-Max-Age', String(maxAge));
1513
- res.setHeader('Vary', 'Origin');
1514
- }
1515
- if (req.method === 'OPTIONS') {
1516
- res.status(204).end();
1517
- return;
1518
- }
1519
- next();
1520
- });
1521
- }
1522
-
1523
- /**
1524
- * Generikus, auth-agnosztikus extension-point orchestrator. A `registerCustomMiddleware`
1525
- * hookot hívja (ha a subclass override-olja) minden AKTÍV Express instance-ra
1526
- * (open + secure), az API route-ok UTÁN, de a SPA static catch-all (`app.get('*')`)
1527
- * ELŐTT — ez az EGYETLEN korrekt pont egy sub-path middleware-hez (pl. reverse-proxy),
1528
- * mert a catch-all minden utána mountolt route-ot elnyel. No-op ha nincs override (back-compat).
1529
- */
1530
- private async mountCustomMiddleware(): Promise<void> {
1531
- if (!this.registerCustomMiddleware) {
1532
- return;
1533
- }
1534
-
1535
- if (this.openExpress) {
1536
- await this.registerCustomMiddleware(this.openExpress, DyNTS_RouteSecurity.open);
1537
- }
1538
-
1539
- if (this.secureExpress) {
1540
- await this.registerCustomMiddleware(this.secureExpress, DyNTS_RouteSecurity.secure);
1541
- }
1542
-
1543
- if (this.logSetup) {
1544
- DyFM_Log.log('\nCustom middleware registered (pre-static).');
1545
- }
1546
- }
1547
-
1548
- /**
1549
- * Ha getStaticClientSettings() visszaad beállításokat, a client static fájlok a '/' alatt
1550
- * lesznek kiszolgálva (API route-ok után). SPA fallback opcionális (fallbackPath).
1551
- * Asset cache (assetCacheMaxAge, assetCacheImmutable) és fallback cache (fallbackCacheMaxAge)
1552
- * opcionálisak; ha nincs megadva fallbackCacheMaxAge, DyNTS_defaultFallbackCacheMaxAge (0) marad.
1553
- */
1554
- private async mountStaticClient(): Promise<void> {
1555
- const settings: DyNTS_StaticClient_Settings | undefined = this.getStaticClientSettings?.();
1556
- if (!settings) {
1557
- return;
1558
- }
1559
-
1560
- const absoluteRoot: string = Path.isAbsolute(settings.root)
1561
- ? settings.root
1562
- : Path.resolve(process.cwd(), settings.root);
1563
-
1564
- const hasAssetCache: boolean =
1565
- settings.assetCacheMaxAge !== undefined || settings.assetCacheImmutable === true;
1566
- const staticOptions: { setHeaders: (res: Express.Response) => void } | undefined =
1567
- hasAssetCache && settings.assetCacheMaxAge !== undefined
1568
- ? {
1569
- setHeaders: (res: Express.Response): void => {
1570
- const maxAge: number = settings.assetCacheMaxAge!;
1571
- const immutable: string =
1572
- settings.assetCacheImmutable === true ? ', immutable' : '';
1573
- res.setHeader('Cache-Control', `max-age=${maxAge}${immutable}`);
1574
- },
1575
- }
1576
- : undefined;
1577
-
1578
- const staticMiddleware: Express.Handler =
1579
- staticOptions
1580
- ? Express.static(absoluteRoot, staticOptions)
1581
- : Express.static(absoluteRoot);
1582
-
1583
- const fallbackMaxAge: number =
1584
- settings.fallbackCacheMaxAge ?? DyNTS_defaultFallbackCacheMaxAge;
1585
-
1586
- if (this.openExpress) {
1587
- this.openExpress.use('/', staticMiddleware);
1588
- if (settings.fallbackPath) {
1589
- this.openExpress.get('*', (req: Express.Request, res: Express.Response): void => {
1590
- res.setHeader('Cache-Control', `max-age=${fallbackMaxAge}`);
1591
- res.sendFile(settings.fallbackPath!, { root: absoluteRoot });
1592
- });
1593
- } else {
1594
- this.openExpress.get('*', (req: Express.Request, res: Express.Response): void => {
1595
- res.status(404).type('html').send(DyNTS_defaultNotFoundPageHtml);
1596
- });
1597
- }
1598
- }
1599
-
1600
- if (this.secureExpress) {
1601
- this.secureExpress.use('/', staticMiddleware);
1602
- if (settings.fallbackPath) {
1603
- this.secureExpress.get('*', (req: Express.Request, res: Express.Response): void => {
1604
- res.setHeader('Cache-Control', `max-age=${fallbackMaxAge}`);
1605
- res.sendFile(settings.fallbackPath!, { root: absoluteRoot });
1606
- });
1607
- } else {
1608
- this.secureExpress.get('*', (req: Express.Request, res: Express.Response): void => {
1609
- res.status(404).type('html').send(DyNTS_defaultNotFoundPageHtml);
1610
- });
1611
- }
1612
- }
1613
-
1614
- if (this.logSetup) {
1615
- DyFM_Log.log(`\nStatic client mounted at / (root: ${absoluteRoot})`);
1616
- }
1617
- }
1618
-
1619
- /**
1620
- *
1621
- */
1622
- private setSecurity(): void {
1623
- if (this.fnLogs && this.deepLog) DyFM_Log.log('\nfn:. setSecurity');
1624
- this._routingModules.forEach((module: DyNTS_RoutingModule): void => {
1625
- if (!module.security) {
1626
- DyFM_Log.warn(`RoutingModule security is not set for ${module.route}\n`);
1627
-
1628
- } else if (!this._security) {
1629
- this._security = module.security;
1630
-
1631
- } else if (this._security !== module.security) {
1632
- this._security = DyNTS_RouteSecurity.both;
1633
- }
1634
- });
1635
-
1636
- if (!this._security) {
1637
- let msg = `Could not set security for the server! (${this.security})`;
1638
-
1639
- msg += '\n RoutingModules:';
1640
- this._routingModules.forEach((module: DyNTS_RoutingModule): void => {
1641
- msg += `\n ${module.route} (security: ${module.security})`;
1642
- });
1643
-
1644
- if (this._routingModules.length === 0) {
1645
- msg += '\n - no RoutingModule found -\n';
1646
- }
1647
- DyFM_Log.warn(msg);
1648
- }
1649
- }
1650
-
1651
- private _getDefaultErrorSettings(
1652
- fnName: string,
1653
- error: DyFM_AnyError
1654
- ): DyFM_Error_Settings {
1655
- return {
1656
- status: (error as DyFM_Error)?.___status ?? 500,
1657
- message: (error as Error)?.message ??
1658
- (error as DyFM_Error)?._message ??
1659
- `${fnName} was UNSUCCESSFUL (NTS)`,
1660
- userMessage: (error as DyFM_Error)?.__userMessage ?? this.defaultErrorUserMsg,
1661
- addECToUserMsg: !(error as DyFM_Error)?.__userMessage,
1662
- issuerService: `${this?.constructor?.name}-DyNTS_App`,
1663
- level: DyFM_ErrorLevel.fatal,
1664
- error: error,
1665
- systemVersion: DyNTS_global_settings.systemVersion,
1666
- };
1667
- }
1668
-
1669
- /**
1670
- * MISSING Description (TODO)
1671
- */
1672
- abstract getAppParams(): DyNTS_App_Params;
1673
-
1674
- /**
1675
- * MISSING Description (TODO)
1676
- */
1677
- abstract getGlobalServiceCollection(): DyNTS_GlobalService_Settings;
1678
-
1679
- /**
1680
- * MISSING Description (TODO)
1681
- */
1682
- abstract getPortSettings(): DyNTS_Http_Settings;
1683
-
1684
- /**
1685
- * MISSING Description (TODO)
1686
- */
1687
- overrideDynamoNTSGlobalSettings?(): void;
1688
-
1689
- /**
1690
- * Ha megadva, a visszaadott értéket használjuk a route prefix-ként
1691
- * (DyNTS_global_settings.baseUrl felülírása erre az app-ra).
1692
- */
1693
- getApiBasePath?(): string;
1694
-
1695
- /**
1696
- * MISSING Description (TODO)
1697
- */
1698
- getRoutingModules?(): DyNTS_RoutingModule[];
1699
-
1700
- /**
1701
- * MISSING Description (TODO)
1702
- */
1703
- getRootServices?(): Promise<any[]>;
1704
-
1705
- /**
1706
- * MISSING Description (TODO)
1707
- */
1708
- getCertificationSettings?(): DyNTS_Certification_Settings;
1709
-
1710
- /**
1711
- * Ha megadva, a client static fájlok a '/' alatt lesznek kiszolgálva (API route-ok után).
1712
- * Opcionális fallbackPath pl. SPA index.html-hez.
1713
- */
1714
- getStaticClientSettings?(): DyNTS_StaticClient_Settings | undefined;
1715
-
1716
- /**
1717
- * FR-041 — Ha megadva, CORS middleware mount-olódik mindkét Express instance-ra
1718
- * (open + secure). Az `allowedOrigins` listán szereplő Origin-eket echo-zza vissza,
1719
- * `Access-Control-Allow-Credentials: true`-val (default) együtt a Bearer JWT
1720
- * cross-domain auth flow miatt. OPTIONS preflight 204-gyel rövidre záródik.
1721
- * Ha undefined → no CORS headers, no preflight handling — back-compat.
1722
- */
1723
- getCorsSettings?(): DyNTS_Cors_Settings | undefined;
1724
-
1725
- /**
1726
- * Opcionális generikus hook: custom Express middleware regisztrálása az API route-ok
1727
- * UTÁN és a SPA static catch-all ELŐTT. Auth-AGNOSZTIKUS framework extension-point
1728
- * a subclass (pl. FDP base App) ezen keresztül mount-olhat reverse-proxyt egy sub-path-re
1729
- * (pl. `/auth-api`), ami a `app.get('*')` catch-all elé kell kerüljön. A hívási időzítést
1730
- * lásd: `mountCustomMiddleware`. Undefined no-op (back-compat).
1731
- */
1732
- registerCustomMiddleware?(
1733
- express: Express.Application,
1734
- security: DyNTS_RouteSecurity,
1735
- ): void | Promise<void>;
1736
-
1737
- /**
1738
- * MISSING Description (TODO)
1739
- */
1740
- createEntries?(): Promise<void>;
1741
-
1742
- /**
1743
- * MISSING Description (TODO)
1744
- */
1745
- postProcess?(): Promise<void>;
1746
-
1747
- }
1
+
2
+ import Mongoose = require('mongoose');
3
+ import Express = require('express');
4
+ /* import Mongoose from 'mongoose';
5
+ import Express from 'express'; */
6
+
7
+ import * as BodyParser from 'body-parser';
8
+ import * as FileSystem from 'fs';
9
+ import * as Http from 'http';
10
+ import * as Https from 'https';
11
+ import * as Path from 'path';
12
+ import * as dotenv from 'dotenv'
13
+
14
+ /* import { version } from '../../../package.json'; */
15
+
16
+ import {
17
+ DyFM_AnyError,
18
+ DyFM_Array,
19
+ DyFM_Async,
20
+ DyFM_Error,
21
+ DyFM_error_defaults,
22
+ DyFM_Error_Settings,
23
+ DyFM_ErrorLevel,
24
+ DyFM_Log,
25
+ DyFM_resolveRetentionTtlSeconds,
26
+ megabyte,
27
+ second
28
+ } from '@futdevpro/fsm-dynamo';
29
+
30
+ import { DyNTS_defaultFallbackCacheMaxAge } from '../../_collections/default-fallback-cache-max-age.const';
31
+ import { DyNTS_defaultNotFoundPageHtml } from '../../_collections/default-not-found-page.const';
32
+ import { DyNTS_global_settings } from '../../_collections/global-settings.const';
33
+ import { startMongoReconnectGuard } from '../../_collections/mongo-reconnect-guard.util';
34
+ import { DyNTS_RouteSecurity } from '../../_enums/route-security.enum';
35
+ import { DyNTS_App_Params } from '../../_models/control-models/app-params.control-model';
36
+ import {
37
+ DyNTS_AppSystemControls
38
+ } from '../../_models/control-models/app-system-controls.control-model';
39
+ import {
40
+ DyNTS_Endpoint_Params
41
+ } from '../../_models/control-models/endpoint-params.control-model';
42
+ import { DyNTS_Http_Settings } from '../../_models/control-models/http-settings.control-model';
43
+ import {
44
+ DyNTS_Certification_Settings
45
+ } from '../../_models/interfaces/certification-settings.interface';
46
+ import {
47
+ DyNTS_GlobalService_Settings
48
+ } from '../../_models/interfaces/global-service-settings.interface';
49
+ import {
50
+ DyNTS_StaticClient_Settings
51
+ } from '../../_models/interfaces/static-client-settings.interface';
52
+ import {
53
+ DyNTS_Cors_Settings
54
+ } from '../../_models/interfaces/cors-settings.interface';
55
+ import { DyNTS_DBService } from '../base/db.service';
56
+ import { DyNTS_SingletonService } from '../base/singleton.service';
57
+ import { DyNTS_GlobalService } from '../core/global.service';
58
+ import { DyNTS_MemoryGuard } from '../core/memory-guard.service';
59
+ import { DyNTS_RoutingModule } from '../route/routing-module.service';
60
+ import { DyNTS_getStarRoute } from '../../_collections/star.controller';
61
+
62
+ /**
63
+ *
64
+ * function MyDecorator(config: any) {
65
+ return function (target: Function) {
66
+ // attach metadata or modify target
67
+ target.prototype.myMeta = config;
68
+ };
69
+ }
70
+
71
+ @MyDecorator({ role: 'admin' })
72
+ class User {
73
+ printRole() {
74
+ console.log((this as any).myMeta.role); // → "admin"
75
+ }
76
+ }
77
+
78
+ */
79
+
80
+ /**
81
+ * This will be the MAIN service of our server project,
82
+ * follow the types and type instructions while setting up your project
83
+ *
84
+ * In this service, there are abstract functions that you will need to implement,
85
+ * where you need to set up the main params for your application.
86
+ *
87
+ * (after the example, you can find the list of services you can/should setup)
88
+ *
89
+ * @example
90
+ * export class App extends DyNTS_AppExtended {
91
+ *
92
+ * ...
93
+ *
94
+ * // Setting up App params, and preparing project global settings
95
+ * setupAppParams(): void {
96
+ * this.params = new DyNTS_AppParams({
97
+ * name: 'Warbots Server',
98
+ * title: warbotsTitleLog,
99
+ * version: version,
100
+ * dbName: 'warbots',
101
+ * });
102
+ *
103
+ * // dynamoNTS_GlobalSettings.logRequestsContent = false;
104
+ * }
105
+ *
106
+ * ...
107
+ *
108
+ * // Setting up DBServices
109
+ * setGlobalServiceCollection(): void {
110
+ * DyNTS_GlobalService.setServices({
111
+ * authService: AuthService.getInstance(),
112
+ * emailServiceCollection: EmailServiceCollectionService.getInstance(),
113
+ * dbModels: [
114
+ * userModelParams,
115
+ * userDataModelParams,
116
+ * userOptionsModelParams,
117
+ * userStatisticsModelParams,
118
+ * userAchievementsModelParams,
119
+ * userNotificationsModelParams,
120
+ *
121
+ * matchStatisticsModelParams,
122
+ * matchDataModelParams,
123
+
124
+ * DyFM_usageSession_dataParams,
125
+ * DyFM_customData_dataParams,
126
+ * ]
127
+ * });
128
+ * }
129
+ *
130
+ * ...
131
+ *
132
+ * // Setting up Routes
133
+ * setupRoutingModules(): void {
134
+ * this.httpPort = env.port;
135
+
136
+ * this.routingModules = [
137
+ * new DyNTS_RoutingModule({
138
+ * route: '/user',
139
+ * controllers: [
140
+ * UserController.getInstance(),
141
+ * UserDataController.getInstance(),
142
+ * UserOptionsController.getInstance(),
143
+ * UserStatisticsController.getInstance(),
144
+ * UserAchievementsController.getInstance(),
145
+ * UserNotificationsController.getInstance()
146
+ * ]
147
+ * }),
148
+ * new DyNTS_RoutingModule({
149
+ * route: '/match',
150
+ * controllers: [
151
+ * MatchController.getInstance(),
152
+ * MatchDistributionController.getInstance(),
153
+ * MatchStatisticsController.getInstance(),
154
+ * ]
155
+ * }),
156
+ * new DyNTS_RoutingModule({
157
+ * route: '/server',
158
+ * controllers: [
159
+ * ServerController.getInstance(),
160
+ * ]
161
+ * }),
162
+
163
+ * getTestRoutingModule(),
164
+ * getUsageRoutingModule()
165
+ * ];
166
+ * }
167
+ * }
168
+ *
169
+ * //
170
+ * // The Services available
171
+ * //
172
+ * // Authentication Service
173
+ * // A commonly used basic service,
174
+ * // which is necessary fur certain functions (such as registering call issuers)
175
+ * //
176
+ * // This will handle Authentication Token checking/refreshing,
177
+ * // checking issuer's identifier and routeParams,
178
+ * // handling JWT Token, or maybe with OAuth2 or other commonly used security procedures
179
+ * //
180
+ * // You can create one with this Dynamo Object:
181
+ * //
182
+ *
183
+ * @example
184
+ * // follow the instructions on the abstract class (DyNTS_AuthService)
185
+ * export class AuthService extends DyNTS_AuthService {}
186
+ *
187
+ *
188
+ *
189
+ * //
190
+ */
191
+
192
+ /**
193
+ * This will be the MAIN service of our server project,
194
+ * follow the types and type instructions while setting up your project
195
+ *
196
+ * In this service, there are abstract functions that you will need to implement,
197
+ * where you need to set up the main params for your application.
198
+ *
199
+ * (after the example, you can find the list of services you can/should setup)
200
+ *
201
+ * You need to setup the following functions:
202
+ * ```ts
203
+ * // this is where you set up the main params for your application
204
+ * getAppParams(): DyNTS_AppParams
205
+ *
206
+ * // this is where you connect your main services
207
+ * getGlobalServiceSettings(): DyNTS_GlobalService_Settings
208
+ *
209
+ * // this is where you set up your ports
210
+ * getPorts(): DyNTS_PortSettings
211
+ *
212
+ * // this is where you set up your routes
213
+ * getRoutingModules(): DyNTS_RoutingModule[]
214
+ *
215
+ *
216
+ *
217
+ * ```
218
+ * optionally you can setup the following functions:
219
+ * ```ts
220
+ * // this is where you set up your certifications
221
+ * getCertificationSettings(): DyNTS_CertificationSettings
222
+ *
223
+ * // this is where you set up additional root services
224
+ * getRootServices(): DyNTS_SingletonService[]
225
+ *
226
+ * // this is where you set up your initial db entries
227
+ * createEntries(): void
228
+ *
229
+ * // this is where you can define post setup processes
230
+ * postProcess(): void
231
+ *
232
+ *
233
+ *
234
+ * ```
235
+ *
236
+ */
237
+
238
+ /** FR-258 / SR-2 — one entry from `collection.indexes()` (only the fields the TTL-installer reads). */
239
+ export interface DyNTS_TtlIndexInfo {
240
+ name?: string;
241
+ key?: Record<string, number>;
242
+ expireAfterSeconds?: number;
243
+ }
244
+
245
+ /**
246
+ * FR-258 / SR-2 the minimal native-driver collection surface the TTL-installer needs. Declared
247
+ * structurally (not importing mongodb types) so it stays dependency-light and is trivially mockable
248
+ * in unit tests.
249
+ */
250
+ export interface DyNTS_TtlIndexCollection {
251
+ indexes?: () => Promise<DyNTS_TtlIndexInfo[]>;
252
+ createIndex: (keys: Record<string, number>, options: { expireAfterSeconds: number }) => Promise<unknown>;
253
+ dropIndex?: (name: string) => Promise<unknown>;
254
+ }
255
+
256
+ /** FR-258 / SR-2 — outcome of {@link DyNTS_App.ensureRetentionTtlIndex}. */
257
+ export type DyNTS_TtlIndexAction = 'created' | 'updated' | 'noop';
258
+
259
+ export abstract class DyNTS_App extends DyNTS_SingletonService {
260
+
261
+ protected systemControls: DyNTS_AppSystemControls = new DyNTS_AppSystemControls();
262
+ get started(): boolean {
263
+ return this.systemControls.app.started;
264
+ }
265
+ protected get superStarted(): boolean {
266
+ return this.systemControls.app.started;
267
+ }
268
+ protected constructErrors: (Error | DyFM_Error)[] = [];
269
+
270
+ /* removed since cant use version from package.json
271
+ private readonly _ntsVersion: string = 'v01.07.18';
272
+ protected get ntsVersion(): string {
273
+ return this._ntsVersion;
274
+ } */
275
+
276
+ get serverName(): string {
277
+ return this.params.name;
278
+ }
279
+
280
+ private _params: DyNTS_App_Params;
281
+ protected get params(): DyNTS_App_Params {
282
+ return this._params;
283
+ }
284
+
285
+ protected mongoose = Mongoose;
286
+
287
+ private _security: DyNTS_RouteSecurity;
288
+ protected get security(): DyNTS_RouteSecurity {
289
+ return this._security;
290
+ }
291
+
292
+ protected _portSettings: DyNTS_Http_Settings = new DyNTS_Http_Settings();
293
+ protected get portSettings(): DyNTS_Http_Settings {
294
+ return this._portSettings;
295
+ }
296
+
297
+ private _cert?: DyNTS_Certification_Settings;
298
+ protected get cert(): DyNTS_Certification_Settings {
299
+ return this._cert;
300
+ }
301
+
302
+ protected openExpress: Express.Application;
303
+ protected secureExpress: Express.Application;
304
+ protected httpsServer: Https.Server;
305
+ protected httpServer: Http.Server;
306
+
307
+ private globalService: DyNTS_GlobalService;
308
+ private _rootServices: DyNTS_SingletonService[] = [];
309
+
310
+ private _routingModules: DyNTS_RoutingModule[] = [];
311
+
312
+ protected readonly defaultReadyTimeout: number = 30 * second;
313
+
314
+ override readonly defaultErrorUserMsg =
315
+ `We encountered an unhandled Server Error, ` +
316
+ `\nplease contact the responsible development team.` +
317
+ `\n(Internal Server error)`;
318
+
319
+ get logSetup(): boolean {
320
+ return DyNTS_global_settings.log_settings.setup;
321
+ }
322
+ get deepLog(): boolean {
323
+ return DyNTS_global_settings.log_settings.deep;
324
+ }
325
+ get fnLogs(): boolean {
326
+ return DyNTS_global_settings.log_settings.functions;
327
+ }
328
+ debugLog: boolean = DyNTS_global_settings.log_settings.server_debug;
329
+
330
+ constructor(/* extended?: boolean */){
331
+ super();
332
+
333
+ /* dotenv.config() */
334
+
335
+ process.on(
336
+ 'unhandledRejection',
337
+ (reason_theError: object, p_passWhatIsThis_maybeThePromise: any): void => {
338
+ if (reason_theError instanceof DyFM_Error) {
339
+ reason_theError.logSimple('Unhandled Rejection');
340
+ } else {
341
+ DyFM_Log.H_error(
342
+ 'Unhandled Rejection:', (p_passWhatIsThis_maybeThePromise as Promise<any>)?.toString(),
343
+ '\n Rejection reason:', (reason_theError as Error)?.stack?.split('at')?.[0],
344
+ /* '\n ErrorCode:', (reason as any)?.code, */
345
+ '\n\n Stack:',
346
+ (reason_theError as Error)?.stack?.replaceAll?.('\n at', '\n at'),
347
+ );
348
+ }
349
+
350
+ try {
351
+ DyNTS_GlobalService.globalErrorHandler?.(
352
+ new DyFM_Error({
353
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-BASE-UR`,
354
+ message:
355
+ `Unhandled Rejection!: "${(reason_theError as Error)?.stack?.split('at')?.[0]}"`,
356
+ userMessage: this.defaultErrorUserMsg,
357
+ addECToUserMsg: true,
358
+ error: reason_theError as Error,
359
+ additionalContent: {
360
+ reason: reason_theError,
361
+ rejectedPromise: p_passWhatIsThis_maybeThePromise,
362
+ },
363
+ systemVersion: DyNTS_global_settings.systemVersion,
364
+ level: DyFM_ErrorLevel.critical,
365
+ })
366
+ );
367
+ } catch (error) {
368
+ DyFM_Log.error('globalErrorHandler (MULTILEVEL) ERROR:', error);
369
+ }
370
+ }
371
+ );
372
+
373
+ this.asyncConstruct(/* extended */).catch((error: any): void => {
374
+ if (error instanceof DyFM_Error) {
375
+ if (error.additionalContent?.constructErrors?.length) {
376
+ error.additionalContent.constructErrors.forEach((errorItem: DyFM_Error): void => {
377
+ DyFM_Error.logSimple('(constructor asyncConstruct.catch) error:', errorItem);
378
+ /* if (errorItem instanceof DyFM_Error) {
379
+ errorItem.logSimple(`(constructor asyncConstruct.catch) error:\n`);
380
+ } else {
381
+ DyFM_Log.H_warn(
382
+ '(constructor asyncConstruct.catch) additional error content:\n',
383
+ errorItem
384
+ );
385
+ } */
386
+ });
387
+ } else {
388
+ DyFM_Log.H_warn(
389
+ '(constructor asyncConstruct.catch) additional error content:\n',
390
+ error?.additionalContent
391
+ );
392
+ }
393
+ }
394
+
395
+ DyFM_Error.logSimple('(constructor asyncConstruct.catch) error:', error);
396
+ /* if (
397
+ !DyNTS_global_settings.log_settings.highDetailedLogs &&
398
+ (error instanceof DyFM_Error)
399
+ ) {
400
+ error.logSimple(
401
+ `Application: "${this.params?.name}" start failed. (constructor asyncConstruct.catch)` +
402
+ '\n all error messages (from this stack):\n\n"' +
403
+ error._messages.join('"\n\n"') + '"\n\n'
404
+ );
405
+ } else if (error instanceof DyFM_Error) {
406
+ DyFM_Log.H_error(
407
+ `Application: "${this.params?.name}" start failed. (constructor asyncConstruct.catch)`,
408
+ `\n ERROR:`, error
409
+ );
410
+ } */
411
+
412
+ const message: string =
413
+ (error as DyFM_Error)?.additionalContent?.constructErrors?.flatMap(
414
+ (errorItem: DyFM_Error): string[] => {
415
+ return errorItem?._messages ?? [];
416
+ }
417
+ )?.join?.('\n') ??
418
+ (error as DyFM_Error)?.errors?.flatMap(
419
+ (errorItem: DyFM_AnyError): string[] =>
420
+ (errorItem as DyFM_Error)?._messages ?? [ (errorItem as Error)?.message ]
421
+ )?.join?.('\n') ??
422
+ error?.message ??
423
+ 'UNKNOWN';
424
+
425
+ DyFM_Log.testError('Application start failed:\n', message);
426
+
427
+ process.exit(1);
428
+ });
429
+ }
430
+
431
+ protected async asyncConstruct(extended?: boolean): Promise<void> {
432
+ if (this.fnLogs && this.deepLog) DyFM_Log.log('\nfn:. asyncConstruct');
433
+
434
+ try {
435
+ this.systemControls.app.init = true;
436
+ this._params = this.getAppParams();
437
+ DyFM_Log.log(
438
+ `\n\n\n\n\n\n\n\n\n\n` +
439
+ `Starting ${this._params?.name}... ` +
440
+ /* `v${version}` + */
441
+ `\n\n\n\n\n\n\n\n\n\n`
442
+ );
443
+
444
+ if (!this._params) {
445
+ throw new Error('getAppParams() must return a DyNTS_AppParams object!');
446
+ }
447
+
448
+ if (this.params.systemShortCodeName) {
449
+ DyNTS_global_settings.systemShortCodeName = this.params.systemShortCodeName;
450
+ }
451
+
452
+ if (this.params.systemName) {
453
+ DyNTS_global_settings.systemName = this.params.systemName;
454
+ DyFM_error_defaults.issuerSystem = this.params.systemName;
455
+ }
456
+
457
+ if (this.params.version) {
458
+ DyNTS_global_settings.systemVersion = this.params.version;
459
+ DyFM_error_defaults.systemVersion = this.params.version;
460
+ }
461
+
462
+ process.stdout.write(
463
+ String.fromCharCode(27) + ']0;' +
464
+ this._params?.name +
465
+ String.fromCharCode(7)
466
+ );
467
+
468
+ DyFM_error_defaults.issuerSystem = this._params.systemName;
469
+
470
+ this.overrideDynamoNTSGlobalSettings?.();
471
+
472
+ if (DyNTS_global_settings.log_settings.setup) {
473
+ DyFM_Log.S_info(`env settings;\n`, {
474
+ systemName: DyNTS_global_settings.systemName,
475
+ systemShortCodeName: DyNTS_global_settings.systemShortCodeName,
476
+ systemVersion: DyNTS_global_settings.systemVersion,
477
+ environment: DyNTS_global_settings.env_settings.environment,
478
+ /* log_settings: DyNTS_global_settings.log_settings,
479
+ env_settings: DyNTS_global_settings.env_settings, */
480
+ });
481
+ }
482
+
483
+ this.globalService = DyNTS_GlobalService.getInstance();
484
+ await DyNTS_GlobalService.setServices(this.getGlobalServiceCollection());
485
+ DyNTS_GlobalService.setParams(this.params);
486
+
487
+ if (this.getPortSettings) {
488
+ this._portSettings = this.getPortSettings();
489
+ }
490
+
491
+ if (this.getCertificationSettings) {
492
+ this._cert = this.getCertificationSettings();
493
+ }
494
+
495
+ if (this.getApiBasePath) {
496
+ DyNTS_global_settings.baseUrl = this.getApiBasePath();
497
+ }
498
+
499
+ if (this.getRoutingModules) {
500
+ this._routingModules = this.getRoutingModules();
501
+
502
+ // ezt egyelőre csak tesztelem, nem kerül be
503
+ /* if (
504
+ !DyNTS_global_settings.dontCreateDefaultRoute &&
505
+ !this._routingModules.some((routingModule: DyNTS_RoutingModule): boolean => routingModule.route === '/*')
506
+ ) {
507
+ this._routingModules.push(
508
+ DyNTS_getStarRoute()
509
+ );
510
+ } */
511
+ }
512
+
513
+ if (this._params.dbUri) {
514
+ await this.startDB();
515
+
516
+ // createEntries csak akkor fut, ha a DB tényleg fel-jött.
517
+ // Ha a startDB jelezte a hibát, de továbbment, akkor a started flag false marad,
518
+ // és nem próbálunk meg írni egy nem létező kapcsolatra.
519
+ if (this.createEntries && this.systemControls.mongoose.started) {
520
+ await this.createEntries();
521
+ }
522
+ } else {
523
+ DyFM_Log.log(
524
+ `\nNo database connection created.`,
525
+ );
526
+ }
527
+
528
+ if (this._routingModules?.length) {
529
+ if (this.logSetup) DyFM_Log.log('\nsetting up express routes...');
530
+
531
+ this.setSecurity();
532
+
533
+ await this.initExpresses();
534
+ await this.startExpresses();
535
+
536
+ if (this._security !== DyNTS_RouteSecurity.secure) {
537
+ await this.mountOpenRoutes();
538
+ }
539
+
540
+ if (this._security !== DyNTS_RouteSecurity.open && this._cert) {
541
+ await this.mountSecureRoutes();
542
+ }
543
+
544
+ // Generikus, auth-agnosztikus extension-point: custom middleware az API
545
+ // route-ok UTÁN, de a SPA static catch-all (mountStaticClient) ELŐTT.
546
+ await this.mountCustomMiddleware();
547
+
548
+ await this.mountStaticClient();
549
+
550
+ if (this.logSetup) {
551
+ DyFM_Log.log(`\nRoutes mounted.... server using security: ${this._security}`);
552
+ }
553
+ } else {
554
+ DyFM_Log.warn(
555
+ `\nNo routes mounted!`,
556
+ );
557
+ }
558
+
559
+ if (this.getRootServices) {
560
+ this._rootServices = await this.getRootServices();
561
+ }
562
+
563
+ if (this.postProcess) {
564
+ await this.postProcess().catch((error: any): void => {
565
+ DyFM_Error.logSimple(`"${this._params.name}" postProcess failed:`, error);
566
+ DyNTS_GlobalService.globalErrorHandler?.(error);
567
+ });
568
+ }
569
+
570
+ // FR-193 — bedrock OOM korai-figyelmeztetés: feltelepítjük a heap-watchdogot, ha
571
+ // engedélyezve (DyNTS_global_settings.memoryGuard.enabled, default true). Biztonságos:
572
+ // a guard egy könnyű setInterval, ami SOHA nem dob; csak near-OOM küszöböknél hagy
573
+ // tartós nyomot az error-sinkbe, mielőtt a fatal heap-OOM megölné a process-t.
574
+ try {
575
+ if (DyNTS_global_settings.memoryGuard?.enabled) {
576
+ DyNTS_MemoryGuard.getInstance().install();
577
+ }
578
+ } catch (memoryGuardError: unknown) {
579
+ DyFM_Log.warn('[DyNTS_MemoryGuard] auto-install skipped (non-fatal):', memoryGuardError);
580
+ }
581
+
582
+ if (!extended) {
583
+ await this.ready();
584
+
585
+ if (this.params.title) {
586
+ DyFM_Log.success(this.params.title);
587
+ }
588
+ DyFM_Log.info(`Version: ${this.params.version}`);
589
+ /* DyFM_Log.info(`NTS Version: ${this.ntsVersion}`); */
590
+ DyFM_Log.H_success(`${this.params.name} started successfully.`);
591
+ }
592
+ } catch (error) {
593
+ this.constructErrors.push(error);
594
+
595
+ if (this.deepLog) {
596
+ if (DyNTS_global_settings.log_settings.highDetailedLogs) {
597
+ DyFM_Log.H_error(
598
+ `"${this._params.name}" start failed (in asyncConstruct (highDetailedLog)). `,
599
+ `\n\n construct ERRORS:`, this.constructErrors,
600
+ '\n\nlast error:', error
601
+ );
602
+ } else {
603
+ DyFM_Log.H_error(
604
+ `"${this._params.name}" start failed (in asyncConstruct). `,
605
+ `\n\n construct ERRORS:`, this.getSimplifiedConstructErrors(),
606
+ '\n\nlast error:', error instanceof DyFM_Error ? error.getErrorSimplified() : error
607
+ );
608
+ }
609
+ }
610
+
611
+ throw new DyFM_Error({
612
+ ...this._getDefaultErrorSettings('asyncConstruct', error),
613
+
614
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-001`,
615
+ additionalContent: {
616
+ constructErrors: this.constructErrors,
617
+ systemControls: this.systemControls,
618
+ systemReadies: {
619
+ app: this.systemControls.app.getIsReady(),
620
+ mongoose: this.systemControls.mongoose.getIsReady(),
621
+ httpServer: this.systemControls.httpServer.getIsReady(),
622
+ httpsServer: this.systemControls.httpsServer.getIsReady(),
623
+ },
624
+ },
625
+ });
626
+ }
627
+ }
628
+
629
+ async ready(timeout: number = this.defaultReadyTimeout): Promise<void> {
630
+ try {
631
+ if (this.fnLogs) DyFM_Log.log('\nfn:. ready');
632
+
633
+ await DyFM_Async.delay(100);
634
+
635
+ let ready: boolean = false;
636
+ const start: number = +new Date();
637
+
638
+ if (this.constructErrors.length) {
639
+ if (this.deepLog) {
640
+ if (DyNTS_global_settings.log_settings.highDetailedLogs) {
641
+ DyFM_Log.H_error(
642
+ `"${this._params.name}" start failed. (ready; constructErrors check 1)`,
643
+ `\n construct ERRORS:`, this.constructErrors
644
+ );
645
+ } else {
646
+ DyFM_Log.H_error(
647
+ `"${this._params.name}" start failed. (ready; constructErrors check 1)`,
648
+ `\n construct ERRORS:`, this.getSimplifiedConstructErrors(),
649
+ );
650
+ }
651
+ }
652
+
653
+ throw new DyFM_Error({
654
+ ...this._getDefaultErrorSettings(
655
+ 'ready',
656
+ new Error(`"${this._params.name}" start failed.`)
657
+ ),
658
+
659
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-R01`,
660
+ additionalContent:
661
+ this.constructErrors.length === 1 ?
662
+ { error: this.constructErrors[0] } :
663
+ { errors: this.constructErrors },
664
+ });
665
+ }
666
+
667
+ while (!ready && +new Date() - start < timeout) {
668
+ if (this.systemControls.app.init) {
669
+ ready = (
670
+ this.systemControls.mongoose.getIsReady() &&
671
+ this.systemControls.httpServer.getIsReady() &&
672
+ this.systemControls.httpsServer.getIsReady()
673
+ );
674
+ } else {
675
+ DyFM_Log.error(`"${this._params.name}" APP NOT INITIALIZED while trying to get ready.`);
676
+ }
677
+
678
+ if (!ready) {
679
+ await DyFM_Async.wait(100);
680
+ }
681
+ }
682
+
683
+ if (timeout < +new Date() - start) {
684
+ if (this.deepLog) {
685
+ if (DyNTS_global_settings.log_settings.highDetailedLogs) {
686
+ DyFM_Log.H_error(
687
+ `"${this._params.name}" start failed. (ready; TIMEOUT check)`,
688
+ `\n construct ERRORS:`, this.constructErrors
689
+ );
690
+ } else {
691
+ DyFM_Log.H_error(
692
+ `"${this._params.name}" start failed. (ready; TIMEOUT check)`,
693
+ `\n construct ERRORS:`, this.getSimplifiedConstructErrors(),
694
+ );
695
+ }
696
+ }
697
+
698
+ throw new DyFM_Error({
699
+ ...this._getDefaultErrorSettings(
700
+ 'ready',
701
+ new Error(`"${this._params.name}" start failed. TIMEOUT`)
702
+ ),
703
+
704
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-R02`,
705
+ additionalContent: {
706
+ constructErrors: this.constructErrors,
707
+ systemControls: this.systemControls,
708
+ systemReadies: {
709
+ mongoose: this.systemControls.mongoose.getIsReady(),
710
+ httpServer: this.systemControls.httpServer.getIsReady(),
711
+ httpsServer: this.systemControls.httpsServer.getIsReady(),
712
+ },
713
+ },
714
+ });
715
+ }
716
+
717
+ if (this.constructErrors.length) {
718
+ if (this.deepLog) {
719
+ if (DyNTS_global_settings.log_settings.highDetailedLogs) {
720
+ DyFM_Log.H_error(
721
+ `"${this._params.name}" start failed. (ready; constructErrors check 2)`,
722
+ `\n construct ERRORS:`, this.constructErrors
723
+ );
724
+ } else {
725
+ DyFM_Log.H_error(
726
+ `"${this._params.name}" start failed. (ready; constructErrors check 2)`,
727
+ `\n construct ERRORS:`, this.getSimplifiedConstructErrors(),
728
+ );
729
+ }
730
+ }
731
+
732
+ throw new DyFM_Error({
733
+ ...this._getDefaultErrorSettings(
734
+ 'ready',
735
+ new Error(`"${this._params.name}" start failed.`)
736
+ ),
737
+
738
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-R03`,
739
+ additionalContent: this.constructErrors,
740
+ });
741
+ }
742
+
743
+ if (ready) {
744
+ this.systemControls.app.started = true;
745
+
746
+ if (this.fnLogs && this.deepLog) DyFM_Log.log('\nfn:. ready: return');
747
+
748
+ return;
749
+ }
750
+
751
+ this.systemControls.app.started = false;
752
+
753
+ let msg: string = `"${this._params.name}" start failed. UNKNOWN`;
754
+
755
+ if (this.systemControls.mongoose.init && !this.systemControls.mongoose.started) {
756
+ msg += `\nMongoose start failed.`;
757
+ }
758
+
759
+ if (this.systemControls.httpServer.init && !this.systemControls.httpServer.started) {
760
+ msg += `\nHTTP Server start failed.`;
761
+ }
762
+
763
+ if (this.systemControls.httpsServer.init && !this.systemControls.httpsServer.started) {
764
+ msg += `\nHTTPS Server start failed.`;
765
+ }
766
+
767
+ DyFM_Log.error(msg, this.constructErrors);
768
+
769
+ throw new DyFM_Error({
770
+ ...this._getDefaultErrorSettings('ready', new Error(msg)),
771
+
772
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-R04`,
773
+ additionalContent: {
774
+ constructErrors: this.constructErrors,
775
+ systemControls: this.systemControls,
776
+ systemReadies: {
777
+ app: this.systemControls.app.getIsReady(),
778
+ mongoose: this.systemControls.mongoose.getIsReady(),
779
+ httpServer: this.systemControls.httpServer.getIsReady(),
780
+ httpsServer: this.systemControls.httpsServer.getIsReady(),
781
+ },
782
+ },
783
+ error: this.constructErrors?.[0] ?? new Error(),
784
+ });
785
+ } catch (error) {
786
+ throw new DyFM_Error({
787
+ ...this._getDefaultErrorSettings('ready', error),
788
+
789
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-READY0`,
790
+ });
791
+ }
792
+ }
793
+
794
+ protected getSimplifiedConstructErrors(): string[] {
795
+ return this.constructErrors.map((error: any): any => {
796
+ if (error instanceof DyFM_Error) {
797
+ return error.getErrorSimplified();
798
+ } else {
799
+ return error;
800
+ }
801
+ });
802
+ }
803
+
804
+ async stop(dontLog?: boolean): Promise<void> {
805
+ try {
806
+ DyFM_Log.info('\nstopping server...\n');
807
+
808
+ await this.ready();
809
+
810
+ if (this.started) {
811
+
812
+ if (this.systemControls.mongoose.init) {
813
+ DyFM_Log.info(`\nstopping Mongoose....`);
814
+
815
+ let tryCount: number = 0;
816
+
817
+ while (
818
+ !this.systemControls.mongoose.started &&
819
+ !this.constructErrors.length &&
820
+ tryCount++ < 10
821
+ ) {
822
+ DyFM_Log.warn(`Mongoose not even started yet....`);
823
+ await DyFM_Async.wait(second);
824
+ }
825
+ this.systemControls.mongoose.started = false;
826
+
827
+ if (this.mongoose) {
828
+ await DyFM_Array.asyncForEach(
829
+ Object.keys(this.mongoose.models),
830
+ async (modelName): Promise<void> => {
831
+ this.mongoose.deleteModel(modelName);
832
+ }
833
+ );
834
+
835
+ const disconnect: Promise<void> = new Promise((resolve): void => {
836
+ this.mongoose.connection.on('disconnecting', (): void => {
837
+ resolve();
838
+ });
839
+ });
840
+
841
+ await this.mongoose.disconnect();
842
+ await this.mongoose.connection.close();
843
+ await disconnect;
844
+
845
+ this.mongoose.connection.removeAllListeners();
846
+
847
+ while (
848
+ this.mongoose.connection.readyState !== 0 &&
849
+ !this.constructErrors.length
850
+ ) {
851
+ DyFM_Log.warn(`\nMongoose still not disconnected....`);
852
+ await DyFM_Async.wait(second);
853
+ }
854
+ } else {
855
+ DyFM_Log.error(`\nMongoose not found.`);
856
+ }
857
+ this.systemControls.mongoose.init = false;
858
+ }
859
+
860
+ if (this.systemControls.httpServer.init) {
861
+ this.systemControls.httpServer.started = false;
862
+
863
+ if (this.httpServer) {
864
+ await new Promise((resolve): void => {
865
+ this.httpServer.close(resolve);
866
+ });
867
+ } else {
868
+ DyFM_Log.error(`\nHTTP Server not found.`);
869
+ }
870
+ this.systemControls.httpServer.init = false;
871
+ }
872
+
873
+ if (this.systemControls.httpsServer.init) {
874
+ this.systemControls.httpsServer.started = false;
875
+
876
+ if (this.httpsServer) {
877
+ await new Promise((resolve): void => {
878
+ this.httpsServer.close(resolve);
879
+ });
880
+ } else {
881
+ DyFM_Log.error(`\nHTTPS Server not found.`);
882
+ }
883
+ this.systemControls.httpsServer.init = false;
884
+ }
885
+
886
+ await DyFM_Async.wait(second);
887
+
888
+ if (!dontLog) {
889
+ DyFM_Log.H_log(`"${this._params.name}" stopped successfully.`);
890
+ }
891
+ }
892
+ } catch (error) {
893
+ throw new DyFM_Error({
894
+ ...this._getDefaultErrorSettings('stop', error),
895
+
896
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-STOP0`,
897
+ });
898
+ }
899
+ }
900
+
901
+ /**
902
+ *
903
+ */
904
+ private async startDB(): Promise<void> {
905
+ if (this.fnLogs && this.deepLog) DyFM_Log.log('\nfn:. startDB');
906
+ else if (this.logSetup) DyFM_Log.log('\nstarting DB connection...');
907
+
908
+ if (!this._params.dbUri) {
909
+ throw new DyFM_Error({
910
+ ...this._getDefaultErrorSettings('startDB', new Error('DB URI is not set.')),
911
+
912
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SDB0`,
913
+ });
914
+ }
915
+
916
+ if (!this._params.dbOptions) {
917
+ throw new DyFM_Error({
918
+ ...this._getDefaultErrorSettings('startDB', new Error('DB OPTIONS are not set.')),
919
+
920
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SDB0`,
921
+ });
922
+ }
923
+
924
+ if (this.systemControls.mongoose.init || this.systemControls.mongoose.started) {
925
+ throw new DyFM_Error({
926
+ ...this._getDefaultErrorSettings('startDB', new Error('Mongoose is already initialized.')),
927
+
928
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SDB0`,
929
+ });
930
+ }
931
+
932
+ try {
933
+ await new Promise<void>(
934
+ (resolve, reject): void => {
935
+ this.systemControls.mongoose.init = true;
936
+
937
+ this.mongoose.connection
938
+ .once('open', (): void => {
939
+ this.systemControls.mongoose.started = true;
940
+ DyFM_Log.success(`\nConnected to MongoDB (${this._params.dbUri})\n`);
941
+
942
+ resolve();
943
+
944
+ // FR-258 / SR-2 — install declared-retention TTL indexes. Fire-and-forget +
945
+ // fully non-fatal: an index build on a large collection must NOT block startup,
946
+ // and any failure here is logged but never crashes the app.
947
+ void this.installRetentionTtlIndexes();
948
+ })
949
+ .on('error', (error): void => {
950
+ if (!this.systemControls.mongoose.started) {
951
+ // Initial DB-csatlakozás sikertelen:
952
+ // jelezzük a hibát (log + globalErrorHandler), de NEM szakítjuk meg az
953
+ // App startup-ot (nincs constructErrors push, nincs reject).
954
+ // A mongoose.init-et resetteljük, hogy a ready() ne várjon a DB-re.
955
+ // Ha a mongoose később mégis tudna csatlakozni, az 'open' event
956
+ // beállítja a started flag-et, így a runtime DB-műveletek elindulnak.
957
+ this.systemControls.mongoose.init = false;
958
+
959
+ const d_error: DyFM_Error = new DyFM_Error({
960
+ ...this._getDefaultErrorSettings('startDB', error),
961
+
962
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SDB1`,
963
+ message: `Unable to connect to MongoDB server (${this._params.dbUri}), ` +
964
+ `ERROR: ${error}`,
965
+ level: DyFM_ErrorLevel.serious,
966
+ });
967
+
968
+ DyFM_Log.H_error(
969
+ `\nUnable to connect to MongoDB server (${this._params.dbUri}).` +
970
+ `\nServer will continue WITHOUT DB connection.` +
971
+ `\nDB-using endpoints will fail at runtime until connection recovers.`
972
+ );
973
+
974
+ if (this.debugLog) DyFM_Log.S_error(
975
+ `\nMongoDB connect ERROR: `,
976
+ error
977
+ );
978
+
979
+ DyNTS_GlobalService.globalErrorHandler?.(d_error);
980
+
981
+ resolve();
982
+
983
+ } else {
984
+ if (this.debugLog) DyFM_Log.error('\nMongoDB ERROR: ', error);
985
+
986
+ const d_error: DyFM_Error = new DyFM_Error({
987
+ ...this._getDefaultErrorSettings('mongoose.connection.on(error)', error),
988
+
989
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SDB2`,
990
+ message: `MongoDB ERROR: ${error}`,
991
+ level: DyFM_ErrorLevel.critical,
992
+ });
993
+
994
+ DyNTS_GlobalService.globalErrorHandler?.(d_error);
995
+ }
996
+ });
997
+
998
+ try {
999
+ this.mongoose.connect(
1000
+ this._params.dbUri,
1001
+ this._params.dbOptions
1002
+ /* {
1003
+ directConnection: true,
1004
+ } */
1005
+ );
1006
+ } catch (error) {
1007
+ throw new DyFM_Error({
1008
+ ...this._getDefaultErrorSettings('startDB', error),
1009
+
1010
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SDB3`,
1011
+ });
1012
+ }
1013
+ }
1014
+ );
1015
+ } catch (error) {
1016
+ throw new DyFM_Error({
1017
+ ...this._getDefaultErrorSettings('startDB', error),
1018
+
1019
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SDB0`,
1020
+ });
1021
+ }
1022
+
1023
+ // 2026-06-20 — Mongo reconnect-guard. A MongoDB-driver a hostnevet connect-kor EGYSZER
1024
+ // resolválja + cache-eli az IP-t; ha a Mongo-konténert recreate-elik (új docker-network IP),
1025
+ // a driver a HALOTT IP-n ragad ECONNREFUSED + buffering-timeout, és a service magától SOHA
1026
+ // nem áll vissza. A guard sustained-disconnect (readyState !== 1) esetén — a driver saját
1027
+ // grace-e UTÁN TELJES disconnect()+connect()-et csinál → ÚJ MongoClient → ÚJ DNS-resolve →
1028
+ // új IP → reconnect. Best-effort, always-on, healthy connection-t (===1) SOHA nem bánt; csak
1029
+ // konténer-IP-frissítés, nem ír adatot. Lásd: mongo-reconnect-guard.util.ts.
1030
+ try {
1031
+ startMongoReconnectGuard({
1032
+ getReadyState: (): number => this.mongoose.connection.readyState,
1033
+ reconnect: async (): Promise<void> => {
1034
+ await this.mongoose.disconnect().catch((): void => undefined);
1035
+ await this.mongoose.connect(this._params.dbUri, this._params.dbOptions);
1036
+ },
1037
+ log: (msg: string): void => DyFM_Log.warn(msg),
1038
+ });
1039
+ } catch (guardErr) {
1040
+ DyFM_Log.warn(`[mongo-reconnect-guard] failed to start (non-fatal): ${guardErr instanceof Error ? guardErr.message : String(guardErr)}`);
1041
+ }
1042
+ }
1043
+
1044
+ /**
1045
+ * FR-258 / SR-2 install MongoDB TTL indexes for every data-model that declared `retention`.
1046
+ *
1047
+ * Runs ONCE right after the DB connection opens. For each registered DB service (including the
1048
+ * auto-created `_archived` siblings, which inherit the parent model's `retention`), it ensures a
1049
+ * TTL index on the `__created` Date field with the resolved `expireAfterSeconds` — so MongoDB
1050
+ * NATIVELY auto-deletes documents older than the configured age. This is the fleet-wide defense
1051
+ * against the unbounded-collection-growth → in-memory-load → GC-thrash/OOM class.
1052
+ *
1053
+ * Guarantees:
1054
+ * - **Non-fatal** — any error is logged but NEVER blocks startup (called fire-and-forget).
1055
+ * - **Idempotent** — an existing index with the same TTL is a no-op; a CHANGED retention (different
1056
+ * `expireAfterSeconds`, or an existing non-TTL `__created` index) is reconciled by drop+recreate
1057
+ * (the field index is restored immediately, so query coverage is preserved).
1058
+ */
1059
+ private async installRetentionTtlIndexes(): Promise<void> {
1060
+ try {
1061
+ const services: DyNTS_DBService<any>[] = DyNTS_GlobalService.getAllDBServices();
1062
+ let ensured: number = 0;
1063
+
1064
+ for (const service of services) {
1065
+ const ttlSeconds: number | undefined = DyFM_resolveRetentionTtlSeconds(service?.dataParams?.retention);
1066
+ if (!ttlSeconds) { continue; }
1067
+
1068
+ const dataName: string = service.dataParams.dataName;
1069
+ // The mongoose Model exposes the native driver collection (.indexes / .createIndex / .dropIndex).
1070
+ const collection: DyNTS_TtlIndexCollection | undefined =
1071
+ (service?.dataModel as unknown as { collection?: DyNTS_TtlIndexCollection })?.collection;
1072
+ if (!collection || typeof collection.createIndex !== 'function') { continue; }
1073
+
1074
+ try {
1075
+ const action: DyNTS_TtlIndexAction = await DyNTS_App.ensureRetentionTtlIndex(collection, ttlSeconds);
1076
+ if (action === 'created' || action === 'updated') {
1077
+ ensured++;
1078
+ if (this.logSetup) {
1079
+ DyFM_Log.success(
1080
+ `[FR-258 retention] TTL index ${action} on "${dataName}" — ` +
1081
+ `${Math.round(ttlSeconds / 86400)}d (${ttlSeconds}s)`,
1082
+ );
1083
+ }
1084
+ }
1085
+ } catch (idxErr: unknown) {
1086
+ DyFM_Log.warn(
1087
+ `[FR-258 retention] TTL index on "${dataName}" failed (non-fatal): ` +
1088
+ `${idxErr instanceof Error ? idxErr.message : String(idxErr)}`,
1089
+ );
1090
+ }
1091
+ }
1092
+
1093
+ if (ensured > 0) {
1094
+ DyFM_Log.success(`[FR-258 retention] ${ensured} TTL index(es) ensured (auto-retention active).`);
1095
+ }
1096
+ } catch (err: unknown) {
1097
+ DyFM_Log.warn(
1098
+ `[FR-258 retention] TTL-installer failed (non-fatal): ` +
1099
+ `${err instanceof Error ? err.message : String(err)}`,
1100
+ );
1101
+ }
1102
+ }
1103
+
1104
+ /**
1105
+ * FR-258 / SR-2 — ensure a single `{ __created: 1 }` TTL index with the given `expireAfterSeconds`.
1106
+ * Extracted as a pure-ish static so it is unit-testable with a mock collection. Idempotent:
1107
+ * - no existing `__created` index → create it → `'created'`
1108
+ * - existing with the SAME TTL → no-op → `'noop'`
1109
+ * - existing with a DIFFERENT TTL / non-TTL → drop + recreate → `'updated'`
1110
+ * Drop+recreate preserves query coverage (the `{__created:1}` index is restored immediately).
1111
+ */
1112
+ static async ensureRetentionTtlIndex(
1113
+ collection: DyNTS_TtlIndexCollection,
1114
+ ttlSeconds: number,
1115
+ ): Promise<DyNTS_TtlIndexAction> {
1116
+ const existingList: DyNTS_TtlIndexInfo[] = typeof collection.indexes === 'function'
1117
+ ? await collection.indexes().catch((): DyNTS_TtlIndexInfo[] => [])
1118
+ : [];
1119
+ const existing: DyNTS_TtlIndexInfo | undefined =
1120
+ existingList.find((ix): boolean => !!ix?.key && ix.key.__created === 1);
1121
+
1122
+ if (!existing) {
1123
+ await collection.createIndex({ __created: 1 }, { expireAfterSeconds: ttlSeconds });
1124
+ return 'created';
1125
+ }
1126
+ if (existing.expireAfterSeconds === ttlSeconds) {
1127
+ return 'noop';
1128
+ }
1129
+ // Retention changed, or an existing non-TTL `__created` index → reconcile by drop+recreate.
1130
+ if (existing.name && typeof collection.dropIndex === 'function') {
1131
+ await collection.dropIndex(existing.name).catch((): void => undefined);
1132
+ }
1133
+ await collection.createIndex({ __created: 1 }, { expireAfterSeconds: ttlSeconds });
1134
+ return 'updated';
1135
+ }
1136
+
1137
+ /**
1138
+ *
1139
+ */
1140
+ private async initExpresses(): Promise<void> {
1141
+ if (this.fnLogs && this.deepLog) DyFM_Log.log('\nfn:. initExpresses');
1142
+
1143
+ try {
1144
+ if (this._security && this._security !== DyNTS_RouteSecurity.secure) {
1145
+ if (this._portSettings.httpPort === undefined || this._portSettings.httpPort === null) {
1146
+ let errorMsg: string =
1147
+ `\nYou have open routes, but httpPort is not set!` +
1148
+ `\nThere are ${this._routingModules.filter(
1149
+ m => m.security != DyNTS_RouteSecurity.secure
1150
+ ).length} open/both routes` +
1151
+ `\nroot security: ${this._security}` +
1152
+ `\nset httpPort in DynamoBEServer - setupRoutingModules() to enable secure routes.`;
1153
+
1154
+ errorMsg += '\n\nThe routes setted to use open server:';
1155
+ this._routingModules.forEach((module: DyNTS_RoutingModule): void => {
1156
+ if (module.security != DyNTS_RouteSecurity.secure) {
1157
+ errorMsg += `\n ${module.route} (security: ${module.security})`;
1158
+ errorMsg += `\n subroutes using open sever:`;
1159
+ module.endpoints.forEach((endpoint: DyNTS_Endpoint_Params): void => {
1160
+ if (endpoint.security != DyNTS_RouteSecurity.secure) {
1161
+ errorMsg += `\n ${endpoint.endpoint} (security: ${endpoint.security})`;
1162
+ }
1163
+ });
1164
+ }
1165
+ });
1166
+
1167
+ const error = new Error(`Open routes cannot be established!\n${errorMsg}`);
1168
+ const errorStack: string[] = error.stack.split('\n');
1169
+
1170
+ errorStack.splice(1, 2);
1171
+ error.stack = errorStack.join('\n');
1172
+
1173
+ DyFM_Log.error(errorMsg);
1174
+
1175
+ throw error;
1176
+ }
1177
+
1178
+ await this.initOpenExpress();
1179
+ }
1180
+
1181
+ if (this._security && this._security !== DyNTS_RouteSecurity.open) {
1182
+ if (!this._cert || !this._portSettings.httpsPort) {
1183
+ let errorMsg: string =
1184
+ `\nYou have secure routes, but the certification paths or httpsPort are not set!` +
1185
+ `\nThere are ${this._routingModules.filter(
1186
+ m => m.security && m.security !== DyNTS_RouteSecurity.open
1187
+ ).length} secure routes` +
1188
+ `\nroot security: ${this._security}` +
1189
+ `\nset...` +
1190
+ `\n(missing exact howto...)` +
1191
+ /* `\n httpsPort and` +
1192
+ `\n cert: {` +
1193
+ `\n keyPath: FileSystem.PathLike,` +
1194
+ `\n certPath: FileSystem.PathLike,` +
1195
+ `\n }` + */
1196
+ `\nin DynamoBEServer - getRoutingModules() to enable secure routes.`;
1197
+
1198
+ errorMsg += '\n\nThe routes setted to use secure server:';
1199
+ this._routingModules.forEach((module: DyNTS_RoutingModule): void => {
1200
+ if (module.security && module.security !== DyNTS_RouteSecurity.open) {
1201
+ errorMsg += `\n ${module.route} (security: ${module.security})`;
1202
+ errorMsg += `\n location: ${module.stackLocation}`;
1203
+ errorMsg += `\n subroutes using secure sever:`;
1204
+ module.endpoints.forEach((endpoint: DyNTS_Endpoint_Params): void => {
1205
+ if (endpoint.security && endpoint.security !== DyNTS_RouteSecurity.open) {
1206
+ errorMsg += `\n ${endpoint.endpoint} (security: ${endpoint.security})`;
1207
+ errorMsg += `\n location: ${endpoint.stackLocation}`;
1208
+ }
1209
+ });
1210
+ }
1211
+ });
1212
+
1213
+ const error = new Error(`Secure routes cannot be established!\n${errorMsg}`);
1214
+ const errorStack: string[] = error.stack.split('\n');
1215
+
1216
+ errorStack.splice(1, 2);
1217
+ error.stack = errorStack.join('\n');
1218
+
1219
+ DyFM_Log.error(errorMsg);
1220
+
1221
+ throw error;
1222
+ }
1223
+
1224
+ await this.initSecureExpress();
1225
+ }
1226
+ } catch (error) {
1227
+ throw new DyFM_Error({
1228
+ ...this._getDefaultErrorSettings('initExpresses', error),
1229
+
1230
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-IE0`,
1231
+ });
1232
+ }
1233
+ }
1234
+
1235
+ /**
1236
+ *
1237
+ */
1238
+ protected async initOpenExpress(): Promise<void> {
1239
+ if (this.fnLogs) DyFM_Log.log('\nfn:. initOpenExpress');
1240
+ this.openExpress = Express();
1241
+ this.openExpress.set('maxHeaderSize', 10 * megabyte); // 1024 * 1024 * 10, // 10MB
1242
+ this.openExpress.use(BodyParser.urlencoded(this._portSettings.httpUrlencoded));
1243
+ this.openExpress.use(BodyParser.json(this._portSettings.httpJson));
1244
+ // FR-041 — CORS allowlist enforcement. No-op if getCorsSettings() is not overridden.
1245
+ this.mountCors(this.openExpress);
1246
+ }
1247
+
1248
+ /**
1249
+ *
1250
+ */
1251
+ protected async initSecureExpress(): Promise<void> {
1252
+ if (this.fnLogs) DyFM_Log.log('\nfn:. initSecureExpress');
1253
+ this.secureExpress = Express();
1254
+ this.secureExpress.use(BodyParser.urlencoded(this._portSettings.httpsUrlencoded));
1255
+ this.secureExpress.use(BodyParser.json(this._portSettings.httpsJson));
1256
+ // FR-041 — CORS allowlist enforcement (same as open express).
1257
+ this.mountCors(this.secureExpress);
1258
+
1259
+ const options = {
1260
+ key: FileSystem.readFileSync(this._cert.keyPath),
1261
+ cert: FileSystem.readFileSync(this._cert.certPath),
1262
+ maxHeaderSize: 10 * megabyte, // 1024 * 1024 * 10, // 10MB
1263
+ };
1264
+
1265
+ this.httpsServer = Https.createServer(options, this.secureExpress);
1266
+ }
1267
+
1268
+ /**
1269
+ *
1270
+ */
1271
+ private async startExpresses(): Promise<void> {
1272
+ if (this.fnLogs && this.deepLog) DyFM_Log.log('\nfn:. startExpresses');
1273
+
1274
+ try {
1275
+ if (this._security && this._security !== DyNTS_RouteSecurity.open) {
1276
+ await new Promise<void>((resolve, reject): void => {
1277
+ this.systemControls.httpsServer.init = true;
1278
+ this.httpsServer
1279
+ .listen(
1280
+ this._portSettings.httpsPort,
1281
+ this.params.secureHost,
1282
+ this.params.expressBacklog,
1283
+ (): void => {
1284
+ this.systemControls.httpsServer.started = true;
1285
+ DyFM_Log.success(
1286
+ `\nHTTPS (secure) server is listening on port: ` +
1287
+ `${this.params.secureHost}:${this._portSettings.httpsPort}`
1288
+ );
1289
+
1290
+ resolve();
1291
+ })
1292
+ .on('error', (error): void => {
1293
+ if (this.debugLog) DyFM_Log.error(`\nHTTPS (secure) server ERROR`, error);
1294
+
1295
+ if (!this.systemControls.httpsServer.started) {
1296
+ const d_error: DyFM_Error = new DyFM_Error({
1297
+ ...this._getDefaultErrorSettings('startExpresses', error),
1298
+
1299
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SE1`,
1300
+ message: `HTTPS (secure) start server ERROR`,
1301
+ });
1302
+
1303
+ this.constructErrors.push(d_error);
1304
+ DyNTS_GlobalService.globalErrorHandler?.(d_error);
1305
+
1306
+ reject(d_error);
1307
+
1308
+ } else {
1309
+ const d_error: DyFM_Error = new DyFM_Error({
1310
+ ...this._getDefaultErrorSettings('httpsServer.on(error)', error),
1311
+
1312
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SE2`,
1313
+ message: `HTTPS (secure) server ERROR`,
1314
+ level: DyFM_ErrorLevel.serious,
1315
+ });
1316
+
1317
+ DyNTS_GlobalService.globalErrorHandler?.(d_error);
1318
+ }
1319
+ })
1320
+ .on('uncaughtException', (exception): void => {
1321
+ const d_error: DyFM_Error = new DyFM_Error({
1322
+ ...this._getDefaultErrorSettings('httpsServer.on(uncaughtException)', exception),
1323
+
1324
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SE3`,
1325
+ message: `HTTPS (secure) server uncaughtException`,
1326
+ level: DyFM_ErrorLevel.critical,
1327
+ });
1328
+
1329
+ if (this.debugLog) DyFM_Log.warn(
1330
+ `\nHTTPS (secure) server uncaughtException`,
1331
+ d_error
1332
+ );
1333
+
1334
+ DyNTS_GlobalService.globalErrorHandler?.(d_error);
1335
+ });
1336
+ });
1337
+ }
1338
+
1339
+ if (this._security && this._security !== DyNTS_RouteSecurity.secure) {
1340
+ this.systemControls.httpServer.init = true;
1341
+ await new Promise<void>((resolve, reject): void => {
1342
+ this.httpServer = this.openExpress
1343
+ .listen(
1344
+ this._portSettings.httpPort,
1345
+ this.params.openHost,
1346
+ this.params.expressBacklog,
1347
+ (): void => {
1348
+ this.systemControls.httpServer.started = true;
1349
+
1350
+ const resolvedPort: number = this.httpServer?.address?.()?.['port'] ?? this._portSettings.httpPort;
1351
+
1352
+ if (this._portSettings.httpPort === 0) {
1353
+ DyFM_Log.H_warn(
1354
+ `\nHTTP (open) server is using a randomly selected port by the OS: ${resolvedPort}` +
1355
+ `\n (httpPort was set to 0 — this is intended for testing only)`
1356
+ );
1357
+ }
1358
+
1359
+ DyFM_Log.success(
1360
+ `\nHTTP (open) server is listening on port: ` +
1361
+ `${this.params.openHost}:${resolvedPort}`
1362
+ );
1363
+
1364
+ resolve();
1365
+ }
1366
+ )
1367
+ .on('error', (error): void => {
1368
+ if (this.debugLog) DyFM_Log.error(`\nHTTP (open) server ERROR`, error);
1369
+
1370
+ if (!this.systemControls.httpServer.started) {
1371
+ const d_error: DyFM_Error = new DyFM_Error({
1372
+ ...this._getDefaultErrorSettings('startExpresses', error),
1373
+
1374
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SE3`,
1375
+ message: `HTTP (open) start server ERROR`,
1376
+ });
1377
+
1378
+ this.constructErrors.push(d_error);
1379
+ DyNTS_GlobalService.globalErrorHandler?.(d_error);
1380
+
1381
+ reject(d_error);
1382
+
1383
+ } else {
1384
+ const d_error: DyFM_Error = new DyFM_Error({
1385
+ ...this._getDefaultErrorSettings('httpServer.on(error)', error),
1386
+
1387
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SE4`,
1388
+ message: `HTTP (open) server ERROR`,
1389
+ level: DyFM_ErrorLevel.serious,
1390
+ });
1391
+
1392
+ DyNTS_GlobalService.globalErrorHandler?.(d_error);
1393
+ }
1394
+ })
1395
+ .on('uncaughtException', (exception): void => {
1396
+ const d_error: DyFM_Error = new DyFM_Error({
1397
+ ...this._getDefaultErrorSettings('httpServer.on(uncaughtException)', exception),
1398
+
1399
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SE5`,
1400
+ message: `HTTP (open) server uncaughtException`,
1401
+ level: DyFM_ErrorLevel.critical,
1402
+ });
1403
+
1404
+ if (this.debugLog) {
1405
+ DyFM_Log.warn(`\nHTTP (open) server uncaughtException`, d_error);
1406
+ }
1407
+
1408
+ DyNTS_GlobalService.globalErrorHandler?.(d_error);
1409
+ });
1410
+ });
1411
+ }
1412
+ } catch (error) {
1413
+ throw new DyFM_Error({
1414
+ ...this._getDefaultErrorSettings('startExpresses', error),
1415
+
1416
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-SE0`,
1417
+ });
1418
+ }
1419
+ }
1420
+
1421
+ /**
1422
+ *
1423
+ */
1424
+ private async expressErrorHandling(error, req, res, next): Promise<void> {
1425
+ try {
1426
+ if (error) {
1427
+ const d_error: DyFM_Error = new DyFM_Error({
1428
+ ...this._getDefaultErrorSettings('expressErrorHandling', error),
1429
+
1430
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-EEH1`,
1431
+ message: `Express ERROR`,
1432
+ additionalContent: {
1433
+ req,
1434
+ res,
1435
+ },
1436
+ level: DyFM_ErrorLevel.error,
1437
+ });
1438
+
1439
+ await DyNTS_GlobalService.globalErrorHandler?.(d_error, req, res);
1440
+
1441
+ res.send(error)
1442
+ } else {
1443
+ DyFM_Log.H_error(
1444
+ 'WTF??? express error; without error?...' +
1445
+ '\nerr:', error,
1446
+ '\nreq:', req,
1447
+ '\nres:', res
1448
+ );
1449
+ }
1450
+ } catch (error) {
1451
+ DyFM_Log.H_error(
1452
+ 'MULTILEVEL ERROR (expressErrorHandling)....' +
1453
+ '\n', error
1454
+ );
1455
+ }
1456
+ }
1457
+
1458
+ /**
1459
+ *
1460
+ */
1461
+ private async mountSecureRoutes (): Promise<void> {
1462
+ try {
1463
+ if (this.fnLogs && this.deepLog) DyFM_Log.log('\nfn:. mountSecureRoutes');
1464
+
1465
+ if (!this.secureExpress) {
1466
+ throw new Error(
1467
+ 'secureExpress was not initialized. ' +
1468
+ 'Secure routes require getCertificationSettings and httpsPort, and cert files must exist.' +
1469
+ '\n\nYou need to set security to secure or both in any route and set httpsPort in getPortSettings.' +
1470
+ '\n\nYou need to set cert files in getCertificationSettings.'
1471
+ );
1472
+ }
1473
+
1474
+ this.secureExpress.use(
1475
+ (error, req, res, next): Promise<void> => this.expressErrorHandling(error, req, res, next)
1476
+ );
1477
+
1478
+ await DyFM_Array.asyncForEach(
1479
+ this._routingModules,
1480
+ async (module: DyNTS_RoutingModule): Promise<void> => {
1481
+ if (module.security !== DyNTS_RouteSecurity.open) {
1482
+ if (this.logSetup) {
1483
+ DyFM_Log.log(`route mount (secure): ${module.route}`);
1484
+ }
1485
+
1486
+ const existingRoutes: DyNTS_RoutingModule[] = this._routingModules.filter(
1487
+ (mod: DyNTS_RoutingModule): boolean => mod.route === module.route
1488
+ );
1489
+
1490
+ if (1 < existingRoutes.length) {
1491
+ const error: Error = new Error(`ROUTE DUPLICATION: ${module.route}`);
1492
+ /* const errorStack: string[] = error.stack.split('\n');
1493
+
1494
+ errorStack.splice(1, 4);
1495
+ error.stack = errorStack.join('\n'); */
1496
+ error.stack = module.stackLocation;
1497
+
1498
+ DyFM_Log.S_error(`ROUTE DUPLICATION: ${module.route}`, error);
1499
+
1500
+ throw new DyFM_Error({
1501
+ ...this._getDefaultErrorSettings('mountSecureRoutes', error),
1502
+
1503
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-MSR1`,
1504
+ message: `ROUTE DUPLICATION: ${module.route}`,
1505
+ });
1506
+ }
1507
+
1508
+ this.secureExpress.use(module.route, module.secureRouter);
1509
+ }
1510
+ }
1511
+ );
1512
+ } catch (error) {
1513
+ throw new DyFM_Error({
1514
+ ...this._getDefaultErrorSettings('mountSecureRoutes', error),
1515
+
1516
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-MSR0`,
1517
+ });
1518
+ }
1519
+ }
1520
+
1521
+ /**
1522
+ *
1523
+ */
1524
+ private async mountOpenRoutes(): Promise<void> {
1525
+ try {
1526
+ if (this.fnLogs && this.deepLog) DyFM_Log.log('\nfn:. mountOpenRoutes');
1527
+
1528
+ if (!this.openExpress) {
1529
+ throw new Error(
1530
+ 'openExpress was not initialized. Open routes require getOpenExpress and httpPort.' +
1531
+ '\n\nYou need to set security to open or both in any route and set httpPort in getPortSettings.'
1532
+ );
1533
+ }
1534
+
1535
+ this.openExpress.use(
1536
+ (error, req, res, next): Promise<void> => this.expressErrorHandling(error, req, res, next)
1537
+ );
1538
+
1539
+ await DyFM_Array.asyncForEach(
1540
+ this._routingModules,
1541
+ async (module: DyNTS_RoutingModule): Promise<void> => {
1542
+ if (module.security !== DyNTS_RouteSecurity.secure) {
1543
+ if (this.logSetup) {
1544
+ DyFM_Log.log(`route mount (open): ${module.route}`);
1545
+ }
1546
+
1547
+ const existingRoutes: DyNTS_RoutingModule[] = this._routingModules.filter(
1548
+ (mod: DyNTS_RoutingModule): boolean => mod.route === module.route
1549
+ );
1550
+
1551
+ if (1 < existingRoutes.length) {
1552
+ const error: Error = new Error(`ROUTE DUPLICATION: ${module.route}`);
1553
+ /* const errorStack: string[] = error.stack.split('\n');
1554
+
1555
+ errorStack.splice(1, 4);
1556
+ error.stack = errorStack.join('\n'); */
1557
+ error.stack = module.stackLocation;
1558
+
1559
+ DyFM_Log.S_error(`ROUTE DUPLICATION: ${module.route}`, error);
1560
+
1561
+ throw new DyFM_Error({
1562
+ ...this._getDefaultErrorSettings('mountOpenRoutes', error),
1563
+
1564
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-MOR1`,
1565
+ message: `ROUTE DUPLICATION: ${module.route}`,
1566
+ });
1567
+ }
1568
+
1569
+ this.openExpress.use(module.route, module.openRouter);
1570
+ }
1571
+ }
1572
+ );
1573
+ } catch (error) {
1574
+ throw new DyFM_Error({
1575
+ ...this._getDefaultErrorSettings('mountOpenRoutes', error),
1576
+
1577
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-AS0-MOR0`,
1578
+ });
1579
+ }
1580
+ }
1581
+
1582
+ /**
1583
+ * FR-041 CORS middleware. Echoes a matching `Access-Control-Allow-Origin`
1584
+ * for any request whose Origin header is in `getCorsSettings().allowedOrigins`,
1585
+ * adds the canonical `Access-Control-*` companion headers, and short-circuits
1586
+ * the OPTIONS preflight with a 204.
1587
+ *
1588
+ * No-op when the subclass does NOT override `getCorsSettings()` — preserves
1589
+ * back-compat for apps that have always been same-origin and never needed
1590
+ * CORS (the typical pre-FR-041 case).
1591
+ *
1592
+ * Why hand-rolled instead of the `cors` npm package:
1593
+ * - ~30 lines, zero new dependency.
1594
+ * - Full control over Vary/credentials/preflight semantics.
1595
+ * - Aligns with FDP "no unnecessary deps" philosophy.
1596
+ */
1597
+ protected mountCors(express: Express.Application): void {
1598
+ const settings: DyNTS_Cors_Settings | undefined = this.getCorsSettings?.();
1599
+ if (!settings) {
1600
+ return;
1601
+ }
1602
+
1603
+ const allowCreds: boolean = settings.allowCredentials !== false;
1604
+ const methods: string[] = settings.allowedMethods ?? [
1605
+ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS',
1606
+ ];
1607
+ const headers: string[] = settings.allowedHeaders ?? [
1608
+ 'Authorization', 'Content-Type', 'X-Admin-Key', 'X-Requested-With',
1609
+ ];
1610
+ const exposed: string[] = settings.exposedHeaders ?? [
1611
+ 'Content-Length', 'Content-Type',
1612
+ ];
1613
+ const maxAge: number = settings.maxAgeSeconds ?? 86400;
1614
+
1615
+ const isAllowed: (origin: string) => boolean = (origin: string): boolean => {
1616
+ if (settings.allowedOrigins === '*') return true;
1617
+ if (typeof settings.allowedOrigins === 'function') {
1618
+ return settings.allowedOrigins(origin);
1619
+ }
1620
+ return settings.allowedOrigins.includes(origin);
1621
+ };
1622
+
1623
+ express.use((req: Express.Request, res: Express.Response, next: Express.NextFunction): void => {
1624
+ const origin: string | undefined = req.headers.origin;
1625
+ if (origin && isAllowed(origin)) {
1626
+ // Echo the matching origin (NOT wildcard) because credentials require it.
1627
+ res.setHeader('Access-Control-Allow-Origin', origin);
1628
+ if (allowCreds) {
1629
+ res.setHeader('Access-Control-Allow-Credentials', 'true');
1630
+ }
1631
+ res.setHeader('Access-Control-Allow-Methods', methods.join(', '));
1632
+ res.setHeader('Access-Control-Allow-Headers', headers.join(', '));
1633
+ res.setHeader('Access-Control-Expose-Headers', exposed.join(', '));
1634
+ res.setHeader('Access-Control-Max-Age', String(maxAge));
1635
+ res.setHeader('Vary', 'Origin');
1636
+ }
1637
+ if (req.method === 'OPTIONS') {
1638
+ res.status(204).end();
1639
+ return;
1640
+ }
1641
+ next();
1642
+ });
1643
+ }
1644
+
1645
+ /**
1646
+ * Generikus, auth-agnosztikus extension-point orchestrator. A `registerCustomMiddleware`
1647
+ * hookot hívja (ha a subclass override-olja) minden AKTÍV Express instance-ra
1648
+ * (open + secure), az API route-ok UTÁN, de a SPA static catch-all (`app.get('*')`)
1649
+ * ELŐTT — ez az EGYETLEN korrekt pont egy sub-path middleware-hez (pl. reverse-proxy),
1650
+ * mert a catch-all minden utána mountolt route-ot elnyel. No-op ha nincs override (back-compat).
1651
+ */
1652
+ private async mountCustomMiddleware(): Promise<void> {
1653
+ if (!this.registerCustomMiddleware) {
1654
+ return;
1655
+ }
1656
+
1657
+ if (this.openExpress) {
1658
+ await this.registerCustomMiddleware(this.openExpress, DyNTS_RouteSecurity.open);
1659
+ }
1660
+
1661
+ if (this.secureExpress) {
1662
+ await this.registerCustomMiddleware(this.secureExpress, DyNTS_RouteSecurity.secure);
1663
+ }
1664
+
1665
+ if (this.logSetup) {
1666
+ DyFM_Log.log('\nCustom middleware registered (pre-static).');
1667
+ }
1668
+ }
1669
+
1670
+ /**
1671
+ * Ha getStaticClientSettings() visszaad beállításokat, a client static fájlok a '/' alatt
1672
+ * lesznek kiszolgálva (API route-ok után). SPA fallback opcionális (fallbackPath).
1673
+ * Asset cache (assetCacheMaxAge, assetCacheImmutable) és fallback cache (fallbackCacheMaxAge)
1674
+ * opcionálisak; ha nincs megadva fallbackCacheMaxAge, DyNTS_defaultFallbackCacheMaxAge (0) marad.
1675
+ */
1676
+ private async mountStaticClient(): Promise<void> {
1677
+ const settings: DyNTS_StaticClient_Settings | undefined = this.getStaticClientSettings?.();
1678
+ if (!settings) {
1679
+ return;
1680
+ }
1681
+
1682
+ const absoluteRoot: string = Path.isAbsolute(settings.root)
1683
+ ? settings.root
1684
+ : Path.resolve(process.cwd(), settings.root);
1685
+
1686
+ const hasAssetCache: boolean =
1687
+ settings.assetCacheMaxAge !== undefined || settings.assetCacheImmutable === true;
1688
+ const staticOptions: { setHeaders: (res: Express.Response) => void } | undefined =
1689
+ hasAssetCache && settings.assetCacheMaxAge !== undefined
1690
+ ? {
1691
+ setHeaders: (res: Express.Response): void => {
1692
+ const maxAge: number = settings.assetCacheMaxAge!;
1693
+ const immutable: string =
1694
+ settings.assetCacheImmutable === true ? ', immutable' : '';
1695
+ res.setHeader('Cache-Control', `max-age=${maxAge}${immutable}`);
1696
+ },
1697
+ }
1698
+ : undefined;
1699
+
1700
+ const staticMiddleware: Express.Handler =
1701
+ staticOptions
1702
+ ? Express.static(absoluteRoot, staticOptions)
1703
+ : Express.static(absoluteRoot);
1704
+
1705
+ const fallbackMaxAge: number =
1706
+ settings.fallbackCacheMaxAge ?? DyNTS_defaultFallbackCacheMaxAge;
1707
+
1708
+ if (this.openExpress) {
1709
+ this.openExpress.use('/', staticMiddleware);
1710
+ if (settings.fallbackPath) {
1711
+ this.openExpress.get('*', (req: Express.Request, res: Express.Response): void => {
1712
+ res.setHeader('Cache-Control', `max-age=${fallbackMaxAge}`);
1713
+ res.sendFile(settings.fallbackPath!, { root: absoluteRoot });
1714
+ });
1715
+ } else {
1716
+ this.openExpress.get('*', (req: Express.Request, res: Express.Response): void => {
1717
+ res.status(404).type('html').send(DyNTS_defaultNotFoundPageHtml);
1718
+ });
1719
+ }
1720
+ }
1721
+
1722
+ if (this.secureExpress) {
1723
+ this.secureExpress.use('/', staticMiddleware);
1724
+ if (settings.fallbackPath) {
1725
+ this.secureExpress.get('*', (req: Express.Request, res: Express.Response): void => {
1726
+ res.setHeader('Cache-Control', `max-age=${fallbackMaxAge}`);
1727
+ res.sendFile(settings.fallbackPath!, { root: absoluteRoot });
1728
+ });
1729
+ } else {
1730
+ this.secureExpress.get('*', (req: Express.Request, res: Express.Response): void => {
1731
+ res.status(404).type('html').send(DyNTS_defaultNotFoundPageHtml);
1732
+ });
1733
+ }
1734
+ }
1735
+
1736
+ if (this.logSetup) {
1737
+ DyFM_Log.log(`\nStatic client mounted at / (root: ${absoluteRoot})`);
1738
+ }
1739
+ }
1740
+
1741
+ /**
1742
+ *
1743
+ */
1744
+ private setSecurity(): void {
1745
+ if (this.fnLogs && this.deepLog) DyFM_Log.log('\nfn:. setSecurity');
1746
+ this._routingModules.forEach((module: DyNTS_RoutingModule): void => {
1747
+ if (!module.security) {
1748
+ DyFM_Log.warn(`RoutingModule security is not set for ${module.route}\n`);
1749
+
1750
+ } else if (!this._security) {
1751
+ this._security = module.security;
1752
+
1753
+ } else if (this._security !== module.security) {
1754
+ this._security = DyNTS_RouteSecurity.both;
1755
+ }
1756
+ });
1757
+
1758
+ if (!this._security) {
1759
+ let msg = `Could not set security for the server! (${this.security})`;
1760
+
1761
+ msg += '\n RoutingModules:';
1762
+ this._routingModules.forEach((module: DyNTS_RoutingModule): void => {
1763
+ msg += `\n ${module.route} (security: ${module.security})`;
1764
+ });
1765
+
1766
+ if (this._routingModules.length === 0) {
1767
+ msg += '\n - no RoutingModule found -\n';
1768
+ }
1769
+ DyFM_Log.warn(msg);
1770
+ }
1771
+ }
1772
+
1773
+ private _getDefaultErrorSettings(
1774
+ fnName: string,
1775
+ error: DyFM_AnyError
1776
+ ): DyFM_Error_Settings {
1777
+ return {
1778
+ status: (error as DyFM_Error)?.___status ?? 500,
1779
+ message: (error as Error)?.message ??
1780
+ (error as DyFM_Error)?._message ??
1781
+ `${fnName} was UNSUCCESSFUL (NTS)`,
1782
+ userMessage: (error as DyFM_Error)?.__userMessage ?? this.defaultErrorUserMsg,
1783
+ addECToUserMsg: !(error as DyFM_Error)?.__userMessage,
1784
+ issuerService: `${this?.constructor?.name}-DyNTS_App`,
1785
+ level: DyFM_ErrorLevel.fatal,
1786
+ error: error,
1787
+ systemVersion: DyNTS_global_settings.systemVersion,
1788
+ };
1789
+ }
1790
+
1791
+ /**
1792
+ * MISSING Description (TODO)
1793
+ */
1794
+ abstract getAppParams(): DyNTS_App_Params;
1795
+
1796
+ /**
1797
+ * MISSING Description (TODO)
1798
+ */
1799
+ abstract getGlobalServiceCollection(): DyNTS_GlobalService_Settings;
1800
+
1801
+ /**
1802
+ * MISSING Description (TODO)
1803
+ */
1804
+ abstract getPortSettings(): DyNTS_Http_Settings;
1805
+
1806
+ /**
1807
+ * MISSING Description (TODO)
1808
+ */
1809
+ overrideDynamoNTSGlobalSettings?(): void;
1810
+
1811
+ /**
1812
+ * Ha megadva, a visszaadott értéket használjuk a route prefix-ként
1813
+ * (DyNTS_global_settings.baseUrl felülírása erre az app-ra).
1814
+ */
1815
+ getApiBasePath?(): string;
1816
+
1817
+ /**
1818
+ * MISSING Description (TODO)
1819
+ */
1820
+ getRoutingModules?(): DyNTS_RoutingModule[];
1821
+
1822
+ /**
1823
+ * MISSING Description (TODO)
1824
+ */
1825
+ getRootServices?(): Promise<any[]>;
1826
+
1827
+ /**
1828
+ * MISSING Description (TODO)
1829
+ */
1830
+ getCertificationSettings?(): DyNTS_Certification_Settings;
1831
+
1832
+ /**
1833
+ * Ha megadva, a client static fájlok a '/' alatt lesznek kiszolgálva (API route-ok után).
1834
+ * Opcionális fallbackPath pl. SPA index.html-hez.
1835
+ */
1836
+ getStaticClientSettings?(): DyNTS_StaticClient_Settings | undefined;
1837
+
1838
+ /**
1839
+ * FR-041 — Ha megadva, CORS middleware mount-olódik mindkét Express instance-ra
1840
+ * (open + secure). Az `allowedOrigins` listán szereplő Origin-eket echo-zza vissza,
1841
+ * `Access-Control-Allow-Credentials: true`-val (default) együtt a Bearer JWT
1842
+ * cross-domain auth flow miatt. OPTIONS preflight 204-gyel rövidre záródik.
1843
+ * Ha undefined → no CORS headers, no preflight handling — back-compat.
1844
+ */
1845
+ getCorsSettings?(): DyNTS_Cors_Settings | undefined;
1846
+
1847
+ /**
1848
+ * Opcionális generikus hook: custom Express middleware regisztrálása az API route-ok
1849
+ * UTÁN és a SPA static catch-all ELŐTT. Auth-AGNOSZTIKUS framework extension-point —
1850
+ * a subclass (pl. FDP base App) ezen keresztül mount-olhat reverse-proxyt egy sub-path-re
1851
+ * (pl. `/auth-api`), ami a `app.get('*')` catch-all elé kell kerüljön. A hívási időzítést
1852
+ * lásd: `mountCustomMiddleware`. Undefined → no-op (back-compat).
1853
+ */
1854
+ registerCustomMiddleware?(
1855
+ express: Express.Application,
1856
+ security: DyNTS_RouteSecurity,
1857
+ ): void | Promise<void>;
1858
+
1859
+ /**
1860
+ * MISSING Description (TODO)
1861
+ */
1862
+ createEntries?(): Promise<void>;
1863
+
1864
+ /**
1865
+ * MISSING Description (TODO)
1866
+ */
1867
+ postProcess?(): Promise<void>;
1868
+
1869
+ }