fix: use pluginCwd for static files + info log on request

Static file path now uses BSB pluginCwd instead of import.meta.dirname.
Added info log with method+path on every request via per-request trace.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Mitchell R 2026-05-26 01:20:17 +02:00
parent 57348b14ab
commit 35d184a6dd
No known key found for this signature in database
3 changed files with 10 additions and 10 deletions

View file

@ -200,10 +200,12 @@ export class Plugin extends BSBService<InstanceType<typeof Config>, typeof Event
onRequest: (event) => { onRequest: (event) => {
const method = event.req.method ?? "GET"; const method = event.req.method ?? "GET";
const path = event.req.url ?? "/"; const path = event.req.url ?? "/";
event.context.obs = self.createTrace(`${method} ${path}`, { const reqObs = self.createTrace(`${method} ${path}`, {
"http.method": method, "http.method": method,
"http.url": path, "http.url": path,
}); });
reqObs.log.info("{method} {path}", { method, path });
event.context.obs = reqObs;
}, },
onError: (error, event) => { onError: (error, event) => {
const reqObs = event.context.obs ?? obs; const reqObs = event.context.obs ?? obs;
@ -231,7 +233,7 @@ export class Plugin extends BSBService<InstanceType<typeof Config>, typeof Event
}); });
registerMiddleware(app, deps); registerMiddleware(app, deps);
registerStaticRoutes(app); registerStaticRoutes(app, this.pluginCwd);
registerSetupRoutes(app, deps); registerSetupRoutes(app, deps);
registerAuthRoutes(app, deps); registerAuthRoutes(app, deps);
registerAdminRoutes(app, deps); registerAdminRoutes(app, deps);

View file

@ -8,11 +8,6 @@ import { existsSync, readFileSync } from "node:fs";
import { join, extname, resolve } from "node:path"; import { join, extname, resolve } from "node:path";
import { type H3, getRouterParam, createError } from "h3"; import { type H3, getRouterParam, createError } from "h3";
const STATIC_DIR = resolve(
import.meta.dirname ?? ".",
"../../web-static",
);
const MIME_TYPES: Record<string, string> = { const MIME_TYPES: Record<string, string> = {
".html": "text/html; charset=utf-8", ".html": "text/html; charset=utf-8",
".css": "text/css; charset=utf-8", ".css": "text/css; charset=utf-8",
@ -25,12 +20,13 @@ const MIME_TYPES: Record<string, string> = {
".woff2": "font/woff2", ".woff2": "font/woff2",
}; };
export function registerStaticRoutes(app: H3): void { export function registerStaticRoutes(app: H3, pluginCwd: string): void {
const STATIC_DIR = resolve(pluginCwd, "../../web-static");
app.get("/static/**:path", (event) => { app.get("/static/**:path", (event) => {
const reqPath = getRouterParam(event, "path"); const reqPath = getRouterParam(event, "path");
if (!reqPath) throw createError({ statusCode: 404 }); if (!reqPath) throw createError({ statusCode: 404 });
// Prevent directory traversal
const resolved = resolve(STATIC_DIR, reqPath); const resolved = resolve(STATIC_DIR, reqPath);
if (!resolved.startsWith(STATIC_DIR)) { if (!resolved.startsWith(STATIC_DIR)) {
throw createError({ statusCode: 403 }); throw createError({ statusCode: 403 });

View file

@ -169,10 +169,12 @@ export class Plugin extends BSBService<InstanceType<typeof Config>, typeof Event
onRequest: (event) => { onRequest: (event) => {
const method = event.req.method ?? "GET"; const method = event.req.method ?? "GET";
const path = event.req.url ?? "/"; const path = event.req.url ?? "/";
event.context.obs = self.createTrace(`${method} ${path}`, { const reqObs = self.createTrace(`${method} ${path}`, {
"http.method": method, "http.method": method,
"http.url": path, "http.url": path,
}); });
reqObs.log.info("{method} {path}", { method, path });
event.context.obs = reqObs;
}, },
onError: (error, event) => { onError: (error, event) => {
const reqObs = event.context.obs ?? obs; const reqObs = event.context.obs ?? obs;