more configuration
This commit is contained in:
parent
083e37a173
commit
c758f66b27
66
local.tsx
66
local.tsx
@ -1,19 +1,32 @@
|
||||
import * as HTTP from "https://deno.land/std@0.177.0/http/server.ts";
|
||||
import Setup, {Transpile} from "./serve.tsx";
|
||||
|
||||
const Directory = `file://${Deno.cwd().replaceAll("\\", "/")}`;
|
||||
Transpile.Config.sourceMaps = "inline";
|
||||
Setup({Proxy:Directory});
|
||||
import Setup, {Transpile, Extension} from "./serve.tsx";
|
||||
|
||||
const SocketsLive:Set<WebSocket> = new Set();
|
||||
const SocketsSend =(inData:string)=>{ console.log(inData); for (const socket of SocketsLive){ socket.send(inData); } }
|
||||
HTTP.serve((_req:Request)=>
|
||||
const Directory = `file://${Deno.cwd().replaceAll("\\", "/")}`;
|
||||
|
||||
Setup({
|
||||
Proxy:Directory,
|
||||
SWCOp:
|
||||
{
|
||||
if(_req.headers.get("upgrade") == "websocket")
|
||||
sourceMaps: "inline",
|
||||
minify: false,
|
||||
jsc:
|
||||
{
|
||||
target:"es2022",
|
||||
parser:
|
||||
{
|
||||
syntax: "typescript",
|
||||
tsx: true,
|
||||
}
|
||||
}
|
||||
},
|
||||
async Serve(inReq, inURL, inExt)
|
||||
{
|
||||
if(inReq.headers.get("upgrade") == "websocket")
|
||||
{
|
||||
try
|
||||
{
|
||||
const { response, socket } = Deno.upgradeWebSocket(_req);
|
||||
const { response, socket } = Deno.upgradeWebSocket(inReq);
|
||||
socket.onopen = () => SocketsLive.add(socket);
|
||||
socket.onclose = () => SocketsLive.delete(socket);
|
||||
socket.onmessage = (e) => {};
|
||||
@ -25,8 +38,34 @@ HTTP.serve((_req:Request)=>
|
||||
return new Response(e);
|
||||
}
|
||||
}
|
||||
return new Response(`websockets only`);
|
||||
}, { port: 4444 });
|
||||
|
||||
if(!inExt)
|
||||
{
|
||||
await fetch(Directory+"/deno.json");
|
||||
|
||||
return new Response(
|
||||
`<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
<script type="importmap">
|
||||
{
|
||||
"imports":
|
||||
{
|
||||
"react":"https://esm.sh/preact/compat"
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<script type="module"></script>
|
||||
</body>
|
||||
</html>`, {headers:{"content-type":"text/html"}});
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
let blocking = false;
|
||||
const filesChanged:Map<string, string> = new Map();
|
||||
@ -39,11 +78,13 @@ for await (const event of Deno.watchFs(Deno.cwd()))
|
||||
setTimeout(async()=>
|
||||
{
|
||||
for await (const [path, action] of filesChanged)
|
||||
{
|
||||
if(Transpile.Check(Extension(path)))
|
||||
{
|
||||
const key = path.substring(Deno.cwd().length).replaceAll("\\", "/");
|
||||
if(action != "remove")
|
||||
{
|
||||
const tsx = await Transpile.Fetch(Directory+key, key, true);
|
||||
const tsx = await Transpile.Fetch(Directory+key, key, true, true);
|
||||
tsx && SocketsSend(key);
|
||||
}
|
||||
else
|
||||
@ -51,6 +92,7 @@ for await (const event of Deno.watchFs(Deno.cwd()))
|
||||
Transpile.Cache.delete(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
filesChanged.clear();
|
||||
blocking = false;
|
||||
}
|
||||
|
98
serve.tsx
98
serve.tsx
@ -2,19 +2,16 @@ 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";
|
||||
import * as SWCW from "https://esm.sh/@swc/wasm-web@1.3.62";
|
||||
|
||||
export type Configuration = {Proxy:string, Allow:string, Reset:string, Local:boolean};
|
||||
export type ConfigurationArgs = {Proxy?:string, Allow?:string, Reset?:string, Local?:boolean};
|
||||
export let Configure:Configuration =
|
||||
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 =
|
||||
{
|
||||
Proxy: "",
|
||||
Allow: "*",
|
||||
Reset: "/clear-cache",
|
||||
Local: false
|
||||
};
|
||||
|
||||
export default (config:ConfigurationArgs)=> Configure = {...Configure, ...config};
|
||||
export const Transpile = {
|
||||
Config:
|
||||
Serve: ()=>false,
|
||||
SWCOp:
|
||||
{
|
||||
sourceMaps: false,
|
||||
minify: true,
|
||||
@ -35,9 +32,19 @@ export const Transpile = {
|
||||
{
|
||||
react: { runtime: "automatic" }
|
||||
}
|
||||
},
|
||||
} as SWCW.Options,
|
||||
}
|
||||
}
|
||||
};
|
||||
export default (config:ConfigurationArgs)=> Configure = {...Configure, ...config};
|
||||
|
||||
export const Transpile =
|
||||
{
|
||||
Cache: new Map() as Map<string, string>,
|
||||
Files: ["tsx", "jsx", "ts", "js", "mjs"],
|
||||
Check(inExtension:string|false)
|
||||
{
|
||||
return inExtension ? this.Files.includes(inExtension) : false;
|
||||
},
|
||||
Clear()
|
||||
{
|
||||
const size = this.Cache.size;
|
||||
@ -45,9 +52,6 @@ export const Transpile = {
|
||||
return size;
|
||||
},
|
||||
Fetch: async function(inPath:string, inKey:string, inCheckCache=true)
|
||||
{
|
||||
console.log("transpile", inPath)
|
||||
if(inPath.endsWith(".tsx") || inPath.endsWith(".jsx") || inPath.endsWith(".js") || inPath.endsWith(".mjs"))
|
||||
{
|
||||
const check = this.Cache.get(inPath);
|
||||
if(check && inCheckCache)
|
||||
@ -60,62 +64,78 @@ export const Transpile = {
|
||||
{
|
||||
const resp = await fetch(inPath);
|
||||
const text = await resp.text();
|
||||
const {code} = await SWCW.transform(text, {...this.Config, filename:inKey});
|
||||
const {code} = await SWCW.transform(text, { ...Configure.SWCOp, filename:inKey});
|
||||
this.Cache.set(inKey, code);
|
||||
return code;
|
||||
}
|
||||
catch(e)
|
||||
{
|
||||
console.log(`xpile.tsx error. Key:${inKey} Path:${inPath} Error:"${e}"`);
|
||||
//console.log(`Transpile.Fetch error. Key:"${inKey}" Path:"${inPath}" Error:"${e}"`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
};
|
||||
|
||||
export const Extension =(inPath:string)=>
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
const posSlash = inPath.lastIndexOf("/");
|
||||
const posDot = inPath.lastIndexOf(".");
|
||||
return posDot > posSlash ? inPath.substring(posDot+1).toLowerCase() : false;
|
||||
};
|
||||
|
||||
SWCW.default();
|
||||
console.log("starting server");
|
||||
HTTP.serve(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": Configure.Allow, "charset":"UTF-8"};
|
||||
|
||||
if(url.pathname.endsWith("/"))
|
||||
// allow for custom handler
|
||||
const custom = await Configure.Serve(req, url, ext);
|
||||
if(custom)
|
||||
{
|
||||
|
||||
return custom;
|
||||
}
|
||||
|
||||
// implied index.html files
|
||||
if(!ext)
|
||||
{
|
||||
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"}});
|
||||
}
|
||||
|
||||
// cache-reset route
|
||||
if(url.pathname === Configure.Reset)
|
||||
{
|
||||
return new Response(`cache cleared (${Transpile.Clear()} items)`);
|
||||
return new Response(`{"cleared":${Transpile.Clear()}}`, {headers});
|
||||
}
|
||||
|
||||
// transpileable files
|
||||
if(Transpile.Check(ext))
|
||||
{
|
||||
const lookup = await Transpile.Fetch(Configure.Proxy + url.pathname, url.pathname);
|
||||
if(lookup === null)
|
||||
{
|
||||
// error
|
||||
return new Response(`transpile error (see console)`, {status:404, headers:{"content-type":"application/javascript", "Access-Control-Allow-Origin": Configure.Allow, charset:"utf-8"}});
|
||||
return new Response(lookup, {status:lookup?200:404, headers:{...headers, "content-type":"application/javascript"}} );
|
||||
}
|
||||
else if(lookup === false)
|
||||
{
|
||||
// not a javascript file
|
||||
|
||||
// all other static files
|
||||
try
|
||||
{
|
||||
const type = MIME.typeByExtension(url.pathname.substring(url.pathname.lastIndexOf("."))) || "text/html";
|
||||
const type = MIME.typeByExtension(ext);
|
||||
const file = await fetch(Configure.Proxy + url.pathname);
|
||||
const text = await file.text();
|
||||
return new Response(text, {headers:{"content-type":type, "Access-Control-Allow-Origin":Configure.Allow, charset:"utf-8"}});
|
||||
return new Response(text, {headers:{...headers, "content-type":type||""}});
|
||||
}
|
||||
catch(e)
|
||||
{
|
||||
return new Response(`404 ${Configure.Proxy + url.pathname}`, {status:404});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Response(lookup, {headers:{"content-type":"application/javascript", "Access-Control-Allow-Origin": Configure.Allow, charset:"utf-8"}});
|
||||
return new Response(`{"error":"${e}", "path":"${url.pathname}"}`, {status:404, headers});
|
||||
}
|
||||
|
||||
});
|
Loading…
Reference in New Issue
Block a user