| 1 | import { URL } from "node:url"; |
| 2 | |
| 3 | import { getHeaders } from "../../../continueServer/stubs/headers"; |
| 4 | import { TRIAL_PROXY_URL } from "../../../control-plane/client"; |
| 5 | import { PageData } from "./DocsCrawler"; |
| 6 | |
| 7 | export class DefaultCrawler { |
| 8 | constructor( |
| 9 | private readonly startUrl: URL, |
| 10 | private readonly maxRequestsPerCrawl: number, |
| 11 | private readonly maxDepth: number, |
| 12 | ) {} |
| 13 | |
| 14 | async crawl(): Promise<PageData[]> { |
| 15 | const resp = await fetch(new URL("crawl", TRIAL_PROXY_URL).toString(), { |
| 16 | method: "POST", |
| 17 | headers: { |
| 18 | "Content-Type": "application/json", |
| 19 | ...(await getHeaders()), |
| 20 | }, |
| 21 | body: JSON.stringify({ |
| 22 | startUrl: this.startUrl.toString(), |
| 23 | maxDepth: this.maxDepth, |
| 24 | limit: this.maxRequestsPerCrawl, |
| 25 | }), |
| 26 | }); |
| 27 | if (!resp.ok) { |
| 28 | const text = await resp.text(); |
| 29 | throw new Error(`Failed to crawl site (${resp.status}): ${text}`); |
| 30 | } |
| 31 | const json = (await resp.json()) as PageData[]; |
| 32 | return json; |
| 33 | } |
| 34 | } |