outpost relay mode started

This commit is contained in:
Seth Trowbridge 2023-07-08 12:21:58 -04:00
parent 66b148d7f5
commit 0ea5c3e562
8 changed files with 80 additions and 52 deletions

View File

@ -1,15 +0,0 @@
import "./run-serve.tsx";
Deno.args.forEach(arg=>
{
if(arg.startsWith("--"))
{
const kvp = arg.substring(2).split("=");
Deno.env.set(kvp[0], kvp[1] || "true");
}
});
if(Deno.env.get("dev"))
{
await import("./run-local.tsx");
}

6
boot.deno.tsx Normal file
View File

@ -0,0 +1,6 @@
import "./run-serve.tsx";
if(Deno.env.get("dev"))
{
await import("./run-local.tsx");
}

View File

@ -1,4 +1,4 @@
import "@able/boot-server.tsx"; import "http://localhost:1234/boot.tsx";
import React from "react"; import React from "react";
const CTXString = React.createContext("lol"); const CTXString = React.createContext("lol");
@ -38,7 +38,7 @@ export default ()=>
return <CTXString.Provider value="intradestink"> return <CTXString.Provider value="intradestink">
<div class="my-4 font-sans"> <div class="my-4 font-sans">
<h1 class="font-black text-xl text-red-500">Title????</h1> <h1 class="font-black text-xl text-red-500">Title????</h1>
<h2>subtitle!</h2> <h2 class="p-4">subtitle!!!!</h2>
<p> <p>
<button onClick={e=>Dispatch(1)}>{Store.name}|{Store.age}?</button> <button onClick={e=>Dispatch(1)}>{Store.name}|{Store.age}?</button>
</p> </p>

View File

@ -3,11 +3,11 @@
"imports": "imports":
{ {
"react":"https://esm.sh/preact@10.15.1/compat", "react":"https://esm.sh/preact@10.15.1/compat",
"@able/":"http://localhost:4507/" "@able/":"http://localhost:8000/"
}, },
"tasks": "tasks":
{ {
"local": "deno run -A --no-lock --reload=http://localhost:4507 app.tsx --dev", "local": "deno run -A --no-lock --reload app.tsx --dev",
"serve": "deno run -A --no-lock --reload=http://localhost:4507 app.tsx" "serve": "deno run -A --no-lock --reload app.tsx"
} }
} }

View File

@ -13,7 +13,7 @@ Socket.addEventListener('message', async(event:{data:string})=>
{ {
// When a file changes, dynamically re-import it to get the updated members // When a file changes, dynamically re-import it to get the updated members
// send the updated members to any listeners for that file // send the updated members to any listeners for that file
const reImport = await import(event.data+"?reload="+Math.random()); const reImport = await import(document.location.origin+event.data+"?reload="+Math.random());
FileListeners.get(event.data)?.forEach(reExport=>reExport(reImport)); FileListeners.get(event.data)?.forEach(reExport=>reExport(reImport));
HMR.update(); HMR.update();
}); });

View File

