summaryrefslogtreecommitdiff
path: root/cgithub/src/scraper.ts
diff options
context:
space:
mode:
authorJoe Mou <dev@mou.fo>2026-04-25 13:35:17 -0400
committerJoe Mou <dev@mou.fo>2026-04-28 02:07:40 -0400
commitcfe74db6c177151da3d1bc8549732e3d39448040 (patch)
tree49359a8587e87b2bc857d0a7b7fc1c3c396cde86 /cgithub/src/scraper.ts
parentdbe8141dce07559c1e16f17800dfd505b0ecdb46 (diff)
Support redirects
Diffstat (limited to 'cgithub/src/scraper.ts')
-rw-r--r--cgithub/src/scraper.ts21
1 files changed, 20 insertions, 1 deletions
diff --git a/cgithub/src/scraper.ts b/cgithub/src/scraper.ts
index 2dcd1d0..04a685b 100644
--- a/cgithub/src/scraper.ts
+++ b/cgithub/src/scraper.ts
@@ -13,6 +13,16 @@ export class GitHubHTTPError extends Error {
}
}
+export class RedirectError extends Error {
+ location: string;
+
+ constructor(location: string) {
+ super(`Redirect to ${location}`);
+ this.name = "RedirectError";
+ this.location = location;
+ }
+}
+
interface TreeItem {
contentType: "directory" | "file";
name: string;
@@ -286,7 +296,16 @@ async function fetchGitHubPage(path: string): Promise<string> {
Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
};
- const response = await fetch(`https://github.com/${path}`, { headers });
+ const response = await fetch(`https://github.com/${path}`, { headers, redirect: "manual" });
+ if (response.status === 301 || response.status === 302) {
+ const location = response.headers.get("location");
+ if (location) {
+ const url = new URL(location);
+ if (url.hostname === "github.com") {
+ throw new RedirectError(url.pathname);
+ }
+ }
+ }
if (!response.ok) throw new GitHubHTTPError(response.status, response.statusText);
return response.text();
}