dev server proxy

This commit is contained in:
Seth Trowbridge 2025-08-07 08:26:18 -04:00
parent b50cae600a
commit c3f6e45a99
5 changed files with 31 additions and 17 deletions

File diff suppressed because one or more lines are too long

View File

@ -1 +0,0 @@
var g=0,c={};function d(){for(let e in c)sessionStorage.setItem(e,c[e]);c={},g=0}function f(e,t){c[e]=t,g||(g=setTimeout(d,500)),console.log("SAVE",e,t)}function h(e){let t=sessionStorage.getItem(e);return console.log("LOAD",e,t),t}var S,u=0;function v(e){u=0,S=e}function m(){return S?S+"_"+u+++"_":""}var I=globalThis.van.state;globalThis.van.state=(e,t="")=>{let n=typeof e,o=r=>r,a=r=>r?.toString()||null;switch(n){case"boolean":o=r=>r==="true";break;case"number":o=parseFloat;break;case"object":o=JSON.parse,a=JSON.stringify;break}let l="HMR_"+m()+t,s=h(l),i=I(s?o(s):e);return van.derive(()=>f(l,a(i.val))),i};var T=globalThis.vanX.reactive;globalThis.vanX={...globalThis.VanX,reactive:(e,t)=>{v(t);let n=T(e);return v(),n}};new WebSocket("ws://"+window.location.host+"/ws").addEventListener("message",e=>e.data==="reload"&&window.location.reload());vanX.Store=(e,t)=>{let n=JSON.stringify(e),o=localStorage.getItem(t+"check");localStorage.setItem(t+"check",n);let a;if(n==o){let s=localStorage.getItem(t);try{a=JSON.parse(s)||e}catch{a=e}}else a=e;let l=vanX.reactive(a);return van.derive(()=>localStorage.setItem(t,JSON.stringify(vanX.compact(l)))),l};

View File

@ -8,6 +8,6 @@ export const HTML = index.replace(
`</head>`,
`
<style>*{margin:0;padding:0;box-sizing:border-box;}html, body{height:100%;width:100%;font-family:Arial, sans-serif;line-height:1.6;}body{-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;}img, video{max-width:100%;height:auto;}a{text-decoration:none;color:inherit;}ul, ol{list-style:none;}button, input, textarea{font-family:inherit;font-size:inherit;line-height:inherit;border:none;background:none;padding:0;margin:0;outline:none;}table{border-collapse:collapse;width:100%;}</style>
<script type="module" src="${Root+"dist/bundle_entry.js?hmr&map"}"></script>
<script type="module" src="/proxy/dist/bundle_entry.js?hmr&map"}"></script>
</head>`);

View File

@ -14,7 +14,8 @@ vanX.Store=(e,t)=>{const a=localStorage.getItem(t),r=vanX.reactive(a?JSON.parse(
if(args.has("hmr"))
{
await import("../src/hmr.js");
const path = "/proxy/"+"src/hmr.js";
await import(path);
}
const root = args.get("root")||"/";

View File

@ -1,12 +1,10 @@
import { contentType } from "jsr:@std/media-types";
import {HTML} from "./assemble_files.ts";
import {HTML, Root} from "./assemble_files.ts";
// Parse the port from the command-line arguments, defaulting to 8000
const port = parseInt(Deno.args[0] || "8000", 10);
const sockets: WebSocket[] = [];
let html = HTML;
function extension(path: string): string {
// Remove trailing slash if it exists
const normalizedPath = path.endsWith("/") ? path.slice(0, -1) : path;
@ -18,32 +16,48 @@ function extension(path: string): string {
// Start the HTTP server using Deno.serve
Deno.serve({ port }, async (req: Request) => {
const url = new URL(req.url);
const path = new URL(req.url).pathname;
// Handle WebSocket connections
if (url.pathname === "/ws") {
if (path === "/ws") {
const { socket, response } = Deno.upgradeWebSocket(req);
sockets.push(socket);
return response;
}
// Serve static files or the predefined HTML for non-file routes
const path = new URL(req.url).pathname;
const ext = extension(path);
// Serve the predefined HTML for non-file routes
if (!ext) {
return new Response(html, {
return new Response(HTML, {
headers: { "Content-Type": "text/html" },
});
}
try {
try
{
const proxyPrefix = "/proxy/";
let streamable;
if(path.startsWith(proxyPrefix))
{
const file = await fetch(Root + path.slice(proxyPrefix.length));
streamable = file.body;
}
else
{
const file = await Deno.open("." + path, { read: true });
return new Response(file.readable, {
streamable = file.readable;
}
return new Response(streamable, {
headers: { "Content-Type": contentType(ext) || "application/javascript" },
});
} catch (err) {
}
catch (err)
{
if (err instanceof Deno.errors.NotFound) {
return new Response("File not found", { status: 404 });
} else {