miqro 6.1.2 → 6.1.3

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 (157) hide show
  1. package/build/esm/editor/auth.d.ts +6 -0
  2. package/build/esm/editor/auth.js +41 -0
  3. package/build/esm/editor/common/admin-interface.d.ts +36 -0
  4. package/build/esm/editor/common/admin-interface.js +44 -0
  5. package/build/esm/editor/common/constants.d.ts +4 -0
  6. package/build/esm/editor/common/constants.js +20 -0
  7. package/build/esm/editor/common/constants.server.d.ts +2 -0
  8. package/build/esm/editor/common/constants.server.js +4 -0
  9. package/build/esm/editor/common/editor-index.d.ts +2 -0
  10. package/build/esm/editor/common/editor-index.js +14 -0
  11. package/build/esm/editor/common/html-encode.d.ts +1 -0
  12. package/build/esm/editor/common/html-encode.js +14 -0
  13. package/build/esm/editor/common/log-socket.d.ts +15 -0
  14. package/build/esm/editor/common/log-socket.js +70 -0
  15. package/build/esm/editor/common/templates.d.ts +11 -0
  16. package/build/esm/editor/common/templates.js +461 -0
  17. package/build/esm/editor/components/api-preview.d.ts +11 -0
  18. package/build/esm/editor/components/api-preview.js +90 -0
  19. package/build/esm/editor/components/editor.d.ts +16 -0
  20. package/build/esm/editor/components/editor.js +357 -0
  21. package/build/esm/editor/components/file-browser.d.ts +37 -0
  22. package/build/esm/editor/components/file-browser.js +126 -0
  23. package/build/esm/editor/components/file-editor-toolbar.d.ts +22 -0
  24. package/build/esm/editor/components/file-editor-toolbar.js +93 -0
  25. package/build/esm/editor/components/file-editor.d.ts +32 -0
  26. package/build/esm/editor/components/file-editor.js +59 -0
  27. package/build/esm/editor/components/filter-query.d.ts +1 -0
  28. package/build/esm/editor/components/filter-query.js +22 -0
  29. package/build/esm/editor/components/highlight-text-area.d.ts +11 -0
  30. package/build/esm/editor/components/highlight-text-area.js +124 -0
  31. package/build/esm/editor/components/log-viewer.d.ts +6 -0
  32. package/build/esm/editor/components/log-viewer.js +69 -0
  33. package/build/esm/editor/components/new-file.d.ts +10 -0
  34. package/build/esm/editor/components/new-file.js +117 -0
  35. package/build/esm/editor/components/scroll-query.d.ts +7 -0
  36. package/build/esm/editor/components/scroll-query.js +21 -0
  37. package/build/esm/editor/components/start-page.d.ts +13 -0
  38. package/build/esm/editor/components/start-page.js +30 -0
  39. package/build/esm/editor/http/admin/editor/api/fs/delete.api.d.ts +3 -0
  40. package/build/esm/editor/http/admin/editor/api/fs/delete.api.js +29 -0
  41. package/build/esm/editor/http/admin/editor/api/fs/read.api.d.ts +5 -0
  42. package/build/esm/editor/http/admin/editor/api/fs/read.api.js +49 -0
  43. package/build/esm/editor/http/admin/editor/api/fs/rename.api.d.ts +4 -0
  44. package/build/esm/editor/http/admin/editor/api/fs/rename.api.js +39 -0
  45. package/build/esm/editor/http/admin/editor/api/fs/scan.api.d.ts +26 -0
  46. package/build/esm/editor/http/admin/editor/api/fs/scan.api.js +149 -0
  47. package/build/esm/editor/http/admin/editor/api/fs/write.api.d.ts +3 -0
  48. package/build/esm/editor/http/admin/editor/api/fs/write.api.js +38 -0
  49. package/build/esm/editor/http/admin/editor/api/server/reload.api.d.ts +10 -0
  50. package/build/esm/editor/http/admin/editor/api/server/reload.api.js +46 -0
  51. package/build/esm/editor/http/admin/editor/api/server/restart.api.d.ts +10 -0
  52. package/build/esm/editor/http/admin/editor/api/server/restart.api.js +45 -0
  53. package/build/esm/editor/http/admin/editor/editor.d.ts +1 -0
  54. package/build/esm/editor/http/admin/editor/editor.js +7 -0
  55. package/build/esm/editor/http/admin/editor/index.api.d.ts +3 -0
  56. package/build/esm/editor/http/admin/editor/index.api.js +21 -0
  57. package/build/esm/editor/server.d.ts +3 -0
  58. package/build/esm/editor/server.js +49 -0
  59. package/build/esm/editor/ws.d.ts +3 -0
  60. package/build/esm/editor/ws.js +11 -0
  61. package/editor/auth.ts +51 -0
  62. package/editor/common/admin-interface.ts +84 -0
  63. package/editor/common/constants.server.ts +5 -0
  64. package/editor/common/constants.ts +21 -0
  65. package/editor/common/editor-index.tsx +17 -0
  66. package/editor/common/html-encode.ts +14 -0
  67. package/editor/common/log-socket.tsx +85 -0
  68. package/editor/common/templates.ts +466 -0
  69. package/editor/components/api-preview.tsx +116 -0
  70. package/editor/components/editor.tsx +486 -0
  71. package/editor/components/file-browser.tsx +308 -0
  72. package/editor/components/file-editor-toolbar.tsx +191 -0
  73. package/editor/components/file-editor.tsx +122 -0
  74. package/editor/components/filter-query.tsx +22 -0
  75. package/editor/components/highlight-text-area.tsx +144 -0
  76. package/editor/components/log-viewer.tsx +110 -0
  77. package/editor/components/new-file.tsx +169 -0
  78. package/editor/components/scroll-query.tsx +22 -0
  79. package/editor/components/start-page.tsx +49 -0
  80. package/editor/http/admin/editor/api/fs/delete.api.tsx +32 -0
  81. package/editor/http/admin/editor/api/fs/read.api.tsx +55 -0
  82. package/editor/http/admin/editor/api/fs/rename.api.tsx +41 -0
  83. package/editor/http/admin/editor/api/fs/scan.api.tsx +181 -0
  84. package/editor/http/admin/editor/api/fs/write.api.tsx +41 -0
  85. package/editor/http/admin/editor/api/server/reload.api.ts +53 -0
  86. package/editor/http/admin/editor/api/server/restart.api.tsx +52 -0
  87. package/editor/http/admin/editor/editor.tsx +8 -0
  88. package/editor/http/admin/editor/index.api.tsx +39 -0
  89. package/editor/server.ts +57 -0
  90. package/editor/ws.ts +15 -0
  91. package/package.json +1 -1
  92. package/src/bin/compile.ts +35 -0
  93. package/src/bin/doc-md.ts +210 -0
  94. package/src/bin/generate-doc.ts +64 -0
  95. package/src/bin/test.ts +92 -0
  96. package/src/bin/types.ts +22 -0
  97. package/src/cluster.ts +27 -0
  98. package/src/common/arguments.ts +503 -0
  99. package/src/common/assets.ts +128 -0
  100. package/src/common/checksum.ts +58 -0
  101. package/src/common/constants.ts +18 -0
  102. package/src/common/content-type.ts +84 -0
  103. package/src/common/esbuild.ts +94 -0
  104. package/src/common/exit.ts +91 -0
  105. package/src/common/fs.ts +17 -0
  106. package/src/common/help.ts +51 -0
  107. package/src/common/jsx.ts +512 -0
  108. package/src/common/paths.ts +158 -0
  109. package/src/common/watch.ts +85 -0
  110. package/src/inflate/inflate-sea.ts +206 -0
  111. package/src/inflate/inflate.ts +99 -0
  112. package/src/inflate/md.ts +25 -0
  113. package/src/inflate/setup-auth.ts +40 -0
  114. package/src/inflate/setup-cors.ts +40 -0
  115. package/src/inflate/setup-db.ts +113 -0
  116. package/src/inflate/setup-error.ts +43 -0
  117. package/src/inflate/setup-http.ts +655 -0
  118. package/src/inflate/setup-log.ts +44 -0
  119. package/src/inflate/setup-middleware.ts +46 -0
  120. package/src/inflate/setup-server-config.ts +47 -0
  121. package/src/inflate/setup-test.ts +23 -0
  122. package/src/inflate/setup-ws.ts +48 -0
  123. package/src/inflate/setup.doc.ts +68 -0
  124. package/src/inflate/utils/sea-utils.ts +14 -0
  125. package/src/lib.ts +19 -0
  126. package/src/main.ts +87 -0
  127. package/src/services/app.ts +658 -0
  128. package/src/services/editor.tsx +101 -0
  129. package/src/services/globals.ts +143 -0
  130. package/src/services/hot-reload.ts +48 -0
  131. package/src/services/migrations.ts +66 -0
  132. package/src/services/utils/admin-interface.ts +37 -0
  133. package/src/services/utils/cache.ts +88 -0
  134. package/src/services/utils/cluster-cache.ts +230 -0
  135. package/src/services/utils/cluster-ws.ts +197 -0
  136. package/src/services/utils/db-manager.ts +92 -0
  137. package/src/services/utils/get-route.ts +70 -0
  138. package/src/services/utils/log-transport.ts +75 -0
  139. package/src/services/utils/log.ts +92 -0
  140. package/src/services/utils/server-interface.ts +172 -0
  141. package/src/services/utils/websocketmanager.ts +149 -0
  142. package/src/types/@esbuild.d.ts +1 -0
  143. package/src/types/@miqro/core.d.ts +2 -0
  144. package/src/types/@miqro/jsx.d.ts +2 -0
  145. package/src/types/@miqro/parser.d.ts +2 -0
  146. package/src/types/@miqro/query.d.ts +2 -0
  147. package/src/types/@miqro/request.d.ts +2 -0
  148. package/src/types/@miqro/test.d.ts +2 -0
  149. package/src/types/@miqro.d.ts +1 -0
  150. package/src/types/@types.d.ts +1 -0
  151. package/src/types/browser.globals.d.ts +9 -0
  152. package/src/types/globals.d.ts +2 -0
  153. package/src/types/jsx.globals.d.ts +37 -0
  154. package/src/types/miqro.d.ts +129 -0
  155. package/src/types/postject.d.ts +1 -0
  156. package/src/types/server.globals.d.ts +72 -0
  157. package/src/types.ts +222 -0
