miqro 6.2.13 → 7.0.1

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 (217) hide show
  1. package/README.md +9 -17
  2. package/build/editor.bundle.js +11601 -10129
  3. package/build/esm/src/bin/types.js +10 -5
  4. package/build/esm/src/cluster.js +1 -1
  5. package/build/esm/src/common/arguments.d.ts +2 -1
  6. package/build/esm/src/common/arguments.js +42 -18
  7. package/build/esm/src/common/assets.d.ts +1 -1
  8. package/build/esm/src/common/assets.js +34 -13
  9. package/build/esm/src/common/esbuild.d.ts +4 -1
  10. package/build/esm/src/common/esbuild.js +31 -26
  11. package/build/esm/src/common/exit.js +4 -4
  12. package/build/esm/src/common/help.d.ts +1 -1
  13. package/build/esm/src/common/help.js +2 -2
  14. package/build/esm/src/common/jsx.d.ts +2 -3
  15. package/build/esm/src/common/jsx.js +70 -75
  16. package/build/esm/src/common/paths.d.ts +0 -1
  17. package/build/esm/src/common/paths.js +3 -3
  18. package/build/esm/src/common/watch.d.ts +0 -1
  19. package/build/esm/src/common/watch.js +4 -1
  20. package/build/esm/src/inflate/inflate-sea.js +35 -24
  21. package/build/esm/src/inflate/inflate.js +4 -4
  22. package/build/esm/src/inflate/setup-auth.js +1 -1
  23. package/build/esm/src/inflate/setup-cors.js +1 -1
  24. package/build/esm/src/inflate/setup-db.js +2 -2
  25. package/build/esm/src/inflate/setup-error.js +1 -1
  26. package/build/esm/src/inflate/setup-http.js +16 -16
  27. package/build/esm/src/inflate/setup-log.js +1 -1
  28. package/build/esm/src/inflate/setup-middleware.js +1 -1
  29. package/build/esm/src/inflate/setup-server-config.js +1 -1
  30. package/build/esm/src/inflate/setup-ws.js +1 -1
  31. package/build/esm/src/inflate/setup.doc.d.ts +2 -2
  32. package/build/esm/src/inflate/setup.doc.js +23 -2
  33. package/build/esm/src/inflate/utils/sea-utils.js +2 -2
  34. package/build/esm/src/lib.d.ts +10 -1
  35. package/build/esm/src/lib.js +16 -1
  36. package/build/esm/src/main.js +2 -1
  37. package/build/esm/src/services/app.d.ts +1 -0
  38. package/build/esm/src/services/app.js +4 -5
  39. package/build/esm/src/services/migrations.js +2 -2
  40. package/build/esm/src/services/utils/jwt.d.ts +2 -0
  41. package/build/esm/src/services/utils/jwt.js +23 -0
  42. package/build/esm/src/services/utils/middleware.d.ts +9 -0
  43. package/build/esm/src/services/utils/middleware.js +9 -0
  44. package/build/esm/src/services/utils/server-interface.js +33 -4
  45. package/build/esm/src/types.d.ts +50 -83
  46. package/build/lib.cjs +15694 -15619
  47. package/package.json +6 -8
  48. package/sea/basic-compile.base64.sh +1 -1
  49. package/sea/basic-compile.sh +1 -1
  50. package/sea/install-nodejs.sh +1 -1
  51. package/sea/node.version.tag +1 -1
  52. package/.eslintrc +0 -15
  53. package/build/esm/editor/auth.d.ts +0 -6
  54. package/build/esm/editor/auth.js +0 -41
  55. package/build/esm/editor/common/admin-interface.d.ts +0 -36
  56. package/build/esm/editor/common/admin-interface.js +0 -44
  57. package/build/esm/editor/common/constants.d.ts +0 -4
  58. package/build/esm/editor/common/constants.js +0 -20
  59. package/build/esm/editor/common/constants.server.d.ts +0 -2
  60. package/build/esm/editor/common/constants.server.js +0 -4
  61. package/build/esm/editor/common/editor-index.d.ts +0 -2
  62. package/build/esm/editor/common/editor-index.js +0 -14
  63. package/build/esm/editor/common/html-encode.d.ts +0 -1
  64. package/build/esm/editor/common/html-encode.js +0 -14
  65. package/build/esm/editor/common/log-socket.d.ts +0 -15
  66. package/build/esm/editor/common/log-socket.js +0 -70
  67. package/build/esm/editor/common/templates.d.ts +0 -11
  68. package/build/esm/editor/common/templates.js +0 -477
  69. package/build/esm/editor/components/api-preview.d.ts +0 -11
  70. package/build/esm/editor/components/api-preview.js +0 -90
  71. package/build/esm/editor/components/editor.d.ts +0 -16
  72. package/build/esm/editor/components/editor.js +0 -365
  73. package/build/esm/editor/components/file-browser.d.ts +0 -37
  74. package/build/esm/editor/components/file-browser.js +0 -126
  75. package/build/esm/editor/components/file-editor-toolbar.d.ts +0 -22
  76. package/build/esm/editor/components/file-editor-toolbar.js +0 -93
  77. package/build/esm/editor/components/file-editor.d.ts +0 -32
  78. package/build/esm/editor/components/file-editor.js +0 -59
  79. package/build/esm/editor/components/filter-query.d.ts +0 -1
  80. package/build/esm/editor/components/filter-query.js +0 -22
  81. package/build/esm/editor/components/highlight-text-area.d.ts +0 -11
  82. package/build/esm/editor/components/highlight-text-area.js +0 -125
  83. package/build/esm/editor/components/log-viewer.d.ts +0 -6
  84. package/build/esm/editor/components/log-viewer.js +0 -69
  85. package/build/esm/editor/components/new-file.d.ts +0 -10
  86. package/build/esm/editor/components/new-file.js +0 -117
  87. package/build/esm/editor/components/scroll-query.d.ts +0 -7
  88. package/build/esm/editor/components/scroll-query.js +0 -21
  89. package/build/esm/editor/components/start-page.d.ts +0 -13
  90. package/build/esm/editor/components/start-page.js +0 -30
  91. package/build/esm/editor/http/admin/editor/api/fs/delete.api.d.ts +0 -3
  92. package/build/esm/editor/http/admin/editor/api/fs/delete.api.js +0 -29
  93. package/build/esm/editor/http/admin/editor/api/fs/read.api.d.ts +0 -5
  94. package/build/esm/editor/http/admin/editor/api/fs/read.api.js +0 -49
  95. package/build/esm/editor/http/admin/editor/api/fs/rename.api.d.ts +0 -4
  96. package/build/esm/editor/http/admin/editor/api/fs/rename.api.js +0 -39
  97. package/build/esm/editor/http/admin/editor/api/fs/scan.api.d.ts +0 -26
  98. package/build/esm/editor/http/admin/editor/api/fs/scan.api.js +0 -149
  99. package/build/esm/editor/http/admin/editor/api/fs/write.api.d.ts +0 -3
  100. package/build/esm/editor/http/admin/editor/api/fs/write.api.js +0 -38
  101. package/build/esm/editor/http/admin/editor/api/server/reload.api.d.ts +0 -10
  102. package/build/esm/editor/http/admin/editor/api/server/reload.api.js +0 -46
  103. package/build/esm/editor/http/admin/editor/api/server/restart.api.d.ts +0 -10
  104. package/build/esm/editor/http/admin/editor/api/server/restart.api.js +0 -45
  105. package/build/esm/editor/http/admin/editor/editor.d.ts +0 -1
  106. package/build/esm/editor/http/admin/editor/editor.js +0 -7
  107. package/build/esm/editor/http/admin/editor/index.api.d.ts +0 -3
  108. package/build/esm/editor/http/admin/editor/index.api.js +0 -21
  109. package/build/esm/editor/server.d.ts +0 -3
  110. package/build/esm/editor/server.js +0 -49
  111. package/build/esm/editor/ws.d.ts +0 -3
  112. package/build/esm/editor/ws.js +0 -11
  113. package/build/esm/src/services/globals.d.ts +0 -3
  114. package/build/esm/src/services/globals.js +0 -182
  115. package/build/jsx.dom.js +0 -1587
  116. package/build/postject.base64.cjs +0 -1
  117. package/editor/auth.ts +0 -51
  118. package/editor/common/admin-interface.ts +0 -84
  119. package/editor/common/constants.server.ts +0 -5
  120. package/editor/common/constants.ts +0 -21
  121. package/editor/common/editor-index.tsx +0 -17
  122. package/editor/common/html-encode.ts +0 -14
  123. package/editor/common/log-socket.tsx +0 -85
  124. package/editor/common/templates.ts +0 -481
  125. package/editor/components/api-preview.tsx +0 -116
  126. package/editor/components/editor.tsx +0 -494
  127. package/editor/components/file-browser.tsx +0 -308
  128. package/editor/components/file-editor-toolbar.tsx +0 -191
  129. package/editor/components/file-editor.tsx +0 -122
  130. package/editor/components/filter-query.tsx +0 -22
  131. package/editor/components/highlight-text-area.tsx +0 -145
  132. package/editor/components/log-viewer.tsx +0 -110
  133. package/editor/components/new-file.tsx +0 -169
  134. package/editor/components/scroll-query.tsx +0 -22
  135. package/editor/components/start-page.tsx +0 -49
  136. package/editor/http/admin/editor/api/fs/delete.api.tsx +0 -32
  137. package/editor/http/admin/editor/api/fs/read.api.tsx +0 -55
  138. package/editor/http/admin/editor/api/fs/rename.api.tsx +0 -41
  139. package/editor/http/admin/editor/api/fs/scan.api.tsx +0 -181
  140. package/editor/http/admin/editor/api/fs/write.api.tsx +0 -41
  141. package/editor/http/admin/editor/api/server/reload.api.ts +0 -53
  142. package/editor/http/admin/editor/api/server/restart.api.tsx +0 -52
  143. package/editor/http/admin/editor/editor.tsx +0 -8
  144. package/editor/http/admin/editor/index.api.tsx +0 -39
  145. package/editor/server.ts +0 -57
  146. package/editor/ws.ts +0 -15
  147. package/sea/types.json +0 -1
  148. package/src/bin/compile.ts +0 -35
  149. package/src/bin/doc-md.ts +0 -210
  150. package/src/bin/generate-doc.ts +0 -64
  151. package/src/bin/test.ts +0 -92
  152. package/src/bin/types.ts +0 -29
  153. package/src/cluster.ts +0 -27
  154. package/src/common/arguments.ts +0 -733
  155. package/src/common/assets.ts +0 -128
  156. package/src/common/checksum.ts +0 -58
  157. package/src/common/constants.ts +0 -18
  158. package/src/common/content-type.ts +0 -84
  159. package/src/common/esbuild.ts +0 -94
  160. package/src/common/exit.ts +0 -91
  161. package/src/common/fs.ts +0 -82
  162. package/src/common/help.ts +0 -60
  163. package/src/common/jsx.ts +0 -547
  164. package/src/common/jwt.ts +0 -85
  165. package/src/common/paths.ts +0 -107
  166. package/src/common/watch.ts +0 -85
  167. package/src/inflate/inflate-sea.ts +0 -226
  168. package/src/inflate/inflate.ts +0 -101
  169. package/src/inflate/md.ts +0 -25
  170. package/src/inflate/setup-auth.ts +0 -41
  171. package/src/inflate/setup-cors.ts +0 -41
  172. package/src/inflate/setup-db.ts +0 -117
  173. package/src/inflate/setup-error.ts +0 -44
  174. package/src/inflate/setup-http.ts +0 -704
  175. package/src/inflate/setup-log.ts +0 -45
  176. package/src/inflate/setup-middleware.ts +0 -47
  177. package/src/inflate/setup-server-config.ts +0 -48
  178. package/src/inflate/setup-test.ts +0 -23
  179. package/src/inflate/setup-ws.ts +0 -50
  180. package/src/inflate/setup.doc.ts +0 -68
  181. package/src/inflate/utils/sea-utils.ts +0 -14
  182. package/src/lib.ts +0 -19
  183. package/src/main.ts +0 -100
  184. package/src/services/app.ts +0 -698
  185. package/src/services/editor.tsx +0 -101
  186. package/src/services/globals.ts +0 -186
  187. package/src/services/hot-reload.ts +0 -51
  188. package/src/services/migrations.ts +0 -68
  189. package/src/services/utils/admin-interface.ts +0 -37
  190. package/src/services/utils/cache.ts +0 -88
  191. package/src/services/utils/cluster-cache.ts +0 -230
  192. package/src/services/utils/cluster-ws.ts +0 -202
  193. package/src/services/utils/db-manager.ts +0 -92
  194. package/src/services/utils/get-route.ts +0 -70
  195. package/src/services/utils/log-transport.ts +0 -81
  196. package/src/services/utils/log.ts +0 -92
  197. package/src/services/utils/server-interface.ts +0 -92
  198. package/src/services/utils/websocketmanager.ts +0 -157
  199. package/src/types/@esbuild.d.ts +0 -1
  200. package/src/types/@miqro/core.d.ts +0 -2
  201. package/src/types/@miqro/jsx.d.ts +0 -2
  202. package/src/types/@miqro/parser.d.ts +0 -2
  203. package/src/types/@miqro/query.d.ts +0 -2
  204. package/src/types/@miqro/request.d.ts +0 -2
  205. package/src/types/@miqro/test.d.ts +0 -2
  206. package/src/types/@miqro.d.ts +0 -1
  207. package/src/types/@types.d.ts +0 -1
  208. package/src/types/browser.globals.d.ts +0 -1
  209. package/src/types/cookie.d.ts +0 -2
  210. package/src/types/globals.d.ts +0 -2
  211. package/src/types/jose.d.ts +0 -2
  212. package/src/types/jsx.globals.d.ts +0 -38
  213. package/src/types/miqro.d.ts +0 -228
  214. package/src/types/postject.d.ts +0 -1
  215. package/src/types/server.globals.d.ts +0 -47
  216. package/src/types.ts +0 -304
  217. package/tsconfig.json +0 -35
