|
| 1 | +import { Octokit } from "@octokit/rest"; |
| 2 | +import { URL } from "node:url"; |
| 3 | +import { EventEmitter } from "events"; |
| 4 | +import { getChromiumPath, getContinueUtilsPath } from "../../util/paths"; |
| 5 | +import { executablePath, Page } from "puppeteer"; |
| 6 | +// @ts-ignore |
| 7 | +import PCR from "puppeteer-chromium-resolver"; |
| 8 | +import * as fs from "fs"; |
| 9 | + |
| 10 | +export type PageData = { |
| 11 | + url: string; |
| 12 | + path: string; |
| 13 | + content: string; |
| 14 | +}; |
| 15 | + |
| 16 | +const MAX_TIME_TO_CRAWL = 1000 * 5; |
| 17 | +const LINK_GROUP_SIZE = 2; // Controls parallelization of crawler |
| 18 | +const GITHUB_HOST = "github.com"; |
| 19 | +const MAX_REQUESTS_PER_CRAWL = 1000; |
| 20 | +const markdownRegex = new RegExp(/\.(md|mdx)$/); |
| 21 | +const octokit = new Octokit({ |
| 22 | + auth: undefined, |
| 23 | +}); |
| 24 | + |
| 25 | +const PCR_CONFIG = { |
| 26 | + downloadPath: getContinueUtilsPath(), |
| 27 | +}; |
| 28 | + |
| 29 | +export function verifyOrInstallChromium() { |
| 30 | + if (!fs.existsSync(getChromiumPath())) { |
| 31 | + PCR(PCR_CONFIG); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +async function getGithubRepoDefaultBranch( |
| 36 | + owner: string, |
| 37 | + repo: string, |
| 38 | +): Promise<string> { |
| 39 | + const repoInfo = await octokit.repos.get({ |
| 40 | + owner, |
| 41 | + repo, |
| 42 | + }); |
| 43 | + |
| 44 | + return repoInfo.data.default_branch; |
| 45 | +} |
| 46 | + |
| 47 | +async function getGitHubRepoPaths(owner: string, repo: string, branch: string) { |
| 48 | + const tree = await octokit.request( |
| 49 | + "GET /repos/{owner}/{repo}/git/trees/{tree_sha}", |
| 50 | + { |
| 51 | + owner, |
| 52 | + repo, |
| 53 | + tree_sha: branch, |
| 54 | + headers: { |
| 55 | + "X-GitHub-Api-Version": "2022-11-28", |
| 56 | + }, |
| 57 | + recursive: "true", |
| 58 | + }, |
| 59 | + ); |
| 60 | + |
| 61 | + const paths = tree.data.tree |
| 62 | + .filter( |
| 63 | + (file: any) => |
| 64 | + file.type === "blob" && markdownRegex.test(file.path ?? ""), |
| 65 | + ) |
| 66 | + .map((file: any) => file.path); |
| 67 | + |
| 68 | + return paths; |
| 69 | +} |
| 70 | + |
| 71 | +async function getGithubRepoFileContent( |
| 72 | + path: string, |
| 73 | + owner: string, |
| 74 | + repo: string, |
| 75 | +) { |
| 76 | + try { |
| 77 | + const response = await octokit.repos.getContent({ |
| 78 | + owner, |
| 79 | + repo, |
| 80 | + path, |
| 81 | + headers: { |
| 82 | + Accept: "application/vnd.github.raw+json", |
| 83 | + }, |
| 84 | + }); |
| 85 | + |
| 86 | + return response.data as unknown as string; |
| 87 | + } catch (error) { |
| 88 | + console.debug("Error fetching file contents:", error); |
| 89 | + return null; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +async function* crawlGithubRepo(url: URL) { |
| 94 | + const urlStr = url.toString(); |
| 95 | + const [_, owner, repo] = url.pathname.split("/"); |
| 96 | + const branch = await getGithubRepoDefaultBranch(owner, repo); |
| 97 | + const paths = await getGitHubRepoPaths(owner, repo, branch); |
| 98 | + |
| 99 | + for await (const path of paths) { |
| 100 | + const content = await getGithubRepoFileContent(path, owner, repo); |
| 101 | + |
| 102 | + yield { |
| 103 | + path, |
| 104 | + url: urlStr, |
| 105 | + content: content ?? "", |
| 106 | + }; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +async function getLinksFromPage(page: Page) { |
| 111 | + // The URL lib is not available by default in the page scope, |
| 112 | + // so we need to expose it to the page through this fn. |
| 113 | + await page.exposeFunction( |
| 114 | + "getCleanedUrlFromAnchorTag", |
| 115 | + (a: HTMLAnchorElement) => { |
| 116 | + let url = new URL(a.href); |
| 117 | + url.hash = ""; |
| 118 | + return url.href; |
| 119 | + }, |
| 120 | + ); |
| 121 | + |
| 122 | + const links: string[] = await page.$$eval("a", (links) => |
| 123 | + links.map((a) => (window as any).getCleanedUrlFromAnchorTag), |
| 124 | + ); |
| 125 | + |
| 126 | + return links; |
| 127 | +} |
| 128 | + |
| 129 | +async function getLinkGroups(page: Page) { |
| 130 | + const links = await getLinksFromPage(page); |
| 131 | + |
| 132 | + const groups = links.reduce((acc, link, i) => { |
| 133 | + const groupIndex = Math.floor(i / LINK_GROUP_SIZE); |
| 134 | + |
| 135 | + if (!acc[groupIndex]) { |
| 136 | + acc.push([]); |
| 137 | + } |
| 138 | + |
| 139 | + acc[groupIndex].push(link); |
| 140 | + |
| 141 | + return acc; |
| 142 | + }, [] as string[][]); |
| 143 | + |
| 144 | + return groups; |
| 145 | +} |
| 146 | + |
| 147 | +function shouldSkipPage(url: URL, rootUrl: URL, visitedLinks: Set<string>) { |
| 148 | + const hasVisitedLink = visitedLinks.has(url.toString()); |
| 149 | + const isInvalidHostOrPath = |
| 150 | + !url.pathname.startsWith(rootUrl.pathname) || rootUrl.host !== url.host; |
| 151 | + |
| 152 | + return hasVisitedLink || isInvalidHostOrPath; |
| 153 | +} |
| 154 | + |
| 155 | +async function* crawlSitePages( |
| 156 | + page: Page, |
| 157 | + url: URL, |
| 158 | + rootUrl: URL, |
| 159 | + maxRequestsPerCrawl: number, |
| 160 | + visitedLinks: Set<string> = new Set(), |
| 161 | + currentRequests: number = 0, |
| 162 | +): AsyncGenerator<any> { |
| 163 | + if (currentRequests >= maxRequestsPerCrawl) { |
| 164 | + console.warn("Max requests per crawl reached. Stopping crawler."); |
| 165 | + return; |
| 166 | + } |
| 167 | + |
| 168 | + if (shouldSkipPage(url, rootUrl, visitedLinks)) { |
| 169 | + console.warn("Skipping ", url.toString()); |
| 170 | + return; |
| 171 | + } |
| 172 | + |
| 173 | + await page.goto(url.toString()); |
| 174 | + |
| 175 | + const htmlContent = await page.content(); |
| 176 | + const linkGroups = await getLinkGroups(page); |
| 177 | + const requestCount = currentRequests + 1; |
| 178 | + |
| 179 | + visitedLinks.add(url.toString()); |
| 180 | + |
| 181 | + yield { |
| 182 | + path: url.pathname, |
| 183 | + url: url.toString(), |
| 184 | + content: htmlContent, |
| 185 | + }; |
| 186 | + |
| 187 | + for (const linkGroup of linkGroups) { |
| 188 | + for (const link of linkGroup) { |
| 189 | + yield* crawlSitePages( |
| 190 | + page, |
| 191 | + new URL(link), |
| 192 | + rootUrl, |
| 193 | + maxRequestsPerCrawl, |
| 194 | + visitedLinks, |
| 195 | + requestCount, |
| 196 | + ); |
| 197 | + } |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +async function* crawlSite( |
| 202 | + startUrl: URL, |
| 203 | + rootUrl: URL, |
| 204 | + maxRequestsPerCrawl: number, |
| 205 | +): AsyncGenerator<PageData> { |
| 206 | + const stats = await PCR(PCR_CONFIG); |
| 207 | + |
| 208 | + const browser = await stats.puppeteer.launch({ |
| 209 | + args: [ |
| 210 | + "--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36", |
| 211 | + ], |
| 212 | + executablePath: stats.executablePath, |
| 213 | + // From the docs: https://pptr.dev/guides/headless-modes |
| 214 | + // If the performance is more important for your use case, switch to chrome-headless-shell as following: |
| 215 | + // { headless: "shell" } |
| 216 | + headless: "shell", |
| 217 | + }); |
| 218 | + |
| 219 | + const page = await browser.newPage(); |
| 220 | + |
| 221 | + try { |
| 222 | + yield* crawlSitePages(page, startUrl, rootUrl, maxRequestsPerCrawl); |
| 223 | + } catch (e) { |
| 224 | + console.debug("Error getting links: ", e); |
| 225 | + } finally { |
| 226 | + await browser.close(); |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +export async function* crawl( |
| 231 | + url: URL, |
| 232 | + maxRequestsPerCrawl: number = MAX_REQUESTS_PER_CRAWL, |
| 233 | +): AsyncGenerator<PageData> { |
| 234 | + if (url.host === GITHUB_HOST) { |
| 235 | + yield* crawlGithubRepo(url); |
| 236 | + } else { |
| 237 | + // TODO: Why both |
| 238 | + yield* crawlSite(url, url, maxRequestsPerCrawl); |
| 239 | + } |
| 240 | +} |
0 commit comments