( sessionService: SessionService, browserService: CDPService, request: SearchRequest, reply: FastifyReply, )
| 338 | }; |
| 339 | |
| 340 | export const handleSearch = async ( |
| 341 | sessionService: SessionService, |
| 342 | browserService: CDPService, |
| 343 | request: SearchRequest, |
| 344 | reply: FastifyReply, |
| 345 | ) => { |
| 346 | const startTime = Date.now(); |
| 347 | let times: Record<string, number> = {}; |
| 348 | const { query, proxyUrl, logUrl } = request.body; |
| 349 | |
| 350 | let proxy: IProxyServer | null = null; |
| 351 | let context: BrowserContext | null = null; |
| 352 | |
| 353 | try { |
| 354 | if (proxyUrl) { |
| 355 | proxy = await sessionService.proxyFactory(proxyUrl); |
| 356 | await proxy.listen(); |
| 357 | } |
| 358 | times.proxyTime = Date.now() - startTime; |
| 359 | |
| 360 | let page: Page; |
| 361 | |
| 362 | if (!browserService.isRunning()) { |
| 363 | await browserService.launch(); |
| 364 | } |
| 365 | |
| 366 | if (proxy) { |
| 367 | // If a proxy is used, we proceed with browser navigation; implementing proxy-aware Node fetch |
| 368 | // would require an HTTP agent and is outside current scope. |
| 369 | context = await browserService.createBrowserContext(proxy.url); |
| 370 | page = await context.newPage(); |
| 371 | times.proxyPageTime = Date.now() - startTime - times.proxyTime; |
| 372 | } else { |
| 373 | page = await browserService.getPrimaryPage(); |
| 374 | times.pageTime = Date.now() - startTime - times.proxyTime; |
| 375 | } |
| 376 | |
| 377 | await page.evaluate(() => { |
| 378 | (window as any).__name = (func: Function) => func; |
| 379 | }); |
| 380 | |
| 381 | // Go to Brave |
| 382 | await page.goto(`https://search.brave.com/search?q=${encodeURIComponent(query)}`, { |
| 383 | waitUntil: "networkidle2", |
| 384 | }); |
| 385 | |
| 386 | // Wait for results to load |
| 387 | await page.waitForSelector("#results"); |
| 388 | |
| 389 | // Scrape results |
| 390 | const results = await page.evaluate(() => { |
| 391 | const items = document.querySelectorAll("div.snippet"); |
| 392 | |
| 393 | return Array.from(items) |
| 394 | .map((item) => { |
| 395 | if ( |
| 396 | [ |
| 397 | "llm-snippet", |
no test coverage detected