mirror of
https://github.com/geoffsee/open-gsio.git
synced 2025-09-08 22:56:46 +00:00
fix static fileserver
This commit is contained in:
@@ -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 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user