miqro 7.0.1 → 7.0.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.
- package/build/esm/editor/auth.d.ts +6 -0
- package/build/esm/editor/auth.js +41 -0
- package/build/esm/editor/common/admin-interface.d.ts +36 -0
- package/build/esm/editor/common/admin-interface.js +44 -0
- package/build/esm/editor/common/constants.d.ts +4 -0
- package/build/esm/editor/common/constants.js +20 -0
- package/build/esm/editor/common/constants.server.d.ts +2 -0
- package/build/esm/editor/common/constants.server.js +4 -0
- package/build/esm/editor/common/editor-index.d.ts +2 -0
- package/build/esm/editor/common/editor-index.js +14 -0
- package/build/esm/editor/common/html-encode.d.ts +1 -0
- package/build/esm/editor/common/html-encode.js +14 -0
- package/build/esm/editor/common/log-socket.d.ts +15 -0
- package/build/esm/editor/common/log-socket.js +71 -0
- package/build/esm/editor/common/templates.d.ts +11 -0
- package/build/esm/editor/common/templates.js +477 -0
- package/build/esm/editor/components/api-preview.d.ts +11 -0
- package/build/esm/editor/components/api-preview.js +92 -0
- package/build/esm/editor/components/editor.d.ts +16 -0
- package/build/esm/editor/components/editor.js +367 -0
- package/build/esm/editor/components/file-browser.d.ts +37 -0
- package/build/esm/editor/components/file-browser.js +127 -0
- package/build/esm/editor/components/file-editor-toolbar.d.ts +22 -0
- package/build/esm/editor/components/file-editor-toolbar.js +95 -0
- package/build/esm/editor/components/file-editor.d.ts +32 -0
- package/build/esm/editor/components/file-editor.js +61 -0
- package/build/esm/editor/components/filter-query.d.ts +1 -0
- package/build/esm/editor/components/filter-query.js +23 -0
- package/build/esm/editor/components/highlight-text-area.d.ts +11 -0
- package/build/esm/editor/components/highlight-text-area.js +127 -0
- package/build/esm/editor/components/log-viewer.d.ts +6 -0
- package/build/esm/editor/components/log-viewer.js +71 -0
- package/build/esm/editor/components/new-file.d.ts +10 -0
- package/build/esm/editor/components/new-file.js +119 -0
- package/build/esm/editor/components/scroll-query.d.ts +7 -0
- package/build/esm/editor/components/scroll-query.js +22 -0
- package/build/esm/editor/components/start-page.d.ts +13 -0
- package/build/esm/editor/components/start-page.js +32 -0
- package/build/esm/editor/http/admin/editor/api/fs/delete.api.d.ts +3 -0
- package/build/esm/editor/http/admin/editor/api/fs/delete.api.js +30 -0
- package/build/esm/editor/http/admin/editor/api/fs/read.api.d.ts +5 -0
- package/build/esm/editor/http/admin/editor/api/fs/read.api.js +50 -0
- package/build/esm/editor/http/admin/editor/api/fs/rename.api.d.ts +4 -0
- package/build/esm/editor/http/admin/editor/api/fs/rename.api.js +40 -0
- package/build/esm/editor/http/admin/editor/api/fs/scan.api.d.ts +26 -0
- package/build/esm/editor/http/admin/editor/api/fs/scan.api.js +150 -0
- package/build/esm/editor/http/admin/editor/api/fs/write.api.d.ts +3 -0
- package/build/esm/editor/http/admin/editor/api/fs/write.api.js +39 -0
- package/build/esm/editor/http/admin/editor/api/server/reload.api.d.ts +10 -0
- package/build/esm/editor/http/admin/editor/api/server/reload.api.js +46 -0
- package/build/esm/editor/http/admin/editor/api/server/restart.api.d.ts +10 -0
- package/build/esm/editor/http/admin/editor/api/server/restart.api.js +46 -0
- package/build/esm/editor/http/admin/editor/editor.d.ts +1 -0
- package/build/esm/editor/http/admin/editor/editor.js +8 -0
- package/build/esm/editor/http/admin/editor/index.api.d.ts +3 -0
- package/build/esm/editor/http/admin/editor/index.api.js +22 -0
- package/build/esm/editor/server.d.ts +3 -0
- package/build/esm/editor/server.js +49 -0
- package/build/esm/editor/ws.d.ts +3 -0
- package/build/esm/editor/ws.js +11 -0
- package/build/esm/src/inflate/inflate-sea.js +8 -1
- package/build/esm/src/services/app.js +7 -2
- package/build/lib.cjs +14 -3
- package/editor/auth.ts +51 -0
- package/editor/common/admin-interface.ts +84 -0
- package/editor/common/constants.server.ts +5 -0
- package/editor/common/constants.ts +21 -0
- package/editor/common/editor-index.tsx +17 -0
- package/editor/common/html-encode.ts +14 -0
- package/editor/common/log-socket.tsx +87 -0
- package/editor/common/templates.ts +481 -0
- package/editor/components/api-preview.tsx +118 -0
- package/editor/components/editor.tsx +496 -0
- package/editor/components/file-browser.tsx +311 -0
- package/editor/components/file-editor-toolbar.tsx +194 -0
- package/editor/components/file-editor.tsx +125 -0
- package/editor/components/filter-query.tsx +26 -0
- package/editor/components/highlight-text-area.tsx +148 -0
- package/editor/components/log-viewer.tsx +113 -0
- package/editor/components/new-file.tsx +172 -0
- package/editor/components/scroll-query.tsx +25 -0
- package/editor/components/start-page.tsx +52 -0
- package/editor/http/admin/editor/api/fs/delete.api.tsx +32 -0
- package/editor/http/admin/editor/api/fs/read.api.tsx +55 -0
- package/editor/http/admin/editor/api/fs/rename.api.tsx +41 -0
- package/editor/http/admin/editor/api/fs/scan.api.tsx +181 -0
- package/editor/http/admin/editor/api/fs/write.api.tsx +41 -0
- package/editor/http/admin/editor/api/server/reload.api.ts +53 -0
- package/editor/http/admin/editor/api/server/restart.api.tsx +52 -0
- package/editor/http/admin/editor/editor.tsx +10 -0
- package/editor/http/admin/editor/index.api.tsx +42 -0
- package/editor/server.ts +57 -0
- package/editor/ws.ts +15 -0
- package/package.json +2 -2
- package/src/bin/compile.ts +35 -0
- package/src/bin/doc-md.ts +210 -0
- package/src/bin/generate-doc.ts +64 -0
- package/src/bin/test.ts +92 -0
- package/src/bin/types.ts +34 -0
- package/src/cluster.ts +27 -0
- package/src/common/arguments.ts +762 -0
- package/src/common/assets.ts +148 -0
- package/src/common/checksum.ts +58 -0
- package/src/common/constants.ts +18 -0
- package/src/common/content-type.ts +84 -0
- package/src/common/esbuild.ts +102 -0
- package/src/common/exit.ts +91 -0
- package/src/common/fs.ts +82 -0
- package/src/common/help.ts +60 -0
- package/src/common/jsx.ts +562 -0
- package/src/common/jwt.ts +85 -0
- package/src/common/paths.ts +107 -0
- package/src/common/watch.ts +88 -0
- package/src/inflate/inflate-sea.ts +244 -0
- package/src/inflate/inflate.ts +101 -0
- package/src/inflate/md.ts +25 -0
- package/src/inflate/setup-auth.ts +41 -0
- package/src/inflate/setup-cors.ts +41 -0
- package/src/inflate/setup-db.ts +117 -0
- package/src/inflate/setup-error.ts +44 -0
- package/src/inflate/setup-http.ts +704 -0
- package/src/inflate/setup-log.ts +45 -0
- package/src/inflate/setup-middleware.ts +47 -0
- package/src/inflate/setup-server-config.ts +48 -0
- package/src/inflate/setup-test.ts +23 -0
- package/src/inflate/setup-ws.ts +50 -0
- package/src/inflate/setup.doc.ts +92 -0
- package/src/inflate/utils/sea-utils.ts +14 -0
- package/src/lib.ts +34 -0
- package/src/main.ts +101 -0
- package/src/services/app.ts +703 -0
- package/src/services/editor.tsx +101 -0
- package/src/services/globals.ts.ignore +186 -0
- package/src/services/hot-reload.ts +51 -0
- package/src/services/migrations.ts +68 -0
- package/src/services/utils/admin-interface.ts +37 -0
- package/src/services/utils/cache.ts +88 -0
- package/src/services/utils/cluster-cache.ts +230 -0
- package/src/services/utils/cluster-ws.ts +202 -0
- package/src/services/utils/db-manager.ts +92 -0
- package/src/services/utils/get-route.ts +70 -0
- package/src/services/utils/jwt.ts +25 -0
- package/src/services/utils/log-transport.ts +81 -0
- package/src/services/utils/log.ts +92 -0
- package/src/services/utils/middleware.ts +10 -0
- package/src/services/utils/server-interface.ts +122 -0
- package/src/services/utils/websocketmanager.ts +157 -0
- package/src/types.ts +307 -0
|
@@ -0,0 +1,704 @@
|
|
|
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: true,
|
|
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: true,
|
|
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
|
+
}
|