diff options
| author | Joe Mou <dev@mou.fo> | 2026-04-25 13:35:17 -0400 |
|---|---|---|
| committer | Joe Mou <dev@mou.fo> | 2026-04-28 02:07:40 -0400 |
| commit | 0d8911577ba3d49df3577d686837541a9aee69bd (patch) | |
| tree | 638be4b23ee14a30fa895d11ccdf2d3779c56b32 /src/scraper.ts | |
| parent | f179a3f77c6617b4375e6149cc696ae5dab4d171 (diff) | |
Support redirects
Diffstat (limited to 'src/scraper.ts')
| -rw-r--r-- | src/scraper.ts | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/src/scraper.ts b/src/scraper.ts index 2dcd1d0..04a685b 100644 --- a/src/scraper.ts +++ b/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(); } |
