Skip to content

Commit

Permalink
Better mime handling
Browse files Browse the repository at this point in the history
  • Loading branch information
neekolas committed Feb 15, 2024
1 parent d6ec0db commit b23adc0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"typescript": "^5.0.0"
},
"dependencies": {
"cheerio": "^1.0.0-rc.12"
"cheerio": "^1.0.0-rc.12",
"mime": "^4.0.1"
}
}
34 changes: 21 additions & 13 deletions src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { CORS_HEADERS } from "./constants";
import { InvalidRequestError, NoRedirectError } from "./errors";
import { extractMetaTags } from "./parser";
import type { GetMetadataResponse, PostRedirectResponse } from "./types";
import { getUrl, handleError } from "./utils";
import {
getMimeType,
getProxySafeMediaHeaders,
getUrl,
handleError,
} from "./utils";

export async function handleGet(req: Request) {
try {
Expand Down Expand Up @@ -83,20 +88,23 @@ export async function handleMedia(req: Request) {
const url = getUrl(req);
console.log(`Processing handleImage request for ${url}`);

const headers = new Headers();
const acceptHeader = req.headers.get("accept");
if (acceptHeader) {
headers.set("accept", acceptHeader);
}
const response = await fetch(url, {
headers,
const media = await fetch(url, {
headers: getProxySafeMediaHeaders(req),
});

return new Response(response.body, {
headers: {
"content-type": response.headers.get("content-type") || "image/png",
...CORS_HEADERS,
},
const responseHeaders = new Headers({ ...CORS_HEADERS });
responseHeaders.set(
"content-type",
media.headers.get("content-type") || getMimeType(url) || "image/png"
);

const responseEncoding = media.headers.get("encoding");
if (responseEncoding) {
responseHeaders.set("encoding", media.headers.get("encoding")!);
}

return new Response(media.body, {
headers: responseHeaders,
});
} catch (e) {
return handleError(e as Error);
Expand Down
25 changes: 25 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { InvalidRequestError, NoRedirectError } from "./errors";
import mime from "mime";

export function getUrl(req: Request) {
const url = new URL(req.url).searchParams.get("url");
Expand All @@ -24,3 +25,27 @@ export function handleError(e: Error) {

return new Response("Internal server error", { status: 500 });
}

export function getMimeType(url: string): string | null {
const extension = new URL(url).pathname.split(".").pop();
if (!extension) {
return null;
}

return mime.getType(extension);
}

export function getProxySafeMediaHeaders(req: Request): Headers {
const headers = new Headers();
const acceptHeader = req.headers.get("accept");
if (acceptHeader) {
headers.set("accept", acceptHeader);
}

const acceptEncodingHeader = req.headers.get("accept-encoding");
if (acceptEncodingHeader) {
headers.set("accept-encoding", acceptEncodingHeader);
}

return headers;
}

0 comments on commit b23adc0

Please sign in to comment.