#Defining multiple folders to act as a `public` folder
1 messages · Page 1 of 1 (latest)
Thats what I was planning to do but I can for see a number or security issues, I think Ill just end up switching to Vite
If you don't mind, having a second pair of eyes scan over this code to make sure there aren't any holes in my security would be appreciated
const STATIC_CONTEXTS = [
{ files: "node_modules/@ruffle-rs/ruffle/", prefix: "/ruffle/" }
];
export default async function handleRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext
): Promise<Response> {
const { pathname } = new URL(request.url);
for (const staticContext of STATIC_CONTEXTS) {
if (pathname.startsWith(staticContext.prefix)) {
// Resolve the file path
const filePath = path.resolve(
staticContext.files,
pathname.slice(staticContext.prefix.length)
);
// Make sure the resolved path starts with the static context (should prevent ../ paths)
if (filePath.startsWith(path.resolve(staticContext.files))) {
try {
// Respond with file
const file = await fs.readFile(filePath);
responseHeaders.set("Content-Type", mime.lookup(filePath) || "");
return new Response(file, {
headers: responseHeaders,
status: 200
});
} catch {
// Do nothing (continue to 404 page)
}
} else {
// Do nothing (continue to 404 page)
}
}
}
...
}