@underpostnet/underpost 2.7.9

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 (297) hide show
  1. package/.dockerignore +14 -0
  2. package/.env.development +6 -0
  3. package/.env.production +6 -0
  4. package/.env.test +6 -0
  5. package/.github/workflows/ghpkg.yml +115 -0
  6. package/.github/workflows/publish.yml +84 -0
  7. package/.github/workflows/pwa-microservices-template.page.yml +54 -0
  8. package/.github/workflows/pwa-microservices-template.test.yml +30 -0
  9. package/.nycrc +9 -0
  10. package/.prettierignore +13 -0
  11. package/.prettierrc +9 -0
  12. package/.vscode/extensions.json +72 -0
  13. package/.vscode/settings.json +106 -0
  14. package/AUTHORS.md +10 -0
  15. package/CHANGELOG.md +85 -0
  16. package/Dockerfile +90 -0
  17. package/LICENSE +21 -0
  18. package/README.md +96 -0
  19. package/bin/cron.js +47 -0
  20. package/bin/db.js +180 -0
  21. package/bin/deploy.js +859 -0
  22. package/bin/file.js +109 -0
  23. package/bin/index.js +82 -0
  24. package/bin/ssl.js +55 -0
  25. package/bin/util.js +204 -0
  26. package/bin/vs.js +35 -0
  27. package/conf.js +265 -0
  28. package/docker-compose.yml +67 -0
  29. package/jsconfig.json +7 -0
  30. package/jsdoc.json +32 -0
  31. package/nodemon.json +6 -0
  32. package/package.json +134 -0
  33. package/prometheus.yml +36 -0
  34. package/src/api/core/core.controller.js +69 -0
  35. package/src/api/core/core.model.js +11 -0
  36. package/src/api/core/core.router.js +23 -0
  37. package/src/api/core/core.service.js +31 -0
  38. package/src/api/crypto/crypto.controller.js +51 -0
  39. package/src/api/crypto/crypto.model.js +23 -0
  40. package/src/api/crypto/crypto.router.js +20 -0
  41. package/src/api/crypto/crypto.service.js +64 -0
  42. package/src/api/default/default.controller.js +69 -0
  43. package/src/api/default/default.model.js +20 -0
  44. package/src/api/default/default.router.js +23 -0
  45. package/src/api/default/default.service.js +31 -0
  46. package/src/api/file/file.controller.js +53 -0
  47. package/src/api/file/file.model.js +19 -0
  48. package/src/api/file/file.router.js +21 -0
  49. package/src/api/file/file.service.js +80 -0
  50. package/src/api/instance/instance.controller.js +69 -0
  51. package/src/api/instance/instance.model.js +36 -0
  52. package/src/api/instance/instance.router.js +33 -0
  53. package/src/api/instance/instance.service.js +48 -0
  54. package/src/api/test/test.controller.js +59 -0
  55. package/src/api/test/test.model.js +14 -0
  56. package/src/api/test/test.router.js +21 -0
  57. package/src/api/test/test.service.js +35 -0
  58. package/src/api/user/user.build.js +16 -0
  59. package/src/api/user/user.controller.js +70 -0
  60. package/src/api/user/user.model.js +65 -0
  61. package/src/api/user/user.router.js +345 -0
  62. package/src/api/user/user.service.js +479 -0
  63. package/src/api.js +23 -0
  64. package/src/client/Default.index.js +40 -0
  65. package/src/client/components/core/Account.js +290 -0
  66. package/src/client/components/core/AgGrid.js +160 -0
  67. package/src/client/components/core/Auth.js +19 -0
  68. package/src/client/components/core/Badge.js +32 -0
  69. package/src/client/components/core/Blockchain.js +41 -0
  70. package/src/client/components/core/Blog.js +9 -0
  71. package/src/client/components/core/BtnIcon.js +101 -0
  72. package/src/client/components/core/CalendarCore.js +458 -0
  73. package/src/client/components/core/Chat.js +64 -0
  74. package/src/client/components/core/ColorPalette.js +5267 -0
  75. package/src/client/components/core/CommonJs.js +742 -0
  76. package/src/client/components/core/Content.js +193 -0
  77. package/src/client/components/core/Css.js +846 -0
  78. package/src/client/components/core/CssCore.js +844 -0
  79. package/src/client/components/core/D3Chart.js +44 -0
  80. package/src/client/components/core/Docs.js +331 -0
  81. package/src/client/components/core/DropDown.js +164 -0
  82. package/src/client/components/core/EventsUI.js +46 -0
  83. package/src/client/components/core/FileExplorer.js +699 -0
  84. package/src/client/components/core/FullScreen.js +45 -0
  85. package/src/client/components/core/Input.js +346 -0
  86. package/src/client/components/core/JoyStick.js +77 -0
  87. package/src/client/components/core/Keyboard.js +73 -0
  88. package/src/client/components/core/LoadingAnimation.js +178 -0
  89. package/src/client/components/core/LogIn.js +187 -0
  90. package/src/client/components/core/LogOut.js +58 -0
  91. package/src/client/components/core/Logger.js +26 -0
  92. package/src/client/components/core/Modal.js +1814 -0
  93. package/src/client/components/core/NotificationManager.js +84 -0
  94. package/src/client/components/core/Panel.js +613 -0
  95. package/src/client/components/core/PanelForm.js +469 -0
  96. package/src/client/components/core/Polyhedron.js +162 -0
  97. package/src/client/components/core/Recover.js +204 -0
  98. package/src/client/components/core/Responsive.js +68 -0
  99. package/src/client/components/core/RichText.js +53 -0
  100. package/src/client/components/core/Router.js +76 -0
  101. package/src/client/components/core/Scroll.js +34 -0
  102. package/src/client/components/core/SignUp.js +125 -0
  103. package/src/client/components/core/SocketIo.js +72 -0
  104. package/src/client/components/core/Stream.js +113 -0
  105. package/src/client/components/core/ToggleSwitch.js +87 -0
  106. package/src/client/components/core/ToolTip.js +26 -0
  107. package/src/client/components/core/Translate.js +446 -0
  108. package/src/client/components/core/Validator.js +100 -0
  109. package/src/client/components/core/VanillaJs.js +463 -0
  110. package/src/client/components/core/Wallet.js +106 -0
  111. package/src/client/components/core/WebComponent.js +44 -0
  112. package/src/client/components/core/Webhook.js +25 -0
  113. package/src/client/components/core/Worker.js +280 -0
  114. package/src/client/components/default/CommonDefault.js +29 -0
  115. package/src/client/components/default/CssDefault.js +13 -0
  116. package/src/client/components/default/ElementsDefault.js +38 -0
  117. package/src/client/components/default/LogInDefault.js +41 -0
  118. package/src/client/components/default/LogOutDefault.js +28 -0
  119. package/src/client/components/default/MenuDefault.js +389 -0
  120. package/src/client/components/default/RoutesDefault.js +48 -0
  121. package/src/client/components/default/SettingsDefault.js +16 -0
  122. package/src/client/components/default/SignUpDefault.js +9 -0
  123. package/src/client/components/default/SocketIoDefault.js +54 -0
  124. package/src/client/components/default/TranslateDefault.js +7 -0
  125. package/src/client/public/default/android-chrome-144x144.png +0 -0
  126. package/src/client/public/default/android-chrome-192x192.png +0 -0
  127. package/src/client/public/default/android-chrome-256x256.png +0 -0
  128. package/src/client/public/default/android-chrome-36x36.png +0 -0
  129. package/src/client/public/default/android-chrome-384x384.png +0 -0
  130. package/src/client/public/default/android-chrome-48x48.png +0 -0
  131. package/src/client/public/default/android-chrome-512x512.png +0 -0
  132. package/src/client/public/default/android-chrome-72x72.png +0 -0
  133. package/src/client/public/default/android-chrome-96x96.png +0 -0
  134. package/src/client/public/default/apple-touch-icon-1024x1024.png +0 -0
  135. package/src/client/public/default/apple-touch-icon-114x114.png +0 -0
  136. package/src/client/public/default/apple-touch-icon-120x120.png +0 -0
  137. package/src/client/public/default/apple-touch-icon-144x144.png +0 -0
  138. package/src/client/public/default/apple-touch-icon-152x152.png +0 -0
  139. package/src/client/public/default/apple-touch-icon-167x167.png +0 -0
  140. package/src/client/public/default/apple-touch-icon-180x180.png +0 -0
  141. package/src/client/public/default/apple-touch-icon-57x57.png +0 -0
  142. package/src/client/public/default/apple-touch-icon-60x60.png +0 -0
  143. package/src/client/public/default/apple-touch-icon-72x72.png +0 -0
  144. package/src/client/public/default/apple-touch-icon-76x76.png +0 -0
  145. package/src/client/public/default/apple-touch-icon-precomposed.png +0 -0
  146. package/src/client/public/default/apple-touch-icon.png +0 -0
  147. package/src/client/public/default/apple-touch-startup-image-1125x2436.png +0 -0
  148. package/src/client/public/default/apple-touch-startup-image-1136x640.png +0 -0
  149. package/src/client/public/default/apple-touch-startup-image-1170x2532.png +0 -0
  150. package/src/client/public/default/apple-touch-startup-image-1179x2556.png +0 -0
  151. package/src/client/public/default/apple-touch-startup-image-1242x2208.png +0 -0
  152. package/src/client/public/default/apple-touch-startup-image-1242x2688.png +0 -0
  153. package/src/client/public/default/apple-touch-startup-image-1284x2778.png +0 -0
  154. package/src/client/public/default/apple-touch-startup-image-1290x2796.png +0 -0
  155. package/src/client/public/default/apple-touch-startup-image-1334x750.png +0 -0
  156. package/src/client/public/default/apple-touch-startup-image-1488x2266.png +0 -0
  157. package/src/client/public/default/apple-touch-startup-image-1536x2048.png +0 -0
  158. package/src/client/public/default/apple-touch-startup-image-1620x2160.png +0 -0
  159. package/src/client/public/default/apple-touch-startup-image-1640x2160.png +0 -0
  160. package/src/client/public/default/apple-touch-startup-image-1668x2224.png +0 -0
  161. package/src/client/public/default/apple-touch-startup-image-1668x2388.png +0 -0
  162. package/src/client/public/default/apple-touch-startup-image-1792x828.png +0 -0
  163. package/src/client/public/default/apple-touch-startup-image-2048x1536.png +0 -0
  164. package/src/client/public/default/apple-touch-startup-image-2048x2732.png +0 -0
  165. package/src/client/public/default/apple-touch-startup-image-2160x1620.png +0 -0
  166. package/src/client/public/default/apple-touch-startup-image-2160x1640.png +0 -0
  167. package/src/client/public/default/apple-touch-startup-image-2208x1242.png +0 -0
  168. package/src/client/public/default/apple-touch-startup-image-2224x1668.png +0 -0
  169. package/src/client/public/default/apple-touch-startup-image-2266x1488.png +0 -0
  170. package/src/client/public/default/apple-touch-startup-image-2388x1668.png +0 -0
  171. package/src/client/public/default/apple-touch-startup-image-2436x1125.png +0 -0
  172. package/src/client/public/default/apple-touch-startup-image-2532x1170.png +0 -0
  173. package/src/client/public/default/apple-touch-startup-image-2556x1179.png +0 -0
  174. package/src/client/public/default/apple-touch-startup-image-2688x1242.png +0 -0
  175. package/src/client/public/default/apple-touch-startup-image-2732x2048.png +0 -0
  176. package/src/client/public/default/apple-touch-startup-image-2778x1284.png +0 -0
  177. package/src/client/public/default/apple-touch-startup-image-2796x1290.png +0 -0
  178. package/src/client/public/default/apple-touch-startup-image-640x1136.png +0 -0
  179. package/src/client/public/default/apple-touch-startup-image-750x1334.png +0 -0
  180. package/src/client/public/default/apple-touch-startup-image-828x1792.png +0 -0
  181. package/src/client/public/default/assets/background/white.jpg +0 -0
  182. package/src/client/public/default/assets/background/white0-min.jpg +0 -0
  183. package/src/client/public/default/assets/background/white0.jpg +0 -0
  184. package/src/client/public/default/assets/logo/base-icon.png +0 -0
  185. package/src/client/public/default/assets/mailer/api-user-check.png +0 -0
  186. package/src/client/public/default/assets/mailer/api-user-invalid-token.png +0 -0
  187. package/src/client/public/default/assets/mailer/api-user-recover.png +0 -0
  188. package/src/client/public/default/browserconfig.xml +12 -0
  189. package/src/client/public/default/favicon-16x16.png +0 -0
  190. package/src/client/public/default/favicon-32x32.png +0 -0
  191. package/src/client/public/default/favicon-48x48.png +0 -0
  192. package/src/client/public/default/favicon.ico +0 -0
  193. package/src/client/public/default/manifest.webmanifest +69 -0
  194. package/src/client/public/default/mstile-144x144.png +0 -0
  195. package/src/client/public/default/mstile-150x150.png +0 -0
  196. package/src/client/public/default/mstile-310x150.png +0 -0
  197. package/src/client/public/default/mstile-310x310.png +0 -0
  198. package/src/client/public/default/mstile-70x70.png +0 -0
  199. package/src/client/public/default/plantuml/client-conf.svg +1 -0
  200. package/src/client/public/default/plantuml/client-schema.svg +1 -0
  201. package/src/client/public/default/plantuml/cron-conf.svg +1 -0
  202. package/src/client/public/default/plantuml/cron-schema.svg +1 -0
  203. package/src/client/public/default/plantuml/server-conf.svg +1 -0
  204. package/src/client/public/default/plantuml/server-schema.svg +1 -0
  205. package/src/client/public/default/plantuml/ssr-conf.svg +1 -0
  206. package/src/client/public/default/plantuml/ssr-schema.svg +1 -0
  207. package/src/client/public/default/site.webmanifest +69 -0
  208. package/src/client/public/default/sitemap +148 -0
  209. package/src/client/public/default/yandex-browser-50x50.png +0 -0
  210. package/src/client/public/default/yandex-browser-manifest.json +9 -0
  211. package/src/client/public/doc/favicon.ico +0 -0
  212. package/src/client/public/doc/sitemap +148 -0
  213. package/src/client/public/test/favicon.ico +0 -0
  214. package/src/client/public/test/sitemap +148 -0
  215. package/src/client/services/core/core.service.js +170 -0
  216. package/src/client/services/crypto/crypto.service.js +70 -0
  217. package/src/client/services/default/default.management.js +343 -0
  218. package/src/client/services/default/default.service.js +89 -0
  219. package/src/client/services/file/file.service.js +70 -0
  220. package/src/client/services/instance/instance.management.js +74 -0
  221. package/src/client/services/instance/instance.service.js +89 -0
  222. package/src/client/services/test/test.service.js +70 -0
  223. package/src/client/services/user/user.management.js +50 -0
  224. package/src/client/services/user/user.service.js +89 -0
  225. package/src/client/ssr/Render.js +237 -0
  226. package/src/client/ssr/common/Alert.js +75 -0
  227. package/src/client/ssr/common/SsrCore.js +91 -0
  228. package/src/client/ssr/common/Translate.js +26 -0
  229. package/src/client/ssr/common/Worker.js +28 -0
  230. package/src/client/ssr/components/body/CacheControl.js +114 -0
  231. package/src/client/ssr/components/body/DefaultSplashScreen.js +90 -0
  232. package/src/client/ssr/components/email/DefaultRecoverEmail.js +21 -0
  233. package/src/client/ssr/components/email/DefaultVerifyEmail.js +17 -0
  234. package/src/client/ssr/components/head/Css.js +241 -0
  235. package/src/client/ssr/components/head/DefaultScripts.js +3 -0
  236. package/src/client/ssr/components/head/Production.js +1 -0
  237. package/src/client/ssr/components/head/Pwa.js +146 -0
  238. package/src/client/ssr/components/head/PwaDefault.js +60 -0
  239. package/src/client/ssr/components/head/Seo.js +14 -0
  240. package/src/client/ssr/pages/404.js +12 -0
  241. package/src/client/ssr/pages/500.js +12 -0
  242. package/src/client/ssr/pages/maintenance.js +14 -0
  243. package/src/client/ssr/pages/offline.js +21 -0
  244. package/src/client/sw/default.sw.js +205 -0
  245. package/src/client/sw/template.sw.js +84 -0
  246. package/src/client.build.js +22 -0
  247. package/src/client.dev.js +21 -0
  248. package/src/db/DataBaseProvider.js +45 -0
  249. package/src/db/mariadb/MariaDB.js +33 -0
  250. package/src/db/mongo/MongooseDB.js +124 -0
  251. package/src/dns.js +22 -0
  252. package/src/index.js +43 -0
  253. package/src/mailer/EmailRender.js +69 -0
  254. package/src/mailer/MailerProvider.js +96 -0
  255. package/src/proxy.js +22 -0
  256. package/src/runtime/lampp/Lampp.js +115 -0
  257. package/src/runtime/nginx/Nginx.js +3 -0
  258. package/src/runtime/xampp/Xampp.js +49 -0
  259. package/src/server/auth.js +235 -0
  260. package/src/server/backup.js +120 -0
  261. package/src/server/client-build-live.js +98 -0
  262. package/src/server/client-build.js +754 -0
  263. package/src/server/client-dev-server.js +60 -0
  264. package/src/server/client-formatted.js +58 -0
  265. package/src/server/client-icons.js +151 -0
  266. package/src/server/conf.js +929 -0
  267. package/src/server/crypto.js +91 -0
  268. package/src/server/dns.js +118 -0
  269. package/src/server/downloader.js +42 -0
  270. package/src/server/logger.js +190 -0
  271. package/src/server/network.js +209 -0
  272. package/src/server/peer.js +33 -0
  273. package/src/server/process.js +66 -0
  274. package/src/server/prompt-optimizer.js +28 -0
  275. package/src/server/proxy.js +118 -0
  276. package/src/server/runtime.js +463 -0
  277. package/src/server/ssl.js +120 -0
  278. package/src/server.js +25 -0
  279. package/src/ws/IoInterface.js +45 -0
  280. package/src/ws/IoServer.js +39 -0
  281. package/src/ws/core/channels/core.ws.chat.js +23 -0
  282. package/src/ws/core/channels/core.ws.mailer.js +35 -0
  283. package/src/ws/core/channels/core.ws.stream.js +31 -0
  284. package/src/ws/core/core.ws.connection.js +28 -0
  285. package/src/ws/core/core.ws.emit.js +14 -0
  286. package/src/ws/core/core.ws.server.js +24 -0
  287. package/src/ws/core/management/core.ws.chat.js +8 -0
  288. package/src/ws/core/management/core.ws.mailer.js +16 -0
  289. package/src/ws/core/management/core.ws.stream.js +8 -0
  290. package/src/ws/default/channels/default.ws.main.js +16 -0
  291. package/src/ws/default/default.ws.connection.js +22 -0
  292. package/src/ws/default/default.ws.emit.js +14 -0
  293. package/src/ws/default/default.ws.server.js +20 -0
  294. package/src/ws/default/management/default.ws.main.js +8 -0
  295. package/startup.js +11 -0
  296. package/supervisord-openssh-server.conf +5 -0
  297. package/test/api.test.js +60 -0
