@futdevpro/nts-dynamo 1.15.56 → 1.15.58

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