This commit is contained in:
Rizky 2024-02-03 21:30:33 +07:00
parent 2a5e0164a4
commit d335510e69
13 changed files with 152 additions and 117 deletions

View File

@ -1 +1,3 @@
u Œ“Tõ5Jk¿“ŸÖË~ɪ¼2<C2BC>Òõ<><C3B5>R€º<E282AC>´ÖñÞRvÃl³p$ƒq§Û<þ æœÅ`aúŠH=(øX9 F²,$´©ï<EFBFBD>­%ïú@0ìÅØ>OºÍ#îýHò«'öVìààGzàŽÝÈe$cý@™¾týÌ«c^ùj1Úd%Q¸š±ôˆPT :ÒüoTeˆÂ¤×Ù$<24>i Æ ŒÔ`Íž±®Tw®³B¬Â¡`y 9ûÿc3¦˜u!k
?AÄ®Óv=þ®Hg$YH†ø…" =˜2àYÑ
4V)á‹mŠ;Dk…]/LuÁ·Gri¯+õtº$ù/}o¥èü <0F>±(»”é§Ýø"ûãveñ$ñj±_äBŹG}GÒC«JÒÿ•Y7?¯ºÎLU/jìÞjêþÔNûu ×m\¸^äÆ?F_V76S

View File

@ -11,6 +11,7 @@
<body className="flex-col flex-1 w-full min-h-screen flex opacity-0"> <body className="flex-col flex-1 w-full min-h-screen flex opacity-0">
<div id="root"></div> <div id="root"></div>
<script src="/index.js"></script> <script src="/index.js"></script>
<script>window.__REACT_DEVTOOLS_GLOBAL_HOOK__ = { isDisabled: true };</script>
</body> </body>
</html> </html>

View File

@ -1,7 +1,7 @@
(BigInt.prototype as any).toJSON = function (): string { (BigInt.prototype as any).toJSON = function (): string {
return `BigInt::` + this.toString(); return `BigInt::` + this.toString();
}; };
let w = window; let w = window as any;
export const fetchViaProxy = async ( export const fetchViaProxy = async (
url: string, url: string,
@ -64,7 +64,7 @@ export const fetchViaProxy = async (
return raw; return raw;
} }
} else { } else {
const res = await fetch(`/_proxy`, { const res = await fetch(`${w.basehost ? w.basehost : ""}/_proxy`, {
method: "POST", method: "POST",
body: JSON.stringify([ body: JSON.stringify([
{ {

View File

@ -9,18 +9,17 @@ import { registerMobile } from "./render/live/logic/mobile";
import { sworkerAddCache, sworkerRegister } from "./sworker-boot"; import { sworkerAddCache, sworkerRegister } from "./sworker-boot";
import { w } from "./utils/types/general"; import { w } from "./utils/types/general";
const start = async () => { const start = async () => {
const base = `${location.protocol}//${location.host}`;
let react = { let react = {
root: null as null | ReactRoot, root: null as null | ReactRoot,
}; };
w.mobile = registerMobile(); w.mobile = registerMobile();
const cur = new URL(location.href); const cur = new URL(w.basehost || location.href);
const base_url = `${cur.protocol}//${cur.host}`; const base_url = `${cur.protocol}//${cur.host}`;
w.db = dbProxy(base_url); w.db = dbProxy(base_url);
try { try {
await loadApiProxyDef(base_url, false); await loadApiProxyDef(base_url, false);
w.api = apiProxy(base_url); w.api = apiProxy(base_url);
@ -28,12 +27,12 @@ const start = async () => {
console.warn("Failed to load API:", base_url); console.warn("Failed to load API:", base_url);
} }
w.serverurl = base; w.serverurl = base_url;
sworkerRegister(react); sworkerRegister(react);
defineReact(); defineReact();
await defineWindow(false); await defineWindow(false);
sworkerAddCache(base); sworkerAddCache(base_url);
const el = document.getElementById("root"); const el = document.getElementById("root");

View File

@ -26,9 +26,13 @@ export const edInitSync = (p: PG) => {
location.href = "/login"; location.href = "/login";
return <Loading note="logging in" />; return <Loading note="logging in" />;
} }
if (session?.data?.user) {
p.user.id = session.data.user.id; p.user.id = session.data.user.id;
p.user.username = session.data.user.username; p.user.username = session.data.user.username;
} else {
p.user.id = "ab1390f5-40d5-448e-a8c3-84b0fb600930";
p.user.username = "anonymous";
}
if (!params.page_id) { if (!params.page_id) {
if (location.pathname.startsWith("/vi/")) { if (location.pathname.startsWith("/vi/")) {
p.preview.show_loading = false; p.preview.show_loading = false;
@ -164,7 +168,7 @@ export const edInitSync = (p: PG) => {
} }
if (!p.sync) { if (!p.sync) {
clientStartSync({ clientStartSync({
user_id: session.data.user.id, user_id: p.user.id,
site_id: params.site_id, site_id: params.site_id,
page_id: params.page_id, page_id: params.page_id,
events: { events: {
@ -250,7 +254,7 @@ export const edInitSync = (p: PG) => {
if (res) { if (res) {
Y.applyUpdate(doc, decompress(res.diff), "sv_remote"); Y.applyUpdate(doc, decompress(res.diff), "sv_remote");
if (data.type === "page") { if (data.type === "page") {
delete p.preview.meta_cache[data.id] delete p.preview.meta_cache[data.id];
await treeRebuild(p, { note: "sv_remote" }); await treeRebuild(p, { note: "sv_remote" });
} else if (data.type === "comp") { } else if (data.type === "comp") {
const updated = await updateComponentMeta(p, doc, data.id); const updated = await updateComponentMeta(p, doc, data.id);

View File

@ -1,3 +1,5 @@
import { w } from "../../../../../../utils/types/general";
function toAbsoluteURL(url: string) { function toAbsoluteURL(url: string) {
const a = document.createElement("a"); const a = document.createElement("a");
a.setAttribute("href", url); // <a href="hoge.html"> a.setAttribute("href", url); // <a href="hoge.html">
@ -28,7 +30,7 @@ export function importModule(url: string) {
resolve((window as any)[vector]); resolve((window as any)[vector]);
destructor(); destructor();
}; };
const absURL = toAbsoluteURL(url); const absURL = w.basehost ? w.basehost + url : toAbsoluteURL(url);
const loader = `import * as m from "${absURL}"; window.${vector} = m;`; // export Module const loader = `import * as m from "${absURL}"; window.${vector} = m;`; // export Module
const blob = new Blob([loader], { type: "text/javascript" }); const blob = new Blob([loader], { type: "text/javascript" });
script.src = URL.createObjectURL(blob); script.src = URL.createObjectURL(blob);

View File

@ -1,7 +1,7 @@
import { apiProxy } from "../../../base/load/api/api-proxy"; import { apiProxy } from "../../../base/load/api/api-proxy";
import { loadApiProxyDef } from "../../../base/load/api/api-proxy-def"; import { loadApiProxyDef } from "../../../base/load/api/api-proxy-def";
import { dbProxy } from "../../../base/load/db/db-proxy"; import { dbProxy } from "../../../base/load/db/db-proxy";
import importModule from "../../../render/editor/tools/dynamic-import"; import importModule from "../../ed/panel/side/style/tools/dynamic-import";
import { VG } from "../render/global"; import { VG } from "../render/global";
import { viScriptArg } from "../render/script/arg"; import { viScriptArg } from "../render/script/arg";

View File

@ -150,7 +150,7 @@ const connect = (
if (!conf.ws) { if (!conf.ws) {
let reconnect = 0; let reconnect = 0;
const retry = () => { const retry = () => {
const url = new URL(location.href); const url = new URL(w.basehost || location.href);
url.pathname = "/sync"; url.pathname = "/sync";
url.protocol = url.protocol === "http:" ? "ws:" : "wss:"; url.protocol = url.protocol === "http:" ? "ws:" : "wss:";

View File

@ -23,6 +23,7 @@ export type PrasiAPI = {
export const w = window as unknown as { export const w = window as unknown as {
isEditor: boolean; isEditor: boolean;
isMobile: boolean; isMobile: boolean;
basehost?: string;
basepath: string; basepath: string;
navigateOverride: (s: string) => string; navigateOverride: (s: string) => string;
isDesktop: boolean; isDesktop: boolean;

View File

@ -1,5 +1,5 @@
import { g } from "utils/global";
import { gzipAsync } from "../../../app/srv/ws/sync/entity/zlib"; import { gzipAsync } from "../../../app/srv/ws/sync/entity/zlib";
import { CORS_HEADERS } from "../server/serve-api";
export const _ = { export const _ = {
url: "/_proxy/*", url: "/_proxy/*",
@ -38,6 +38,6 @@ export const _ = {
delete headers["content-encoding"]; delete headers["content-encoding"];
} }
return new Response(body, { headers }); return new Response(body, { headers: { ...headers, ...CORS_HEADERS } });
}, },
}; };

View File

@ -1,4 +1,5 @@
import { g } from "utils/global"; import { g } from "utils/global";
import { CORS_HEADERS } from "../serve-api";
const parseQueryParams = (ctx: any) => { const parseQueryParams = (ctx: any) => {
const pageHref = ctx.req.url; const pageHref = ctx.req.url;
@ -75,5 +76,9 @@ export const createResponse = (existingRes: any, body: any) => {
res.headers.set(key, value); res.headers.set(key, value);
} }
for (const [k, v] of Object.entries(CORS_HEADERS)) {
res.headers.set(k, v);
}
return res; return res;
}; };

View File

@ -2,6 +2,11 @@ import { createRouter } from "radix3";
import { g } from "utils/global"; import { g } from "utils/global";
import { createResponse } from "./api/api-ctx"; import { createResponse } from "./api/api-ctx";
export const CORS_HEADERS = {
"access-control-allow-origin": "*",
"access-control-allow-headers": "content-type",
};
export const serveAPI = { export const serveAPI = {
init: async () => { init: async () => {
g.router = createRouter({ strictTrailingSlash: false }); g.router = createRouter({ strictTrailingSlash: false });
@ -38,6 +43,12 @@ export const serveAPI = {
}); });
if (req.method !== "GET") { if (req.method !== "GET") {
if (req.method === "OPTIONS") {
return new Response("OK", {
headers: CORS_HEADERS,
});
}
if ( if (
!req.headers.get("content-type")?.startsWith("multipart/form-data") !req.headers.get("content-type")?.startsWith("multipart/form-data")
) { ) {
@ -82,6 +93,10 @@ export const serveAPI = {
const finalResponse = await current.fn(...args); const finalResponse = await current.fn(...args);
if (finalResponse instanceof Response) { if (finalResponse instanceof Response) {
for (const [k, v] of Object.entries(CORS_HEADERS)) {
finalResponse.headers.set(k, v);
}
return finalResponse; return finalResponse;
} }
@ -101,6 +116,10 @@ export const serveAPI = {
res.headers.set(k, v); res.headers.set(k, v);
}); });
for (const [k, v] of Object.entries(CORS_HEADERS)) {
res.headers.set(k, v);
}
return res; return res;
} }

View File

@ -3,6 +3,7 @@ import { exists, 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 { watch } from "fs";
import { CORS_HEADERS } from "./serve-api";
import mime from "mime"; import mime from "mime";
import { g } from "utils/global"; import { g } from "utils/global";
@ -30,7 +31,7 @@ export const serveStatic = {
const list = await inspectTreeAsync(dir.path(`app/${web.path}`)); const list = await inspectTreeAsync(dir.path(`app/${web.path}`));
const walk = async ( const walk = async (
list: InspectTreeResult, list: InspectTreeResult,
parent?: InspectTreeResult[], parent?: InspectTreeResult[]
) => { ) => {
if (list.type === "dir") { if (list.type === "dir") {
for (const item of list.children) { for (const item of list.children) {
@ -76,6 +77,7 @@ export const serveStatic = {
if (file) { if (file) {
return new Response(file.content, { return new Response(file.content, {
headers: { headers: {
...CORS_HEADERS,
...{ "content-type": file.type }, ...{ "content-type": file.type },
...(file.compression ? { "content-encoding": file.compression } : {}), ...(file.compression ? { "content-encoding": file.compression } : {}),
}, },