fix(api): 🐛 Fix proxy returning incorrect media due to compression

This commit is contained in:
Jesse Wierzbinski 2024-05-06 17:50:50 +00:00
parent 8557867ad8
commit ad4466a260
No known key found for this signature in database
3 changed files with 6 additions and 48 deletions

View file

@ -39,7 +39,11 @@ export default (app: Hono) =>
400,
);
return fetch(id).then((res) => {
return fetch(id, {
headers: {
"Accept-Encoding": "identity",
},
}).then((res) => {
return response(res.body, res.status, res.headers.toJSON());
});
},

View file

@ -1,46 +0,0 @@
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",
ratelimits: {
max: 100,
duration: 60,
},
auth: {
required: false,
},
});
export const schemas = {
query: z.object({
url: z
.string()
.transform((val) => Buffer.from(val, "base64url").toString()),
}),
};
export default (app: Hono) =>
app.on(
meta.allowedMethods,
meta.route,
zValidator("query", schemas.query, handleZodError),
async (context) => {
const { url } = context.req.valid("query");
// Check if URL is valid
if (!URL.canParse(url))
return errorResponse(
"Invalid URL (it should be encoded as base64url",
400,
);
return fetch(url).then((res) => {
return response(res.body, res.status, res.headers.toJSON());
});
},
);

View file

@ -112,7 +112,7 @@ describe("POST /api/auth/login/", () => {
describe("GET /oauth/authorize/", () => {
test("should get a code", async () => {
const response = await sendTestRequest(
new Request(new URL(`/oauth/authorize`, base_url), {
new Request(new URL("/oauth/authorize", base_url), {
method: "POST",
headers: {
Cookie: `jwt=${jwt}`,