From 03aae4d8db4d319f15af3869da93e196d3970849 Mon Sep 17 00:00:00 2001 From: geoffsee <> Date: Wed, 4 Jun 2025 19:00:10 -0400 Subject: [PATCH] fix static fileserver --- packages/server/server.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/server/server.ts b/packages/server/server.ts index b43fd99..482b0fb 100644 --- a/packages/server/server.ts +++ b/packages/server/server.ts @@ -1,4 +1,3 @@ -// import Server from "./packages/server/index.ts"; import {BunSqliteKVNamespace} from "./storage/BunSqliteKVNamespace"; import {readdir} from 'node:fs/promises'; import type { RequestLike } from "itty-router"; @@ -9,7 +8,7 @@ import DurableObjectLocal from "./ServerCoordinatorBun"; const router = Server.Router(); -config({ path: ['../../.dev.vars'] }) +config({ path: ['./.env'] }) export default { port: 3003, @@ -65,7 +64,7 @@ const assetHandler = { const url = new URL(request.url); // 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}); // Get the filename from pathname and remove any path traversal attempts @@ -75,18 +74,20 @@ const assetHandler = { if (url.pathname === "/") { url.pathname = "/index.html"; + } else if (isStatic && !url.pathname.startsWith('/static')) { + // leave it alone } else if (isStatic) { url.pathname = `/static${url.pathname}`; } - const dist = "../client/dist/client" + const dist = new URL('../client/dist/client', import.meta.url).pathname; try { return new Response(Bun.file(`${dist}${url.pathname}`)); } catch (error) { // Log the error with the original requested path 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 }); } } }