From 3dc13caab8ced8e4d8134acb7532e46f3dad8082 Mon Sep 17 00:00:00 2001 From: Rizky Date: Sat, 16 Dec 2023 17:57:05 +0700 Subject: [PATCH] wip fix safari --- app/web/src/base/load/api/client-api.tsx | 1 + app/web/src/index.tsx | 2 + .../src/nova/ed/panel/popup/api/api-utils.ts | 5 +-- app/web/src/nova/vi/load/load-legacy.tsx | 5 ++- pkgs/core/index.ts | 3 +- pkgs/core/server/create.ts | 5 ++- pkgs/core/server/serve-api.ts | 2 +- pkgs/core/server/serve-static.ts | 44 +++++++++++++------ 8 files changed, 45 insertions(+), 22 deletions(-) create mode 100644 app/web/src/base/load/api/client-api.tsx diff --git a/app/web/src/base/load/api/client-api.tsx b/app/web/src/base/load/api/client-api.tsx new file mode 100644 index 00000000..ca99f8c6 --- /dev/null +++ b/app/web/src/base/load/api/client-api.tsx @@ -0,0 +1 @@ +export const apiClient = (base: string) => {}; diff --git a/app/web/src/index.tsx b/app/web/src/index.tsx index 76438813..a56b3703 100644 --- a/app/web/src/index.tsx +++ b/app/web/src/index.tsx @@ -15,6 +15,8 @@ const start = async () => { }; w.mobile = registerMobile(); w.db = dbClient("prasi", location.origin); + w.serverurl = base; + sworkerRegister(react); defineReact(); await defineWindow(false); diff --git a/app/web/src/nova/ed/panel/popup/api/api-utils.ts b/app/web/src/nova/ed/panel/popup/api/api-utils.ts index 0084ccee..04008e78 100644 --- a/app/web/src/nova/ed/panel/popup/api/api-utils.ts +++ b/app/web/src/nova/ed/panel/popup/api/api-utils.ts @@ -1,4 +1,3 @@ -import { createAPI, initApi } from "../../../../../utils/script/init-api"; import { PG } from "../../../logic/ed-global"; export const dev = JSON.parse(localStorage.getItem("prasi-dev") || "{}") as { @@ -51,8 +50,8 @@ export const checkAPI = async (p: PG) => { try { if (!apiRef[url]) { - await initApi({ api_url: url }, "dev"); - apiRef[url] = createAPI(url); + // await initApi({ api_url: url }, "dev"); + // apiRef[url] = createAPI(url); } const capi = apiRef[url]; let res = await capi._deploy({ diff --git a/app/web/src/nova/vi/load/load-legacy.tsx b/app/web/src/nova/vi/load/load-legacy.tsx index d27cc06e..74a276c3 100644 --- a/app/web/src/nova/vi/load/load-legacy.tsx +++ b/app/web/src/nova/vi/load/load-legacy.tsx @@ -1,5 +1,5 @@ import importModule from "../../../render/editor/tools/dynamic-import"; -import { initApi } from "../../../utils/script/init-api"; +import { viScriptArg } from "../render/script/arg"; export const viLoadLegacy = async (vi: { site: { @@ -38,7 +38,7 @@ export const viLoadLegacy = async (vi: { let api_url = vi.site.api_url; if (!api_url) api_url = ((site.config as any) || {}).api_url || ""; - await initApi(site.config); + // await initApi(site.config); const path = `/npm/site/${vi.site.id}/site.js`; await importModule(path); @@ -62,6 +62,7 @@ export const viLoadLegacy = async (vi: { return res; }; const scope = { + ...viScriptArg(), types: {}, exports: w.exports, load: importModule, diff --git a/pkgs/core/index.ts b/pkgs/core/index.ts index a5ee74ab..9f24f5d8 100644 --- a/pkgs/core/index.ts +++ b/pkgs/core/index.ts @@ -9,6 +9,7 @@ import { syncActionDefinition } from "utils/sync-def"; import { user } from "../../app/srv/ws/sync/entity/user"; import { snapshot } from "../../app/srv/ws/sync/entity/snapshot"; import { initSrv } from "../../app/srv/init"; +import { prepareApiRoutes } from "./server/api/api-scan"; g.status = "init"; @@ -52,7 +53,7 @@ if (!g.apiPrepared) { await initSrv(); await syncActionDefinition(); g.log.info("WS Action defined"); - + await prepareApiRoutes(); await prepareAPITypes(); g.log.info("API Prepared"); g.apiPrepared = true; diff --git a/pkgs/core/server/create.ts b/pkgs/core/server/create.ts index d5b50265..a09414bc 100644 --- a/pkgs/core/server/create.ts +++ b/pkgs/core/server/create.ts @@ -46,7 +46,10 @@ export const createServer = async () => { return serveStatic.serve(url); } - await serveAPI.serve(url, req); + const api_response = await serveAPI.serve(url, req); + if (api_response) { + return api_response; + } return serveStatic.serve(url); }, diff --git a/pkgs/core/server/serve-api.ts b/pkgs/core/server/serve-api.ts index 592eff7d..0f126d70 100644 --- a/pkgs/core/server/serve-api.ts +++ b/pkgs/core/server/serve-api.ts @@ -9,7 +9,6 @@ export const serveAPI = { for (const route of Object.values(g.api)) { g.router.insert(route.url.replace(/\*/gi, "**"), route); } - await prepareApiRoutes(); }, serve: async (url: URL, req: Request) => { let found = g.router.lookup(url.pathname); @@ -81,6 +80,7 @@ export const serveAPI = { if (finalResponse) { return createResponse(current.res, finalResponse); } + if ( (current.res as any)._status && (current.res as any)._status !== current.res.status diff --git a/pkgs/core/server/serve-static.ts b/pkgs/core/server/serve-static.ts index cad6fe8f..d87e863a 100644 --- a/pkgs/core/server/serve-static.ts +++ b/pkgs/core/server/serve-static.ts @@ -2,7 +2,10 @@ import { dir } from "dir"; import { inspectTreeAsync } from "fs-jetpack"; import { InspectTreeResult } from "fs-jetpack/types"; import { join } from "path"; +import { watch } from "fs"; + import mime from "mime"; +import { g } from "utils/global"; const webPath = "app/static"; const cache = { @@ -16,26 +19,39 @@ export const serveStatic = { list: InspectTreeResult, parent?: InspectTreeResult[] ) => { - for (const item of list.children) { - if (item.type === "file") { - const path = join( - ...(parent || [{ name: "static" }]).map((e) => e.name), - item.name - ); - const file = await Bun.file(dir.path(`app/${path}`)); + if (list.type === "dir") { + for (const item of list.children) { + await walk(item, [...(parent || []), list]); + } + } else { + const path = join(...(parent || []).map((e) => e.name), list.name); + const file = Bun.file(dir.path(`app/${path}`)); + if (await file.exists()) { + cache.static[path.substring("static".length)] = { + type: mime.getType(path) || "application/octet-stream", + content: await file.arrayBuffer(), + }; + } + } + }; + if (list) { + await walk(list); + } + + if (g.mode === "dev") { + watch(dir.path(`app/static`), async (_, filename) => { + if (filename) { + const path = join("static", filename); + const file = Bun.file(dir.path(`app/${path}`)); + console.log(_, filename); if (await file.exists()) { - cache.static[path.substring("static".length)] = { + cache.static[`/${filename}`] = { type: mime.getType(path) || "application/octet-stream", content: await file.arrayBuffer(), }; } - } else { - await walk(item, parent ? [...parent, list] : [list]); } - } - }; - if (list) { - await walk(list); + }); } }, exists: (url: URL) => {