Skip to content

Commit

Permalink
fix issue #3
Browse files Browse the repository at this point in the history
  • Loading branch information
georg-stone committed Oct 3, 2024
1 parent 75b5172 commit e727a2c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"@radix-ui/react-slot": "^1.1.0",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"iconv-lite": "^0.6.3",
"jschardet": "^3.1.4",
"lucide-react": "^0.447.0",
"next": "14.2.14",
"react": "^18",
Expand Down
25 changes: 25 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 22 additions & 8 deletions src/app/api/fetchFeed/route.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
// route.js
import jschardet from "jschardet";
import iconv from "iconv-lite";

export async function GET(req) {
const { searchParams } = new URL(req.url);
const url = searchParams.get("url");

if (!url) {
return new Response('Missing "url" parameter', { status: 400 });
return new Response("Request missing required parameter 'url'.", {
status: 400,
});
}

if (url.endsWith("/api/fetchFeed")) {
return new Response("nice try", { status: 400 });
return new Response("You can't recursively call this endpoint.", {
status: 400,
});
}

try {
const response = await fetch(url);

if (!response.ok) {
return new Response("Failed to fetch the URL", {
return new Response("Failed to fetch the feed.", {
status: response.status,
});
}

const text = await response.text();
return new Response(text, {
const buffer = await response.arrayBuffer();

const detectedEncoding = jschardet.detect(Buffer.from(buffer));
let encoding = detectedEncoding.encoding || "utf-8";

const decodedText = iconv.decode(Buffer.from(buffer), encoding);

return new Response(decodedText, {
headers: {
"Content-Type": response.headers.get("Content-Type"),
"Content-Type": response.headers.get("Content-Type") || "text/plain",
},
});
} catch (error) {
return new Response("Error fetching the URL", { status: 500 });
console.error("There was an error while fetching this feed: ", error);
return new Response("There was an error while fetching this feed", {
status: 500,
});
}
}

0 comments on commit e727a2c

Please sign in to comment.