This commit is contained in:
Rizky
2024-02-14 21:40:12 +07:00
parent 2e6c0de9d5
commit 3b553b8349
10 changed files with 264 additions and 134 deletions
+15
View File
@@ -0,0 +1,15 @@
export const simpleHash = (str: string, seed = 0) => {
let h1 = 0xdeadbeef ^ seed,
h2 = 0x41c6ce57 ^ seed;
for (let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i);
h1 = Math.imul(h1 ^ ch, 2654435761);
h2 = Math.imul(h2 ^ ch, 1597334677);
}
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);
return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString();
};
+23 -2
View File
@@ -1,8 +1,9 @@
import { dirAsync, read } from "fs-jetpack";
import { dirAsync, read, removeAsync, writeAsync } from "fs-jetpack";
import { dir } from "./dir";
import { g } from "./global";
import { gunzipAsync } from "./gzip";
import { createRouter } from "radix3";
import { prodIndex } from "./prod-index";
const decoder = new TextDecoder();
export const deploy = {
@@ -29,6 +30,8 @@ export const deploy = {
)
);
g.deploy.index = prodIndex(this.config.site_id);
if (g.deploy.gz) {
for (const page of g.deploy.gz.layouts) {
if (page.is_default_layout) {
@@ -41,7 +44,9 @@ export const deploy = {
}
g.deploy.router = createRouter();
g.deploy.pages = {};
for (const page of g.deploy.gz.pages) {
g.deploy.pages[page.id] = page;
g.deploy.router.insert(page.url, page);
}
@@ -49,6 +54,19 @@ export const deploy = {
for (const comp of g.deploy.gz.comps) {
g.deploy.comps[comp.id] = comp.content_tree;
}
if (g.deploy.gz.code.server) {
setTimeout(async () => {
if (g.deploy.gz) {
delete require.cache[dir(`app/web/server/index.js`)];
await removeAsync(dir(`app/web/server`));
await dirAsync(dir(`app/web/server`));
for (const [k, v] of Object.entries(g.deploy.gz.code.server)) {
await writeAsync(dir(`app/web/server/${k}`), v);
}
}
}, 300);
}
}
} catch (e) {
console.log("Failed to load site", this.config.site_id);
@@ -82,11 +100,14 @@ export const deploy = {
g.deploy = {
comps: {},
layout: null,
pages: {},
router: createRouter(),
config: { deploy: { ts: "" }, site_id: "" },
init: false,
raw: null,
gz: null as any,
gz: null,
server: null,
index: null,
};
}
+25 -2
View File
@@ -1,10 +1,11 @@
import { Server } from "bun";
import { Server, WebSocketHandler } from "bun";
import { Logger } from "pino";
import { RadixRouter } from "radix3";
import { PrismaClient } from "../../app/db/db";
import admin from "firebase-admin";
import { Database } from "bun:sqlite";
import { prodIndex } from "./prod-index";
type SingleRoute = {
url: string;
@@ -13,6 +14,18 @@ type SingleRoute = {
path: string;
};
type PrasiServer = {
ws?: WebSocketHandler<{ url: string }>;
http: (arg: {
url: { raw: URL; pathname: string };
req: Request;
server: Server;
mode: "dev" | "prod";
handle: (req: Request) => Promise<Response>;
index: { head: string[]; body: string[]; render: () => string };
}) => Promise<Response>;
};
export const g = global as unknown as {
db: PrismaClient;
dburl: string;
@@ -39,12 +52,20 @@ export const g = global as unknown as {
js: string;
etag: string;
};
cache: {
br: Record<string, Uint8Array>;
br_timeout: Set<string>;
};
deploy: {
init: boolean;
raw: any;
router: RadixRouter<{ url: string; id: string }>;
router?: RadixRouter<{ url: string; id: string }>;
layout: null | any;
comps: Record<string, any>;
pages: Record<
string,
{ id: string; url: string; name: true; content_tree: any }
>;
gz: null | {
layouts: {
id: string;
@@ -66,5 +87,7 @@ export const g = global as unknown as {
site_id: string;
deploy: { ts: string };
};
server: PrasiServer | null;
index: ReturnType<typeof prodIndex> | null;
};
};
+29
View File
@@ -0,0 +1,29 @@
export const prodIndex = (site_id: string) => {
return {
head: [] as string[],
body: [] as string[],
render() {
return `\
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0, user-scalable=1.0, minimum-scale=1.0, maximum-scale=1.0">
<link rel="stylesheet" href="/index.css">
${this.head.join("\n")}
</head>
<body class="flex-col flex-1 w-full min-h-screen flex opacity-0">
${this.body.join("\n")}
<div id="root"></div>
<script>
window._prasi = { basepath: "/", site_id: "${site_id}" }
</script>
<script src="/main.js" type="module"></script>
</body>
</html>`;
},
};
};
+23
View File
@@ -0,0 +1,23 @@
export function humanFileSize(bytes: any, si = false, dp = 1) {
const thresh = si ? 1000 : 1024;
if (Math.abs(bytes) < thresh) {
return bytes + " B";
}
const units = si
? ["kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
: ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
let u = -1;
const r = 10 ** dp;
do {
bytes /= thresh;
++u;
} while (
Math.round(Math.abs(bytes) * r) / r >= thresh &&
u < units.length - 1
);
return bytes.toFixed(dp) + " " + units[u];
}