This commit is contained in:
Rizky
2024-02-13 20:32:27 +07:00
parent e30b666e81
commit a71b32a9a8
15 changed files with 183 additions and 226 deletions
+14 -6
View File
@@ -109,10 +109,18 @@ export const createResponse = (
return res;
};
function simpleHash(str: string) {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = ((hash << 5) - hash + str.charCodeAt(i)) | 0;
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);
}
return (hash >>> 0).toString(36);
}
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();
};
+5 -4
View File
@@ -1,13 +1,11 @@
import { file } from "bun";
import { inspectAsync, listAsync } from "fs-jetpack";
import { join } from "path";
import { createRouter } from "radix3";
import { dir } from "../utils/dir";
import { g } from "../utils/global";
import { parseArgs } from "./parse-args";
import { serveAPI } from "./serve-api";
import { serveWeb } from "./serve-web";
import { dir } from "../utils/dir";
import { file } from "bun";
import { trim } from "radash";
export const createServer = async () => {
g.router = createRouter({ strictTrailingSlash: true });
@@ -72,6 +70,9 @@ export const createServer = async () => {
});
};
if (g.deploy.gz?.code.server) {
}
return handle(req);
},
});
-35
View File
@@ -1,40 +1,5 @@
import { file } from "bun";
import { $ } from "execa";
import {
dirAsync,
existsAsync,
inspectTreeAsync,
readAsync,
removeAsync,
writeAsync,
} from "fs-jetpack";
import { gunzipSync } from "zlib";
import { downloadFile } from "../api/_deploy";
import { dir } from "../utils/dir";
import { g } from "../utils/global";
const decoder = new TextDecoder();
export const loadWeb = async () => {
await dirAsync(dir(`app/static`));
};
export const loadWebCache = async (site_id: string, ts: number | string) => {
const web = g.web;
if (web) {
const path = dir(`app/web/deploys/${ts}`);
if (await existsAsync(path)) {
const fileContent = await readAsync(path, "buffer");
if (fileContent) {
console.log(
`Loading site ${site_id}: ${humanFileSize(fileContent.byteLength)}`
);
const res = gunzipSync(fileContent);
}
}
}
};
function humanFileSize(bytes: any, si = false, dp = 1) {
const thresh = si ? 1000 : 1024;
+1 -55
View File
@@ -2,62 +2,8 @@ import { statSync } from "fs";
import { join } from "path";
import { dir } from "utils/dir";
const index = {
html: "",
css: {
src: null as any,
encoding: "",
},
isFile: {} as Record<string, boolean>,
};
export const serveWeb = async (url: URL, req: Request) => {
let site_id = "";
if (!site_id) {
return false;
}
const base = dir(`app/static/site`);
let path = join(base, url.pathname);
if (url.pathname === "/site_id") {
return new Response(site_id);
}
if (url.pathname.startsWith("/index.css")) {
if (!index.css.src) {
const res = await fetch("https://prasi.app/index.css");
index.css.src = await res.arrayBuffer();
index.css.encoding = res.headers.get("content-encoding") || "";
}
return new Response(index.css.src, {
headers: {
"content-type": "text/css",
"content-encoding": index.css.encoding,
},
});
}
try {
if (typeof index.isFile[path] === "undefined") {
const s = statSync(path);
if (s.isFile()) {
index.isFile[path] = true;
return new Response(Bun.file(path));
}
} else if (index.isFile[path]) {
return new Response(Bun.file(path));
}
} catch (e) {}
if (!index.html) {
index.html = generateIndexHtml("", site_id);
}
return { site_id, index: index.html };
return {};
};
export const generateIndexHtml = (base_url: string, site_id: string) => {