Repository navigation
feat(connectors): crawl a website by its links when it has no sitemap, keeping robots.txt Crawl-delay #10553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
waleedlatif1
wants to merge
4
commits into
staging
Choose a base branch
from
feat/website-links-mode
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+728
−103
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
978ea55
feat(connectors): crawl a website by its links when it has no sitemap…
waleedlatif1 07ee6f7
fix(connectors): resolve crawled links where the page was served, and…
waleedlatif1 6769a58
fix(connectors): bound link crawls by their deadline and honor nofoll…
waleedlatif1 fc1ae50
fix(connectors): let a start page outside the include paths redirect,…
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import { mapSettledWithConcurrency } from '@sim/utils/concurrency' | ||
| import { | ||
| scopedPage, | ||
| type WebsiteDiscovery, | ||
| type WebsitePage, | ||
| type WebsiteScope, | ||
| } from '@/connectors/website/sitemap' | ||
|
|
||
| /** Pages read at once while crawling, bounding the load a sync puts on one site. */ | ||
| const CRAWL_CONCURRENCY = 4 | ||
|
|
||
| /** | ||
| * What reading one page told the crawl: whether the page belongs in the listing (with the hash of | ||
| * its text, the listing's change indicator), and the links it offers. A page the site no longer | ||
| * serves, or that is not an HTML page, is not listed. | ||
| */ | ||
| export interface CrawledPage { | ||
| listed: boolean | ||
| contentHash?: string | ||
| links: string[] | ||
| /** Where the page's relative links resolve: its final URL after redirects, or its `<base>`. */ | ||
| base?: string | ||
| /** The page exists but could not be read, so the links it holds are unknown. */ | ||
| unreadable?: boolean | ||
| } | ||
|
|
||
| interface CrawlLimits { | ||
| maxPages: number | ||
| maxDepth: number | ||
| /** | ||
| * Ends the crawl: reads in flight are cancelled and none start after it, and what the crawl found | ||
| * by then is a truncated listing. | ||
| */ | ||
| signal: AbortSignal | ||
| /** Stops the crawl once this many pages are listed, when only some are needed. */ | ||
| maxListed?: number | ||
| } | ||
|
|
||
| /** | ||
| * Discovers a site's pages by following links breadth-first from the configured URL: each | ||
| * in-scope page is read once, never deeper than `maxDepth` links from the start, and no more pages | ||
| * are queued than `maxPages`. A crawl cut by the page cap or its signal is truncated, and one | ||
| * where a page could not be read is incomplete, so neither lets an unlisted page read as removed. | ||
| * A configured URL outside the include paths is still read for its links, but not listed: `read` | ||
| * receives the scope a page and its redirects must stay within. | ||
| */ | ||
| export async function crawlSitePages( | ||
| scope: WebsiteScope, | ||
| read: (page: WebsitePage, signal: AbortSignal, within: WebsiteScope) => Promise<CrawledPage>, | ||
| limits: CrawlLimits | ||
| ): Promise<WebsiteDiscovery> { | ||
| // Relative links resolve against the URL a page is finally served at, as a browser's would. | ||
| const seed = (within: WebsiteScope) => | ||
| scopedPage(scope.site.baseUrl, within) ?? scopedPage(`${scope.site.baseUrl}/`, within) | ||
| const listedStart = seed(scope) | ||
| const startScope = listedStart ? scope : { ...scope, include: [] } | ||
| const start = listedStart ?? seed(startScope) | ||
| if (!start) | ||
| throw new Error( | ||
| 'The website URL is excluded by the configured paths or disallowed by robots.txt' | ||
| ) | ||
| const pages: WebsitePage[] = [] | ||
| const seen = new Set([start.url]) | ||
| let level = [start] | ||
| let truncated = false | ||
| let incomplete = false | ||
| for (let depth = 0; level.length; depth++) { | ||
| const next: WebsitePage[] = [] | ||
| for (let offset = 0; offset < level.length; offset += CRAWL_CONCURRENCY) { | ||
| if (limits.signal.aborted) return { pages, truncated: true, incomplete } | ||
| const batch = level.slice(offset, offset + CRAWL_CONCURRENCY) | ||
| const results = await mapSettledWithConcurrency(batch, CRAWL_CONCURRENCY, (page) => | ||
| read(page, limits.signal, depth === 0 ? startScope : scope) | ||
| ) | ||
| for (const [index, result] of results.entries()) { | ||
| const page = batch[index] | ||
| if (!page) continue | ||
| if (result.status === 'rejected' || result.value.unreadable) { | ||
| incomplete = true | ||
| continue | ||
| } | ||
| if (result.value.listed && (depth > 0 || listedStart)) | ||
| pages.push({ ...page, contentHash: result.value.contentHash }) | ||
| if (limits.maxListed && pages.length >= limits.maxListed) | ||
| return { pages, truncated: true, incomplete } | ||
| if (depth >= limits.maxDepth) continue | ||
| const before = seen.size | ||
| for (const link of result.value.links) { | ||
| const found = scopedPage(link, scope, result.value.base ?? page.url) | ||
| if (!found || seen.has(found.url)) continue | ||
| if (seen.size >= limits.maxPages) { | ||
| truncated = true | ||
| break | ||
| } | ||
| seen.add(found.url) | ||
| next.push(found) | ||
| } | ||
| // A start page that leads nowhere is more likely unread (a client-rendered site) than a | ||
| // site of one page, so it does not prove the site's other pages are gone. | ||
| if (depth === 0 && seen.size === before && !truncated) incomplete = true | ||
| } | ||
| } | ||
| level = next | ||
| } | ||
| return { pages, truncated, incomplete } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.