Something went wrong. Try again.
A simple DAV sync application / cli
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071import { readFile } from "node:fs/promises";import { createServer, type Server } from "node:http";import { stripTypeScriptTypes } from "node:module";import { extname, join, resolve, sep } from "node:path";import { fileURLToPath } from "node:url";
// Resolved from the app's own root, so it works the same from a checkout and// from inside a mounted bundle.const APP_ROOT = fileURLToPath(new URL("../../", import.meta.url));const PUBLIC_DIR = join(APP_ROOT, "public");const WEB_DIR = join(APP_ROOT, "web");
const MIME_TYPES: Record<string, string> = { ".html": "text/html; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".ts": "text/javascript; charset=utf-8", // served with its types stripped ".css": "text/css; charset=utf-8", ".svg": "image/svg+xml", ".png": "image/png",};
/** * Serves the setup page on a random loopback port. Nothing is bundled: * - `/`: public/index.html * - `/web/*`: the page's modules; TypeScript has its types stripped by node * - everything else: `public/` * The page talks to node through the window's CDP bridge, not over HTTP, so * this server has nothing to say about folders or credentials. */export async function startServer(): Promise<{ server: Server; url: string }> { const server = createServer(async (req, res) => { const pathname = decodeURIComponent(new URL(req.url ?? "/", "http://localhost").pathname); try { let body: string | Uint8Array; let path: string; if (pathname === "/" || pathname === "/index.html") { path = join(PUBLIC_DIR, "index.html"); body = await readFile(path); } else if (pathname.startsWith("/web/")) { path = withinDir(WEB_DIR, pathname.slice("/web/".length)); body = extname(path) === ".ts" ? stripTypeScriptTypes(await readFile(path, "utf8")) : await readFile(path); } else { path = withinDir(PUBLIC_DIR, pathname.slice(1)); body = await readFile(path); } res.writeHead(200, { "Content-Type": MIME_TYPES[extname(path)] ?? "application/octet-stream", "Cache-Control": "no-store" }); res.end(body); } catch (error) { const code = (error as NodeJS.ErrnoException).code; if (code === "ENOENT" || code === "EISDIR") res.writeHead(404).end(); else if (code === "OUTSIDE") res.writeHead(403).end(); else { console.error(error); res.writeHead(500, { "Content-Type": "text/plain" }).end(String(error)); } } });
await new Promise<void>((done) => server.listen(0, "127.0.0.1", done)); const address = server.address(); if (address === null || typeof address === "string") throw new Error("Unexpected server address"); return { server, url: `http://127.0.0.1:${address.port}/` };}
function withinDir(dir: string, path: string): string { const root = resolve(dir); const absolute = resolve(root, path); if (!absolute.startsWith(root + sep)) throw Object.assign(new Error("Outside served directory"), { code: "OUTSIDE" }); return absolute;}