This commit is contained in:
Rizky 2024-01-14 20:33:09 +07:00
parent 269c2a58b9
commit d7740d323c
5 changed files with 44 additions and 14 deletions

View File

@ -1,4 +1,6 @@
import { apiProxy } from "../../../base/load/api/api-proxy";
import { loadApiProxyDef } from "../../../base/load/api/api-proxy-def";
import { dbProxy } from "../../../base/load/db/db-proxy";
import importModule from "../../../render/editor/tools/dynamic-import";
import { viScriptArg } from "../render/script/arg";
@ -53,9 +55,11 @@ export const viLoadLegacy = async (vi: {
const path = `/npm/site/${vi.site.id}/site.js`;
await importModule(path);
if (!vi.site.db.get()) {
vi.site.db.set(dbProxy(api_url));
}
if (!vi.site.api.get()) {
vi.site.api.set(apiProxy(api_url));
}
const w = window as any;
@ -73,7 +77,7 @@ export const viLoadLegacy = async (vi: {
return res;
};
const scope = {
...viScriptArg(),
...viScriptArg({ site: vi.site }),
types: {},
exports: w.exports,
load: importModule,

View File

@ -111,17 +111,22 @@ export const viEvalProps = (
}
};
export const updatePropScope = (meta: IMeta, scope: any) => {
export const updatePropScope = (
vi: { site: { db: any; api: any } },
meta: IMeta,
scope: any
) => {
if (meta.item.script?.props) {
const scopes = { ...scope, api: vi.site.api, db: vi.site.db };
for (const [name, prop] of Object.entries(meta.item.script.props)) {
if (prop.fn) {
const fn = new Function(
...Object.keys(scope),
...Object.keys(scopes),
`// [${meta.item.name}] ${name}: ${meta.item.id}
return ${prop.value || ""}
`
);
prop.fn = fn(...Object.values(scope));
prop.fn = fn(...Object.values(scopes));
}
}
}

View File

@ -27,7 +27,7 @@ export const viEvalScript = (
meta.script = {
passprop,
result: null,
Local: createViLocal(vi.meta, meta, vi.script?.init_local_effect),
Local: createViLocal(vi, meta),
PassProp: createViPassProp(vi, meta),
};
} else {
@ -35,7 +35,6 @@ export const viEvalScript = (
}
const script = meta.script;
const exports = (window as any).exports;
const arg = {
useEffect,
@ -57,7 +56,7 @@ export const viEvalScript = (
if (typeof passprop === "object") {
for (const [k, v] of Object.entries(passprop)) {
if (typeof v === "object" && (v as any)._jsx) {
if (typeof v === "object" && v && (v as any)._jsx) {
const jprop = v as unknown as {
_jsx: true;
fn: (arg: { passprop: any; meta: IMeta }) => ReactNode;
@ -69,7 +68,6 @@ export const viEvalScript = (
const js = meta.item.adv?.jsBuilt || "";
const src = replaceWithObject(js, replacement) || "";
const fn = new Function(
...Object.keys(arg),
`// ${meta.item.name}: ${meta.item.id}
@ -78,7 +76,7 @@ ${src}
);
fn(...Object.values(arg));
updatePropScope(meta, passprop);
updatePropScope(vi, meta, passprop);
};
const JsxProp: FC<{

View File

@ -4,9 +4,14 @@ import { updatePropScope } from "./eval-prop";
import { modifyChild } from "./passprop";
export const createViLocal = (
metas: Record<string, IMeta>,
meta: IMeta,
init_local_effect: any
vi: {
site: { db: any; api: any };
meta: Record<string, IMeta>;
script?: {
init_local_effect: any;
};
},
meta: IMeta
) => {
return <T extends Record<string, any>>(arg: {
children: ReactNode;
@ -16,11 +21,13 @@ export const createViLocal = (
effect?: (local: T) => void | Promise<void>;
}) => {
const { children } = arg;
const init_local_effect = vi.script?.init_local_effect;
const metas = vi.meta;
const ref = useRef<any>(arg.value);
const local = ref.current;
local.render = meta.render;
updatePropScope(meta, meta.script?.passprop);
updatePropScope(vi, meta, meta.script?.passprop);
if (arg.hook) {
arg.hook(local);

View File

@ -1,4 +1,5 @@
import { g } from "utils/global";
import { gzipAsync } from "../../../app/srv/ws/sync/entity/zlib";
export const _ = {
url: "/_proxy/*",
@ -20,6 +21,21 @@ export const _ = {
headers: arg.headers,
}
);
return res as any;
let body: any = null;
const headers: any = {};
res.headers.forEach((v, k) => {
headers[k] = v;
});
body = await res.arrayBuffer();
if (headers["content-encoding"] === "gzip") {
body = await gzipAsync(new Uint8Array(body));
} else {
delete headers["content-encoding"];
}
return new Response(body, { headers });
},
};