// Automated bundle server export async function processRequest( req: Request, hostpath: string, runner = Deno.run, fetcher = fetch, ): Promise { if (req.method == "OPTIONS") { return new Response(undefined, { headers: { "Access-Control-Allow-Origin": "*", }, }); } if (req.method != "GET") { return new Response(undefined, { status: 405 }); } const params = req.url.split("/").filter((x) => !!x).slice(2); if (params.length < 2) { return new Response(undefined, { status: 404 }); } const [lib, version] = params[0].split("@", 2); const file = params.slice(1).join("/"); const branch = version || "master"; const path = `https://${hostpath}/${lib}/raw/${branch}/${file}`; if (!isName(lib) || !isName(branch) || !isPath(file)) { return new Response( `console.error("bundler error - Invalid path ${path}");`, { status: 400 }, ); } if (path.endsWith(".js")) { return await bundle(path.slice(0, -3) + ".ts", runner); } else { return await fetcher(path, { redirect: "follow" }); } } async function bundle( path: string, runner: typeof Deno.run, ): Promise { const process = runner({ cmd: ["deno", "bundle", path], stdout: "piped", }); const output = await process.output(); const status = await process.status(); if (status.success) { return new Response(output, { headers: { "Content-Type": "text/javascript", "Access-Control-Allow-Origin": "*", }, }); } else { return new Response( `console.error("bundler error - Failed to bundle ${path}");`, { status: 500 }, ); } } export async function serveBundles(hostpath: string, port: number) { const listen = { hostname: "0.0.0.0", port }; console.log( `Serving ${hostpath} bundles on ${listen.hostname}:${listen.port} ...`, ); for await (const conn of Deno.listen(listen)) { for await (const req of Deno.serveHttp(conn)) { try { const response = await processRequest(req.request, hostpath); await req.respondWith(response); } catch (err) { console.error(err); await req.respondWith(Response.error()); } } } } function isName(input: string): boolean { return !!input.match(/^[a-zA-Z0-9_\-\.]+$/); } function isPath(input: string): boolean { return !input.split("/").some((part) => !isName(part)); }