From 0ea5c3e562dc0e0c2c48f472ac90a46fff4c1356 Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Sat, 8 Jul 2023 12:21:58 -0400 Subject: [PATCH] outpost relay mode started --- boot-server.tsx | 15 ------ boot.deno.tsx | 6 +++ boot-browser.tsx => boot.tsx | 0 example/app.tsx | 4 +- example/deno.json | 6 +-- hmr-listen.tsx | 2 +- run-local.tsx | 4 +- run-serve.tsx | 95 +++++++++++++++++++++++++----------- 8 files changed, 80 insertions(+), 52 deletions(-) delete mode 100644 boot-server.tsx create mode 100644 boot.deno.tsx rename boot-browser.tsx => boot.tsx (100%) diff --git a/boot-server.tsx b/boot-server.tsx deleted file mode 100644 index 459f706..0000000 --- a/boot-server.tsx +++ /dev/null @@ -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"); -} \ No newline at end of file diff --git a/boot.deno.tsx b/boot.deno.tsx new file mode 100644 index 0000000..29c2669 --- /dev/null +++ b/boot.deno.tsx @@ -0,0 +1,6 @@ +import "./run-serve.tsx"; + +if(Deno.env.get("dev")) +{ + await import("./run-local.tsx"); +} \ No newline at end of file diff --git a/boot-browser.tsx b/boot.tsx similarity index 100% rename from boot-browser.tsx rename to boot.tsx diff --git a/example/app.tsx b/example/app.tsx index add001e..19f4170 100644 --- a/example/app.tsx +++ b/example/app.tsx @@ -1,4 +1,4 @@ -import "@able/boot-server.tsx"; +import "http://localhost:1234/boot.tsx"; import React from "react"; const CTXString = React.createContext("lol"); @@ -38,7 +38,7 @@ export default ()=> return

Title????

-

subtitle!

+

subtitle!!!!

diff --git a/example/deno.json b/example/deno.json index 314726a..9fab40a 100644 --- a/example/deno.json +++ b/example/deno.json @@ -3,11 +3,11 @@ "imports": { "react":"https://esm.sh/preact@10.15.1/compat", - "@able/":"http://localhost:4507/" + "@able/":"http://localhost:8000/" }, "tasks": { - "local": "deno run -A --no-lock --reload=http://localhost:4507 app.tsx --dev", - "serve": "deno run -A --no-lock --reload=http://localhost:4507 app.tsx" + "local": "deno run -A --no-lock --reload app.tsx --dev", + "serve": "deno run -A --no-lock --reload app.tsx" } } \ No newline at end of file diff --git a/hmr-listen.tsx b/hmr-listen.tsx index aad4269..4ec21be 100644 --- a/hmr-listen.tsx +++ b/hmr-listen.tsx @@ -13,7 +13,7 @@ Socket.addEventListener('message', async(event:{data:string})=> { // When a file changes, dynamically re-import it to get the updated members // 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)); HMR.update(); }); diff --git a/run-local.tsx b/run-local.tsx index 665788b..0314cf6 100644 --- a/run-local.tsx +++ b/run-local.tsx @@ -21,7 +21,7 @@ Configure({ Remap: (inImports, inConfig)=> { inImports["react-original"] = inImports["react"]; - inImports["react"] = `${inConfig.Spoof}/hmr-react.tsx`; + inImports["react"] = import.meta.resolve(`./hmr-react.tsx`); return inImports; }, async Serve(inReq, inURL, inExt, inMap, inConfig) @@ -33,7 +33,7 @@ Configure({ for( const key in imp ) { members.push(key); } 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"; ${ members.map(m=>`let proxy_${m} = Import.${m}; export { proxy_${m} as ${m} };`).join("\n") } FileListen("${inURL.pathname}", (updatedModule)=> diff --git a/run-serve.tsx b/run-serve.tsx index 38cfd85..1b3aeea 100644 --- a/run-serve.tsx +++ b/run-serve.tsx @@ -1,7 +1,25 @@ 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"; + +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}; const ImportMap:DenoConfig = {imports:{}}; let ImportMapOriginal = {}; @@ -59,17 +77,7 @@ let Configuration:Configuration = Allow: "*", Reset: "/clear-cache", Spoof: "/@able", - 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"}} ); - } - } - }, + async Serve(inReq, inURL, inExt, inMap, inConfig){}, Remap: (inImports, inConfig)=> { const reactURL = inImports["react"]; @@ -95,17 +103,14 @@ let Configuration:Configuration =
`, {status:200, headers:{"content-type":"text/html"}}); }, SWCOp: { - env:{ - dynamicImport:false - }, sourceMaps: false, minify: true, jsc: @@ -149,15 +154,23 @@ export const Transpile = */ async Patch(inPath:string, inKey:string, inMap?:DenoConfig) { - const file = await fetch(inPath); - const text = await file.text(); - const check = this.Cache.get(inKey); if(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 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)=> { const posSlash = inPath.lastIndexOf("/"); @@ -255,7 +281,7 @@ export const Configure =(config:ConfigurationArgs)=> await ImportMapReload(); 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 ext = Extension(url.pathname); @@ -279,19 +305,30 @@ HTTP.serve(async(req: Request)=> { let code; 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("/")); - const clipPath = url.pathname.substring(url.pathname.indexOf("/", 1)); - if(clipPath.startsWith("/boot-")) + try { - 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 + { + throw new Error("404") + } } - else + catch(e) { - path = clipRoot + clipPath; + 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 {