refactor(frontend): 🐛 Refactor frontend/glitch resolution strategy to prevent bugs

This commit is contained in:
Jesse Wierzbinski 2024-04-22 09:48:18 -10:00
parent 5dd6ea4d10
commit 1aef094fc2
No known key found for this signature in database
2 changed files with 49 additions and 43 deletions

View file

@ -128,6 +128,25 @@ export const createServer = (
); );
} }
const routePaths = [
"/api",
"/media",
"/nodeinfo",
"/.well-known",
"/users",
"/objects",
"/oauth/token",
"/oauth/providers",
];
// Check if URL starts with routePath
if (
routePaths.some((path) =>
new URL(req.url).pathname.startsWith(path),
) ||
(new URL(req.url).pathname.startsWith("/oauth/authorize") &&
req.method === "POST")
) {
// If route is .well-known, remove dot because the filesystem router can't handle dots for some reason // If route is .well-known, remove dot because the filesystem router can't handle dots for some reason
const matchedRoute = matchRoute( const matchedRoute = matchRoute(
new Request(req.url.replace(".well-known", "well-known"), { new Request(req.url.replace(".well-known", "well-known"), {
@ -139,12 +158,24 @@ export const createServer = (
matchedRoute?.filePath && matchedRoute?.filePath &&
matchedRoute.name !== "/[...404]" && matchedRoute.name !== "/[...404]" &&
!( !(
new URL(req.url).pathname.startsWith("/oauth/authorize") && new URL(req.url).pathname.startsWith(
req.method === "GET" "/oauth/authorize",
) && req.method === "GET"
) )
) { ) {
return await processRoute(matchedRoute, req, logger); return await processRoute(matchedRoute, req, logger);
} }
}
if (config.frontend.glitch.enabled) {
if (!new URL(req.url).pathname.startsWith("/oauth")) {
const glitch = await handleGlitchRequest(req, dualLogger);
if (glitch) {
return glitch;
}
}
}
const base_url_with_http = config.http.base_url.replace( const base_url_with_http = config.http.base_url.replace(
"https://", "https://",
@ -177,16 +208,12 @@ export const createServer = (
proxy?.headers.set("Cache-Control", "max-age=31536000"); proxy?.headers.set("Cache-Control", "max-age=31536000");
if (!proxy || proxy.status === 404) { if (!proxy || proxy.status === 404) {
if (config.frontend.glitch.enabled) { return errorResponse(
return ( "Route not found on proxy or API route",
(await handleGlitchRequest(req, dualLogger)) ?? 404,
errorResponse("Route not found", 404)
); );
} }
} else {
return proxy;
}
return errorResponse("Route not found", 404); return proxy;
}, },
}); });

View file

@ -1,21 +0,0 @@
import { apiRoute, applyConfig } from "@api";
import { errorResponse } from "@response";
export const meta = applyConfig({
allowedMethods: ["POST", "GET", "PUT", "PATCH", "DELETE"],
auth: {
required: false,
},
ratelimits: {
duration: 60,
max: 100,
},
route: "/[...404]",
});
/**
* Default catch-all route, returns a 404 error.
*/
export default apiRoute(() => {
return errorResponse("Route not found", 404);
});