Skip to content

Commit

Permalink
Cache all bad decodeURI requets
Browse files Browse the repository at this point in the history
  • Loading branch information
LitoMore committed Aug 25, 2024
1 parent f3577a9 commit 25be84d
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions source/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ import { getIconSvg, getSimpleIcon } from "./icon.js";
import { Application } from "jsr:@oak/oak/application";
import { Router } from "jsr:@oak/oak/router";

const router = new Router();
const allowCrossOrigin = (ctx) => {
ctx.response.headers.set("Access-Control-Allow-Origin", "*");
};

router.get("/", (ctx) => {
const cacheForSevenDays = (ctx) => {
ctx.response.headers.set(
"Cache-Control",
"public, max-age=86400, s-maxage=31536000, stale-while-revalidate=604800",
);
};

const router = new Router();
router.get("/", (ctx) => {
cacheForSevenDays(ctx);
ctx.response.status = 307;
ctx.response.redirect("https://github.com/LitoMore/simple-icons-cdn");
});
router.get("/favicon.ico", (ctx) => {
ctx.response.headers.set(
"Cache-Control",
"public, max-age=31536000, s-maxage=31536000, immutable",
);
cacheForOneYear(ctx);
ctx.response.status = 204;
});
router.get("/:iconSlug/:color?/:darkModeColor?", (ctx) => {
ctx.response.headers.set("Access-Control-Allow-Origin", "*");
ctx.response.headers.set(
"Cache-Control",
"public, max-age=86400, s-maxage=31536000, stale-while-revalidate=604800",
);

allowCrossOrigin(ctx);
cacheForSevenDays(ctx);
const { iconSlug, color, darkModeColor } = ctx.params;
const viewbox = ctx.request.url.searchParams.get("viewbox");
const icon = getSimpleIcon(iconSlug);
Expand All @@ -42,6 +42,19 @@ router.get("/:iconSlug/:color?/:darkModeColor?", (ctx) => {
});

const app = new Application();
app.use(async (ctx, next) => {
try {
await next();
} catch (error) {
if (error instanceof URIError) {
cacheForSevenDays(ctx);
ctx.response.status = 404;
return;
}
console.log(error);
throw error;
}
});
app.use(router.routes());
app.use(router.allowedMethods());
app.listen({ port: 8080 });
Expand Down

0 comments on commit 25be84d

Please sign in to comment.