fix(api): 🐛 Fix incorrect proxy behaviour

This commit is contained in:
Jesse Wierzbinski 2024-05-06 17:31:12 +00:00
parent afda1f1211
commit 8557867ad8
No known key found for this signature in database
5 changed files with 52 additions and 4 deletions

View file

@ -6,7 +6,7 @@ import { z } from "zod";
export const meta = applyConfig({
allowedMethods: ["GET"],
route: "/api/media/:id",
route: "/media/:id",
ratelimits: {
max: 100,
duration: 60,

View file

@ -0,0 +1,46 @@
import { applyConfig, handleZodError } from "@api";
import { zValidator } from "@hono/zod-validator";
import { errorResponse, response } from "@response";
import type { Hono } from "hono";
import { z } from "zod";
export const meta = applyConfig({
allowedMethods: ["GET"],
route: "/media/proxy/:id",
ratelimits: {
max: 100,
duration: 60,
},
auth: {
required: false,
},
});
export const schemas = {
param: z.object({
id: z
.string()
.transform((val) => Buffer.from(val, "base64url").toString()),
}),
};
export default (app: Hono) =>
app.on(
meta.allowedMethods,
meta.route,
zValidator("param", schemas.param, handleZodError),
async (context) => {
const { id } = context.req.valid("param");
// Check if URL is valid
if (!URL.canParse(id))
return errorResponse(
"Invalid URL (it should be encoded as base64url",
400,
);
return fetch(id).then((res) => {
return response(res.body, res.status, res.headers.toJSON());
});
},
);

View file

@ -6,7 +6,7 @@ import { z } from "zod";
export const meta = applyConfig({
allowedMethods: ["GET"],
route: "/api/media/proxy",
route: "/media/proxy",
ratelimits: {
max: 100,
duration: 60,