fix static fileserver

This commit is contained in:
geoffsee
2025-06-04 19:00:10 -04:00
parent 5d7a7b740a
commit 03aae4d8db

View File

@@ -1,4 +1,3 @@
// import Server from "./packages/server/index.ts";
import {BunSqliteKVNamespace} from "./storage/BunSqliteKVNamespace"; import {BunSqliteKVNamespace} from "./storage/BunSqliteKVNamespace";
import {readdir} from 'node:fs/promises'; import {readdir} from 'node:fs/promises';
import type { RequestLike } from "itty-router"; import type { RequestLike } from "itty-router";
@@ -9,7 +8,7 @@ import DurableObjectLocal from "./ServerCoordinatorBun";
const router = Server.Router(); const router = Server.Router();
config({ path: ['../../.dev.vars'] }) config({ path: ['./.env'] })
export default { export default {
port: 3003, port: 3003,
@@ -65,7 +64,7 @@ const assetHandler = {
const url = new URL(request.url); const url = new URL(request.url);
// List all files in the public directory // List all files in the public directory
const PUBLIC_DIR = '../client/public/'; const PUBLIC_DIR = new URL('../client/public/', import.meta.url).pathname;
const publicFiles = await readdir(PUBLIC_DIR, {recursive: true}); const publicFiles = await readdir(PUBLIC_DIR, {recursive: true});
// Get the filename from pathname and remove any path traversal attempts // Get the filename from pathname and remove any path traversal attempts
@@ -75,18 +74,20 @@ const assetHandler = {
if (url.pathname === "/") { if (url.pathname === "/") {
url.pathname = "/index.html"; url.pathname = "/index.html";
} else if (isStatic && !url.pathname.startsWith('/static')) {
// leave it alone
} else if (isStatic) { } else if (isStatic) {
url.pathname = `/static${url.pathname}`; url.pathname = `/static${url.pathname}`;
} }
const dist = "../client/dist/client" const dist = new URL('../client/dist/client', import.meta.url).pathname;
try { try {
return new Response(Bun.file(`${dist}${url.pathname}`)); return new Response(Bun.file(`${dist}${url.pathname}`));
} catch (error) { } catch (error) {
// Log the error with the original requested path // Log the error with the original requested path
console.error(`Error reading asset from path ${originalUrl.pathname}:`, error); console.error(`Error reading asset from path ${originalUrl.pathname}:`, error);
return new Response('Asset not found on disk', { status: 404 }); return new Response(null, { status: 404 });
} }
} }
} }