/** * Copyright 2026 aphrody-code * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @module bxc/crawler/BrowserCrawler * * Crawler that loads pages using bxc's native Browser (supporting static, fast, stealth, or max profiles) * and evaluates interactions on them. */ import * as cheerio from "cheerio"; import { BasicCrawler, type CrawlContext, type BasicCrawlerOptions, } from "./Crawler.ts"; import { WorkerPool } from "./WorkerPool.ts"; export interface BrowserCrawlingContext extends CrawlContext { page: any; response: any; $: cheerio.CheerioAPI; } export interface BrowserCrawlerOptions extends BasicCrawlerOptions { profile?: "static" | "http" | "fast" | "stealth" | "max"; headless?: boolean; cookies?: string | any[]; userAgent?: string; viewport?: { width: number; height: number }; insecure?: boolean; proxy?: string; proxyPool?: string[]; proxyAuth?: string; spawnOpts?: any; useWorkers?: boolean; requestHandlerPath?: string; } export class BrowserCrawler extends BasicCrawler { private profile: "static" | "http" | "fast" | "stealth" | "max"; private headless: boolean; private workerPool: WorkerPool | null = null; protected declare options: BrowserCrawlerOptions; constructor(options: BrowserCrawlerOptions) { super(options); this.profile = options.profile ?? "stealth"; this.headless = options.headless ?? true; if (options.useWorkers) { this.workerPool = new WorkerPool({ minWorkers: options.minConcurrency ?? 2, maxWorkers: options.maxConcurrency ?? 20, onPushData: async (data) => { if (this.dataset) await this.dataset.pushData(data); }, onAddRequests: (urls) => { this.requestQueue.addRequests(urls); }, onLog: (msg) => this.log(msg), }); } } async processRequest(req: any): Promise { if (this.options.useWorkers) { await this.processRequestWithWorker(req); } else { await this.processRequestDirect(req); } } private async processRequestDirect(req: any): Promise { const { isCrawlFailure, profilesOrder } = require("./crawl-utils.ts"); const startProfile = this.profile; const idx = profilesOrder.indexOf(startProfile); const escalationPath = idx === -1 ? [startProfile] : profilesOrder.slice(idx); let lastError: Error | null = null; let success = false; for (const profile of escalationPath) { let selectedProxy = this.options.proxy; if (this.options.proxyPool && this.options.proxyPool.length > 0) { const proxyIdx = Math.floor(Math.random() * this.options.proxyPool.length); selectedProxy = this.options.proxyPool[proxyIdx]; } this.log(`BrowserCrawler (${profile}) crawling: ${req.url} (proxy: ${selectedProxy || "none"})`); const { Browser } = require("../api/browser.ts"); let page: any = null; try { page = await Browser.newPage({ profile: profile, headless: this.headless, cookies: this.options.cookies, userAgent: this.options.userAgent, viewport: this.options.viewport, insecure: this.options.insecure, proxy: selectedProxy, proxyAuth: this.options.proxyAuth, spawnOpts: this.options.spawnOpts, }); const response = await page.goto(req.url); let body = await page.content(); let title = await page.title(); const status = response?.status; if (body.replace(/<[^>]*>/g, " ").trim().length < 50) { await new Promise((resolve) => setTimeout(resolve, 3000)); body = await page.content(); title = await page.title(); } if (isCrawlFailure(status, body, title)) { throw new Error( `Crawl failure detected (status: ${status}, title: "${title}", content length: ${body.length})` ); } let cachedCheerio: cheerio.CheerioAPI | null = null; const getCheerio = () => { if (!cachedCheerio) { cachedCheerio = cheerio.load(body); } return cachedCheerio; }; const context: BrowserCrawlingContext = { request: req, page, response, get $() { return getCheerio(); }, enqueueLinks: async (opts) => { const selector = opts?.selector ?? "a[href]"; const allowedDomains = opts?.allowedDomains; const $ = getCheerio(); const links: Array<{ url: string; opts?: any }> = []; const base = new URL(req.url); $(selector).each((_, el) => { const href = $(el).attr("href"); if ( href && !href.startsWith("javascript:") && !href.startsWith("#") && !href.startsWith("mailto:") ) { try { const absUrl = new URL(href, base).href; if (opts?.exclude?.some((motif) => motif.test(absUrl))) return; if (allowedDomains) { const hostname = new URL(absUrl).hostname; if ( allowedDomains.some( (d) => hostname === d || hostname.endsWith("." + d), ) ) { links.push({ url: absUrl, opts: opts?.userData ? { userData: opts.userData } : {}, }); } } else { links.push({ url: absUrl, opts: opts?.userData ? { userData: opts.userData } : {}, }); } } catch { // Ignore } } }); if (links.length > 0) { this.requestQueue.addRequests(links); } }, pushData: async (data) => { if (this.dataset) { await this.dataset.pushData(data); } }, log: (msg) => this.log(msg), }; await this.requestHandler(context); success = true; break; } catch (err) { const msg = err instanceof Error ? err.message : String(err); this.log(`BrowserCrawler profile ${profile} failed for ${req.url}: ${msg}. Escalating...`); lastError = err instanceof Error ? err : new Error(msg); } finally { if (page) { try { await page.close(); } catch {} } } } if (!success) { throw lastError ?? new Error(`Failed to crawl ${req.url} with all profiles in escalation path.`); } } private async processRequestWithWorker(req: any): Promise { if (!this.workerPool) { this.workerPool = new WorkerPool({ minWorkers: this.options.minConcurrency ?? 2, maxWorkers: this.options.maxConcurrency ?? 20, onPushData: async (data) => { if (this.dataset) await this.dataset.pushData(data); }, onAddRequests: (urls) => { this.requestQueue.addRequests(urls); }, onLog: (msg) => this.log(msg), }); } let handlerCode: string | undefined; if (!this.options.requestHandlerPath) { handlerCode = this.requestHandler.toString(); } let selectedProxy = this.options.proxy; if (this.options.proxyPool && this.options.proxyPool.length > 0) { const idx = Math.floor(Math.random() * this.options.proxyPool.length); selectedProxy = this.options.proxyPool[idx]; } await this.workerPool.dispatch( req, { profile: this.profile, headless: this.headless, cookies: this.options.cookies, userAgent: this.options.userAgent, viewport: this.options.viewport, insecure: this.options.insecure, proxy: selectedProxy, proxyAuth: this.options.proxyAuth, spawnOpts: this.options.spawnOpts, }, handlerCode, this.options.requestHandlerPath, ); } override stop(): void { super.stop(); this.workerPool?.terminate(); } }