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