@futdevpro/nts-dynamo 1.15.55 → 1.15.57

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