-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75b5172
commit e727a2c
Showing
3 changed files
with
49 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
} |