@underpostnet/underpost 2.7.82

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 (290) 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 +75 -0
  6. package/.github/workflows/publish.yml +84 -0
  7. package/.github/workflows/pwa-microservices-template.test.yml +30 -0
  8. package/.nycrc +9 -0
  9. package/.prettierignore +13 -0
  10. package/.prettierrc +9 -0
  11. package/.vscode/extensions.json +72 -0
  12. package/.vscode/settings.json +100 -0
  13. package/AUTHORS.md +10 -0
  14. package/CHANGELOG.md +37 -0
  15. package/Dockerfile +90 -0
  16. package/LICENSE +21 -0
  17. package/README.md +96 -0
  18. package/bin/db.js +172 -0
  19. package/bin/deploy.js +674 -0
  20. package/bin/file.js +93 -0
  21. package/bin/index.js +82 -0
  22. package/bin/ssl.js +55 -0
  23. package/bin/util.js +182 -0
  24. package/bin/vs.js +35 -0
  25. package/conf.js +251 -0
  26. package/docker-compose.yml +67 -0
  27. package/jsconfig.json +7 -0
  28. package/jsdoc.json +32 -0
  29. package/nodemon.json +6 -0
  30. package/package.json +134 -0
  31. package/prometheus.yml +36 -0
  32. package/src/api/core/core.controller.js +69 -0
  33. package/src/api/core/core.model.js +11 -0
  34. package/src/api/core/core.router.js +23 -0
  35. package/src/api/core/core.service.js +29 -0
  36. package/src/api/crypto/crypto.controller.js +51 -0
  37. package/src/api/crypto/crypto.model.js +23 -0
  38. package/src/api/crypto/crypto.router.js +20 -0
  39. package/src/api/crypto/crypto.service.js +64 -0
  40. package/src/api/default/default.controller.js +69 -0
  41. package/src/api/default/default.model.js +20 -0
  42. package/src/api/default/default.router.js +23 -0
  43. package/src/api/default/default.service.js +31 -0
  44. package/src/api/file/file.controller.js +53 -0
  45. package/src/api/file/file.model.js +19 -0
  46. package/src/api/file/file.router.js +21 -0
  47. package/src/api/file/file.service.js +80 -0
  48. package/src/api/instance/instance.controller.js +69 -0
  49. package/src/api/instance/instance.model.js +36 -0
  50. package/src/api/instance/instance.router.js +33 -0
  51. package/src/api/instance/instance.service.js +48 -0
  52. package/src/api/test/test.controller.js +59 -0
  53. package/src/api/test/test.model.js +14 -0
  54. package/src/api/test/test.router.js +21 -0
  55. package/src/api/test/test.service.js +35 -0
  56. package/src/api/user/user.build.js +16 -0
  57. package/src/api/user/user.controller.js +70 -0
  58. package/src/api/user/user.model.js +65 -0
  59. package/src/api/user/user.router.js +345 -0
  60. package/src/api/user/user.service.js +479 -0
  61. package/src/api.js +23 -0
  62. package/src/client/Default.index.js +40 -0
  63. package/src/client/components/core/Account.js +290 -0
  64. package/src/client/components/core/AgGrid.js +160 -0
  65. package/src/client/components/core/Auth.js +19 -0
  66. package/src/client/components/core/Badge.js +32 -0
  67. package/src/client/components/core/Blockchain.js +41 -0
  68. package/src/client/components/core/Blog.js +9 -0
  69. package/src/client/components/core/BtnIcon.js +101 -0
  70. package/src/client/components/core/CalendarCore.js +458 -0
  71. package/src/client/components/core/Chat.js +64 -0
  72. package/src/client/components/core/ColorPalette.js +5267 -0
  73. package/src/client/components/core/CommonJs.js +742 -0
  74. package/src/client/components/core/Content.js +193 -0
  75. package/src/client/components/core/Css.js +846 -0
  76. package/src/client/components/core/CssCore.js +817 -0
  77. package/src/client/components/core/D3Chart.js +44 -0
  78. package/src/client/components/core/Docs.js +231 -0
  79. package/src/client/components/core/DropDown.js +164 -0
  80. package/src/client/components/core/EventsUI.js +46 -0
  81. package/src/client/components/core/FileExplorer.js +699 -0
  82. package/src/client/components/core/FullScreen.js +45 -0
  83. package/src/client/components/core/Input.js +346 -0
  84. package/src/client/components/core/JoyStick.js +77 -0
  85. package/src/client/components/core/Keyboard.js +73 -0
  86. package/src/client/components/core/LoadingAnimation.js +178 -0
  87. package/src/client/components/core/LogIn.js +187 -0
  88. package/src/client/components/core/LogOut.js +58 -0
  89. package/src/client/components/core/Logger.js +26 -0
  90. package/src/client/components/core/Modal.js +1612 -0
  91. package/src/client/components/core/NotificationManager.js +84 -0
  92. package/src/client/components/core/Panel.js +613 -0
  93. package/src/client/components/core/PanelForm.js +468 -0
  94. package/src/client/components/core/Polyhedron.js +162 -0
  95. package/src/client/components/core/Recover.js +204 -0
  96. package/src/client/components/core/Responsive.js +53 -0
  97. package/src/client/components/core/RichText.js +51 -0
  98. package/src/client/components/core/Router.js +76 -0
  99. package/src/client/components/core/Scroll.js +34 -0
  100. package/src/client/components/core/SignUp.js +125 -0
  101. package/src/client/components/core/SocketIo.js +72 -0
  102. package/src/client/components/core/Stream.js +113 -0
  103. package/src/client/components/core/ToggleSwitch.js +87 -0
  104. package/src/client/components/core/ToolTip.js +26 -0
  105. package/src/client/components/core/Translate.js +446 -0
  106. package/src/client/components/core/Validator.js +100 -0
  107. package/src/client/components/core/VanillaJs.js +463 -0
  108. package/src/client/components/core/Wallet.js +106 -0
  109. package/src/client/components/core/Webhook.js +25 -0
  110. package/src/client/components/core/Worker.js +282 -0
  111. package/src/client/components/default/CommonDefault.js +29 -0
  112. package/src/client/components/default/CssDefault.js +13 -0
  113. package/src/client/components/default/ElementsDefault.js +38 -0
  114. package/src/client/components/default/LogInDefault.js +41 -0
  115. package/src/client/components/default/LogOutDefault.js +28 -0
  116. package/src/client/components/default/MenuDefault.js +389 -0
  117. package/src/client/components/default/RoutesDefault.js +48 -0
  118. package/src/client/components/default/SettingsDefault.js +16 -0
  119. package/src/client/components/default/SignUpDefault.js +9 -0
  120. package/src/client/components/default/SocketIoDefault.js +54 -0
  121. package/src/client/components/default/TranslateDefault.js +7 -0
  122. package/src/client/public/default/android-chrome-144x144.png +0 -0
  123. package/src/client/public/default/android-chrome-192x192.png +0 -0
  124. package/src/client/public/default/android-chrome-256x256.png +0 -0
  125. package/src/client/public/default/android-chrome-36x36.png +0 -0
  126. package/src/client/public/default/android-chrome-384x384.png +0 -0
  127. package/src/client/public/default/android-chrome-48x48.png +0 -0
  128. package/src/client/public/default/android-chrome-512x512.png +0 -0
  129. package/src/client/public/default/android-chrome-72x72.png +0 -0
  130. package/src/client/public/default/android-chrome-96x96.png +0 -0
  131. package/src/client/public/default/apple-touch-icon-1024x1024.png +0 -0
  132. package/src/client/public/default/apple-touch-icon-114x114.png +0 -0
  133. package/src/client/public/default/apple-touch-icon-120x120.png +0 -0
  134. package/src/client/public/default/apple-touch-icon-144x144.png +0 -0
  135. package/src/client/public/default/apple-touch-icon-152x152.png +0 -0
  136. package/src/client/public/default/apple-touch-icon-167x167.png +0 -0
  137. package/src/client/public/default/apple-touch-icon-180x180.png +0 -0
  138. package/src/client/public/default/apple-touch-icon-57x57.png +0 -0
  139. package/src/client/public/default/apple-touch-icon-60x60.png +0 -0
  140. package/src/client/public/default/apple-touch-icon-72x72.png +0 -0
  141. package/src/client/public/default/apple-touch-icon-76x76.png +0 -0
  142. package/src/client/public/default/apple-touch-icon-precomposed.png +0 -0
  143. package/src/client/public/default/apple-touch-icon.png +0 -0
  144. package/src/client/public/default/apple-touch-startup-image-1125x2436.png +0 -0
  145. package/src/client/public/default/apple-touch-startup-image-1136x640.png +0 -0
  146. package/src/client/public/default/apple-touch-startup-image-1170x2532.png +0 -0
  147. package/src/client/public/default/apple-touch-startup-image-1179x2556.png +0 -0
  148. package/src/client/public/default/apple-touch-startup-image-1242x2208.png +0 -0
  149. package/src/client/public/default/apple-touch-startup-image-1242x2688.png +0 -0
  150. package/src/client/public/default/apple-touch-startup-image-1284x2778.png +0 -0
  151. package/src/client/public/default/apple-touch-startup-image-1290x2796.png +0 -0
  152. package/src/client/public/default/apple-touch-startup-image-1334x750.png +0 -0
  153. package/src/client/public/default/apple-touch-startup-image-1488x2266.png +0 -0
  154. package/src/client/public/default/apple-touch-startup-image-1536x2048.png +0 -0
  155. package/src/client/public/default/apple-touch-startup-image-1620x2160.png +0 -0
  156. package/src/client/public/default/apple-touch-startup-image-1640x2160.png +0 -0
  157. package/src/client/public/default/apple-touch-startup-image-1668x2224.png +0 -0
  158. package/src/client/public/default/apple-touch-startup-image-1668x2388.png +0 -0
  159. package/src/client/public/default/apple-touch-startup-image-1792x828.png +0 -0
  160. package/src/client/public/default/apple-touch-startup-image-2048x1536.png +0 -0
  161. package/src/client/public/default/apple-touch-startup-image-2048x2732.png +0 -0
  162. package/src/client/public/default/apple-touch-startup-image-2160x1620.png +0 -0
  163. package/src/client/public/default/apple-touch-startup-image-2160x1640.png +0 -0
  164. package/src/client/public/default/apple-touch-startup-image-2208x1242.png +0 -0
  165. package/src/client/public/default/apple-touch-startup-image-2224x1668.png +0 -0
  166. package/src/client/public/default/apple-touch-startup-image-2266x1488.png +0 -0
  167. package/src/client/public/default/apple-touch-startup-image-2388x1668.png +0 -0
  168. package/src/client/public/default/apple-touch-startup-image-2436x1125.png +0 -0
  169. package/src/client/public/default/apple-touch-startup-image-2532x1170.png +0 -0
  170. package/src/client/public/default/apple-touch-startup-image-2556x1179.png +0 -0
  171. package/src/client/public/default/apple-touch-startup-image-2688x1242.png +0 -0
  172. package/src/client/public/default/apple-touch-startup-image-2732x2048.png +0 -0
  173. package/src/client/public/default/apple-touch-startup-image-2778x1284.png +0 -0
  174. package/src/client/public/default/apple-touch-startup-image-2796x1290.png +0 -0
  175. package/src/client/public/default/apple-touch-startup-image-640x1136.png +0 -0
  176. package/src/client/public/default/apple-touch-startup-image-750x1334.png +0 -0
  177. package/src/client/public/default/apple-touch-startup-image-828x1792.png +0 -0
  178. package/src/client/public/default/assets/background/white.jpg +0 -0
  179. package/src/client/public/default/assets/background/white0-min.jpg +0 -0
  180. package/src/client/public/default/assets/background/white0.jpg +0 -0
  181. package/src/client/public/default/assets/logo/base-icon.png +0 -0
  182. package/src/client/public/default/assets/mailer/api-user-check.png +0 -0
  183. package/src/client/public/default/assets/mailer/api-user-invalid-token.png +0 -0
  184. package/src/client/public/default/assets/mailer/api-user-recover.png +0 -0
  185. package/src/client/public/default/browserconfig.xml +12 -0
  186. package/src/client/public/default/favicon-16x16.png +0 -0
  187. package/src/client/public/default/favicon-32x32.png +0 -0
  188. package/src/client/public/default/favicon-48x48.png +0 -0
  189. package/src/client/public/default/favicon.ico +0 -0
  190. package/src/client/public/default/manifest.webmanifest +69 -0
  191. package/src/client/public/default/mstile-144x144.png +0 -0
  192. package/src/client/public/default/mstile-150x150.png +0 -0
  193. package/src/client/public/default/mstile-310x150.png +0 -0
  194. package/src/client/public/default/mstile-310x310.png +0 -0
  195. package/src/client/public/default/mstile-70x70.png +0 -0
  196. package/src/client/public/default/plantuml/client-conf.svg +1 -0
  197. package/src/client/public/default/plantuml/client-schema.svg +1 -0
  198. package/src/client/public/default/plantuml/cron-conf.svg +1 -0
  199. package/src/client/public/default/plantuml/cron-schema.svg +1 -0
  200. package/src/client/public/default/plantuml/server-conf.svg +1 -0
  201. package/src/client/public/default/plantuml/server-schema.svg +1 -0
  202. package/src/client/public/default/plantuml/ssr-conf.svg +1 -0
  203. package/src/client/public/default/plantuml/ssr-schema.svg +1 -0
  204. package/src/client/public/default/sitemap +148 -0
  205. package/src/client/public/default/yandex-browser-50x50.png +0 -0
  206. package/src/client/public/default/yandex-browser-manifest.json +9 -0
  207. package/src/client/public/doc/favicon.ico +0 -0
  208. package/src/client/public/doc/sitemap +148 -0
  209. package/src/client/public/test/favicon.ico +0 -0
  210. package/src/client/public/test/sitemap +148 -0
  211. package/src/client/services/core/core.service.js +170 -0
  212. package/src/client/services/crypto/crypto.service.js +70 -0
  213. package/src/client/services/default/default.management.js +343 -0
  214. package/src/client/services/default/default.service.js +89 -0
  215. package/src/client/services/file/file.service.js +70 -0
  216. package/src/client/services/instance/instance.management.js +74 -0
  217. package/src/client/services/instance/instance.service.js +89 -0
  218. package/src/client/services/test/test.service.js +70 -0
  219. package/src/client/services/user/user.management.js +50 -0
  220. package/src/client/services/user/user.service.js +89 -0
  221. package/src/client/ssr/Render.js +237 -0
  222. package/src/client/ssr/common/Alert.js +75 -0
  223. package/src/client/ssr/common/SsrCore.js +91 -0
  224. package/src/client/ssr/common/Translate.js +26 -0
  225. package/src/client/ssr/common/Worker.js +28 -0
  226. package/src/client/ssr/components/body/CacheControl.js +114 -0
  227. package/src/client/ssr/components/body/DefaultSplashScreen.js +90 -0
  228. package/src/client/ssr/components/email/DefaultRecoverEmail.js +21 -0
  229. package/src/client/ssr/components/email/DefaultVerifyEmail.js +17 -0
  230. package/src/client/ssr/components/head/Css.js +241 -0
  231. package/src/client/ssr/components/head/DefaultScripts.js +3 -0
  232. package/src/client/ssr/components/head/PwaDefault.js +60 -0
  233. package/src/client/ssr/offline/default.index.js +31 -0
  234. package/src/client/ssr/pages/404.js +12 -0
  235. package/src/client/ssr/pages/500.js +12 -0
  236. package/src/client/sw/default.sw.js +203 -0
  237. package/src/client/sw/template.sw.js +84 -0
  238. package/src/client.build.js +22 -0
  239. package/src/client.dev.js +21 -0
  240. package/src/cron.js +30 -0
  241. package/src/db/DataBaseProvider.js +34 -0
  242. package/src/db/mariadb/MariaDB.js +33 -0
  243. package/src/db/mongo/MongooseDB.js +125 -0
  244. package/src/dns.js +22 -0
  245. package/src/index.js +43 -0
  246. package/src/mailer/EmailRender.js +69 -0
  247. package/src/mailer/MailerProvider.js +96 -0
  248. package/src/proxy.js +22 -0
  249. package/src/runtime/lampp/Lampp.js +115 -0
  250. package/src/runtime/nginx/Nginx.js +3 -0
  251. package/src/runtime/xampp/Xampp.js +49 -0
  252. package/src/server/auth.js +235 -0
  253. package/src/server/backup.js +108 -0
  254. package/src/server/client-build-live.js +98 -0
  255. package/src/server/client-build.js +821 -0
  256. package/src/server/client-dev-server.js +60 -0
  257. package/src/server/client-formatted.js +58 -0
  258. package/src/server/client-icons.js +151 -0
  259. package/src/server/conf.js +883 -0
  260. package/src/server/cron.js +35 -0
  261. package/src/server/dns.js +89 -0
  262. package/src/server/downloader.js +42 -0
  263. package/src/server/logger.js +190 -0
  264. package/src/server/network.js +122 -0
  265. package/src/server/peer.js +33 -0
  266. package/src/server/process.js +66 -0
  267. package/src/server/prompt-optimizer.js +28 -0
  268. package/src/server/proxy.js +118 -0
  269. package/src/server/runtime.js +461 -0
  270. package/src/server/ssl.js +120 -0
  271. package/src/server.js +25 -0
  272. package/src/ws/IoInterface.js +45 -0
  273. package/src/ws/IoServer.js +39 -0
  274. package/src/ws/core/channels/core.ws.chat.js +23 -0
  275. package/src/ws/core/channels/core.ws.mailer.js +35 -0
  276. package/src/ws/core/channels/core.ws.stream.js +31 -0
  277. package/src/ws/core/core.ws.connection.js +28 -0
  278. package/src/ws/core/core.ws.emit.js +14 -0
  279. package/src/ws/core/core.ws.server.js +24 -0
  280. package/src/ws/core/management/core.ws.chat.js +8 -0
  281. package/src/ws/core/management/core.ws.mailer.js +16 -0
  282. package/src/ws/core/management/core.ws.stream.js +8 -0
  283. package/src/ws/default/channels/default.ws.main.js +16 -0
  284. package/src/ws/default/default.ws.connection.js +22 -0
  285. package/src/ws/default/default.ws.emit.js +14 -0
  286. package/src/ws/default/default.ws.server.js +20 -0
  287. package/src/ws/default/management/default.ws.main.js +8 -0
  288. package/startup.js +11 -0
  289. package/supervisord-openssh-server.conf +5 -0
  290. package/test/api.test.js +60 -0