@@ -0,0 +1,66 @@
1
+ // https://nodejs.org/api/process
2
+
3
+ import shell from 'shelljs';
4
+ import dotenv from 'dotenv';
5
+ import fs from 'fs-extra';
6
+
7
+ import { loggerFactory } from './logger.js';
8
+
9
+ dotenv.config();
10
+
11
+ const logger = loggerFactory(import.meta);
12
+
13
+ // process.exit();
14
+
15
+ const getRootDirectory = () => process.cwd().replace(/\\/g, '/');
16
+
17
+ const ProcessController = {
18
+ SIG: [
19
+ 'SIGPIPE',
20
+ 'SIGHUP',
21
+ 'SIGTERM',
22
+ 'SIGINT',
23
+ 'SIGBREAK',
24
+ 'SIGWINCH',
25
+ // 'SIGKILL',
26
+ // 'SIGSTOP',
27
+ 'SIGBUS',
28
+ 'SIGFPE',
29
+ 'SIGSEGV',
30
+ 'SIGILL',
31
+ ],
32
+ onSigListen: function () {
33
+ return this.SIG.map((sig) =>
34
+ process.on(sig, (...args) => {
35
+ this.logger.info(`process on ${sig}`, args);
36
+ switch (sig) {
37
+ case 'SIGINT':
38
+ return process.exit();
39
+
40
+ default:
41
+ break;
42
+ }
43
+ }),
44
+ );
45
+ },
46
+ init: function (logger) {
47
+ this.logger = logger;
48
+ process.on('exit', (...args) => {
49
+ this.logger.info(`process on exit`, args);
50
+ });
51
+ this.onSigListen();
52
+ if (fs.existsSync(`./tmp/await-deploy`)) fs.remove(`./tmp/await-deploy`);
53
+ },
54
+ };
55
+
56
+ const shellExec = (cmd, options = { silent: false, async: false, stdout: false, disableLog: false }) => {
57
+ if (!options.disableLog) logger.info(`cmd`, cmd);
58
+ return options.stdout ? shell.exec(cmd, options).stdout : shell.exec(cmd, options);
59
+ };
60
+
61
+ const shellCd = (cd, options = { disableLog: false }) => {
62
+ if (!options.disableLog) logger.info(`cd`, cd);
63
+ return shell.cd(cd);
64
+ };
65
+
66
+ export { ProcessController, getRootDirectory, shellExec, shellCd };
@@ -0,0 +1,28 @@
1
+ // https://github.com/xenova/transformers.js/blob/f43d3dd348fd7b293008802590bb3a1afa218dc7/src/models.js#L10
2
+
3
+ import { AutoModelForSeq2SeqLM, AutoTokenizer } from '@xenova/transformers';
4
+ import { loggerFactory } from './logger.js';
5
+ import dotenv from 'dotenv';
6
+
7
+ dotenv.config();
8
+
9
+ const logger = loggerFactory(import.meta);
10
+
11
+ const tokenizer = await AutoTokenizer.from_pretrained('Xenova/t5-small');
12
+
13
+ const model = await AutoModelForSeq2SeqLM.from_pretrained('Xenova/t5-small');
14
+
15
+ const prompt = 'translate English to German: I love transformers!';
16
+
17
+ logger.info('input', { prompt });
18
+
19
+ const tokenizerData = await tokenizer(prompt);
20
+
21
+ const { input_ids } = tokenizerData;
22
+
23
+ const outputs = await model.generate(input_ids);
24
+
25
+ for (const output of outputs) {
26
+ const decoded = tokenizer.decode(output, { skip_special_tokens: true });
27
+ logger.info('decoded', { decoded });
28
+ }
@@ -0,0 +1,118 @@
1
+ 'use strict';
2
+
3
+ import express from 'express';
4
+ import fs from 'fs-extra';
5
+ import dotenv from 'dotenv';
6
+
7
+ import { createProxyMiddleware } from 'http-proxy-middleware';
8
+ import { loggerFactory, loggerMiddleware } from './logger.js';
9
+ import { listenPortController, network } from './network.js';
10
+ import { orderArrayFromAttrInt } from '../client/components/core/CommonJs.js';
11
+ import { createSslServer, sslRedirectMiddleware } from './ssl.js';
12
+ import { buildProxyRouter, maintenanceMiddleware } from './conf.js';
13
+ import { getRootDirectory } from './process.js';
14
+
15
+ dotenv.config();
16
+
17
+ const logger = loggerFactory(import.meta);
18
+
19
+ const buildProxy = async () => {
20
+ // default target
21
+ await network.port.portClean(process.env.PORT);
22
+ express().listen(process.env.PORT);
23
+
24
+ const proxyRouter = buildProxyRouter();
25
+
26
+ for (let port of Object.keys(proxyRouter)) {
27
+ port = parseInt(port);
28
+ const hosts = proxyRouter[port];
29
+ const proxyPath = '/';
30
+ const proxyHost = 'localhost';
31
+ const runningData = { host: proxyHost, path: proxyPath, client: null, runtime: 'nodejs', meta: import.meta };
32
+ const app = express();
33
+
34
+ // set logger
35
+ app.use(loggerMiddleware(import.meta));
36
+
37
+ // instance proxy options
38
+ // https://github.com/chimurai/http-proxy-middleware/tree/v2.0.4#readme
39
+
40
+ // proxy middleware options
41
+ /** @type {import('http-proxy-middleware/dist/types').Options} */
42
+ const options = {
43
+ ws: true,
44
+ // changeOrigin: true,
45
+ // autoRewrite: false,
46
+ target: `http://localhost:${process.env.PORT}`,
47
+ router: {},
48
+ xfwd: true, // adds x-forward headers
49
+ // preserveHeaderKeyCase: true,
50
+ // secure: true, warn validator
51
+ onProxyReq: (proxyReq, req, res, options) => {
52
+ // https://wtools.io/check-http-status-code
53
+ // http://nexodev.org
54
+ maintenanceMiddleware(req, res, port, proxyRouter);
55
+ sslRedirectMiddleware(req, res, port, proxyRouter);
56
+ },
57
+ pathRewrite: {
58
+ // only add path
59
+ // '^/target-path': '/',
60
+ },
61
+ };
62
+ if (!process.argv.includes('maintenance')) {
63
+ // build router
64
+ Object.keys(hosts).map((hostKey) => {
65
+ let { host, path, target, proxy, peer } = hosts[hostKey];
66
+ if (process.env.NODE_ENV === 'development') host = `localhost`;
67
+
68
+ if (!proxy.includes(port)) return;
69
+ const absoluteHost = [80, 443].includes(port)
70
+ ? `${host}${path === '/' ? '' : path}`
71
+ : `${host}:${port}${path === '/' ? '' : path}`;
72
+
73
+ if (!(absoluteHost in options.router)) options.router[absoluteHost] = target;
74
+ });
75
+ if (Object.keys(options.router).length === 0) continue;
76
+
77
+ // order router
78
+ const router = {};
79
+ for (const absoluteHostKey of orderArrayFromAttrInt(Object.keys(options.router), 'length'))
80
+ router[absoluteHostKey] = options.router[absoluteHostKey];
81
+ options.router = router;
82
+ }
83
+
84
+ const filter = false
85
+ ? (pathname, req) => {
86
+ // return pathname.match('^/api') && req.method === 'GET';
87
+ return true;
88
+ }
89
+ : proxyPath;
90
+ app.use(proxyPath, createProxyMiddleware(filter, options));
91
+ await network.port.portClean(port);
92
+
93
+ switch (process.env.NODE_ENV) {
94
+ case 'production':
95
+ switch (port) {
96
+ case 443:
97
+ const { ServerSSL } = await createSslServer(app, hosts);
98
+ await listenPortController(ServerSSL, port, runningData);
99
+ break;
100
+
101
+ default:
102
+ await listenPortController(app, port, runningData);
103
+
104
+ break;
105
+ }
106
+
107
+ break;
108
+
109
+ default:
110
+ await listenPortController(app, port, runningData);
111
+
112
+ break;
113
+ }
114
+ logger.info('Proxy running', { port, options });
115
+ }
116
+ };
117
+
118
+ export { buildProxy };
@@ -0,0 +1,463 @@
1
+ import fs from 'fs-extra';
2
+ import express from 'express';
3
+ import cors from 'cors';
4
+ import dotenv from 'dotenv';
5
+ import fileUpload from 'express-fileupload';
6
+ import swaggerUi from 'swagger-ui-express';
7
+ import * as promClient from 'prom-client';
8
+ import compression from 'compression';
9
+
10
+ import { createServer } from 'http';
11
+ import { getRootDirectory } from './process.js';
12
+ import { network, listenPortController, saveRuntimeRouter, logRuntimeRouter, listenServerFactory } from './network.js';
13
+ import { loggerFactory, loggerMiddleware } from './logger.js';
14
+ import { newInstance } from '../client/components/core/CommonJs.js';
15
+ import { Xampp } from '../runtime/xampp/Xampp.js';
16
+ import { MailerProvider } from '../mailer/MailerProvider.js';
17
+ import { DataBaseProvider } from '../db/DataBaseProvider.js';
18
+ import { createProxyMiddleware } from 'http-proxy-middleware';
19
+ import { createPeerServer } from './peer.js';
20
+ import { Lampp } from '../runtime/lampp/Lampp.js';
21
+ import { getDeployId } from './conf.js';
22
+
23
+ dotenv.config();
24
+
25
+ const logger = loggerFactory(import.meta);
26
+
27
+ const buildRuntime = async () => {
28
+ const deployId = getDeployId();
29
+
30
+ const collectDefaultMetrics = promClient.collectDefaultMetrics;
31
+ collectDefaultMetrics();
32
+
33
+ if (fs.existsSync(`/root/.bashrc`) && !fs.readFileSync(`/root/.bashrc`, 'utf8').match(`underpost-engine`)) {
34
+ fs.writeFileSync(
35
+ `/root/.bashrc`,
36
+ `${fs.readFileSync(`/root/.bashrc`, 'utf8')}
37
+ ` +
38
+ `export NVM_DIR="$HOME/.nvm"
39
+ [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
40
+ [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm underpost-engine bash_completion
41
+
42
+ export PATH=$PATH:/opt/lampp/bin`,
43
+ 'utf8',
44
+ );
45
+ }
46
+
47
+ const promCounterOption = {
48
+ name: `${deployId.replaceAll('-', '_')}_http_requests_total`,
49
+ help: 'Total number of HTTP requests',
50
+ labelNames: ['instance', 'method', 'status_code'],
51
+ };
52
+
53
+ // logger.info('promCounterOption', promCounterOption);
54
+
55
+ const requestCounter = new promClient.Counter(promCounterOption);
56
+
57
+ const ipInstance = ''; // await ip.public.ipv4();
58
+ const initPort = parseInt(process.env.PORT) + 1;
59
+ let currentPort = initPort;
60
+ const confServer = JSON.parse(fs.readFileSync(`./conf/conf.server.json`, 'utf8'));
61
+ const singleReplicaHosts = [];
62
+ for (const host of Object.keys(confServer)) {
63
+ if (singleReplicaHosts.length > 0 && !singleReplicaHosts.includes(host)) {
64
+ currentPort += singleReplicaHosts.reduce((accumulator, currentValue) => accumulator + currentValue.replicas, 0);
65
+ }
66
+ const rootHostPath = `/public/${host}`;
67
+ for (const path of Object.keys(confServer[host])) {
68
+ confServer[host][path].port = newInstance(currentPort);
69
+ const {
70
+ runtime,
71
+ port,
72
+ client,
73
+ apis,
74
+ origins,
75
+ directory,
76
+ ws,
77
+ mailer,
78
+ db,
79
+ redirect,
80
+ peer,
81
+ singleReplica,
82
+ replicas,
83
+ } = confServer[host][path];
84
+
85
+ if (singleReplica && replicas && replicas.length > 0 && !singleReplicaHosts.includes(host)) {
86
+ singleReplicaHosts.push({
87
+ host,
88
+ replicas: replicas.length,
89
+ });
90
+ continue;
91
+ }
92
+
93
+ const runningData = {
94
+ host,
95
+ path,
96
+ runtime,
97
+ client,
98
+ meta: import.meta,
99
+ apis,
100
+ };
101
+
102
+ let redirectUrl;
103
+ let redirectTarget;
104
+ if (redirect) {
105
+ redirectUrl = new URL(redirect);
106
+ redirectTarget = redirect[redirect.length - 1] === '/' ? redirect.slice(0, -1) : redirect;
107
+ }
108
+
109
+ switch (runtime) {
110
+ case 'lampp':
111
+ if (!Lampp.enabled()) continue;
112
+ if (!Lampp.ports.includes(port)) Lampp.ports.push(port);
113
+ if (currentPort === initPort) Lampp.removeRouter();
114
+ Lampp.appendRouter(`
115
+
116
+ Listen ${port}
117
+
118
+ <VirtualHost *:${port}>
119
+ DocumentRoot "${directory ? directory : `${getRootDirectory()}${rootHostPath}`}"
120
+ ServerName ${host}:${port}
121
+
122
+ <Directory "${directory ? directory : `${getRootDirectory()}${rootHostPath}`}">
123
+ Options Indexes FollowSymLinks MultiViews
124
+ AllowOverride All
125
+ Require all granted
126
+ </Directory>
127
+
128
+ ${
129
+ redirect
130
+ ? `
131
+ RewriteEngine on
132
+
133
+ RewriteCond %{REQUEST_URI} !^/.well-known/acme-challenge
134
+ RewriteRule ^(.*)$ ${redirectTarget}%{REQUEST_URI} [R=302,L]
135
+ `
136
+ : ''
137
+ }
138
+
139
+ ErrorDocument 400 ${path === '/' ? '' : path}/400.html
140
+ ErrorDocument 404 ${path === '/' ? '' : path}/400.html
141
+ ErrorDocument 500 ${path === '/' ? '' : path}/500.html
142
+ ErrorDocument 502 ${path === '/' ? '' : path}/500.html
143
+ ErrorDocument 503 ${path === '/' ? '' : path}/500.html
144
+ ErrorDocument 504 ${path === '/' ? '' : path}/500.html
145
+
146
+ </VirtualHost>
147
+
148
+ `);
149
+ // ERR too many redirects:
150
+ // Check: SELECT * FROM database.wp_options where option_name = 'siteurl' or option_name = 'home';
151
+ // Check: wp-config.php
152
+ // if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
153
+ // $_SERVER['HTTPS'] = 'on';
154
+ // }
155
+
156
+ // ErrorDocument 404 /custom_404.html
157
+ // ErrorDocument 500 /custom_50x.html
158
+ // ErrorDocument 502 /custom_50x.html
159
+ // ErrorDocument 503 /custom_50x.html
160
+ // ErrorDocument 504 /custom_50x.html
161
+
162
+ // Respond When Error Pages are Directly Requested
163
+
164
+ // <Files "custom_404.html">
165
+ // <If "-z %{ENV:REDIRECT_STATUS}">
166
+ // RedirectMatch 404 ^/custom_404.html$
167
+ // </If>
168
+ // </Files>
169
+
170
+ // <Files "custom_50x.html">
171
+ // <If "-z %{ENV:REDIRECT_STATUS}">
172
+ // RedirectMatch 404 ^/custom_50x.html$
173
+ // </If>
174
+ // </Files>
175
+
176
+ // Add www or https with htaccess rewrite
177
+
178
+ // Options +FollowSymLinks
179
+ // RewriteEngine On
180
+ // RewriteCond %{HTTP_HOST} ^ejemplo.com [NC]
181
+ // RewriteRule ^(.*)$ http://ejemplo.com/$1 [R=301,L]
182
+
183
+ // Redirect http to https with htaccess rewrite
184
+
185
+ // RewriteEngine On
186
+ // RewriteCond %{SERVER_PORT} 80
187
+ // RewriteRule ^(.*)$ https://www.ejemplo.com/$1 [R,L]
188
+
189
+ // Redirect to HTTPS with www subdomain
190
+
191
+ // RewriteEngine On
192
+ // RewriteCond %{HTTPS} off [OR]
193
+ // RewriteCond %{HTTP_HOST} ^www\. [NC]
194
+ // RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
195
+ // RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
196
+
197
+ await listenPortController(listenServerFactory(), port, runningData);
198
+ break;
199
+ case 'xampp':
200
+ if (!Xampp.enabled()) continue;
201
+ if (!Xampp.ports.includes(port)) Xampp.ports.push(port);
202
+ if (currentPort === initPort) Xampp.removeRouter();
203
+ Xampp.appendRouter(`
204
+
205
+ Listen ${port}
206
+
207
+ <VirtualHost *:${port}>
208
+ DocumentRoot "${directory ? directory : `${getRootDirectory()}${rootHostPath}`}"
209
+ ServerName ${host}:${port}
210
+
211
+ <Directory "${directory ? directory : `${getRootDirectory()}${rootHostPath}`}">
212
+ Options Indexes FollowSymLinks MultiViews
213
+ AllowOverride All
214
+ Require all granted
215
+ </Directory>
216
+
217
+ ${
218
+ redirect
219
+ ? `
220
+ RewriteEngine on
221
+
222
+ RewriteCond %{REQUEST_URI} !^/.well-known/acme-challenge
223
+ RewriteRule ^(.*)$ ${redirectTarget}%{REQUEST_URI} [R=302,L]
224
+ `
225
+ : ''
226
+ }
227
+
228
+ ErrorDocument 400 ${path === '/' ? '' : path}/400.html
229
+ ErrorDocument 404 ${path === '/' ? '' : path}/400.html
230
+ ErrorDocument 500 ${path === '/' ? '' : path}/500.html
231
+ ErrorDocument 502 ${path === '/' ? '' : path}/500.html
232
+ ErrorDocument 503 ${path === '/' ? '' : path}/500.html
233
+ ErrorDocument 504 ${path === '/' ? '' : path}/500.html
234
+
235
+ </VirtualHost>
236
+
237
+ `);
238
+ // ERR too many redirects:
239
+ // Check: SELECT * FROM database.wp_options where option_name = 'siteurl' or option_name = 'home';
240
+ // Check: wp-config.php
241
+ // if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
242
+ // $_SERVER['HTTPS'] = 'on';
243
+ // }
244
+ await listenPortController(listenServerFactory(), port, runningData);
245
+ break;
246
+ case 'nodejs':
247
+ const app = express();
248
+
249
+ app.use((req, res, next) => {
250
+ // const info = `${req.headers.host}${req.url}`;
251
+ return next();
252
+ });
253
+
254
+ // https://github.com/prometheus/prometheus/blob/main/documentation/examples/prometheus.yml
255
+ // https://github.com/grafana/grafana/tree/main/conf
256
+ // https://medium.com/@diego.coder/monitoreo-de-aplicaciones-con-node-js-grafana-y-prometheus-afd2b33e3f91
257
+ // for grafana prometheus server: host.docker.internal:9090
258
+
259
+ app.use((req, res, next) => {
260
+ requestCounter.inc({
261
+ instance: `${host}:${port}${path}`,
262
+ method: req.method,
263
+ status_code: res.statusCode,
264
+ });
265
+ // decodeURIComponent(req.url)
266
+ return next();
267
+ });
268
+
269
+ app.get(`${path === '/' ? '' : path}/metrics`, async (req, res) => {
270
+ res.set('Content-Type', promClient.register.contentType);
271
+ return res.end(await promClient.register.metrics());
272
+ });
273
+
274
+ // set logger
275
+ app.use(loggerMiddleware(import.meta));
276
+
277
+ // instance public static
278
+ app.use('/', express.static(directory ? directory : `.${rootHostPath}`));
279
+
280
+ // js src compression
281
+ app.use(compression({ filter: shouldCompress }));
282
+ function shouldCompress(req, res) {
283
+ if (req.headers['x-no-compression']) {
284
+ // don't compress responses with this request header
285
+ return false;
286
+ }
287
+
288
+ // fallback to standard filter function
289
+ return compression.filter(req, res);
290
+ }
291
+
292
+ if (process.argv.includes('static')) {
293
+ logger.info('Build static server runtime', `${host}${path}`);
294
+ currentPort += 2;
295
+ const staticPort = newInstance(currentPort);
296
+ await network.port.portClean(staticPort);
297
+ await listenPortController(app, staticPort, runningData);
298
+ currentPort++;
299
+ continue;
300
+ }
301
+ logger.info('Build api server runtime', `${host}${path}`);
302
+
303
+ // parse requests of content-type - application/json
304
+ app.use(express.json({ limit: '100MB' }));
305
+
306
+ // parse requests of content-type - application/x-www-form-urlencoded
307
+ app.use(express.urlencoded({ extended: true, limit: '20MB' }));
308
+
309
+ // file upload middleware
310
+ app.use(fileUpload());
311
+
312
+ // json formatted response
313
+ app.set('json spaces', 2);
314
+
315
+ // lang handling middleware
316
+ app.use(function (req, res, next) {
317
+ const lang = req.headers['accept-language'] || 'en';
318
+ if (typeof lang === 'string' && lang.toLowerCase().match('es')) {
319
+ req.lang = 'es';
320
+ } else req.lang = 'en';
321
+ return next();
322
+ });
323
+
324
+ // cors
325
+ const originPayload = {
326
+ origin: origins.concat(
327
+ apis && process.env.NODE_ENV === 'development' ? [`http://localhost:${currentPort + 2}`] : [],
328
+ ),
329
+ };
330
+ logger.info('originPayload', originPayload);
331
+ app.use(cors(originPayload));
332
+
333
+ if (redirect) {
334
+ app.use(function (req = express.Request, res = express.Response, next = express.NextFunction) {
335
+ if (process.env.NODE_ENV === 'production' && !req.url.startsWith(`/.well-known/acme-challenge`))
336
+ return res.status(302).redirect(redirectTarget + req.url);
337
+ // if (!req.url.startsWith(`/.well-known/acme-challenge`)) return res.status(302).redirect(redirect);
338
+ return next();
339
+ });
340
+ // app.use(
341
+ // '*',
342
+ // createProxyMiddleware({
343
+ // target: redirect,
344
+ // changeOrigin: true,
345
+ // }),
346
+ // );
347
+ await network.port.portClean(port);
348
+ await listenPortController(app, port, runningData);
349
+ break;
350
+ }
351
+
352
+ const swaggerJsonPath = `./public/${host}${path === '/' ? path : `${path}/`}swagger-output.json`;
353
+ if (fs.existsSync(swaggerJsonPath)) {
354
+ // logger.info('Build swagger serve', swaggerJsonPath);
355
+
356
+ const swaggerInstance =
357
+ (swaggerDoc) =>
358
+ (...args) =>
359
+ swaggerUi.setup(swaggerDoc)(...args);
360
+
361
+ const swaggerDoc = JSON.parse(fs.readFileSync(swaggerJsonPath, 'utf8'));
362
+
363
+ app.use(`${path === '/' ? `/api-docs` : `${path}/api-docs`}`, swaggerUi.serve, swaggerInstance(swaggerDoc));
364
+ }
365
+
366
+ if (db && apis) await DataBaseProvider.load({ apis, host, path, db });
367
+
368
+ if (mailer)
369
+ await MailerProvider.load({
370
+ id: `${host}${path}`,
371
+ meta: `mailer-${host}${path}`,
372
+ host,
373
+ path,
374
+ ...mailer,
375
+ });
376
+
377
+ if (apis) {
378
+ const apiPath = `${path === '/' ? '' : path}/${process.env.BASE_API}`;
379
+ for (const api of apis)
380
+ await (async () => {
381
+ const { ApiRouter } = await import(`../api/${api}/${api}.router.js`);
382
+ const router = ApiRouter({ host, path, apiPath, mailer, db });
383
+ // router.use(cors({ origin: origins }));
384
+ // logger.info('Load api router', { host, path: apiPath, api });
385
+ app.use(`${apiPath}/${api}`, router);
386
+ })();
387
+ }
388
+ app.use(function (req, res, next) {
389
+ const path404 = `${directory ? directory : `${getRootDirectory()}${rootHostPath}`}/404.html`;
390
+ if (fs.existsSync(path404)) return res.status(404).sendFile(path404);
391
+ else res.status(404).send('Sorry cant find that!');
392
+ });
393
+
394
+ app.use(function (err, req, res, next) {
395
+ logger.error(err, err.stack);
396
+ const path500 = `${directory ? directory : `${getRootDirectory()}${rootHostPath}`}/500.html`;
397
+ if (fs.existsSync(path500)) return res.status(500).sendFile(path500);
398
+ res.status(500).send('Something broke!');
399
+ });
400
+
401
+ // instance server
402
+ const server = createServer({}, app);
403
+
404
+ if (ws)
405
+ await (async () => {
406
+ const { createIoServer } = await import(`../ws/${ws}/${ws}.ws.server.js`);
407
+ // logger.info('Load socket.io ws router', { host, ws });
408
+ // start socket.io
409
+ const { options, meta } = await createIoServer(server, {
410
+ host,
411
+ path,
412
+ db,
413
+ port,
414
+ origins,
415
+ });
416
+ await listenPortController(listenServerFactory(), port, {
417
+ runtime: 'nodejs',
418
+ client: null,
419
+ host,
420
+ path: options.path,
421
+ meta,
422
+ });
423
+ })();
424
+
425
+ if (peer) {
426
+ currentPort++;
427
+ const peerPort = newInstance(currentPort);
428
+ const { options, meta, peerServer } = await createPeerServer({
429
+ port: peerPort,
430
+ devPort: port,
431
+ origins,
432
+ host,
433
+ path,
434
+ });
435
+ await network.port.portClean(peerPort);
436
+ await listenPortController(peerServer, peerPort, {
437
+ runtime: 'nodejs',
438
+ client: null,
439
+ host,
440
+ path: options.path,
441
+ meta,
442
+ });
443
+ }
444
+
445
+ await network.port.portClean(port);
446
+ await listenPortController(server, port, runningData);
447
+
448
+ break;
449
+ default:
450
+ break;
451
+ }
452
+ currentPort++;
453
+ }
454
+ }
455
+
456
+ if (Xampp.enabled() && Xampp.router) await Xampp.initService({ daemon: true });
457
+ if (Lampp.enabled() && Lampp.router) await Lampp.initService({ daemon: true });
458
+
459
+ saveRuntimeRouter();
460
+ logRuntimeRouter();
461
+ };
462
+
463
+ export { buildRuntime };