@ -21,7 +21,7 @@ Configure({
Remap: (inImports, inConfig)=> Remap: (inImports, inConfig)=>
{ {
inImports["react-original"] = inImports["react"]; inImports["react-original"] = inImports["react"];
inImports["react"] = `${inConfig.Spoof}/hmr-react.tsx`; inImports["react"] = import.meta.resolve(`./hmr-react.tsx`);
return inImports; return inImports;
}, },
async Serve(inReq, inURL, inExt, inMap, inConfig) async Serve(inReq, inURL, inExt, inMap, inConfig)
@ -33,7 +33,7 @@ Configure({
for( const key in imp ) { members.push(key); } for( const key in imp ) { members.push(key); }
const code =` const code =`
import {FileListen} from "${inConfig.Spoof}/hmr-listen.tsx"; import {FileListen} from "${import.meta.resolve(`./hmr-listen.tsx`)}";
import * as Import from "${inURL.pathname}?reload=0"; import * as Import from "${inURL.pathname}?reload=0";
${ members.map(m=>`let proxy_${m} = Import.${m}; export { proxy_${m} as ${m} };`).join("\n") } ${ members.map(m=>`let proxy_${m} = Import.${m}; export { proxy_${m} as ${m} };`).join("\n") }
FileListen("${inURL.pathname}", (updatedModule)=> FileListen("${inURL.pathname}", (updatedModule)=>

View File

@ -1,7 +1,25 @@
import * as MIME from "https://deno.land/std@0.180.0/media_types/mod.ts"; 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"; import * as SWCW from "https://esm.sh/@swc/wasm-web@1.3.62";
Deno.args.forEach(arg=>
{
if(arg.startsWith("--"))
{
const kvp = arg.substring(2).split("=");
Deno.env.set(kvp[0], kvp[1] || "true");
}
});
const urls = {
Cwd: new URL(`file://${Deno.cwd().replaceAll("\\", "/")}`).toString(),
App: Deno.mainModule,
Mod: import.meta.url,
Look: import.meta.resolve("./boot.tsx")
};
console.log(JSON.stringify(urls, null, " "));
type DenoConfig = {imports:Record<string, string>}; type DenoConfig = {imports:Record<string, string>};
const ImportMap:DenoConfig = {imports:{}}; const ImportMap:DenoConfig = {imports:{}};
let ImportMapOriginal = {}; let ImportMapOriginal = {};
@ -59,17 +77,7 @@ let Configuration:Configuration =
Allow: "*", Allow: "*",
Reset: "/clear-cache", Reset: "/clear-cache",
Spoof: "/@able", Spoof: "/@able",
async Serve(inReq, inURL, inExt, inMap, inConfig) async Serve(inReq, inURL, inExt, inMap, inConfig){},
{
if( inReq.headers.get("user-agent")?.startsWith("Deno") || inURL.searchParams.get("deno") )
{
if(inExt && Extension(inExt))
{
const text = await Transpile.Patch(inConfig.Proxy + inURL.pathname, "deno-"+inURL.pathname, inMap);
return new Response(text, {headers:{"content-type":"application/javascript"}} );
}
}
},
Remap: (inImports, inConfig)=> Remap: (inImports, inConfig)=>
{ {
const reactURL = inImports["react"]; const reactURL = inImports["react"];
@ -95,17 +103,14 @@ let Configuration:Configuration =
<div id="app"></div> <div id="app"></div>
<script type="importmap">${JSON.stringify(inMap)}</script> <script type="importmap">${JSON.stringify(inMap)}</script>
<script type="module"> <script type="module">
import Mount from "${inConfig.Spoof}/boot-browser.tsx"; import Mount from "${import.meta.resolve("./boot.tsx")}";
Mount("#app", "${parts[1]??"/app.tsx"}"); Mount("#app", document.location.origin+"${parts[1]??"/app.tsx"}");
</script> </script>
</body> </body>
</html>`, {status:200, headers:{"content-type":"text/html"}}); </html>`, {status:200, headers:{"content-type":"text/html"}});
}, },
SWCOp: SWCOp:
{ {
env:{
dynamicImport:false
},
sourceMaps: false, sourceMaps: false,
minify: true, minify: true,
jsc: jsc:
@ -149,15 +154,23 @@ export const Transpile =
*/ */
async Patch(inPath:string, inKey:string, inMap?:DenoConfig) async Patch(inPath:string, inKey:string, inMap?:DenoConfig)
{ {
const file = await fetch(inPath);
const text = await file.text();
const check = this.Cache.get(inKey); const check = this.Cache.get(inKey);
if(check) if(check)
{ {
return check; return check;
} }
let file, text;
try
{
file = await fetch(inPath);
text = await file.text();
}
catch(e)
{
return false;
}
const remap = inMap ? (inPath:string)=> const remap = inMap ? (inPath:string)=>
{ {
const match = inMap.imports[inPath]; const match = inMap.imports[inPath];
@ -240,6 +253,19 @@ export const Transpile =
} }
}; };
export const ExtensionPrefix =(string:string, suffix:string)=>
{
const lastDotIndex = string.lastIndexOf(".");
if (lastDotIndex === -1) {
return string;
}
const prefix = string.substring(0, lastDotIndex);
const suffixString = string.substring(lastDotIndex + 1);
return prefix + "." + suffix + "." + suffixString;
};
export const Extension =(inPath:string)=> export const Extension =(inPath:string)=>
{ {
const posSlash = inPath.lastIndexOf("/"); const posSlash = inPath.lastIndexOf("/");
@ -255,7 +281,7 @@ export const Configure =(config:ConfigurationArgs)=>
await ImportMapReload(); await ImportMapReload();
await SWCW.default(); await SWCW.default();
HTTP.serve(async(req: Request)=> const server = Deno.serve({port:parseInt(Deno.env.get("port")||"8000")}, async(req: Request)=>
{ {
const url:URL = new URL(req.url); const url:URL = new URL(req.url);
const ext = Extension(url.pathname); const ext = Extension(url.pathname);
@ -279,19 +305,30 @@ HTTP.serve(async(req: Request)=>
{ {
let code; let code;
let path; let path;
if(url.pathname.startsWith(Configuration.Spoof+"/")) let file;
if( req.headers.get("user-agent")?.startsWith("Deno") || url.searchParams.has("deno") )
{ {
const clipRoot = import.meta.url.substring(0, import.meta.url.lastIndexOf("/")); try
const clipPath = url.pathname.substring(url.pathname.indexOf("/", 1));
if(clipPath.startsWith("/boot-"))
{ {
path = clipRoot+"/boot-browser.tsx"; path = Configuration.Proxy + ExtensionPrefix(url.pathname, "deno");
console.log("looking for", path);
file = await fetch(path) as Response;
if(file.ok)
{
code = await file.text();
} }
else else
{ {
path = clipRoot + clipPath; throw new Error("404")
}
}
catch(e)
{
path = Configuration.Proxy + url.pathname;
console.log("falling back to", path);
file = await fetch(path);
code = await file.text();
} }
code = await Transpile.Fetch(path, url.pathname, true);
} }
else else
{ {