@@ -0,0 +1,85 @@
1
+ import "../services/globals.js";
2
+ import { FSWatcher, existsSync, readdirSync, statSync, watch } from "node:fs";
3
+ import { resolve } from "node:path";
4
+ import { Miqro } from "../services/app.js";
5
+
6
+ export async function watchAndServer(app: Miqro) {
7
+ let timeout: any;
8
+ const watchLogger = app.logger;
9
+ const watchers: FSWatcher[] = [];
10
+
11
+ /*await start({
12
+ logger,
13
+ webSocketManager,
14
+ dbManager,
15
+ services,
16
+ editor,
17
+ inflateTests: false,
18
+ inflateDir: false,
19
+ inflateSea: false,
20
+ runOnTestPort: false
21
+ });*/
22
+
23
+ //await app.start();
24
+
25
+ function watchHandler(eventType: any, filename: any) {
26
+ stopWatch();
27
+ watchLogger?.debug(`event type is: ${eventType}`);
28
+ if (filename) {
29
+ watchLogger?.debug(`filename provided: ${filename}`);
30
+ } else {
31
+ watchLogger?.debug('filename not provided');
32
+ }
33
+ watchLogger?.info(`${eventType} on ${filename}`);
34
+ clearTimeout(timeout);
35
+ timeout = setTimeout(async () => {
36
+ try {
37
+ stopWatch();
38
+ setTimeout(async () => {
39
+ watchLogger?.debug("closed");
40
+ await app.reload();
41
+ reWatch();
42
+ }, 500);
43
+ } catch (e) {
44
+ watchLogger?.error(e);
45
+ }
46
+ }, 2000);
47
+ }
48
+
49
+ function watchDir(toWatch: string) {
50
+ const files = existsSync(toWatch) ? readdirSync(toWatch) : [];
51
+ for (const file of files) {
52
+ const filePath = resolve(toWatch, file);
53
+ if (statSync(filePath).isDirectory()) {
54
+ watchDir(filePath);
55
+ } else {
56
+ //console.log("toWatch=" + filePath);
57
+ watchers.push(watch(filePath, watchHandler));
58
+ }
59
+ }
60
+ }
61
+
62
+ function stopWatch() {
63
+ const toClose = watchers.splice(0, watchers.length);
64
+ for (const watcher of toClose) {
65
+ watcher.close();
66
+ }
67
+ }
68
+
69
+
70
+ function reWatch() {
71
+ stopWatch();
72
+ for (const service of app.options.services) {
73
+ const toWatch = resolve(process.cwd(), service);
74
+ watchDir(toWatch);
75
+ }
76
+ }
77
+
78
+ watchLogger?.info("watching for changes on [%s]", app.options.services.join(","));
79
+ reWatch();
80
+ return {
81
+ stopWatch: () => {
82
+ stopWatch();
83
+ }
84
+ };
85
+ }
@@ -0,0 +1,206 @@
1
+ import { Logger } from "@miqro/core";
2
+ import { chmodSync, constants, mkdirSync, writeFileSync } from "node:fs";
3
+ import { dirname, extname, join, relative, resolve } from "node:path";
4
+ import { cwd, platform } from "node:process";
5
+
6
+ import { RouteFileMap, StaticFileMap } from "./setup-http.js";
7
+ import { getAuthConfigPath, getCORSConfigPath, getDBConfigPath, getErrorConfigPath, getMiddlewareConfigPath, getMigrationsPath, getServerConfigPath, getServicePath, getWSConfigPath } from "../common/paths.js";
8
+ import { getAsset } from "../common/assets.js";
9
+ import { migration } from "@miqro/query";
10
+
11
+ //export const libCJSBuffer = Buffer.from(getAsset("lib.cjs"));
12
+ /*export const COMPILESH = Buffer.from(Buffer.from(getAsset("compile.base64.sh")).toString(), "base64");
13
+ export const SIGN_REMOVESH = Buffer.from(getAsset("sign-remove.sh"));
14
+ export const SIGN_ADDSH = Buffer.from(getAsset("sign-add.sh"));
15
+ export const INSTALLNODEJSSH = Buffer.from(getAsset("install-nodejs.sh"));
16
+ export const APPSH = Buffer.from(getAsset("app.sh"));
17
+ export const NODESH = Buffer.from(getAsset("node.sh"));
18
+ export const SEACONFIGJSON = Buffer.from(getAsset("sea.basic.config.json"));
19
+ export const POSTJECTCJS = Buffer.from(Buffer.from(getAsset("postject.base64.cjs")).toString(), "base64");*/
20
+
21
+ export async function inflateSeaAssets(logger: Logger, inflateDir: string) {
22
+ const esbuildBinaryBuffer = Buffer.from(getAsset("esbuild-binary"));
23
+ if (platform === "win32") {
24
+ writeFile(logger, resolve(inflateDir, "sea", "esbuild.exe"), esbuildBinaryBuffer);
25
+ chmodSync(resolve(inflateDir, "sea", "esbuild.exe"), constants.S_IXUSR | constants.S_IRUSR | constants.S_IWUSR);
26
+ } else {
27
+ writeFile(logger, resolve(inflateDir, "sea", "esbuild"), esbuildBinaryBuffer);
28
+ chmodSync(resolve(inflateDir, "sea", "esbuild"), constants.S_IXUSR | constants.S_IRUSR | constants.S_IWUSR);
29
+ }
30
+ writeFile(logger, resolve(inflateDir, "sea", "postject.cjs"), Buffer.from(Buffer.from(getAsset("postject.base64.cjs")).toString(), "base64"));
31
+ writeFile(logger, resolve(inflateDir, "sea", "config.json"), Buffer.from(getAsset("sea.basic.config.json")));
32
+ writeFile(logger, resolve(inflateDir, "sea", "run.sh"), Buffer.from(getAsset("app.sh")));
33
+ writeFile(logger, resolve(inflateDir, "sea", "node.sh"), Buffer.from(getAsset("node.sh")));
34
+ writeFile(logger, resolve(inflateDir, "compile.sh"), Buffer.from(Buffer.from(getAsset("compile.base64.sh")).toString(), "base64"));
35
+ writeFile(logger, resolve(inflateDir, "sea", "sign-add.sh"), Buffer.from(getAsset("sign-add.sh")));
36
+ writeFile(logger, resolve(inflateDir, "sea", "sign-remove.sh"), Buffer.from(getAsset("sign-remove.sh")));
37
+ writeFile(logger, resolve(inflateDir, "install-nodejs.sh"), Buffer.from(getAsset("install-nodejs.sh")));
38
+ }
39
+
40
+ export async function inflateAppForSea(logger: Logger, inflateDir: string, services: string[]) {
41
+
42
+ inflateSeaAssets(logger, inflateDir);
43
+
44
+ writeFile(logger, resolve(inflateDir, "sea", "lib.cjs"), Buffer.from(getAsset("lib.cjs")));
45
+
46
+ const WSLIST = services.filter(service => getWSConfigPath(resolve(cwd(), service))).map(service => {
47
+ return `(await import("../${service}/ws.js")).default`;
48
+ }).join(",")
49
+
50
+ const SERVERCONFIGLIST = services.filter(service => getServerConfigPath(resolve(cwd(), service))).map(service => {
51
+ return `(await import("../${service}/server.js")).default`;
52
+ }).join(",\n");
53
+
54
+ const DBCONFIGLIST = services.filter(service => getDBConfigPath(resolve(cwd(), service))).map(service => {
55
+ return `new Promise(async (resolve, reject) => {
56
+ try {
57
+ const db = await dbManager.setupDB((await import("../${service}/db.js")).default);
58
+ await (await import("./${service}/migration-up.js")).runMigrations(db);
59
+ resolve();
60
+ } catch(e) {
61
+ reject(e);
62
+ }
63
+
64
+ })`;
65
+ }).join(",");
66
+
67
+ writeFile(logger, join(inflateDir, "sea", "package.json"), `{ "type": "module", "private": true }`);
68
+
69
+ writeFile(logger, join(inflateDir, "sea", "app.cjs"), `const { ServerInterfaceImpl, ServerRequestHandler, WebSocketManager, initGlobals, DBManager, App, LoggerHandler, LogProvider, LocalCache, ClusterCache } = require("./lib.cjs");
70
+
71
+ async function main() {
72
+ const PORT = process.env["PORT"] ? process.env["PORT"] : 8080;
73
+ const logProvider = new LogProvider();
74
+ const localCache = new LocalCache();
75
+ const cache = new ClusterCache();
76
+ const webSocketManager = new WebSocketManager();
77
+ const dbManager = new DBManager();
78
+ await initGlobals();
79
+ const serverInterface = new ServerInterfaceImpl({
80
+ cache,
81
+ localCache,
82
+ logProvider,
83
+ wsManager: webSocketManager,
84
+ logger: logProvider.getLogger("server"),
85
+ dbManager,
86
+ port: PORT
87
+ });
88
+
89
+ await Promise.all([${DBCONFIGLIST}]);
90
+
91
+ ${!WSLIST ? "" : `\n webSocketManager.replaceALLWS(await Promise.all([${WSLIST}]))`}
92
+ const app = new App({
93
+ onUpgrade: webSocketManager.onUpgrade
94
+ });
95
+ ${SERVERCONFIGLIST ? `\n await Promise.all([${SERVERCONFIGLIST}].filter(config=>config.preload).map(config=>config.preload(serverInterface)));\n` : ""}
96
+ app.use(ServerRequestHandler(serverInterface));
97
+ app.use(LoggerHandler());
98
+ ${services.map(service => `app.use(await (await import("./${join(service, "router.js")}")).setupRouter());`).join("\n")}
99
+ ${SERVERCONFIGLIST ? `\n await Promise.all([${SERVERCONFIGLIST}].filter(config=>config.load).map(config=>config.load(serverInterface)));\n` : ""}
100
+
101
+ await app.listen(PORT);
102
+ ${SERVERCONFIGLIST ? `\n await Promise.all([${SERVERCONFIGLIST}].filter(config=>config.start).map(config=>config.start(serverInterface)));` : ""}
103
+ }
104
+ main().catch(e=>console.error(e));
105
+ `
106
+ );
107
+ }
108
+
109
+ export async function inflateServiceForSea(logger: Logger, inflateDir: string, service: string, servicePath: string/*, serviceMigrations: string[]*/, serviceRouteFileMap: RouteFileMap, serviceStaticFileMap: StaticFileMap) {
110
+ const migrationsFolderPath = getMigrationsPath(servicePath);
111
+
112
+ const serviceMigrations: string[] = migrationsFolderPath ? migration.getSortedMigrations(migrationsFolderPath) : [];
113
+ writeFile(logger, join(inflateDir, "sea", service, "router.js"), `import { appendAPIModule, Router } from "./../lib.cjs";\n
114
+ export async function setupRouter() {
115
+ const router = new Router();
116
+ ${getErrorConfigPath(servicePath) ? `
117
+ const errorConfig = (await import("../../${service}/catch.js")).default;
118
+ if(errorConfig && errorConfig.catch) {
119
+ for(const m of errorConfig.catch) {
120
+ router.catch(m);
121
+ }
122
+ }` : ""}
123
+ ${getCORSConfigPath(servicePath) ? ` router.use(server.middleware.cors((await import("../../${service}/cors.js")).default));` : ""}
124
+ ${getAuthConfigPath(servicePath) ? ` router.use(server.middleware.session((await import("../../${service}/auth.js")).default));` : ""}
125
+ ${getMiddlewareConfigPath(servicePath) ? `
126
+ const middlewareConfig = (await import("../../${service}/middleware.js")).default;
127
+ if(middlewareConfig && middlewareConfig.middleware) {
128
+ for(const m of middlewareConfig.middleware) {
129
+ router.use(m);
130
+ }
131
+ }` : ""}
132
+ ${Object.keys(serviceRouteFileMap)
133
+ .map(filePath => serviceRouteFileMap[filePath])
134
+ .filter(data => data.previewMethod === "api")
135
+ .map(data => data.routes.map(r => {
136
+ const rPath = r.inflatePath;
137
+ if (rPath) {
138
+ const apiInflatedPath = join("..", "..", service, "http", rPath + ".api.js");
139
+ return ` await appendAPIModule(router, "../../${service}/http", "./${apiInflatedPath}", (await import("./${apiInflatedPath}")).default);`;
140
+ } else {
141
+ return "";
142
+ }
143
+ }).filter(l => l)[0])
144
+ .join("\n")}
145
+ router.use(await (await import("./static-router.js")).setupRouter())
146
+
147
+ ${getMiddlewareConfigPath(servicePath) ? `
148
+ if(middlewareConfig && middlewareConfig.post) {
149
+ for(const m of middlewareConfig.post) {
150
+ router.use(m);
151
+ }
152
+ }` : ""}
153
+
154
+ return router;
155
+ }`);
156
+
157
+ writeFile(logger, join(inflateDir, "sea", service, "migration-up.js"), `import { migration } from "./../lib.cjs";\n
158
+ export async function runMigrations(db) {
159
+ await migration.init(db);
160
+ ${serviceMigrations.map(file => {
161
+ const name = `${file.substring(0, file.length - extname(file).length)}`;
162
+ return ` await migration.up.module(db, "${name}", (await import("../../${service}/migration/${name}.js")).default)`;
163
+ }).join("\n")}
164
+ }`);
165
+
166
+ writeFile(logger, join(inflateDir, "sea", service, "migration-down.js"), `import { migration } from "./../lib.cjs";\n
167
+ export async function runMigrations(db) {
168
+ await migration.init(db);
169
+ ${serviceMigrations.reverse().map(file => {
170
+ const name = `${file.substring(0, file.length - extname(file).length)}`;
171
+ return ` await migration.down.module(db, "${name}", (await import("../../${service}/migration/${name}.js")).default)`;
172
+ }).join("\n")}
173
+ }`);
174
+
175
+ const staticFiles = Object.keys(serviceStaticFileMap);
176
+ /*if (staticFiles.length !== 0) {
177
+ writeFile(logger, join(inflateDir, "sea", service, "static.base64.json"), JSON.stringify(serviceStaticFileMap));
178
+ }*/
179
+ writeFile(logger, join(inflateDir, "sea", service, "static-router.js"), `import { appendAPIModule, Router } from "./../lib.cjs";\n
180
+ export async function setupRouter() {
181
+ const router = new Router();
182
+ ${staticFiles.length === 0 ? "" : `
183
+ ${staticFiles.map((filePath) => {
184
+ return ` router.get("${serviceStaticFileMap[filePath].path}", async (_, res) => {
185
+ return res.asyncEnd({
186
+ status: 200,
187
+ headers: {
188
+ ["Content-Type"]: "${serviceStaticFileMap[filePath].contentType}"
189
+ },
190
+ body: Buffer.from("${serviceStaticFileMap[filePath].body.toString("base64")}", "base64")
191
+ });
192
+ });`;
193
+ }).join("\n")}
194
+ `}
195
+ return router;
196
+ }`);
197
+
198
+ }
199
+
200
+ function writeFile(logger: Logger, path: string, buffer: Buffer | string) {
201
+ logger.log("writing [%s]", relative(cwd(), path));
202
+ mkdirSync(dirname(path), {
203
+ recursive: true
204
+ });
205
+ writeFileSync(path, buffer);
206
+ }
@@ -0,0 +1,99 @@
1
+ import { InflateError } from "../common/jsx.js";
2
+ import { Logger, Router } from "@miqro/core";
3
+ import { RouteFileMap, setupHTTPRouter, StaticFileMap } from "./setup-http.js";
4
+ //import { WSMapConfig } from "./utils/websocketmanager.js";
5
+ //import { ServerConfigMap, setupServerConfig } from "./setup-server-config.js";
6
+ import { assertGlobalTampered } from "../services/globals.js";
7
+ import { getServicePath } from "../common/paths.js";
8
+ import { inflateWSConfig } from "./setup-ws.js";
9
+ import { inflateAppForSea, inflateServiceForSea } from "./inflate-sea.js";
10
+ import { ServerInterface, WSConfig } from "../types.js";
11
+ import { LogConfigMap, setupLogConfig } from "./setup-log.js";
12
+ import { setupDoc } from "./setup.doc.js";
13
+
14
+ export interface InflateAppOptions {
15
+ logger?: Logger;
16
+ services: string[];
17
+ //dbManager: DBManager;
18
+ inflateDir: string | undefined | false;
19
+ inflateSea: boolean;
20
+ //editor: boolean;
21
+ //inflateTests: boolean;
22
+ hotreload?: boolean;
23
+ serverInterface: ServerInterface;
24
+ }
25
+
26
+ export async function inflateApp({ serverInterface, logger, hotreload, services/*, dbManager*/, inflateDir, inflateSea/*, editor, inflateTests*/ }: InflateAppOptions): Promise<[Router, InflateError[] | null, RouteFileMap, WSConfig[]/*, ServerConfigMap*/, LogConfigMap]> {
27
+ logger.trace("inflateApp");
28
+ const errors: InflateError[] = [];
29
+ //const migrations: string[] = [];
30
+ let routeFileMap: RouteFileMap = {};
31
+ const wsConfigList: WSConfig[] = [];
32
+ const router = new Router();
33
+ const logConfigMap: LogConfigMap = {};
34
+ //const serverConfigMap: ServerConfigMap = {};
35
+
36
+ router.use(assertGlobalTampered);
37
+
38
+ /*if (editor) {
39
+ logger.info("setting up editor on %s", BASEEDITOR_PATH);
40
+ const editorRouter = await createEditorRouter();
41
+ router.use(editorRouter);
42
+ logger.info("setting up ws on [%s]", LOG_SOCKET_PATH);
43
+ wsConfigList.push(editorWSConfig);
44
+ serverConfigMap[EDITOR_CONFIG_KEY] = editorServerConfig;
45
+ }*/
46
+
47
+
48
+
49
+ for (const service of services) {
50
+ const serviceRouteFileMap: RouteFileMap = {};
51
+ const serviceStaticFileMap: StaticFileMap | null = inflateSea ? {} : null;
52
+ const servicePath = getServicePath(service);
53
+
54
+ /*const migrationsFolderPath = getMigrationsPath(servicePath);
55
+
56
+ const serviceMigrations: string[] = migrationsFolderPath ? migration.getSortedMigrations(migrationsFolderPath) : [];*/
57
+
58
+ /*const db = dbManager.getDB(service) ? dbManager.getDB(service) : await setupDB(logger, service, dbManager, inflateDir);;
59
+
60
+ if (db && (cluster.isPrimary || process.env["CLUSTER_NODE_NUMBER"] === "0")) {
61
+ await runMigrations(logger, db, servicePath, service, inflateDir, serviceMigrations);
62
+ }
63
+ if (db) {
64
+ migrations.push(...serviceMigrations);
65
+ }*/
66
+
67
+ //await setupDB(logger, service, dbConfigList, inflateDir);
68
+
69
+ await setupLogConfig(logger, servicePath, service, logConfigMap, inflateSea ? inflateDir : false, errors);
70
+
71
+ router.use(await setupHTTPRouter(serverInterface, logger, hotreload ? hotreload : false, servicePath, service, serviceRouteFileMap, serviceStaticFileMap, inflateDir, inflateSea, errors));
72
+ routeFileMap = {
73
+ ...routeFileMap,
74
+ ...serviceRouteFileMap
75
+ };
76
+
77
+ await setupDoc(logger, servicePath, service, router, routeFileMap, inflateDir, errors);
78
+
79
+ await inflateWSConfig(logger, servicePath, service, wsConfigList, inflateSea ? inflateDir : undefined, errors);
80
+
81
+ //await setupServerConfig(logger, servicePath, service, serverConfigMap, inflateSea ? inflateDir : undefined, errors);
82
+
83
+ if (inflateDir && inflateSea) {
84
+ await inflateServiceForSea(logger, inflateDir, service, servicePath /*, serviceMigrations*/, serviceRouteFileMap, serviceStaticFileMap);
85
+ }
86
+
87
+ /*if (inflateTests) {
88
+ await setupTests(logger, servicePath);
89
+ }*/
90
+ }
91
+
92
+ if (inflateDir && inflateSea) {
93
+ await inflateAppForSea(logger, inflateDir, services);
94
+ }
95
+
96
+ router.use(assertGlobalTampered);
97
+
98
+ return errors.length === 0 ? [router, null, routeFileMap/*, migrations*/, wsConfigList/*, serverConfigMap*/, logConfigMap] : [router, errors, routeFileMap/*, migrations*/, wsConfigList/*, serverConfigMap*/, logConfigMap];
99
+ }
@@ -0,0 +1,25 @@
1
+
2
+ import * as showdown from "showdown";
3
+ import { readFileSync } from "node:fs";
4
+ import { Logger } from "@miqro/core";
5
+
6
+ export function inflateMD2HTML(inFile: string, logger?: Logger): string {
7
+ try {
8
+ const text = readFileSync(inFile).toString();
9
+ const html = inflateMDString2HTML(text);
10
+ return html;
11
+ } catch (e) {
12
+ logger?.error("error with: " + inFile);
13
+ logger?.error(e);
14
+ throw e;
15
+ }
16
+ }
17
+
18
+ export function inflateMDString2HTML(text: string, logger?: Logger): string {
19
+ const converter = new (showdown as any).default.Converter();
20
+ converter.setFlavor('github');
21
+ /*converter.setOption("tables", true);
22
+ converter.setOption("rawHeaderId", true);*/
23
+ const html = converter.makeHtml(text);
24
+ return html;
25
+ }
@@ -0,0 +1,40 @@
1
+ import { Logger, Router, SessionHandler } from "@miqro/core";
2
+ import { importAuthModule, InflateError, inflateJSX } from "../common/jsx.js";
3
+ import { getAuthConfigPath } from "../common/paths.js";
4
+ import { basename, dirname, join, relative, resolve } from "node:path";
5
+ import { mkdirSync, writeFileSync } from "node:fs";
6
+ import { cwd } from "node:process";
7
+
8
+ export async function setupAUTH(logger: Logger, servicePath: string, service: string, mainRouter: Router, inflateDir: string | undefined | false, inflateSea: boolean, errors: InflateError[]) {
9
+ const authPath = getAuthConfigPath(servicePath); //resolve(process.cwd(), service, "auth.ts");
10
+
11
+ if (authPath) {
12
+ try {
13
+ const authModule = await importAuthModule(authPath, logger);
14
+ logger.debug("setting up authentication from [%s]", join(service, basename(authPath)));
15
+ mainRouter.use(SessionHandler(authModule));
16
+
17
+ if (inflateDir && inflateSea) {
18
+ const inflatePath = resolve(inflateDir, service, "auth.js");
19
+ mkdirSync(dirname(inflatePath), {
20
+ recursive: true
21
+ });
22
+ logger.log("writing [%s]", relative(cwd(), inflatePath));
23
+ writeFileSync(inflatePath, await inflateJSX(authPath, {
24
+ embemedJSX: false,
25
+ minify: false,
26
+ useExport: true,
27
+ logger
28
+ }));
29
+ }
30
+
31
+ } catch (e) {
32
+ errors.push({
33
+ filePath: authPath,
34
+ error: e
35
+ });
36
+ logger.error("error with " + authPath);
37
+ logger.error(e);
38
+ }
39
+ }
40
+ }
@@ -0,0 +1,40 @@
1
+ import { mkdirSync, writeFileSync } from "node:fs";
2
+ import { importCORSModule, InflateError, inflateJSX } from "../common/jsx.js";
3
+ import { getCORSConfigPath } from "../common/paths.js";
4
+ import { CORS, Logger, Router } from "@miqro/core";
5
+ import { basename, dirname, join, relative, resolve } from "node:path";
6
+ import { cwd } from "node:process";
7
+
8
+ export async function setupCORS(logger: Logger, servicePath: string, service: string, mainRouter: Router, inflateDir: string | undefined | false, inflateSea: boolean, errors: InflateError[]) {
9
+ const corsPath = getCORSConfigPath(servicePath);
10
+
11
+ if (corsPath) {
12
+ try {
13
+ const corsOptions = await importCORSModule(corsPath, logger);
14
+ logger.debug("setting up cors from [%s]", join(service, basename(corsPath)));
15
+ mainRouter.use(CORS(corsOptions));
16
+
17
+ if (inflateDir && inflateSea) {
18
+ const inflatePath = resolve(inflateDir, service, "cors.js");
19
+ mkdirSync(dirname(inflatePath), {
20
+ recursive: true
21
+ });
22
+ logger.log("writing [%s]", relative(cwd(), inflatePath));
23
+ writeFileSync(inflatePath, await inflateJSX(corsPath, {
24
+ embemedJSX: false,
25
+ minify: false,
26
+ useExport: true,
27
+ logger
28
+ }));
29
+ }
30
+
31
+ } catch (e) {
32
+ errors.push({
33
+ filePath: corsPath,
34
+ error: e
35
+ });
36
+ logger.error("error with " + corsPath);
37
+ logger.error(e);
38
+ }
39
+ }
40
+ }
@@ -0,0 +1,113 @@
1
+ import { Logger } from "@miqro/core";
2
+ import { Migration, migration } from "@miqro/query";
3
+ import { importDBConfigModule, importMigrationModule, InflateError, inflateJSX } from "../common/jsx.js";
4
+ import { getDBConfigPath, getMigrationsPath, getServicePath } from "../common/paths.js";
5
+ import { dirname, extname, relative, resolve } from "node:path";
6
+ import { mkdirSync, writeFileSync } from "node:fs";
7
+ import { cwd } from "node:process";
8
+ import { DBConfig, NamedMigration } from "../types.js";
9
+
10
+ export interface MigrationModule extends Migration, NamedMigration {
11
+
12
+ }
13
+
14
+ export async function inflateDBConfig(logger: Logger, service: string, dbConfigList: DBConfig[] | undefined, inflateDir: string | undefined | false, errors: InflateError[]) {
15
+ const servicePath = getServicePath(service);
16
+ const dbConfigPath = getDBConfigPath(servicePath);
17
+ if (dbConfigPath) {
18
+ try {
19
+ //logger.debug("loading DBConfig for service[%s]", service);
20
+ const config = await importDBConfigModule(dbConfigPath, logger);
21
+ if (config && dbConfigList && dbConfigList.filter(c => c.name === config.name).length > 0) {
22
+ throw new Error(`ws path [${config.name}] already defined! error from [${dbConfigPath}]`);
23
+ } else if (config) {
24
+ //logger.debug("DBConfig [%s] loaded from service [%s]", config.name, service);
25
+ if (dbConfigList) {
26
+ dbConfigList.push(config);
27
+ }
28
+ }
29
+ if (config) {
30
+ if (inflateDir) {
31
+ const inflatePath = resolve(inflateDir, service, "db.js");
32
+ mkdirSync(dirname(inflatePath), {
33
+ recursive: true
34
+ });
35
+ logger.log("writing [%s]", relative(cwd(), inflatePath));
36
+ writeFileSync(inflatePath, await inflateJSX(dbConfigPath, {
37
+ embemedJSX: false,
38
+ minify: false,
39
+ useExport: true,
40
+ logger
41
+ }));
42
+ }
43
+
44
+ //const db = await dbManager.setupDB(service, config);
45
+
46
+ //return db;
47
+ return config;
48
+ }
49
+ } catch (e) {
50
+ errors.push({
51
+ filePath: dbConfigPath,
52
+ error: e
53
+ });
54
+ logger.error("error with " + dbConfigPath);
55
+ logger.error(e);
56
+ return false;
57
+ }
58
+ }
59
+ return false;
60
+ }
61
+
62
+ export async function inflateDBMigrations(logger: Logger, service: string, dbName: string, inflateDir: string | undefined | false, errors: InflateError[]) {
63
+ const servicePath = getServicePath(service);
64
+ const migrationsFolderPath = getMigrationsPath(servicePath);
65
+ if (migrationsFolderPath) {
66
+
67
+ logger?.trace("loading migrations from service [%s]", service);
68
+
69
+ const serviceMigrations = migration.getSortedMigrations(migrationsFolderPath);
70
+
71
+ const migrationModules: MigrationModule[] = [];
72
+
73
+ for (const migrationName of serviceMigrations) {
74
+ const migrationPath = resolve(migrationsFolderPath, migrationName);
75
+ try {
76
+ const migrationModule = await importMigrationModule(migrationPath);
77
+ migrationModules.push({
78
+ name: migrationName,
79
+ service,
80
+ dbName,
81
+ ...migrationModule
82
+ });
83
+
84
+ if (inflateDir) {
85
+ const inflatePath = resolve(inflateDir, service, "migration", migrationName.substring(0, migrationName.length - extname(migrationName).length) + ".js");
86
+ mkdirSync(dirname(inflatePath), {
87
+ recursive: true
88
+ });
89
+ logger?.log("writing [%s]", relative(cwd(), inflatePath));
90
+ writeFileSync(inflatePath, await inflateJSX(resolve(migrationsFolderPath, migrationName), {
91
+ embemedJSX: false,
92
+ minify: false,
93
+ useExport: true,
94
+ logger
95
+ }));
96
+ }
97
+
98
+ } catch (e) {
99
+ errors.push({
100
+ filePath: migrationPath,
101
+ error: e
102
+ });
103
+ logger.error("error with " + migrationPath);
104
+ logger.error(e);
105
+ return false;
106
+ }
107
+ }
108
+
109
+ return migrationModules;
110
+
111
+ }
112
+ return false;
113
+ }
@@ -0,0 +1,43 @@
1
+ import { Logger, Router } from "@miqro/core";
2
+ import { importErrorConfigModule, InflateError, inflateJSX } from "../common/jsx.js";
3
+ import { getErrorConfigPath } from "../common/paths.js";
4
+ import { basename, dirname, join, relative, resolve } from "node:path";
5
+ import { mkdirSync, writeFileSync } from "node:fs";
6
+ import { cwd } from "node:process";
7
+
8
+ export async function setupError(logger: Logger, servicePath: string, service: string, mainRouter: Router, inflateDir: string | undefined | false, inflateSea: boolean, errors: InflateError[]): Promise<void> {
9
+ const errorPath = getErrorConfigPath(servicePath); //resolve(process.cwd(), service, "auth.ts");
10
+
11
+ if (errorPath) {
12
+ try {
13
+ const errorModule = await importErrorConfigModule(errorPath, logger);
14
+ logger.debug("setting up error handling from [%s]", join(service, basename(errorPath)));
15
+ if (errorModule && errorModule.catch) {
16
+ for (const m of errorModule.catch) {
17
+ mainRouter.catch(m);
18
+ }
19
+ }
20
+ if (inflateDir && inflateSea) {
21
+ const inflatePath = resolve(inflateDir, service, "catch.js");
22
+ mkdirSync(dirname(inflatePath), {
23
+ recursive: true
24
+ });
25
+ logger.log("writing [%s]", relative(cwd(), inflatePath));
26
+ writeFileSync(inflatePath, await inflateJSX(errorPath, {
27
+ embemedJSX: false,
28
+ minify: false,
29
+ useExport: true,
30
+ logger
31
+ }));
32
+ }
33
+
34
+ } catch (e) {
35
+ errors.push({
36
+ filePath: errorPath,
37
+ error: e
38
+ });
39
+ logger.error("error with " + errorPath);
40
+ logger.error(e);
41
+ }
42
+ }
43
+ }