refactor(web): migrate to Bun and TypeScript with README screenshots

This commit is contained in:
Loki 2026-10-24 15:34:41 +02:00
parent 87372041dd
commit d6a9745df0
Signed by: boba
GPG key ID: 253067914055423B
28 changed files with 3152 additions and 386 deletions

18
scripts/browser.ts Normal file
View file

@ -0,0 +1,18 @@
import { fileURLToPath } from "node:url";
export async function browserBundle(minify = false): Promise<Blob> {
const result = await Bun.build({
entrypoints: [fileURLToPath(new URL("../src/app.ts", import.meta.url))],
target: "browser",
format: "esm",
minify,
sourcemap: "none",
});
if (!result.success || !result.outputs[0]) {
throw new AggregateError(
result.logs,
"Browser TypeScript compilation failed",
);
}
return result.outputs[0];
}

View file

@ -1,17 +0,0 @@
import { cp, mkdir, rm, chmod, readdir } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
const root = fileURLToPath(new URL('../', import.meta.url));
await rm(`${root}dist`, { recursive: true, force: true });
await mkdir(`${root}dist`, { recursive: true });
await cp(`${root}public`, `${root}dist`, { recursive: true });
await cp(`${root}src`, `${root}dist/src`, { recursive: true });
async function permissions(directory) {
await chmod(directory, 0o755);
for (const entry of await readdir(directory, { withFileTypes: true })) {
const path = `${directory}/${entry.name}`;
if (entry.isDirectory()) await permissions(path);
else await chmod(path, 0o644);
}
}
await permissions(`${root}dist`);
console.log('Built dependency-free static website in dist/');

21
scripts/build.ts Normal file
View file

@ -0,0 +1,21 @@
import { cp, mkdir, rm, chmod, readdir } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import { browserBundle } from "./browser";
const root = fileURLToPath(new URL("../", import.meta.url));
await rm(`${root}dist`, { recursive: true, force: true });
await mkdir(`${root}dist/src`, { recursive: true });
await cp(`${root}public`, `${root}dist`, { recursive: true });
await cp(`${root}src/styles.css`, `${root}dist/src/styles.css`);
await Bun.write(`${root}dist/src/app.js`, await browserBundle(true));
async function permissions(directory: string): Promise<void> {
await chmod(directory, 0o755);
for (const entry of await readdir(directory, { withFileTypes: true })) {
const path = `${directory}/${entry.name}`;
if (entry.isDirectory()) await permissions(path);
else await chmod(path, 0o644);
}
}
await permissions(`${root}dist`);
console.log("Built TypeScript browser bundle and static website in dist/");

View file

@ -1,29 +0,0 @@
import { createServer } from 'node:http';
import { readFile, stat } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { extname, resolve, sep } from 'node:path';
const root = fileURLToPath(new URL('../', import.meta.url));
const production = process.argv.includes('--dist');
const port = Number(process.env.PORT ?? 4176);
const types = {'.html':'text/html; charset=utf-8','.js':'text/javascript; charset=utf-8','.css':'text/css; charset=utf-8','.svg':'image/svg+xml','.png':'image/png','.json':'application/json'};
const server = createServer(async (request,response) => {
try {
const path = decodeURIComponent(new URL(request.url,'http://localhost').pathname);
if (request.method!=='GET' && request.method!=='HEAD') { response.writeHead(405);response.end();return; }
if (path.startsWith('/v1/') || ['/health','/ready','/version'].includes(path)) {
response.writeHead(503,{'content-type':'application/json','cache-control':'no-store'});
response.end(JSON.stringify({error:'preview_has_no_backend'}));return;
}
const sourcePath = !production && path.startsWith('/src/');
const base = production ? resolve(root,'dist') : sourcePath ? resolve(root,'src') : resolve(root,'public');
let file = resolve(base, sourcePath ? path.slice('/src/'.length) : `.${path}`);
if (path==='/' || path==='/account' || path==='/account/') file=resolve(base,'index.html');
if (!file.startsWith(base+sep)) { response.writeHead(403);response.end();return; }
const info=await stat(file);
if (!info.isFile()) throw new Error('not_file');
response.writeHead(200,{'content-type':types[extname(file)]??'application/octet-stream','cache-control':'no-store','x-content-type-options':'nosniff'});
response.end(request.method==='HEAD'?undefined:await readFile(file));
} catch { response.writeHead(404,{'content-type':'text/plain'});response.end('Not found'); }
});
server.listen(port,'127.0.0.1',()=>console.log(`VNOX Web: http://127.0.0.1:${port}`));
for (const signal of ['SIGINT','SIGTERM']) process.on(signal,()=>server.close(()=>process.exit(0)));

74
scripts/serve.ts Normal file
View file

@ -0,0 +1,74 @@
import { stat } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import { extname, resolve, sep } from "node:path";
import { browserBundle } from "./browser";
const root = fileURLToPath(new URL("../", import.meta.url));
const production = Bun.argv.includes("--dist");
const port = Number(Bun.env.PORT ?? 4176);
const types: Record<string, string> = {
".html": "text/html; charset=utf-8",
".js": "text/javascript; charset=utf-8",
".css": "text/css; charset=utf-8",
".svg": "image/svg+xml",
".png": "image/png",
};
const headers = {
"cache-control": "no-store",
"x-content-type-options": "nosniff",
};
const server = Bun.serve({
port,
hostname: "127.0.0.1",
async fetch(request): Promise<Response> {
try {
const path = decodeURIComponent(new URL(request.url).pathname);
if (request.method !== "GET" && request.method !== "HEAD")
return new Response(null, { status: 405 });
if (path.split("/").includes(".."))
return new Response(null, { status: 403 });
if (
path.startsWith("/v1/") ||
["/health", "/ready", "/version"].includes(path)
) {
return Response.json(
{ error: "preview_has_no_backend" },
{ status: 503, headers },
);
}
if (!production && path === "/src/app.js") {
const bundle = await browserBundle();
return new Response(request.method === "HEAD" ? null : bundle, {
headers: { ...headers, "content-type": types[".js"]! },
});
}
const sourceStyle = !production && path === "/src/styles.css";
const base = production
? resolve(root, "dist")
: sourceStyle
? resolve(root, "src")
: resolve(root, "public");
let file = resolve(base, sourceStyle ? "styles.css" : `.${path}`);
if (path === "/" || path === "/account" || path === "/account/")
file = resolve(base, "index.html");
if (!file.startsWith(base + sep))
return new Response(null, { status: 403 });
if (!(await stat(file)).isFile())
return new Response("Not found", { status: 404 });
return new Response(request.method === "HEAD" ? null : Bun.file(file), {
headers: {
...headers,
"content-type": types[extname(file)] ?? "application/octet-stream",
},
});
} catch {
return new Response("Not found", { status: 404, headers });
}
},
});
console.log(`VNOX Web: http://127.0.0.1:${server.port}`);
for (const signal of ["SIGINT", "SIGTERM"] as const)
process.on(signal, () => {
void server.stop(true).then(() => process.exit(0));
});