2023-06-06 12:50:50 -04:00
|
|
|
import * as MIME from "https://deno.land/std@0.180.0/media_types/mod.ts";
|
|
|
|
import * as HTTP from "https://deno.land/std@0.177.0/http/server.ts";
|
2023-06-06 22:24:43 -04:00
|
|
|
import * as SWCW from "https://esm.sh/@swc/wasm-web@1.3.62";
|
2023-06-06 12:50:50 -04:00
|
|
|
|
2023-06-07 11:22:49 -04:00
|
|
|
type CustomHTTPHandler = (inReq:Request, inURL:URL, inExtension:string|false)=>false|Response|Promise<Response|false>
|
|
|
|
type Configuration = {Proxy:string, Allow:string, Reset:string, SWCOp:SWCW.Options, Serve:CustomHTTPHandler};
|
|
|
|
type ConfigurationArgs = {Proxy?:string, Allow?:string, Reset?:string, SWCOp?:SWCW.Options, Serve?:CustomHTTPHandler};
|
|
|
|
let Configure:Configuration =
|
2023-06-06 12:50:50 -04:00
|
|
|
{
|
|
|
|
Proxy: "",
|
|
|
|
Allow: "*",
|
2023-06-06 17:22:14 -04:00
|
|
|
Reset: "/clear-cache",
|
2023-06-07 11:22:49 -04:00
|
|
|
Serve: ()=>false,
|
|
|
|
SWCOp:
|
2023-06-06 22:24:43 -04:00
|
|
|
{
|
|
|
|
sourceMaps: false,
|
|
|
|
minify: true,
|
|
|
|
jsc:
|
|
|
|
{
|
|
|
|
target:"es2017",
|
|
|
|
minify:
|
|
|
|
{
|
|
|
|
compress: { unused: true },
|
|
|
|
mangle: true
|
|
|
|
},
|
|
|
|
parser:
|
|
|
|
{
|
|
|
|
syntax: "typescript",
|
|
|
|
tsx: true,
|
|
|
|
},
|
|
|
|
transform:
|
|
|
|
{
|
|
|
|
react: { runtime: "automatic" }
|
|
|
|
}
|
2023-06-07 11:22:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
export default (config:ConfigurationArgs)=> Configure = {...Configure, ...config};
|
|
|
|
|
|
|
|
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();
|
|
|
|
return size;
|
|
|
|
},
|
|
|
|
Fetch: async function(inPath:string, inKey:string, inCheckCache=true)
|
|
|
|
{
|
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();
|
|
|
|
const {code} = await SWCW.transform(text, { ...Configure.SWCOp, filename:inKey});
|
|
|
|
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 11:22:49 -04:00
|
|
|
//console.log(`Transpile.Fetch error. Key:"${inKey}" Path:"${inPath}" Error:"${e}"`);
|
|
|
|
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-06 22:24:43 -04:00
|
|
|
SWCW.default();
|
2023-06-06 12:50:50 -04:00
|
|
|
HTTP.serve(async(req: Request)=>
|
|
|
|
{
|
|
|
|
const url:URL = new URL(req.url);
|
2023-06-07 11:22:49 -04:00
|
|
|
const ext = Extension(url.pathname);
|
|
|
|
const headers = {"content-type":"application/json", "Access-Control-Allow-Origin": Configure.Allow, "charset":"UTF-8"};
|
|
|
|
|
|
|
|
// allow for custom handler
|
|
|
|
const custom = await Configure.Serve(req, url, ext);
|
|
|
|
if(custom)
|
|
|
|
{
|
|
|
|
return custom;
|
|
|
|
}
|
2023-06-06 12:50:50 -04:00
|
|
|
|
2023-06-07 11:22:49 -04:00
|
|
|
// implied index.html files
|
|
|
|
if(!ext)
|
2023-06-06 22:48:45 -04:00
|
|
|
{
|
2023-06-07 11:22:49 -04:00
|
|
|
return new Response(`<!doctype html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<script type="importmap"></script>
|
|
|
|
<script type="module"></script>
|
|
|
|
</body>
|
|
|
|
</html>`, {headers:{...headers, "content-type":"text/html"}});
|
2023-06-06 22:48:45 -04:00
|
|
|
}
|
2023-06-07 11:22:49 -04:00
|
|
|
|
|
|
|
// cache-reset route
|
2023-06-06 12:50:50 -04:00
|
|
|
if(url.pathname === Configure.Reset)
|
|
|
|
{
|
2023-06-07 11:22:49 -04:00
|
|
|
return new Response(`{"cleared":${Transpile.Clear()}}`, {headers});
|
2023-06-06 12:50:50 -04:00
|
|
|
}
|
|
|
|
|
2023-06-07 11:22:49 -04:00
|
|
|
// transpileable files
|
|
|
|
if(Transpile.Check(ext))
|
2023-06-06 12:50:50 -04:00
|
|
|
{
|
2023-06-07 11:22:49 -04:00
|
|
|
const lookup = await Transpile.Fetch(Configure.Proxy + url.pathname, url.pathname);
|
|
|
|
return new Response(lookup, {status:lookup?200:404, headers:{...headers, "content-type":"application/javascript"}} );
|
2023-06-06 12:50:50 -04:00
|
|
|
}
|
2023-06-07 11:22:49 -04:00
|
|
|
|
|
|
|
// all other static files
|
|
|
|
try
|
2023-06-06 12:50:50 -04:00
|
|
|
{
|
2023-06-07 11:22:49 -04:00
|
|
|
const type = MIME.typeByExtension(ext);
|
|
|
|
const file = await fetch(Configure.Proxy + url.pathname);
|
|
|
|
const text = await file.text();
|
|
|
|
return new Response(text, {headers:{...headers, "content-type":type||""}});
|
2023-06-06 12:50:50 -04:00
|
|
|
}
|
2023-06-07 11:22:49 -04:00
|
|
|
catch(e)
|
2023-06-06 12:50:50 -04:00
|
|
|
{
|
2023-06-07 11:22:49 -04:00
|
|
|
return new Response(`{"error":"${e}", "path":"${url.pathname}"}`, {status:404, headers});
|
2023-06-06 12:50:50 -04:00
|
|
|
}
|
2023-06-07 11:22:49 -04:00
|
|
|
|
2023-06-06 12:50:50 -04:00
|
|
|
});
|