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,512 @@
1
+ import { Runtime } from "@miqro/jsx";
2
+ import { createNodeRuntime } from "@miqro/jsx-node";
3
+ import { basename, dirname, relative, resolve } from "node:path";
4
+ import { randomUUID } from "node:crypto";
5
+ import { mkdirSync, readFileSync, rmdirSync, unlinkSync, writeFileSync } from "node:fs";
6
+ import { Request, Response, CORSOptions, Logger, APIRoute } from "@miqro/core";
7
+ import { Parser, Schema } from "@miqro/parser";
8
+ import { APIRouteSchema, SessionHandlerOptionsSchema } from "@miqro/core";
9
+ import { cwd } from "node:process";
10
+
11
+ import { esBuild } from "./esbuild.js";
12
+ import { assertGlobalTampered, browserJSXGlobals } from "../services/globals.js";
13
+ import { APIOptions, ServerConfig, WSConfig, AuthConfig, DBConfig, LogConfig, MiddlewareConfig, ErrorConfig, DocConfig } from "../types.js";
14
+ import { getJSXJSPath, JSX_TMP_DIR } from "./paths.js";
15
+ import { CLEAR_JSX_CACHE } from "./constants.js";
16
+ import { getAsset, initAsset, validateAsset } from "./assets.js";
17
+ import { calculateChecksumFromBuffer } from "./checksum.js";
18
+ import { HandlerWithOptionsSchema, RouteOptionsSchema } from "@miqro/core/build/types.js";
19
+ import { Migration } from "@miqro/query";
20
+
21
+ let jsxJSBuffer: null | Buffer = null; // Buffer.from(getAsset("jsx.dom.js"));
22
+ let jsxJSBufferChecksumPromise: null | Promise<string> = null; // calculateChecksumFromBuffer(jsxJSBuffer);
23
+
24
+ export async function initJSXJS(logger: Logger) {
25
+ jsxJSBuffer = jsxJSBuffer ? jsxJSBuffer : Buffer.from(getAsset("jsx.dom.js"));
26
+ jsxJSBufferChecksumPromise = jsxJSBufferChecksumPromise ? jsxJSBufferChecksumPromise : calculateChecksumFromBuffer(jsxJSBuffer);
27
+ return initAsset(logger, getJSXJSPath(), jsxJSBuffer, true, jsxJSBufferChecksumPromise, false);
28
+ }
29
+
30
+ export async function validateJSXJS(logger: Logger) {
31
+ const checksum = await jsxJSBufferChecksumPromise;
32
+ if (!checksum) {
33
+ throw new Error("error calculating checksum");
34
+ }
35
+ return validateAsset(logger, getJSXJSPath(), checksum);
36
+ }
37
+
38
+ export interface Dict<T> {
39
+ [key: string]: T | Dict<T>;
40
+ }
41
+
42
+ const parser = new Parser();
43
+
44
+ export interface InflateError {
45
+ filePath: string;
46
+ error: Error;
47
+ }
48
+
49
+ export interface InflateOptions {
50
+ embemedJSX: boolean;
51
+ minify: boolean;
52
+ useExport: boolean;
53
+ logger?: Logger | Console;
54
+ }
55
+
56
+ const DEFAULT_ESOPTION = {
57
+ platform: "neutral",
58
+ bundle: true,
59
+ jsxFactory: "JSX.createElement",
60
+ jsxFragment: "JSX.Fragment"
61
+ };
62
+
63
+ export async function inflateJSX(inFile: string, options: InflateOptions): Promise<string> {
64
+ const tmpBuildDir = resolve(JSX_TMP_DIR, String(process.pid), "build", Date.now() + "-" + randomUUID());
65
+ const inFileTmp = resolve(tmpBuildDir, basename(inFile) + ".mjs");
66
+ const jsxJSPath = getJSXJSPath();//resolve(tmpBuildDir, "jsx.js");
67
+ const logger = options.logger; //getLogger(`${SERVER_IDENTIFIER}_JSX`);
68
+
69
+ try {
70
+
71
+ if (!options.embemedJSX) {
72
+ mkdirSync(tmpBuildDir, {
73
+ recursive: true
74
+ });
75
+ writeFileSync(inFileTmp, browserJSXGlobals(inFile, false, options.useExport));
76
+ //writeFileSync(inFileTmp, browserJSXGlobals(relative(tmpBuildDir, inFile), false));
77
+ logger?.trace("inflating [%s] from [%s]. to change the import folder set JSX_TMP", relative(cwd(), inFile), dirname(relative(JSX_TMP_DIR, inFileTmp)));
78
+ const { outputFiles: [{ contents }] } = await esBuild({
79
+ ...DEFAULT_ESOPTION,
80
+ entryPoints: [inFileTmp],
81
+ minify: options.minify
82
+ });
83
+ if (CLEAR_JSX_CACHE) {
84
+ logger?.trace("clearing cache at [%s] to change this behaivor set CLEAR_JSX_CACHE to 0", tmpBuildDir);
85
+ unlinkSync(inFileTmp);
86
+ rmdirSync(tmpBuildDir);
87
+ }
88
+ return contents;
89
+ } else {
90
+ mkdirSync(tmpBuildDir, {
91
+ recursive: true
92
+ });
93
+ //writeFileSync(jsxJSPath, Buffer.from(getAsset("jsx-dom-bundle")));
94
+ writeFileSync(inFileTmp, browserJSXGlobals(inFile, jsxJSPath));
95
+ //writeFileSync(inFileTmp, browserJSXGlobals(relative(tmpBuildDir, inFile), relative(tmpBuildDir, jsxJSPath)));
96
+ logger?.trace("inflating [%s] from [%s] with jsx.js embedded. to change the import folder set JSX_TMP", relative(cwd(), inFile), dirname(relative(JSX_TMP_DIR, inFileTmp)));
97
+ const { outputFiles: [{ contents }] } = await esBuild({
98
+ ...DEFAULT_ESOPTION,
99
+ entryPoints: [inFileTmp],
100
+ minify: options.minify
101
+ });
102
+ if (CLEAR_JSX_CACHE) {
103
+ logger?.trace("clearing cache at [%s] to change this behaivor set CLEAR_JSX_CACHE to 0", tmpBuildDir);
104
+ unlinkSync(inFileTmp);
105
+ //unlinkSync(jsxJSPath);
106
+ rmdirSync(tmpBuildDir);
107
+ }
108
+ return contents;
109
+ }
110
+ } catch (e) {
111
+ logger?.error("error with: " + inFile);
112
+ logger?.error(e);
113
+ if (options.embemedJSX) {
114
+ if (CLEAR_JSX_CACHE) {
115
+ logger?.trace("clearing cache at [%s] to change this behaivor set CLEAR_JSX_CACHE to 0", tmpBuildDir);
116
+ unlinkSync(inFileTmp);
117
+ //unlinkSync(jsxJSPath);
118
+ rmdirSync(tmpBuildDir);
119
+ } else {
120
+ //console.error("errors on: " + tmpBuildDir);
121
+ logger?.error("error with: %s", inFileTmp);
122
+ logger?.trace("NOT clearing cache. to change this behaivor set CLEAR_JSX_CACHE to 1", tmpBuildDir);
123
+ }
124
+ }
125
+ throw e;
126
+ }
127
+ }
128
+
129
+ export const APIOptionsSchema: Schema<APIOptions> = {
130
+ type: "object?",
131
+ properties: {
132
+ basePath: "string?",
133
+ path: "string?|string[]?",
134
+ method: "string?|string[]?",
135
+ parser: {
136
+ type: "object?",
137
+ properties: {
138
+ parse: "function"
139
+ },
140
+ mode: "add_extra"
141
+ },
142
+ middleware: "function[]!?",
143
+ session: {
144
+ type: "object?|function?",
145
+ properties: SessionHandlerOptionsSchema.properties
146
+ },
147
+ ...RouteOptionsSchema.properties
148
+ },
149
+ //mode:"add_extra"
150
+ };
151
+
152
+ export interface HTMLModule {
153
+ default: JSX.Element | ((req: Request | null, res: Response | null) => (Promise<JSX.Element> | JSX.Element));
154
+ apiOptions?: APIOptions;
155
+ }
156
+
157
+ export const HTMLModuleSchema: Schema<HTMLModule> = {
158
+ type: "object",
159
+ properties: {
160
+ default: {
161
+ type: "function|object"
162
+ },
163
+ apiOptions: APIOptionsSchema
164
+ },
165
+ //mode:"add_extra"
166
+ }
167
+
168
+ export type JSONModuleValue = string | object;
169
+
170
+ export interface JSONModule {
171
+ default: JSONModuleValue | ((req: Request | null, res: Response | null) => (Promise<JSONModuleValue> | JSONModuleValue));
172
+ apiOptions?: APIOptions;
173
+ }
174
+
175
+ export const JSONModuleSchema: Schema<JSONModule> = {
176
+ type: "object",
177
+ properties: {
178
+ default: {
179
+ type: "function|object|string"
180
+ },
181
+ apiOptions: APIOptionsSchema
182
+ },
183
+ //mode:"add_extra"
184
+ }
185
+
186
+ export const MigrationSchema: Schema<Migration> = {
187
+ type: "object",
188
+ properties: {
189
+ up: "function",
190
+ down: "function"
191
+ },
192
+ mode: "add_extra"
193
+ }
194
+
195
+ export const CORSOptionsSchema: Schema<CORSOptions> = {
196
+ type: "object",
197
+ properties: {
198
+ origins: "string[]?|string?",
199
+ validate: "function?",
200
+ methods: "string?",
201
+ preflightContinue: "boolean?"
202
+ },
203
+ //mode:"add_extra"
204
+ }
205
+
206
+ export const WSConfigSchema: Schema<WSConfig> = {
207
+ type: "object",
208
+ properties: {
209
+ path: "string",
210
+ disabled: "boolean?",
211
+ maxConnections: "number?",
212
+ validate: "function?",
213
+ onConnection: "function?",
214
+ onMessage: "function?",
215
+ onDisconnect: "function?"
216
+ },
217
+ //mode:"add_extra"
218
+ };
219
+
220
+ export const DBConfigSchema: Schema<DBConfig> = {
221
+ type: "object",
222
+ properties: {
223
+ url: "string?",
224
+ disabled: "boolean?",
225
+ storage: "string?",
226
+ dialect: "string?",
227
+ name: "string"
228
+ },
229
+ //mode:"add_extra"
230
+ }
231
+
232
+ export const ServerConfigSchema: Schema<ServerConfig> = {
233
+ type: "object",
234
+ properties: {
235
+ preload: "function?",
236
+ unload: "function?",
237
+ stop: "function?",
238
+ start: "function?",
239
+ load: "function?"
240
+ },
241
+ //mode:"add_extra"
242
+ };
243
+
244
+ export const LogConfigSchema: Schema<LogConfig> = {
245
+ type: "object",
246
+ properties: {
247
+ level: {
248
+ type: "enum?",
249
+ enumValues: ["error", "warn", "info", "debug", "trace", "none"]
250
+ },
251
+ replaceConsoleTransport: "boolean?",
252
+ replaceFileTransport: "boolean?",
253
+ write: "function"
254
+ },
255
+ //mode:"add_extra"
256
+ };
257
+
258
+ export const AuthConfigSchema: Schema<AuthConfig> = {
259
+ type: "object",
260
+ properties: {
261
+ ...SessionHandlerOptionsSchema.properties
262
+ },
263
+ //mode:"add_extra"
264
+ }
265
+
266
+ export const MiddlewareConfigSchema: Schema<MiddlewareConfig> = {
267
+ type: "object",
268
+ properties: {
269
+ middleware: {
270
+ type: "Array",
271
+ arrayType: "function|object",
272
+ required: false,
273
+ properties: {
274
+ ...HandlerWithOptionsSchema.properties
275
+ }
276
+ },
277
+ post: {
278
+ type: "Array",
279
+ arrayType: "function|object",
280
+ required: false,
281
+ properties: {
282
+ ...HandlerWithOptionsSchema.properties
283
+ }
284
+ }
285
+ },
286
+ }
287
+
288
+ export const ErrorConfigSchema: Schema<ErrorConfig> = {
289
+ type: "object",
290
+ properties: {
291
+ catch: {
292
+ type: "Array",
293
+ required: false,
294
+ arrayType: "function"
295
+ }
296
+ },
297
+ }
298
+
299
+ export const DocConfigSchema: Schema<DocConfig> = {
300
+ type: "object",
301
+ properties: {
302
+ publish: {
303
+ type: "dict?",
304
+ dictType: "object",
305
+ properties: {
306
+ type: {
307
+ type: "enum?",
308
+ enumValues: ["HTML", "MD", "JSON"]
309
+ },
310
+ all: "boolean?"
311
+ }
312
+ }
313
+ }
314
+ }
315
+
316
+ export async function importAPIRoute(inFile: string, logger?: Logger) {
317
+ const mod = (await importJSXFile(inFile, logger)).default;
318
+ const module = typeof mod === "function" ? { handler: mod } : parser.parse(mod, APIRouteSchema, basename(inFile));
319
+ if (module !== undefined) {
320
+ return module as APIRoute;
321
+ } else {
322
+ throw new Error(`error with module [${inFile}] undefined`);
323
+ }
324
+ }
325
+
326
+ export async function importMigrationModule(inFile: string, logger?: Logger) {
327
+ const module = parser.parse((await importJSXFile(inFile, logger)).default, MigrationSchema, basename(inFile));
328
+ if (module !== undefined) {
329
+ return module;
330
+ } else {
331
+ throw new Error(`error with module [${inFile}] undefined`);
332
+ }
333
+ }
334
+
335
+ export async function importHTMLModule(inFile: string, logger?: Logger) {
336
+ const module = (await importJSXFile(inFile, logger));
337
+ parser.parse(module.default, HTMLModuleSchema.properties.default, `${basename(inFile)}.default`);
338
+ parser.parse(module.apiOptions, APIOptionsSchema, `${basename(inFile)}.apiOptions`);
339
+ if (module !== undefined) {
340
+ return module;
341
+ } else {
342
+ throw new Error(`error with module [${inFile}] undefined`);
343
+ }
344
+ }
345
+
346
+ export async function importJSONModule(inFile: string, logger?: Logger) {
347
+ const module = (await importJSXFile(inFile, logger));
348
+ parser.parse(module.default, JSONModuleSchema.properties.default, `${basename(inFile)}.default`);
349
+ parser.parse(module.apiOptions, APIOptionsSchema, `${basename(inFile)}.apiOptions`);
350
+ if (module !== undefined) {
351
+ return module;
352
+ } else {
353
+ throw new Error(`error with module [${inFile}] undefined`);
354
+ }
355
+ }
356
+
357
+ export async function importAuthModule(inFile: string, logger?: Logger) {
358
+ const module = parser.parse((await importJSXFile(inFile, logger)).default, AuthConfigSchema, basename(inFile));
359
+ if (module !== undefined) {
360
+ return module;
361
+ } else {
362
+ throw new Error(`error with module [${inFile}] undefined`);
363
+ }
364
+ }
365
+
366
+ export async function importMiddlewareConfigModule(inFile: string, logger?: Logger) {
367
+ const module = parser.parse((await importJSXFile(inFile, logger)).default, MiddlewareConfigSchema, basename(inFile));
368
+ if (module !== undefined) {
369
+ return module;
370
+ } else {
371
+ throw new Error(`error with module [${inFile}] undefined`);
372
+ }
373
+ }
374
+
375
+ export async function importErrorConfigModule(inFile: string, logger?: Logger) {
376
+ const module = parser.parse((await importJSXFile(inFile, logger)).default, ErrorConfigSchema, basename(inFile));
377
+ if (module !== undefined) {
378
+ return module;
379
+ } else {
380
+ throw new Error(`error with module [${inFile}] undefined`);
381
+ }
382
+ }
383
+
384
+ export async function importDocConfigModule(inFile: string, logger?: Logger) {
385
+ const module = parser.parse((await importJSXFile(inFile, logger)).default, DocConfigSchema, basename(inFile));
386
+ if (module !== undefined) {
387
+ return module;
388
+ } else {
389
+ throw new Error(`error with module [${inFile}] undefined`);
390
+ }
391
+ }
392
+
393
+ export async function importConfigConfigModule(inFile: string, logger?: Logger) {
394
+ const module = parser.parse((await importJSXFile(inFile, logger)).default, MiddlewareConfigSchema, basename(inFile));
395
+ if (module !== undefined) {
396
+ return module;
397
+ } else {
398
+ throw new Error(`error with module [${inFile}] undefined`);
399
+ }
400
+ }
401
+
402
+ export async function importCORSModule(inFile: string, logger?: Logger) {
403
+ const module = parser.parse((await importJSXFile(inFile, logger)).default, CORSOptionsSchema, basename(inFile));
404
+ if (module !== undefined) {
405
+ return module;
406
+ } else {
407
+ throw new Error(`error with module [${inFile}] undefined`);
408
+ }
409
+ }
410
+
411
+ export async function importWSConfigModule(inFile: string, logger?: Logger) {
412
+ const module = parser.parse((await importJSXFile(inFile, logger)).default, WSConfigSchema, basename(inFile));
413
+ if (module !== undefined) {
414
+ return module;
415
+ } else {
416
+ throw new Error(`error with module [${inFile}] undefined`);
417
+ }
418
+ }
419
+
420
+ export async function importDBConfigModule(inFile: string, logger?: Logger) {
421
+ const module = parser.parse((await importJSXFile(inFile, logger)).default, DBConfigSchema, basename(inFile));
422
+ if (module !== undefined) {
423
+ return module;
424
+ } else {
425
+ throw new Error(`error with module [${inFile}] undefined`);
426
+ }
427
+ }
428
+
429
+ export async function importLogConfigModule(inFile: string, logger?: Logger) {
430
+ const module = parser.parse((await importJSXFile(inFile, logger)).default, LogConfigSchema, basename(inFile));
431
+ if (module !== undefined) {
432
+ return module;
433
+ } else {
434
+ throw new Error(`error with module [${inFile}] undefined`);
435
+ }
436
+ }
437
+
438
+ export async function importServerConfigModule(inFile: string, logger?: Logger) {
439
+ const module = parser.parse((await importJSXFile(inFile, logger)).default, ServerConfigSchema, basename(inFile));
440
+ if (module !== undefined) {
441
+ return module;
442
+ } else {
443
+ throw new Error(`error with module [${inFile}] undefined`);
444
+ }
445
+ }
446
+
447
+ export async function importJSXFile(inFile: string, logger?: Logger | Console): Promise<any> {
448
+ const inflatedCode = await inflateJSX(inFile, {
449
+ embemedJSX: false,
450
+ minify: false,
451
+ useExport: true,
452
+ logger
453
+ });
454
+ const tmpBuildDir = resolve(JSX_TMP_DIR, String(process.pid), "import", Date.now() + "-" + randomUUID());
455
+ const inFileTmp = resolve(tmpBuildDir, basename(inFile) + ".mjs");
456
+ //const logger = getLogger(`${SERVER_IDENTIFIER}_JSX`);
457
+ mkdirSync(tmpBuildDir, {
458
+ recursive: true
459
+ });
460
+ try {
461
+ writeFileSync(inFileTmp, inflatedCode);
462
+ assertGlobalTampered();
463
+ logger?.trace("importing [%s] from [%s]. to change the import folder set JSX_TMP", relative(cwd(), inFile), dirname(relative(JSX_TMP_DIR, inFileTmp)));
464
+ logger?.debug("importing [%s]", relative(cwd(), inFile));
465
+ const module = await import(inFileTmp);
466
+ assertGlobalTampered();
467
+ if (CLEAR_JSX_CACHE) {
468
+ logger?.trace("clearing cache at [%s]. to change this behaivor set CLEAR_JSX_CACHE to 0", tmpBuildDir);
469
+ unlinkSync(inFileTmp);
470
+ rmdirSync(tmpBuildDir);
471
+ }
472
+ return module;
473
+ } catch (e) {
474
+ logger?.error(e);
475
+ logger?.error("error with: " + inFile);
476
+ if (CLEAR_JSX_CACHE) {
477
+ logger?.trace("clearing cache at [%s] to change this behaivor set CLEAR_JSX_CACHE to 0", tmpBuildDir);
478
+ unlinkSync(inFileTmp);
479
+ rmdirSync(tmpBuildDir);
480
+ } else {
481
+ //console.error("errors on: " + tmpBuildDir);
482
+ logger?.error("error with: %s", inFileTmp);
483
+ logger?.trace("NOT clearing cache. to change this behaivor set CLEAR_JSX_CACHE to 1", tmpBuildDir);
484
+ }
485
+ throw e;
486
+ }
487
+ }
488
+
489
+ export function jsx2HTML(out: JSX.Element, runtime?: Runtime): string {
490
+ runtime = runtime ? runtime : createNodeRuntime();
491
+ const element = runtime.createElement("root");
492
+ const container = runtime.createContainer(element, {
493
+ shadowInit: false, runtimeOptions: {
494
+ disableEffects: true,
495
+ disableEvents: true,
496
+ disableRefListener: true,
497
+ disableRefresh: true
498
+ }
499
+ });
500
+ container.render(out);
501
+ //console.log("jsx2HTML [%s]", element);
502
+ let HTML = "";
503
+ for (let i = 0; i < element.childNodes.length; i++) {
504
+ const child = element.childNodes[i];
505
+ //console.log("jsx2HTML child [%s]", child);
506
+ //console.log("jsx2HTML child [%s]", (child as any).toString(" ", -1));
507
+ HTML += (child as any).toString(" ", -1);
508
+ }
509
+ container.disconnect();
510
+ //console.log("jsx2HTML [%s]", HTML);
511
+ return HTML;
512
+ }
@@ -0,0 +1,158 @@
1
+ import { checkEnvVariable } from "@miqro/core";
2
+ import { randomUUID } from "node:crypto";
3
+ import { existsSync, statSync } from "node:fs";
4
+ import { tmpdir } from "node:os";
5
+ import { resolve } from "node:path";
6
+ import { cwd, platform } from "node:process";
7
+
8
+ export const JSX_TMP_DIR = checkEnvVariable(`JSX_TMP`, resolve(tmpdir(), "jsx_tmp"));
9
+
10
+ export const TEST_SOCKET = resolve(JSX_TMP_DIR, `test.${randomUUID()}.sock`);
11
+
12
+ export function getServicePath(service: string) {
13
+ return resolve(cwd(), service);
14
+ }
15
+
16
+ export function getLogConfigPath(servicePath: string) {
17
+ const logConfigPath = resolve(servicePath, "log.ts");
18
+ const logConfigPathJS = resolve(servicePath, "log.js");
19
+ if (existsSync(logConfigPath) && !statSync(logConfigPath).isDirectory()) {
20
+ return logConfigPath;
21
+ } else if (existsSync(logConfigPathJS) && !statSync(logConfigPathJS).isDirectory()) {
22
+ return logConfigPathJS;
23
+ }
24
+ return false;
25
+ }
26
+
27
+ export function getCORSConfigPath(servicePath: string) {
28
+ const corsPath = resolve(servicePath, "cors.ts");
29
+ const corsPathJS = resolve(servicePath, "cors.js");
30
+ if (existsSync(corsPath) && !statSync(corsPath).isDirectory()) {
31
+ return corsPath;
32
+ } else if (existsSync(corsPathJS) && !statSync(corsPathJS).isDirectory()) {
33
+ return corsPathJS;
34
+ }
35
+ return false;
36
+ }
37
+
38
+ export function getServerConfigPath(servicePath: string) {
39
+ const serverPath = resolve(servicePath, "server.ts");
40
+ const serverPathJS = resolve(servicePath, "server.js");
41
+ if (existsSync(serverPath)) {
42
+ return serverPath;
43
+ } else if (existsSync(serverPathJS)) {
44
+ return serverPathJS;
45
+ }
46
+ return false;
47
+ }
48
+
49
+ export function getWSConfigPath(servicePath: string) {
50
+ const wsPath = resolve(servicePath, "ws.ts");
51
+ const wsPathJS = resolve(servicePath, "ws.js");
52
+ if (existsSync(wsPath)) {
53
+ return wsPath;
54
+ } else if (existsSync(wsPathJS)) {
55
+ return wsPathJS;
56
+ }
57
+ return false;
58
+ }
59
+
60
+ export function getDBConfigPath(servicePath: string) {
61
+ const dbPath = resolve(servicePath, "db.ts");
62
+ const dbPathJS = resolve(servicePath, "db.js");
63
+ if (existsSync(dbPath)) {
64
+ return dbPath;
65
+ } else if (existsSync(dbPathJS)) {
66
+ return dbPathJS;
67
+ }
68
+ return false;
69
+ }
70
+
71
+ export function getStaticFilesPath(servicePath: string) {
72
+ const staticFilesPath = resolve(servicePath, "static");
73
+ if (existsSync(staticFilesPath) && statSync(staticFilesPath).isDirectory()) {
74
+ return staticFilesPath;
75
+ }
76
+ return false;
77
+ }
78
+
79
+ export function getMiddlewareConfigPath(servicePath: string) {
80
+ const middlewarePath = resolve(servicePath, "middleware.ts");
81
+ const middlewarePathJS = resolve(servicePath, "middleware.js");
82
+ if (existsSync(middlewarePath) && !statSync(middlewarePath).isDirectory()) {
83
+ return middlewarePath;
84
+ } else if (existsSync(middlewarePathJS) && !statSync(middlewarePathJS).isDirectory()) {
85
+ return middlewarePathJS;
86
+ }
87
+ return false;
88
+ }
89
+
90
+ export function getErrorConfigPath(servicePath: string) {
91
+ const errorPath = resolve(servicePath, "catch.ts");
92
+ const errorPathJS = resolve(servicePath, "catch.js");
93
+ if (existsSync(errorPath) && !statSync(errorPath).isDirectory()) {
94
+ return errorPath;
95
+ } else if (existsSync(errorPathJS) && !statSync(errorPathJS).isDirectory()) {
96
+ return errorPathJS;
97
+ }
98
+ return false;
99
+ }
100
+
101
+ export function getDocConfigPath(servicePath: string) {
102
+ const docPath = resolve(servicePath, "doc.ts");
103
+ const docPathJS = resolve(servicePath, "doc.js");
104
+ if (existsSync(docPath) && !statSync(docPath).isDirectory()) {
105
+ return docPath;
106
+ } else if (existsSync(docPathJS) && !statSync(docPathJS).isDirectory()) {
107
+ return docPathJS;
108
+ }
109
+ return false;
110
+ }
111
+
112
+ export function getMiqroJSONPath() {
113
+ const miqroRCPath = resolve(cwd(), "miqro.json");
114
+ if (existsSync(miqroRCPath) && !statSync(miqroRCPath).isDirectory()) {
115
+ return miqroRCPath;
116
+ }
117
+ return false;
118
+ }
119
+
120
+ export function getAuthConfigPath(servicePath: string) {
121
+ const authPath = resolve(servicePath, "auth.ts");
122
+ const authPathJS = resolve(servicePath, "auth.js");
123
+ if (existsSync(authPath) && !statSync(authPath).isDirectory()) {
124
+ return authPath;
125
+ } else if (existsSync(authPathJS) && !statSync(authPathJS).isDirectory()) {
126
+ return authPathJS;
127
+ }
128
+ return false;
129
+ }
130
+
131
+ export function getHTTPRouterPath(servicePath: string) {
132
+ const apiRouterPath = resolve(servicePath, "http");
133
+ if (existsSync(apiRouterPath) && statSync(apiRouterPath).isDirectory()) {
134
+ return apiRouterPath;
135
+ }
136
+ return false;
137
+ }
138
+
139
+ export function getMigrationsPath(servicePath: string) {
140
+ const migrationsFolderPath = resolve(servicePath, "migration");
141
+ if (existsSync(migrationsFolderPath) && statSync(migrationsFolderPath).isDirectory()) {
142
+ return migrationsFolderPath;
143
+ }
144
+ return false;
145
+ }
146
+
147
+ export function getESBuildBinaryPath() {
148
+ if (platform === "win32") {
149
+ return resolve(JSX_TMP_DIR, String(process.pid), "esbuild.exe");
150
+ } else {
151
+ return resolve(JSX_TMP_DIR, String(process.pid), "esbuild");
152
+ }
153
+ }
154
+
155
+ export function getJSXJSPath() {
156
+ return resolve(JSX_TMP_DIR, String(process.pid), "jsx.js");
157
+ }
158
+