2023-06-06 12:50:50 -04:00
|
|
|
import * as MIME from "https://deno.land/std@0.180.0/media_types/mod.ts";
|
2023-06-06 22:24:43 -04:00
|
|
|
import * as SWCW from "https://esm.sh/@swc/wasm-web@1.3.62";
|
2024-05-15 09:19:10 -04:00
|
|
|
import SSR from "./bundled/preact/ssr.mjs";
|
2024-05-05 16:03:00 -04:00
|
|
|
import { HuntConfig, Root } from "./checker.tsx";
|
2023-07-30 13:04:26 -04:00
|
|
|
import CustomServe from ">able/api.tsx";
|
2023-07-29 18:21:59 -04:00
|
|
|
|
2023-06-07 23:35:00 -04:00
|
|
|
type DenoConfig = {imports:Record<string, string>};
|
|
|
|
const ImportMap:DenoConfig = {imports:{}};
|
2023-08-12 20:30:44 -04:00
|
|
|
|
2023-06-07 14:15:57 -04:00
|
|
|
const ImportMapReload =async()=>
|
|
|
|
{
|
2024-05-05 16:03:00 -04:00
|
|
|
const [, {json}] = await HuntConfig();
|
2023-08-12 20:30:44 -04:00
|
|
|
const imports = (json as DenoConfig).imports;
|
2023-06-20 22:56:20 -04:00
|
|
|
|
2023-08-12 20:30:44 -04:00
|
|
|
if(imports)
|
2023-06-20 22:56:20 -04:00
|
|
|
{
|
2023-08-12 20:30:44 -04:00
|
|
|
Object.entries(imports).forEach(([key, value])=>
|
2023-07-29 13:57:44 -04:00
|
|
|
{
|
2024-05-05 16:03:00 -04:00
|
|
|
// re-write deno project-relative paths (e.g. "./app.tsx") to root-relative for the browser ("/app.tsx");
|
2023-07-29 16:45:41 -04:00
|
|
|
if(value.startsWith("./"))
|
|
|
|
{
|
2023-08-12 20:30:44 -04:00
|
|
|
imports[key] = value.substring(1);
|
2023-07-29 16:45:41 -04:00
|
|
|
}
|
2024-05-05 16:03:00 -04:00
|
|
|
|
|
|
|
if(key.startsWith(">") && key.endsWith("/"))
|
2023-07-29 16:45:41 -04:00
|
|
|
{
|
2024-05-05 16:03:00 -04:00
|
|
|
imports[key] = "/"+key;
|
2023-07-29 16:45:41 -04:00
|
|
|
}
|
2023-08-12 20:30:44 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
ImportMap.imports = Configuration.Remap(imports, Configuration);
|
|
|
|
}
|
2023-07-17 22:41:22 -04:00
|
|
|
|
2023-06-07 14:15:57 -04:00
|
|
|
};
|
|
|
|
|
2023-07-30 09:16:17 -04:00
|
|
|
export type CustomHTTPHandler = (inReq:Request, inURL:URL, inExt:string|false, inMap:{imports:Record<string, string>}, inConfig:Configuration)=>void|false|Response|Promise<Response|void|false>;
|
|
|
|
export type CustomRemapper = (inImports:Record<string, string>, inConfig:Configuration)=>Record<string, string>;
|
2024-05-14 10:40:52 -04:00
|
|
|
export type Configuration = {Start:string, Allow:string, Reset:string, SWCOp:SWCW.Options, Serve:CustomHTTPHandler, Extra:CustomHTTPHandler, Remap:CustomRemapper};
|
2023-07-30 09:16:17 -04:00
|
|
|
export type ConfigurationArgs = Partial<Configuration>;
|
2023-06-07 17:09:40 -04:00
|
|
|
let Configuration:Configuration =
|
2023-06-06 12:50:50 -04:00
|
|
|
{
|
2023-07-30 13:04:26 -04:00
|
|
|
Start: ">able/app.tsx",
|
2023-06-06 12:50:50 -04:00
|
|
|
Allow: "*",
|
2023-06-06 17:22:14 -04:00
|
|
|
Reset: "/clear-cache",
|
2023-07-30 09:16:17 -04:00
|
|
|
async Extra(inReq, inURL, inExt, inMap, inConfig){},
|
2023-07-30 13:04:26 -04:00
|
|
|
Serve: CustomServe,
|
2023-06-21 21:20:24 -04:00
|
|
|
Remap: (inImports, inConfig)=>
|
2023-06-07 14:15:57 -04:00
|
|
|
{
|
|
|
|
return inImports;
|
|
|
|
},
|
2023-06-07 11:22:49 -04:00
|
|
|
SWCOp:
|
2023-06-06 22:24:43 -04:00
|
|
|
{
|
|
|
|
sourceMaps: false,
|
|
|
|
minify: true,
|
|
|
|
jsc:
|
|
|
|
{
|
2023-06-20 09:50:14 -04:00
|
|
|
target:"es2022",
|
2023-06-06 22:24:43 -04:00
|
|
|
minify:
|
|
|
|
{
|
|
|
|
compress: { unused: true },
|
|
|
|
mangle: true
|
|
|
|
},
|
|
|
|
parser:
|
|
|
|
{
|
|
|
|
syntax: "typescript",
|
|
|
|
tsx: true,
|
|
|
|
},
|
|
|
|
transform:
|
|
|
|
{
|
|
|
|
react: { runtime: "automatic" }
|
|
|
|
}
|
2023-06-07 11:22:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const Transpile =
|
|
|
|
{
|
2023-06-06 22:24:43 -04:00
|
|
|
Cache: new Map() as Map<string, string>,
|
2023-06-07 11:22:49 -04:00
|
|
|
Files: ["tsx", "jsx", "ts", "js", "mjs"],
|
|
|
|
Check(inExtension:string|false)
|
|
|
|
{
|
|
|
|
return inExtension ? this.Files.includes(inExtension) : false;
|
|
|
|
},
|
2023-06-06 22:24:43 -04:00
|
|
|
Clear()
|
|
|
|
{
|
|
|
|
const size = this.Cache.size;
|
|
|
|
this.Cache.clear();
|
2023-06-07 14:15:57 -04:00
|
|
|
ImportMapReload();
|
2023-06-06 22:24:43 -04:00
|
|
|
return size;
|
|
|
|
},
|
2023-07-03 22:27:35 -04:00
|
|
|
async Fetch(inPath:string, inKey:string, inCheckCache=true)
|
2023-06-06 22:24:43 -04:00
|
|
|
{
|
2023-06-07 11:22:49 -04:00
|
|
|
const check = this.Cache.get(inPath);
|
|
|
|
if(check && inCheckCache)
|
2023-06-06 22:24:43 -04:00
|
|
|
{
|
2023-06-07 11:22:49 -04:00
|
|
|
return check;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
try
|
2023-06-06 22:24:43 -04:00
|
|
|
{
|
2023-06-07 11:22:49 -04:00
|
|
|
const resp = await fetch(inPath);
|
|
|
|
const text = await resp.text();
|
2023-06-07 17:09:40 -04:00
|
|
|
const {code} = await SWCW.transform(text, { ...Configuration.SWCOp, filename:inKey});
|
2023-06-07 11:22:49 -04:00
|
|
|
this.Cache.set(inKey, code);
|
|
|
|
return code;
|
2023-06-06 22:24:43 -04:00
|
|
|
}
|
2023-06-07 11:22:49 -04:00
|
|
|
catch(e)
|
2023-06-06 22:24:43 -04:00
|
|
|
{
|
2023-06-07 23:35:00 -04:00
|
|
|
console.log(`Transpile.Fetch error. Key:"${inKey}" Path:"${inPath}" Error:"${e}"`);
|
2023-06-07 11:22:49 -04:00
|
|
|
return null;
|
2023-06-06 22:24:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2023-06-06 12:50:50 -04:00
|
|
|
|
2023-06-07 11:22:49 -04:00
|
|
|
export const Extension =(inPath:string)=>
|
|
|
|
{
|
|
|
|
const posSlash = inPath.lastIndexOf("/");
|
|
|
|
const posDot = inPath.lastIndexOf(".");
|
|
|
|
return posDot > posSlash ? inPath.substring(posDot+1).toLowerCase() : false;
|
|
|
|
};
|
|
|
|
|
2023-06-15 17:13:51 -04:00
|
|
|
export const Configure =(config:ConfigurationArgs)=>
|
|
|
|
{
|
|
|
|
Configuration = {...Configuration, ...config};
|
|
|
|
ImportMapReload();
|
|
|
|
}
|
2023-06-07 14:15:57 -04:00
|
|
|
|
2023-07-30 13:29:15 -04:00
|
|
|
|
|
|
|
let running = false;
|
2023-07-30 13:04:26 -04:00
|
|
|
export default async()=>
|
2023-07-29 17:13:49 -04:00
|
|
|
{
|
2023-07-30 13:29:15 -04:00
|
|
|
if(running){return};
|
|
|
|
running = true;
|
|
|
|
|
2023-07-30 13:04:26 -04:00
|
|
|
try
|
2023-06-06 12:50:50 -04:00
|
|
|
{
|
2023-09-24 09:19:50 -04:00
|
|
|
await ImportMapReload();
|
2023-07-30 13:04:26 -04:00
|
|
|
await SWCW.default();
|
2023-06-06 12:50:50 -04:00
|
|
|
}
|
2023-07-30 13:04:26 -04:00
|
|
|
catch(e)
|
2023-07-29 13:57:44 -04:00
|
|
|
{
|
2023-07-30 13:04:26 -04:00
|
|
|
console.log("swc init error:", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
const server = Deno.serve({port:parseInt(Deno.env.get("port")||"8000")}, async(req: Request)=>
|
|
|
|
{
|
|
|
|
const url:URL = new URL(req.url);
|
|
|
|
const ext = Extension(url.pathname);
|
|
|
|
const headers = {"content-type":"application/json", "Access-Control-Allow-Origin": Configuration.Allow, "charset":"UTF-8"};
|
|
|
|
let proxy = Root + url.pathname;
|
|
|
|
|
|
|
|
if(url.pathname.includes("__/") || url.pathname.lastIndexOf("__.") > -1)
|
|
|
|
{
|
|
|
|
return new Response(`{"error":"unmatched route", "path":"${url.pathname}"}`, {status:404, headers});
|
|
|
|
}
|
|
|
|
|
|
|
|
// proxy imports
|
|
|
|
if(url.pathname.startsWith(encodeURI("/>")))
|
2023-07-29 13:57:44 -04:00
|
|
|
{
|
2024-05-05 16:03:00 -04:00
|
|
|
/** pathname with no leading slash */
|
|
|
|
const clippedPath = decodeURI(url.pathname.substring(1));
|
|
|
|
proxy = import.meta.resolve(clippedPath);
|
2023-07-29 13:57:44 -04:00
|
|
|
}
|
2023-07-30 13:04:26 -04:00
|
|
|
|
|
|
|
// allow for custom handlers
|
|
|
|
const custom = await Configuration.Extra(req, url, ext, ImportMap, Configuration);
|
|
|
|
if(custom)
|
2023-07-29 13:57:44 -04:00
|
|
|
{
|
2023-07-30 13:04:26 -04:00
|
|
|
return custom;
|
|
|
|
}
|
|
|
|
const api = await Configuration.Serve(req, url, ext, ImportMap, Configuration);
|
|
|
|
if(api)
|
2023-06-20 21:40:49 -04:00
|
|
|
{
|
2023-07-30 13:04:26 -04:00
|
|
|
return api;
|
|
|
|
}
|
|
|
|
|
|
|
|
// transpileable files
|
2024-05-15 09:19:10 -04:00
|
|
|
if(Transpile.Check(ext) && !url.pathname.startsWith("/bundled/"))
|
2023-06-08 23:19:03 -04:00
|
|
|
{
|
2023-07-30 13:04:26 -04:00
|
|
|
const code = await Transpile.Fetch(proxy, url.pathname);
|
|
|
|
if(code)
|
|
|
|
{
|
|
|
|
return new Response(code, {headers:{...headers, "content-type":"application/javascript"}} );
|
|
|
|
}
|
2023-06-08 23:19:03 -04:00
|
|
|
}
|
2023-07-30 13:04:26 -04:00
|
|
|
|
|
|
|
// custom page html
|
|
|
|
if(!ext)
|
2023-06-07 14:15:57 -04:00
|
|
|
{
|
2024-05-14 10:40:52 -04:00
|
|
|
let markup = "";
|
|
|
|
let styles = "";
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const User = await import("./run-browser.tsx");
|
|
|
|
markup = SSR(<User.CustomApp/>);
|
|
|
|
const {css, html} = User.CustomTwindExtractor(markup);
|
|
|
|
markup = html;
|
|
|
|
styles = css;
|
|
|
|
}
|
|
|
|
catch(e)
|
2023-07-30 13:04:26 -04:00
|
|
|
{
|
2024-05-14 10:40:52 -04:00
|
|
|
console.log("SSR error:")
|
|
|
|
console.log(e);
|
2023-07-30 13:04:26 -04:00
|
|
|
}
|
2024-05-14 10:40:52 -04:00
|
|
|
return new Response(
|
|
|
|
`<!doctype html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
|
|
|
|
<meta charset="utf-8"/>
|
|
|
|
<style>${styles}</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="app">${markup}</div>
|
|
|
|
<script type="importmap">${JSON.stringify(ImportMap, null, " ")}</script>
|
|
|
|
<script type="module">
|
|
|
|
import Mount from ">able/run-browser.tsx";
|
2024-05-15 09:19:10 -04:00
|
|
|
Mount("#app");
|
2024-05-14 10:40:52 -04:00
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>`, {status:200, headers:{"content-type":"text/html"}});
|
2023-06-07 14:15:57 -04:00
|
|
|
}
|
2023-07-30 13:04:26 -04:00
|
|
|
|
|
|
|
// cache-reset route
|
|
|
|
if(url.pathname === Configuration.Reset)
|
2023-06-07 14:15:57 -04:00
|
|
|
{
|
2023-07-30 13:04:26 -04:00
|
|
|
return new Response(`{"cleared":${Transpile.Clear()}}`, {headers});
|
2023-06-07 14:15:57 -04:00
|
|
|
}
|
2023-07-30 13:04:26 -04:00
|
|
|
|
|
|
|
// all other static files
|
|
|
|
if(ext)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const type = MIME.typeByExtension(ext);
|
|
|
|
const file = await fetch(proxy);
|
|
|
|
return new Response(file.body, {headers:{...headers, "content-type":type||""}});
|
|
|
|
}
|
|
|
|
catch(e)
|
|
|
|
{
|
|
|
|
return new Response(`{"error":"${e}", "path":"${url.pathname}"}`, {status:404, headers});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Response(`{"error":"unmatched route", "path":"${url.pathname}"}`, {status:404, headers});
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|