@@ -0,0 +1,35 @@
1
+ import cron from 'node-cron';
2
+ import { loggerFactory } from './logger.js';
3
+
4
+ const logger = loggerFactory(import.meta);
5
+
6
+ const CronManagement = {
7
+ data: {},
8
+ init: function () {
9
+ // verify tokens
10
+ // https://github.com/settings/tokens
11
+ for (const cronKey of Object.keys(this.data)) {
12
+ if (this.data[cronKey].valid) {
13
+ this.data[cronKey].task.start();
14
+ logger.info(`Cron task "${this.data[cronKey].name}" started`);
15
+ } else {
16
+ logger.error(
17
+ `Invalid cron expression "${this.data[cronKey].expression}" for task "${this.data[cronKey].name}"`,
18
+ );
19
+ }
20
+ }
21
+ },
22
+ add: function (name = 'task', expression = '* * * * *', callback = async () => null) {
23
+ const args = { name, expression, valid: cron.validate(expression) };
24
+ this.data[name] = {
25
+ ...args,
26
+ task: cron.schedule(expression, callback, {
27
+ scheduled: false,
28
+ timezone: process.env.TIME_ZONE || 'America/New_York',
29
+ name,
30
+ }),
31
+ };
32
+ },
33
+ };
34
+
35
+ export { CronManagement };
@@ -0,0 +1,89 @@
1
+ import axios from 'axios';
2
+ import dotenv from 'dotenv';
3
+ import fs from 'fs';
4
+ import cron from 'node-cron';
5
+
6
+ import { ip } from './network.js';
7
+ import { loggerFactory } from './logger.js';
8
+ import { isIPv4 } from 'is-ip';
9
+
10
+ dotenv.config();
11
+
12
+ const logger = loggerFactory(import.meta);
13
+
14
+ const Dns = {
15
+ ip: null,
16
+ ipDaemon: null,
17
+ callback: () => null,
18
+ InitIpDaemon: async function () {
19
+ // WAN | NAT-VPS | LAN
20
+ // enabled DMZ Host to proxy IP 80-443 (79-444) sometimes router block first port
21
+ // LAN server or device's local servers port -> 3000-3100 (2999-3101)
22
+ // DNS Records: [ANAME](Address Dynamic) -> [A](ipv4) host | [AAAA](ipv6) host -> [ip]
23
+ // DHCP (Dynamic Host Configuration Protocol) LAN reserver IP -> MAC ID
24
+ // Forward the router's TCP/UDP ports to the LAN device's IP address
25
+
26
+ const privateCronConfPath = `./engine-private/conf/${process.argv[2]}/conf.cron.json`;
27
+
28
+ const confCronPath = fs.existsSync(privateCronConfPath) ? privateCronConfPath : './conf/conf.cron.json';
29
+
30
+ let confCronData = JSON.parse(fs.readFileSync(confCronPath, 'utf8'));
31
+ if (confCronData.ipDaemon.disabled) return;
32
+ Dns.ip = confCronData.ipDaemon.ip;
33
+ logger.info(`Current ip`, Dns.ip);
34
+ if (Dns.ipDaemon) clearInterval(Dns.ipDaemon);
35
+ const callback = async () => {
36
+ let testIp;
37
+ try {
38
+ testIp = await ip.public.ipv4();
39
+ } catch (error) {
40
+ logger.error(error, { testIp, stack: error.stack });
41
+ }
42
+ if (testIp && typeof testIp === 'string' && isIPv4(testIp) && Dns.ip !== testIp) {
43
+ logger.info(`New ip`, testIp);
44
+ Dns.ip = testIp;
45
+ confCronData.ipDaemon.ip = Dns.ip;
46
+ fs.writeFileSync(confCronPath, JSON.stringify(confCronData, null, 4), 'utf8');
47
+ for (const recordType of Object.keys(confCronData.records)) {
48
+ switch (recordType) {
49
+ case 'A':
50
+ for (const dnsProvider of confCronData.records[recordType]) {
51
+ if (typeof Dns.services.updateIp[dnsProvider.dns] === 'function')
52
+ await Dns.services.updateIp[dnsProvider.dns](dnsProvider);
53
+ }
54
+ break;
55
+
56
+ default:
57
+ break;
58
+ }
59
+ }
60
+ }
61
+ };
62
+ this.callback = callback;
63
+ return callback;
64
+ },
65
+ services: {
66
+ updateIp: {
67
+ dondominio: (options) => {
68
+ const { user, api_key, host, dns } = options;
69
+ const url = `https://dondns.dondominio.com/json/?user=${user}&password=${api_key}&host=${host}&ip=${Dns.ip}`;
70
+ logger.info(`${dns} update ip url`, url);
71
+ if (process.env.NODE_ENV !== 'production') return false;
72
+ return new Promise((resolve) => {
73
+ axios
74
+ .get(url)
75
+ .then((response) => {
76
+ logger.info(`${dns} update ip success`, response.data);
77
+ return resolve(true);
78
+ })
79
+ .catch((error) => {
80
+ logger.error(error, `${dns} update ip error`);
81
+ return resolve(false);
82
+ });
83
+ });
84
+ },
85
+ },
86
+ },
87
+ };
88
+
89
+ export { Dns };
@@ -0,0 +1,42 @@
1
+ import axios from 'axios';
2
+ import fs from 'fs';
3
+ import { loggerFactory } from './logger.js';
4
+ import dotenv from 'dotenv';
5
+ import https from 'https';
6
+
7
+ dotenv.config();
8
+
9
+ const httpsAgent = new https.Agent({
10
+ rejectUnauthorized: false,
11
+ });
12
+
13
+ axios.defaults.httpsAgent = httpsAgent;
14
+
15
+ const logger = loggerFactory(import.meta);
16
+
17
+ const Downloader = (url, fullPath, options = { method: 'get', responseType: 'stream' }) =>
18
+ new Promise((resolve, reject) =>
19
+ axios({
20
+ url,
21
+ ...options,
22
+ })
23
+ .then((response) => {
24
+ // Create a write stream to save the file to the specified path
25
+ const writer = fs.createWriteStream(fullPath);
26
+ response.data.pipe(writer);
27
+ writer.on('finish', () => {
28
+ logger.info('Download complete. File saved at', fullPath);
29
+ return resolve(fullPath);
30
+ });
31
+ writer.on('error', (error) => {
32
+ logger.error(error, 'Error downloading the file');
33
+ return reject(error);
34
+ });
35
+ })
36
+ .catch((error) => {
37
+ logger.error(error, 'Error in the request');
38
+ return reject(error);
39
+ }),
40
+ );
41
+
42
+ export { Downloader };
@@ -0,0 +1,190 @@
1
+ /**
2
+ * Module for managing logger control and configuration
3
+ * @module src/server/logger.js
4
+ * @namespace Logger
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ import dotenv from 'dotenv';
10
+ import winston from 'winston';
11
+ import morgan from 'morgan';
12
+ import colorize from 'json-colorizer';
13
+ import colors from 'colors';
14
+ import v8 from 'v8';
15
+ import isAdmin from 'is-admin';
16
+ import { clearTerminalStringColor, formatBytes } from '../client/components/core/CommonJs.js';
17
+
18
+ colors.enable();
19
+ dotenv.config();
20
+
21
+ // Define your severity levels.
22
+ // With them, You can create log files,
23
+ // see or hide levels based on the running ENV.
24
+ const levels = {
25
+ error: 0,
26
+ warn: 1,
27
+ info: 2,
28
+ http: 3,
29
+ debug: 4,
30
+ };
31
+
32
+ // This method set the current severity based on
33
+ // the current NODE_ENV: show all the log levels
34
+ // if the server was run in development mode; otherwise,
35
+ // if it was run in production, show only warn and error messages.
36
+ const level = () => 'info'; // (process.env.NODE_ENV || 'development' ? 'debug' : 'warn');
37
+
38
+ // Define different colors for each level.
39
+ // Colors make the log message more visible,
40
+ // adding the ability to focus or ignore messages.
41
+
42
+ // Tell winston that you want to link the colors
43
+ // defined above to the severity levels.
44
+ winston.addColors({
45
+ error: 'red',
46
+ warn: 'yellow',
47
+ info: 'green',
48
+ http: 'magenta',
49
+ debug: 'white',
50
+ });
51
+
52
+ // Chose the aspect of your log customizing the log format.
53
+ const format = (meta) =>
54
+ winston.format.combine(
55
+ // winston.format.errors({ stack: true }),
56
+ // Add the message timestamp with the preferred format
57
+ winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss:ms' }),
58
+ // Tell Winston that the logs must be colored
59
+ winston.format.colorize({ all: true }),
60
+ // Define the format of the message showing the timestamp, the level and the message
61
+ winston.format.printf((info) => {
62
+ const symbols = Object.getOwnPropertySymbols(info);
63
+ return `${`[${meta}]`.green} ${info.timestamp} ${info.level} ${
64
+ symbols[1]
65
+ ? `${clearTerminalStringColor(info.message)}: ${colorize(JSON.stringify(info[symbols[1]][0], null, 4), {
66
+ colors: {
67
+ STRING_KEY: 'green',
68
+ STRING_LITERAL: 'magenta.bold',
69
+ NUMBER_LITERAL: '#FF0000',
70
+ },
71
+ })}`
72
+ : info.message
73
+ }`;
74
+ }),
75
+ );
76
+
77
+ /**
78
+ * Logs information about the current process environment to the console.
79
+ *
80
+ * This function is used to log details about
81
+ * the execution context, such as command-line arguments,
82
+ * environment variables, the process's administrative privileges,
83
+ * and the maximum available heap space size.
84
+ *
85
+ * @param {winston.Logger} logger - A pre-configured Winston logger object.
86
+ * @memberof Logger
87
+ */
88
+ const setUpInfo = async (logger = new winston.Logger()) => {
89
+ logger.info('argv', process.argv);
90
+ logger.info('platform', process.platform);
91
+ logger.info('env', process.env.NODE_ENV);
92
+ logger.info('admin', await isAdmin());
93
+ logger.info('--max-old-space-size', {
94
+ total_available_size: formatBytes(v8.getHeapStatistics().total_available_size),
95
+ });
96
+ };
97
+
98
+ /**
99
+ * The function `loggerFactory` creates a logger instance with specified transports for printing out
100
+ * messages.
101
+ * @param meta - The `meta` parameter in the `loggerFactory` function is used to extract the last part
102
+ * of a URL and use it to create log files in a specific directory.
103
+ * @returns {winston.Logger} The `loggerFactory` function returns a logger instance created using Winston logger
104
+ * library. The logger instance is configured with various transports for printing out messages to
105
+ * different destinations such as the terminal, error.log file, and all.log file. The logger instance
106
+ * also has a method `setUpInfo` attached to it for setting up additional information.
107
+ * @memberof Logger
108
+ */
109
+ const loggerFactory = (meta = { url: '' }) => {
110
+ meta = meta.url.split('/').pop();
111
+ // Define which transports the logger must use to print out messages.
112
+ // In this example, we are using three different transports
113
+ const transports = [
114
+ // Allow the use the terminal to print the messages
115
+ new winston.transports.Console(),
116
+ // Allow to print all the error level messages inside the error.log file
117
+ new winston.transports.File({
118
+ filename: `logs/${meta}/error.log`,
119
+ level: 'error',
120
+ }),
121
+ // Allow to print all the error message inside the all.log file
122
+ // (also the error log that are also printed inside the error.log(
123
+ new winston.transports.File({ filename: `logs/${meta}/all.log` }),
124
+ ];
125
+
126
+ // Create the logger instance that has to be exported
127
+ // and used to log messages.
128
+ const logger = winston.createLogger({
129
+ defaultMeta: meta,
130
+ level: level(),
131
+ levels,
132
+ format: format(meta),
133
+ transports,
134
+ // exceptionHandlers: [new winston.transports.File({ filename: 'exceptions.log' })],
135
+ // rejectionHandlers: [new winston.transports.File({ filename: 'rejections.log' })],
136
+ // exitOnError: false,
137
+ });
138
+ logger.setUpInfo = async () => {
139
+ await setUpInfo(logger);
140
+ };
141
+ return logger;
142
+ };
143
+
144
+ /**
145
+ * The `loggerMiddleware` function creates a middleware for logging HTTP requests using Morgan with
146
+ * custom message format and options.
147
+ * @param meta - The `meta` parameter in the `loggerMiddleware` function is an object that contains
148
+ * information about the request URL. It has a default value of an empty object `{ url: '' }`. This
149
+ * object is used to provide additional metadata for logging purposes.
150
+ * @returns {Handler<any, any>} The `loggerMiddleware` function returns a middleware function that uses the Morgan library
151
+ * to log HTTP request information. The middleware function formats the log message using predefined
152
+ * tokens provided by Morgan and custom tokens like `:host` to include specific request details. The
153
+ * log message format includes information such as remote address, HTTP method, host, URL, status code,
154
+ * content length, and response time in milliseconds. The middleware
155
+ * @memberof Logger
156
+ */
157
+ const loggerMiddleware = (meta = { url: '' }) => {
158
+ const stream = {
159
+ // Use the http severity
160
+ write: (message) => loggerFactory(meta).http(message),
161
+ };
162
+
163
+ const skip = (req, res) => process.env.NODE_ENV === 'production';
164
+
165
+ morgan.token('host', function (req, res) {
166
+ return req.headers['host'];
167
+ });
168
+
169
+ return morgan(
170
+ // Define message format string (this is the default one).
171
+ // The message format is made from tokens, and each token is
172
+ // defined inside the Morgan library.
173
+ // You can create your custom token to show what do you want from a request.
174
+ `:remote-addr :method :host:url :status :res[content-length] - :response-time ms`,
175
+ // Options: in this case, I overwrote the stream and the skip logic.
176
+ // See the methods above.
177
+ { stream, skip },
178
+ );
179
+ };
180
+
181
+ const underpostASCI = () => `
182
+ ▗▖ ▗▖▗▖ ▗▖▗▄▄▄ ▗▄▄▄▖▗▄▄▖ ▄▄▄▄ ▄▄▄ ▄▄▄ ■
183
+ ▐▌ ▐▌▐▛▚▖▐▌▐▌ █ ▐▌ ▐▌ ▐▌█ █ █ █ ▀▄▄▗▄▟▙▄▖
184
+ ▐▌ ▐▌▐▌ ▝▜▌▐▌ █ ▐▛▀▀▘▐▛▀▚▖█▄▄▄▀ ▀▄▄▄▀ ▄▄▄▀ ▐▌
185
+ ▝▚▄▞▘▐▌ ▐▌▐▙▄▄▀ ▐▙▄▄▖▐▌ ▐▌█ ▐▌
186
+ ▀ ▐▌
187
+
188
+ `;
189
+
190
+ export { loggerFactory, loggerMiddleware, setUpInfo, underpostASCI };
@@ -0,0 +1,122 @@
1
+ import detect from 'detect-port';
2
+ import fs from 'fs-extra';
3
+
4
+ import { publicIp, publicIpv4, publicIpv6 } from 'public-ip';
5
+ import { killPortProcess } from 'kill-port-process';
6
+ import { loggerFactory } from './logger.js';
7
+ import { orderArrayFromAttrInt } from '../client/components/core/CommonJs.js';
8
+
9
+ // Network Address Translation Management
10
+
11
+ // import dotenv from 'dotenv';
12
+ // dotenv.config();
13
+
14
+ const logger = loggerFactory(import.meta);
15
+
16
+ const network = {
17
+ port: {
18
+ status: async (ports) => {
19
+ const status = [];
20
+ for (const port of ports) {
21
+ status.push({
22
+ port,
23
+ open: await new Promise((resolve) =>
24
+ detect(port)
25
+ .then((_port) => {
26
+ if (port == _port)
27
+ // `port: ${port} was not occupied`
28
+ return resolve(false);
29
+
30
+ // `port: ${port} was occupied, try port: ${_port}`
31
+ return resolve(true);
32
+ })
33
+ .catch((error) => resolve(`${error.message}`)),
34
+ ),
35
+ });
36
+ }
37
+ return status;
38
+ },
39
+ kill: async (ports) => await killPortProcess(ports),
40
+ portClean: async function (port) {
41
+ const [portStatus] = await this.status([port]);
42
+ // logger.info('port status', portStatus);
43
+ if (portStatus.open) await this.kill([port]);
44
+ },
45
+ },
46
+ };
47
+
48
+ const ip = {
49
+ public: {
50
+ get: async () => await publicIp(), // => 'fe80::200:f8ff:fe21:67cf'
51
+ ipv4: async () => await publicIpv4(), // => '46.5.21.123'
52
+ ipv6: async () => await publicIpv6(), // => 'fe80::200:f8ff:fe21:67cf'
53
+ },
54
+ };
55
+
56
+ let ipInstance = '';
57
+ const networkRouter = {};
58
+
59
+ const logRuntimeRouter = () => {
60
+ const displayLog = {};
61
+
62
+ for (const host of Object.keys(networkRouter))
63
+ for (const path of Object.keys(networkRouter[host]))
64
+ displayLog[networkRouter[host][path].publicHost] = networkRouter[host][path].local;
65
+
66
+ logger.info('Runtime network', displayLog);
67
+ };
68
+
69
+ const saveRuntimeRouter = () =>
70
+ fs.writeFileSync(
71
+ `./tmp/runtime-router.${process.argv[3] ? process.argv[3] : 'default'}.json`,
72
+ JSON.stringify(networkRouter, null, 4),
73
+ 'utf-8',
74
+ );
75
+
76
+ const listenServerFactory = (logic = async () => {}) => {
77
+ return {
78
+ listen: async (...args) => (logic ? await logic(...args) : undefined, args[1]()),
79
+ };
80
+ };
81
+
82
+ const listenPortController = async (server, port, metadata) =>
83
+ new Promise((resolve) => {
84
+ try {
85
+ if (!server) server = listenServerFactory();
86
+
87
+ const { host, path, client, runtime, meta } = metadata;
88
+ const error = [];
89
+ if (port === undefined) error.push(`port`);
90
+ if (host === undefined) error.push(`host`);
91
+ if (path === undefined) error.push(`path`);
92
+ if (client === undefined) error.push(`client`);
93
+ if (runtime === undefined) error.push(`runtime`);
94
+ if (meta === undefined) error.push(`meta`);
95
+ if (error.length > 0) throw new Error('Listen port controller requires values: ' + error.join(', '));
96
+
97
+ server.listen(port, () => {
98
+ if (!networkRouter[host]) networkRouter[host] = {};
99
+ networkRouter[host][path] = {
100
+ meta,
101
+ client,
102
+ runtime,
103
+ port,
104
+ public: `http://${ipInstance}:${port}${path}`,
105
+ publicHost:
106
+ port === 80
107
+ ? `http://${host}${path}`
108
+ : port === 443
109
+ ? `https://${host}${path}`
110
+ : `http://${host}:${port}${path}`,
111
+ local: `http://localhost:${port}${path}`,
112
+ };
113
+
114
+ return resolve(true);
115
+ });
116
+ } catch (error) {
117
+ logger.error(error, { metadata, port, stack: error.stack });
118
+ resolve(false);
119
+ }
120
+ });
121
+
122
+ export { ip, network, listenPortController, networkRouter, saveRuntimeRouter, logRuntimeRouter, listenServerFactory };
@@ -0,0 +1,33 @@
1
+ import { PeerServer } from 'peer';
2
+ import dotenv from 'dotenv';
3
+ import { loggerFactory } from './logger.js';
4
+ import fs from 'fs-extra';
5
+ import { listenServerFactory } from './network.js';
6
+
7
+ dotenv.config();
8
+
9
+ const logger = loggerFactory(import.meta);
10
+
11
+ // https://github.com/peers/peerjs
12
+ // https://github.com/peers/peerjs-server
13
+
14
+ const createPeerServer = async ({ port, devPort, origins, host, path }) => {
15
+ if (process.env.NODE_ENV === 'development' && devPort) origins.push(`http://localhost:${devPort}`);
16
+ /** @type {import('peer').IConfig} */
17
+ const options = {
18
+ port,
19
+ path: `${path === '/' ? '' : path}/peer`,
20
+ corsOptions: {
21
+ origin: origins,
22
+ },
23
+ proxied: true,
24
+ // key: fs.readFileSync(''),
25
+ // cert: fs.readFileSync(''),
26
+ // ca: fs.readFileSync(''),
27
+ };
28
+ const peerServer = listenServerFactory(() => PeerServer(options));
29
+
30
+ return { options, peerServer, meta: import.meta };
31
+ };
32
+
33
+ export { createPeerServer };
@@ -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
+ }