@@ -1,704 +0,0 @@
1
- import { Router, newURL, Response, Request, RouterHandlerOptions, Logger, APIRoute, normalizePath, HandlerWithOptions, Handler } from "@miqro/core";
2
- import { existsSync, mkdir, readFile, readFileSync, readdirSync, statSync } from "node:fs";
3
- import { dirname, join, relative, resolve as pathResolve } from "node:path";
4
-
5
- import { CONTENT_TYPE_MAP, DEFAULT_CONTENT_TYPE } from "../common/content-type.js";
6
- import { InflateError, importHTMLModule, importAPIRoute, inflateJSX, importJSONModule, JSONModuleValue, jsx2HTML } from "../common/jsx.js";
7
- import { createNodeRuntime } from "@miqro/jsx-node";
8
- import { getHotReloadScript } from "../services/hot-reload.js";
9
- import { RuntimeURL } from "@miqro/jsx";
10
- import { cwd } from "node:process";
11
- import { assertGlobalTampered } from "../services/globals.js";
12
- import { getHTTPRouterPath, getStaticFilesPath } from "../common/paths.js";
13
- import { setupCORS } from "./setup-cors.js";
14
- import { setupAUTH } from "./setup-auth.js";
15
- import { getRoutes } from "../services/utils/get-route.js";
16
- import { describeFilePath, mkdirASync, writeFileASync } from "../common/fs.js";
17
- import { inflateMD2HTML } from "./md.js";
18
- import { MiddlewareConfig, ServerInterface, ServerRequest } from "../types.js";
19
- import { setupMiddleware } from "./setup-middleware.js";
20
- import { setupError } from "./setup-error.js";
21
-
22
- export interface RouteFileMap {
23
- [filePath: string]: {
24
- routes: {
25
- path?: string;
26
- method?: string;
27
- options?: RouterHandlerOptions;
28
- inflatePath?: string;
29
- }[];
30
- filePath: string;
31
- service: string;
32
- previewMethod: "api" | "html" | null;
33
- }
34
- }
35
-
36
- export interface StaticFileMap {
37
- [filePathKey: string]: {
38
- path: string;
39
- filePath: string;
40
- method: string | null;
41
- inflatePath?: string;
42
- contentType: string;
43
- previewMethod: "html" | null;
44
- body: Buffer;
45
- }
46
- }
47
-
48
- export async function setupHTTPRouter(server: ServerInterface, logger: Logger, hotreload: boolean, servicePath: string, service: string, routeFileMap: RouteFileMap, staticFileMap: StaticFileMap | null, inflateDir: string | undefined | false, inflateSea: boolean, errors: InflateError[], inflateParallel?: number) {
49
- const mainRouter = new Router();
50
- const apiRouterPath = getHTTPRouterPath(servicePath); //resolve(process.cwd(), service, "http");
51
- let middlewareConfig: MiddlewareConfig | null = null;
52
-
53
- await setupError(logger, servicePath, service, mainRouter, inflateDir, inflateSea, errors);
54
-
55
- await setupCORS(logger, servicePath, service, mainRouter, inflateDir, inflateSea, errors);
56
- await setupAUTH(logger, servicePath, service, mainRouter, inflateDir, inflateSea, errors);
57
- middlewareConfig = await setupMiddleware(logger, servicePath, service, mainRouter, inflateDir, inflateSea, errors);
58
-
59
- if (apiRouterPath) {
60
- logger.trace("setting up http routes from [%s]", service);
61
- const { router: httpRouter } = await createRouterFromDirectory(server, hotreload, service, logger, apiRouterPath, errors, routeFileMap, staticFileMap, inflateDir, inflateSea, inflateParallel);
62
- mainRouter.use(httpRouter);
63
- }
64
-
65
- const staticFilesPath = getStaticFilesPath(servicePath); //resolve(process.cwd(), service, "static");
66
- if (staticFilesPath) {
67
- logger.trace("setting up static file routes from [%s]", service);
68
- const staticRouter = await createStaticRouterFromDirectory(service, logger, staticFilesPath, inflateDir, routeFileMap, staticFileMap, inflateParallel);
69
- mainRouter.use(staticRouter);
70
- }
71
-
72
- if (middlewareConfig && middlewareConfig.post) {
73
- for (const m of middlewareConfig.post) {
74
- mainRouter.use(m);
75
- }
76
- }
77
-
78
- return mainRouter;
79
- }
80
-
81
- async function createStaticRoute(service: string, logger: Logger, router: Router, dir: string, file: ScannedFile, inflateDir?: string | undefined | false, routeFileMap?: RouteFileMap, staticFileMap?: StaticFileMap) {
82
- return new Promise<void>(async (resolve, reject) => {
83
- try {
84
- logger.trace("creating static route for [%s]", file.filePath);
85
- logger.trace("[%o]", {
86
- file,
87
- dir
88
- });
89
- const contentType = CONTENT_TYPE_MAP[String(file.ext).toLocaleLowerCase()];
90
- const path = join("/", relative(dir, file.filePath));
91
-
92
-
93
- routeFileMap[file.filePath] = {
94
- routes: [{
95
- method: "GET",
96
- path: normalizePath(path)
97
- }],
98
- service,
99
- filePath: file.filePath,
100
- previewMethod: "html"
101
- };
102
-
103
- if (inflateDir) {
104
- const inflatePath = join(inflateDir, service, "static", path);
105
- mkdir(dirname(inflatePath), {
106
- recursive: true
107
- }, (err) => {
108
-
109
- })
110
- await mkdirASync(dirname(inflatePath), {
111
- recursive: true
112
- });
113
- logger.log("writing [%s]", relative(cwd(), inflatePath));
114
- const body = readFileSync(file.filePath);
115
- await writeFileASync(inflatePath, body);
116
- if (staticFileMap) {
117
- staticFileMap[file.filePath] = {
118
- contentType,
119
- filePath: file.filePath,
120
- previewMethod: "html",
121
- method: "GET",
122
- path: normalizePath(path),
123
- body: Buffer.from(body),
124
- inflatePath: inflateDir ? join(inflateDir, service, "static", path) : undefined
125
- }
126
- }
127
- }
128
-
129
-
130
-
131
- router.use(assertGlobalTampered);
132
- router.get(path, async function (_req, res) {
133
- await new Promise<void>((resolve, reject) => {
134
- try {
135
- readFile(file.filePath, async (err, body) => {
136
- if (err) {
137
- reject(err);
138
- } else {
139
- try {
140
- await res.asyncEnd({
141
- status: 200,
142
- headers: {
143
- ["Content-Type"]: contentType ? contentType : DEFAULT_CONTENT_TYPE
144
- },
145
- body
146
- });
147
- resolve();
148
- } catch (e) {
149
- reject(e);
150
- }
151
- }
152
- })
153
- } catch (e) {
154
- reject(e);
155
- }
156
- });
157
- });
158
- resolve();
159
- } catch (e) {
160
- reject(e);
161
- }
162
- });
163
- }
164
-
165
- async function createStaticRouterFromDirectory(service: string, logger: Logger, dir: string, inflateDir: string | false | undefined, routeFileMap: RouteFileMap | undefined, staticFileMap: StaticFileMap | null, inflateParallel?: number): Promise<Router> {
166
- const router = new Router();
167
- const maxParallel = inflateParallel ? inflateParallel : 1;
168
- logger.debug("loading static directory with parallel [%s]", maxParallel);
169
- let tR = [];
170
- const files = scanFiles(dir);
171
- for (const file of files) {
172
- tR.push(await createStaticRoute(service, logger, router, dir, file, inflateDir, routeFileMap, staticFileMap));
173
- if (tR.length >= maxParallel) {
174
- await Promise.all(tR);
175
- tR = [];
176
- }
177
- }
178
- if (tR.length > 0) {
179
- await Promise.all(tR);
180
- tR = [];
181
- }
182
- return router;
183
- }
184
-
185
- async function createRouterFromDirectory(server: ServerInterface, hotreload: boolean, service: string, logger: Logger, dir: string, errors: InflateError[] = [], routeFileMap: RouteFileMap = {}, staticFileMap: StaticFileMap | null = null, inflateDir: string | undefined | false, inflateSea: boolean, inflateParallel?: number): Promise<{
186
- router: Router;
187
- errors: InflateError[];
188
- routeFileMap: RouteFileMap;
189
- }> {
190
- const router = new Router();
191
- const maxParallel = inflateParallel ? inflateParallel : 1;
192
- server.logger.debug("loading http directory with parallel [%s]", maxParallel);
193
- let tR = [];
194
- router.use(assertGlobalTampered);
195
- const files = scanFiles(dir);
196
- for (const file of files) {
197
- tR.push(new Promise<void>(async (resolve) => {
198
- try {
199
- switch (file.ext) {
200
- case ".jsx":
201
- case ".cjs":
202
- case ".js":
203
- case ".ts":
204
- case ".tsx": {
205
- switch (file.subExt) {
206
- case ".test":
207
- return resolve();
208
- case ".ignore":
209
- logger.warn("ignoring [%s]", file.filePath);
210
- return resolve();
211
- case ".api": {
212
-
213
- const module = await importAPIRoute(file.filePath, logger);
214
-
215
- const routes = getRoutes(join("/", dirname(relative(dir, file.filePath))), file.subName, module);
216
-
217
- routeFileMap[file.filePath] = {
218
- routes,
219
- service,
220
- filePath: file.filePath,
221
- previewMethod: "api"
222
- };
223
-
224
- const inflatedCode = inflateDir ? await inflateJSX(file.filePath, {
225
- embemedJSX: false,
226
- minify: false,
227
- useExport: true,
228
- platform: "node",
229
- logger
230
- }) : "";
231
-
232
- for (const r of routes) {
233
-
234
- /*if (inflateDir && r.inflatePath) {
235
- const rPath = r.inflatePath;
236
- const inflatePath = join(inflateDir, service, "http", rPath + ".api.js");
237
- mkdirSync(dirname(inflatePath), {
238
- recursive: true
239
- });
240
- logger.log("writing [%s]", relative(cwd(), inflatePath));
241
- writeFileSync(inflatePath, inflatedCode);
242
- }*/
243
-
244
- if (inflateDir && r.defaultInflatePath && inflateSea) {
245
- const rPath = r.defaultInflatePath;
246
- const inflatePath = join(inflateDir, service, "http", rPath + ".api.cjs");
247
- await mkdirASync(dirname(inflatePath), {
248
- recursive: true
249
- });
250
- logger.log("writing [%s]", relative(cwd(), inflatePath));
251
- await writeFileASync(inflatePath, inflatedCode);
252
- }
253
-
254
-
255
- router.use(assertGlobalTampered);
256
- router.use(module.handler, r.path, r.method as any, r.options);
257
- }
258
- return resolve();
259
- }
260
- case ".json": {
261
- const module = await importJSONModule(file.filePath, logger);
262
- const routes = getRoutes(join("/", dirname(relative(dir, file.filePath))), file.subName + ".json", module.apiOptions as Partial<APIRoute>);
263
-
264
- routeFileMap[file.filePath] = {
265
- routes,
266
- service,
267
- filePath: file.filePath,
268
- previewMethod: "html"
269
- };
270
-
271
- for (const r of routes) {
272
-
273
- const contentType = CONTENT_TYPE_MAP[".json"] ? CONTENT_TYPE_MAP[".json"] : DEFAULT_CONTENT_TYPE;
274
-
275
-
276
-
277
- if (inflateDir) {
278
-
279
- if (r.inflatePath) {
280
- //if (r.method === "GET" || r.method === "get") {
281
- const rPath = r.inflatePath;
282
- const inflatePath = join(inflateDir, service, "static", rPath);
283
- await mkdirASync(dirname(inflatePath), {
284
- recursive: true
285
- });
286
- if (existsSync(inflatePath) && statSync(inflatePath).isDirectory()) {
287
- logger.trace("ignoring writing over directory [%s] for file [%s]", relative(cwd(), inflatePath), file.filePath);
288
- continue;
289
- }
290
- const JSON_STATIC = await getJSON({ server } as ServerRequest, null, newURL(r.path), module.apiOptions?.basePath, module.default);
291
- //const JSON = await getJSON({ server } as ServerRequest, null, newURL(r.path), module.apiOptions?.basePath, module.default);
292
- logger.log("writing [%s]", relative(cwd(), inflatePath));
293
- await writeFileASync(inflatePath, JSON_STATIC);
294
- //}
295
-
296
- if (staticFileMap && inflateSea) {
297
- staticFileMap[file.filePath] = {
298
- contentType,
299
- filePath: file.filePath,
300
- method: r.method,
301
- previewMethod: "html",
302
- path: r.path,
303
- body: Buffer.from(JSON_STATIC),
304
- inflatePath: inflateDir ? join(inflateDir, service, "static", r.inflatePath) : undefined
305
- }
306
- }
307
- }
308
- }
309
-
310
- router.use(assertGlobalTampered);
311
- router.use(async function (req: Request, res: Response) {
312
-
313
- const JSON = await getJSON(req, res, newURL(req.path), module.apiOptions?.basePath, module.default);
314
-
315
- return res.asyncEnd({
316
- status: 200,
317
- headers: {
318
- ["Content-Type"]: contentType ? contentType : DEFAULT_CONTENT_TYPE
319
- },
320
- body: JSON
321
- });
322
- }, r.path, r.method as any, r.options)
323
- }
324
-
325
- return resolve();
326
- }
327
- case ".html": {
328
-
329
- const module = await importHTMLModule(file.filePath, logger);
330
-
331
- const routes = getRoutes(join("/", dirname(relative(dir, file.filePath))), file.subName + ".html", module.apiOptions as Partial<APIRoute>);
332
-
333
- routeFileMap[file.filePath] = {
334
- routes,
335
- filePath: file.filePath,
336
- service,
337
- previewMethod: "html"
338
- };
339
-
340
- for (const r of routes) {
341
-
342
- const contentType = CONTENT_TYPE_MAP[".html"] ? CONTENT_TYPE_MAP[".html"] : DEFAULT_CONTENT_TYPE;
343
-
344
- if (inflateDir) {
345
-
346
- if (r.inflatePath) {
347
- //if (r.method === "GET" || r.method === "get") {
348
- const rPath = r.inflatePath;
349
- const inflatePath = join(inflateDir, service, "static", rPath);
350
- await mkdirASync(dirname(inflatePath), {
351
- recursive: true
352
- });
353
- if (existsSync(inflatePath) && statSync(inflatePath).isDirectory()) {
354
- logger.trace("ignoring writing over directory [%s] for file [%s]", relative(cwd(), inflatePath), file.filePath);
355
- continue;
356
- }
357
- const toRender = typeof module.default === "function" ? module.default({ server } as ServerRequest, null) : module.default;
358
- const HTML_STATIC = await getHTML(hotreload, { server } as ServerRequest, null, newURL(r.path), module.apiOptions?.basePath, await toRender);
359
-
360
- logger.log("writing [%s]", relative(cwd(), inflatePath));
361
- await writeFileASync(inflatePath, HTML_STATIC);
362
- //}
363
-
364
-
365
- if (staticFileMap && inflateSea) {
366
- staticFileMap[file.filePath + r.method + r.path] = {
367
- filePath: file.filePath,
368
- contentType,
369
- method: r.method,
370
- previewMethod: "html",
371
- path: r.path,
372
- body: Buffer.from(HTML_STATIC),
373
- inflatePath: inflateDir ? join(inflateDir, service, "static", r.inflatePath) : undefined
374
- }
375
- }
376
- }
377
- }
378
-
379
- router.use(assertGlobalTampered);
380
- router.use(async function (req: Request, res: Response) {
381
- const toRender = typeof module.default === "function" ? module.default(req, res) : module.default;
382
- const HTML = await getHTML(hotreload, req, res, newURL(req.path), module.apiOptions?.basePath, await toRender);
383
-
384
- return res.asyncEnd({
385
- status: 200,
386
- headers: {
387
- ["Content-Type"]: contentType ? contentType : DEFAULT_CONTENT_TYPE
388
- },
389
- body: HTML
390
- });
391
- }, r.path, r.method as any, r.options)
392
- }
393
- return resolve();
394
- }
395
- case ".min":
396
- case ".js":
397
- default: {
398
- // allow fall-through when extension is .js and .ts because is a static route without embemedJSX
399
- if (file.ext !== ".js" && file.ext !== ".ts") {
400
- const code = await inflateJSX(file.filePath, {
401
- embemedJSX: true,
402
- minify: file.subExt === ".min" ? true : false,
403
- useExport: false,
404
- logger
405
- });
406
- const contentType = CONTENT_TYPE_MAP[".js"];
407
- const path = join("/", dirname(relative(dir, file.filePath)), file.name + ".js");
408
-
409
- routeFileMap[file.filePath] = {
410
- routes: [{
411
- method: "GET",
412
- path
413
- }],
414
- filePath: file.filePath,
415
- service,
416
- previewMethod: "html"
417
- };
418
-
419
- if (inflateDir) {
420
- const inflatePath = join(inflateDir, service, "static", path);
421
- await mkdirASync(dirname(inflatePath), {
422
- recursive: true
423
- });
424
- logger.log("writing [%s]", relative(cwd(), inflatePath));
425
- await writeFileASync(inflatePath, code);
426
-
427
-
428
- if (staticFileMap && inflateSea) {
429
- staticFileMap[file.filePath] = {
430
- contentType,
431
- filePath: file.filePath,
432
- method: "GET",
433
- previewMethod: "html",
434
- path,
435
- body: Buffer.from(code),
436
- inflatePath: inflateDir ? join(inflateDir, service, "static", path) : undefined
437
- }
438
- }
439
- }
440
-
441
- router.use(assertGlobalTampered);
442
- router.get(path, async function (req, res) {
443
- return res.asyncEnd({
444
- status: 200,
445
- headers: {
446
- ["Content-Type"]: contentType ? contentType : DEFAULT_CONTENT_TYPE
447
- },
448
- body: code
449
- });
450
- });
451
- return resolve();
452
- }
453
- }
454
- }
455
- }
456
- case ".md": {
457
- switch (file.subExt) {
458
- case ".html": {
459
- const code = await inflateMD2HTML(file.filePath, logger);
460
- const contentType = CONTENT_TYPE_MAP[".html"];
461
- const path = join("/", dirname(relative(dir, file.filePath)), file.name);
462
- routeFileMap[file.filePath] = {
463
- routes: [{
464
- method: "GET",
465
- path
466
- }],
467
- service,
468
- filePath: file.filePath,
469
- previewMethod: "html"
470
- };
471
-
472
- if (inflateDir) {
473
- const inflatePath = join(inflateDir, service, "static", path);
474
- await mkdirASync(dirname(inflatePath), {
475
- recursive: true
476
- });
477
- logger.log("writing [%s]", relative(cwd(), inflatePath));
478
- await writeFileASync(inflatePath, code);
479
- if (staticFileMap && inflateSea) {
480
- staticFileMap[file.filePath] = {
481
- contentType,
482
- method: "GET",
483
- filePath: file.filePath,
484
- previewMethod: "html",
485
- path,
486
- body: Buffer.from(code),
487
- inflatePath: inflateDir ? join(inflateDir, service, "static", path) : undefined
488
- }
489
- }
490
- }
491
-
492
- router.use(assertGlobalTampered);
493
- router.get(path, async function (_req, res) {
494
- res.asyncEnd({
495
- status: 200,
496
- headers: {
497
- ["Content-Type"]: contentType ? contentType : DEFAULT_CONTENT_TYPE
498
- },
499
- body: code
500
- });
501
- });
502
- return resolve();
503
- }
504
- }
505
- }
506
- default:
507
- if (file.ext === ".js" || file.ext === ".ts") {
508
- switch (file.subExt) {
509
- case ".ignore": {
510
- logger.warn("ignoring [%s]", file.filePath);
511
- return resolve();
512
- }
513
- case ".bundle":
514
- case ".min": {
515
- const code = await inflateJSX(file.filePath, {
516
- embemedJSX: false,
517
- minify: file.subExt === ".min" ? true : false,
518
- useExport: false,
519
- logger
520
- });
521
- const contentType = CONTENT_TYPE_MAP[".js"];
522
- const path = join("/", dirname(relative(dir, file.filePath)), file.name + ".js");
523
- routeFileMap[file.filePath] = {
524
- routes: [{
525
- method: "GET",
526
- path
527
- }],
528
- filePath: file.filePath,
529
- service,
530
- previewMethod: "html"
531
- };
532
-
533
- if (inflateDir) {
534
- const inflatePath = join(inflateDir, service, "static", path);
535
- await mkdirASync(dirname(inflatePath), {
536
- recursive: true
537
- });
538
- logger.log("writing [%s]", relative(cwd(), inflatePath));
539
- await writeFileASync(inflatePath, code);
540
- if (staticFileMap && inflateSea) {
541
- staticFileMap[file.filePath] = {
542
- contentType,
543
- method: "GET",
544
- filePath: file.filePath,
545
- previewMethod: "html",
546
- path,
547
- body: Buffer.from(code),
548
- inflatePath: inflateDir ? join(inflateDir, service, "static", path) : undefined
549
- }
550
- }
551
- }
552
-
553
- router.use(assertGlobalTampered);
554
- router.get(path, async function (_req, res) {
555
- res.asyncEnd({
556
- status: 200,
557
- headers: {
558
- ["Content-Type"]: contentType ? contentType : DEFAULT_CONTENT_TYPE
559
- },
560
- body: code
561
- });
562
- });
563
- return resolve();
564
- }
565
- }
566
- } else if (file.ext === ".bundle") {
567
- switch (file.subExt) {
568
- case ".ignore": {
569
- logger.warn("ignoring [%s]", file.filePath);
570
- return resolve();
571
- }
572
- case ".css": {
573
- const code = readFileSync(file.filePath).toString()
574
- .split("\n")
575
- .filter(c => c)
576
- .filter(c => c.charAt(0) !== "#")
577
- .map(cssPath => readFileSync(pathResolve(dirname(file.filePath), cssPath)).toString())
578
- .join("\n");
579
- const contentType = CONTENT_TYPE_MAP[".css"];
580
- const path = join("/", dirname(relative(dir, file.filePath)), file.name);
581
- routeFileMap[file.filePath] = {
582
- routes: [{
583
- method: "GET",
584
- path
585
- }],
586
- service,
587
- filePath: file.filePath,
588
- previewMethod: "html"
589
- };
590
-
591
- if (inflateDir) {
592
- const inflatePath = join(inflateDir, service, "static", path);
593
- await mkdirASync(dirname(inflatePath), {
594
- recursive: true
595
- });
596
- logger.log("writing [%s]", relative(cwd(), inflatePath));
597
- await writeFileASync(inflatePath, code);
598
- if (staticFileMap && inflateSea) {
599
- staticFileMap[file.filePath] = {
600
- contentType,
601
- method: "GET",
602
- filePath: file.filePath,
603
- previewMethod: "html",
604
- path,
605
- body: Buffer.from(code),
606
- inflatePath: inflateDir ? join(inflateDir, service, "static", path) : undefined
607
- }
608
- }
609
- }
610
-
611
- router.use(assertGlobalTampered);
612
- router.get(path, async function (_req, res) {
613
- res.asyncEnd({
614
- status: 200,
615
- headers: {
616
- ["Content-Type"]: contentType ? contentType : DEFAULT_CONTENT_TYPE
617
- },
618
- body: code
619
- });
620
- });
621
- return resolve();
622
- }
623
- }
624
- }
625
- await createStaticRoute(service, logger, router, dir, file, inflateDir, routeFileMap, staticFileMap);
626
- return resolve();
627
-
628
- }
629
- } catch (e) {
630
- logger.error("error with " + file.filePath);
631
- logger.error(e);
632
- errors.push({
633
- filePath: file.filePath,
634
- error: e
635
- });
636
- } finally {
637
- return resolve();
638
- }
639
- }));
640
- if (tR.length >= maxParallel) {
641
- await Promise.all(tR);
642
- tR = [];
643
- }
644
- }
645
- if (tR.length > 0) {
646
- await Promise.all(tR);
647
- tR = [];
648
- }
649
- router.use(assertGlobalTampered);
650
- return {
651
- routeFileMap,
652
- router,
653
- errors
654
- };
655
- }
656
-
657
- interface ScannedFile {
658
- filePath: string;
659
- ext: string;
660
- fileName: string;
661
- name: string;
662
- subExt: string;
663
- subName: string;
664
- }
665
-
666
- export function scanFiles(path: string, ret: ScannedFile[] = []): ScannedFile[] {
667
- const files = readdirSync(path).sort();
668
- for (const file of files) {
669
- const filePath = pathResolve(path, file);
670
- if (statSync(filePath).isDirectory()) {
671
- scanFiles(filePath, ret);
672
- continue;
673
- } else {
674
- const description = describeFilePath(filePath);
675
- if (description.fileName === ".DS_Store") {
676
- continue;
677
- }
678
- ret.push(description);
679
- }
680
- }
681
- return ret;
682
- }
683
-
684
-
685
-
686
- function getHTML(hotreload: boolean, req: ServerRequest, res: Response | null, url: RuntimeURL, basePath: string | undefined, out: JSX.Element): string {
687
- let HTML = `<!DOCTYPE html>\n${jsx2HTML(out, createNodeRuntime({
688
- url,
689
- basePath
690
- }))}`;
691
-
692
- //console.log("GET_HTML[%s] [%o]", HTML, out);
693
-
694
- if (hotreload) {
695
- HTML += getHotReloadScript()
696
- }
697
- return HTML;
698
- }
699
-
700
- async function getJSON(req: ServerRequest, res: Response | null, url: RuntimeURL, basePath: string | undefined, out: JSONModuleValue | Promise<JSONModuleValue> | ((req: Request | null, res: Response | null) => JSONModuleValue | Promise<JSONModuleValue>)) {
701
-
702
- const toRender = await (typeof out === "function" ? out(req, res) : out);
703
- return JSON.stringify(toRender, undefined, 2);
704
- }