wip fix safari

This commit is contained in:
Rizky 2023-12-16 17:57:05 +07:00
parent d274636ff9
commit 3dc13caab8
8 changed files with 45 additions and 22 deletions

View File

@ -0,0 +1 @@
export const apiClient = (base: string) => {};

View File

@ -15,6 +15,8 @@ const start = async () => {
}; };
w.mobile = registerMobile(); w.mobile = registerMobile();
w.db = dbClient("prasi", location.origin); w.db = dbClient("prasi", location.origin);
w.serverurl = base;
sworkerRegister(react); sworkerRegister(react);
defineReact(); defineReact();
await defineWindow(false); await defineWindow(false);

View File

@ -1,4 +1,3 @@
import { createAPI, initApi } from "../../../../../utils/script/init-api";
import { PG } from "../../../logic/ed-global"; import { PG } from "../../../logic/ed-global";
export const dev = JSON.parse(localStorage.getItem("prasi-dev") || "{}") as { export const dev = JSON.parse(localStorage.getItem("prasi-dev") || "{}") as {
@ -51,8 +50,8 @@ export const checkAPI = async (p: PG) => {
try { try {
if (!apiRef[url]) { if (!apiRef[url]) {
await initApi({ api_url: url }, "dev"); // await initApi({ api_url: url }, "dev");
apiRef[url] = createAPI(url); // apiRef[url] = createAPI(url);
} }
const capi = apiRef[url]; const capi = apiRef[url];
let res = await capi._deploy({ let res = await capi._deploy({

View File

@ -1,5 +1,5 @@
import importModule from "../../../render/editor/tools/dynamic-import"; 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: { export const viLoadLegacy = async (vi: {
site: { site: {
@ -38,7 +38,7 @@ export const viLoadLegacy = async (vi: {
let api_url = vi.site.api_url; let api_url = vi.site.api_url;
if (!api_url) api_url = ((site.config as any) || {}).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`; const path = `/npm/site/${vi.site.id}/site.js`;
await importModule(path); await importModule(path);
@ -62,6 +62,7 @@ export const viLoadLegacy = async (vi: {
return res; return res;
}; };
const scope = { const scope = {
...viScriptArg(),
types: {}, types: {},
exports: w.exports, exports: w.exports,
load: importModule, load: importModule,

View File

@ -9,6 +9,7 @@ import { syncActionDefinition } from "utils/sync-def";
import { user } from "../../app/srv/ws/sync/entity/user"; import { user } from "../../app/srv/ws/sync/entity/user";
import { snapshot } from "../../app/srv/ws/sync/entity/snapshot"; import { snapshot } from "../../app/srv/ws/sync/entity/snapshot";
import { initSrv } from "../../app/srv/init"; import { initSrv } from "../../app/srv/init";
import { prepareApiRoutes } from "./server/api/api-scan";
g.status = "init"; g.status = "init";
@ -52,7 +53,7 @@ if (!g.apiPrepared) {
await initSrv(); await initSrv();
await syncActionDefinition(); await syncActionDefinition();
g.log.info("WS Action defined"); g.log.info("WS Action defined");
await prepareApiRoutes();
await prepareAPITypes(); await prepareAPITypes();
g.log.info("API Prepared"); g.log.info("API Prepared");
g.apiPrepared = true; g.apiPrepared = true;

View File

@ -46,7 +46,10 @@ export const createServer = async () => {
return serveStatic.serve(url); 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); return serveStatic.serve(url);
}, },

View File

@ -9,7 +9,6 @@ export const serveAPI = {
for (const route of Object.values(g.api)) { for (const route of Object.values(g.api)) {
g.router.insert(route.url.replace(/\*/gi, "**"), route); g.router.insert(route.url.replace(/\*/gi, "**"), route);
} }
await prepareApiRoutes();
}, },
serve: async (url: URL, req: Request) => { serve: async (url: URL, req: Request) => {
let found = g.router.lookup(url.pathname); let found = g.router.lookup(url.pathname);
@ -81,6 +80,7 @@ export const serveAPI = {
if (finalResponse) { if (finalResponse) {
return createResponse(current.res, finalResponse); return createResponse(current.res, finalResponse);
} }
if ( if (
(current.res as any)._status && (current.res as any)._status &&
(current.res as any)._status !== current.res.status (current.res as any)._status !== current.res.status

View File

@ -2,7 +2,10 @@ import { dir } from "dir";
import { inspectTreeAsync } from "fs-jetpack"; import { inspectTreeAsync } from "fs-jetpack";
import { InspectTreeResult } from "fs-jetpack/types"; import { InspectTreeResult } from "fs-jetpack/types";
import { join } from "path"; import { join } from "path";
import { watch } from "fs";
import mime from "mime"; import mime from "mime";
import { g } from "utils/global";
const webPath = "app/static"; const webPath = "app/static";
const cache = { const cache = {
@ -16,26 +19,39 @@ export const serveStatic = {
list: InspectTreeResult, list: InspectTreeResult,
parent?: InspectTreeResult[] parent?: InspectTreeResult[]
) => { ) => {
for (const item of list.children) { if (list.type === "dir") {
if (item.type === "file") { for (const item of list.children) {
const path = join( await walk(item, [...(parent || []), list]);
...(parent || [{ name: "static" }]).map((e) => e.name), }
item.name } else {
); const path = join(...(parent || []).map((e) => e.name), list.name);
const file = await Bun.file(dir.path(`app/${path}`)); 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()) { if (await file.exists()) {
cache.static[path.substring("static".length)] = { cache.static[`/${filename}`] = {
type: mime.getType(path) || "application/octet-stream", type: mime.getType(path) || "application/octet-stream",
content: await file.arrayBuffer(), content: await file.arrayBuffer(),
}; };
} }
} else {
await walk(item, parent ? [...parent, list] : [list]);
} }
} });
};
if (list) {
await walk(list);
} }
}, },
exists: (url: URL) => { exists: (url: URL) => {