diff --git a/backend/src/services/puppeteer-preflight.ts b/backend/src/services/puppeteer-preflight.ts index 276c0dd4..ea26835e 100644 --- a/backend/src/services/puppeteer-preflight.ts +++ b/backend/src/services/puppeteer-preflight.ts @@ -13,7 +13,7 @@ * Based on test-intercept.js which successfully captures 1000+ products */ -import { PreflightResult, CrawlRotator } from './crawl-rotator'; +import { PreflightResult, CrawlRotator, getEvomiConfig, buildEvomiProxyUrl } from './crawl-rotator'; // GraphQL hash for FilteredProducts query - MUST match CLAUDE.md const FILTERED_PRODUCTS_HASH = 'ee29c060826dc41c527e470e9ae502c9b2c169720faa0a9f5d25e1b9a530a4a0'; @@ -105,26 +105,41 @@ export async function runPuppeteerPreflight( let browser: any = null; try { - // Step 0: Get a proxy from the pool + // Step 0: Get a proxy - prefer Evomi API, fall back to DB pool let proxyUrl: string | null = null; let expectedProxyHost: string | null = null; - if (crawlRotator) { + // Try Evomi first (dynamic residential proxies) + const evomiConfig = getEvomiConfig(); + if (evomiConfig.enabled) { + // Use AZ as default state for preflight testing + const evomiProxy = buildEvomiProxyUrl('AZ', 'preflight-test'); + if (evomiProxy) { + result.proxyAvailable = true; + proxyUrl = evomiProxy.url; + expectedProxyHost = evomiConfig.host; + result.expectedProxyIp = expectedProxyHost; + console.log(`[PuppeteerPreflight] Using Evomi proxy: ${evomiProxy.geo}`); + } + } + + // Fall back to DB pool if Evomi not available + if (!proxyUrl && crawlRotator) { const currentProxy = crawlRotator.proxy.getCurrent(); if (currentProxy) { result.proxyAvailable = true; proxyUrl = crawlRotator.proxy.getProxyUrl(currentProxy); expectedProxyHost = currentProxy.host; result.expectedProxyIp = expectedProxyHost; - console.log(`[PuppeteerPreflight] Using proxy: ${currentProxy.host}:${currentProxy.port}`); - } else { - result.error = 'No proxy available from pool'; - console.log(`[PuppeteerPreflight] FAILED - No proxy available`); - return result; + console.log(`[PuppeteerPreflight] Using DB proxy: ${currentProxy.host}:${currentProxy.port}`); } - } else { - console.log(`[PuppeteerPreflight] WARNING: No CrawlRotator provided - using direct connection`); - result.proxyAvailable = true; // No proxy needed for direct + } + + // No proxy available from either source + if (!proxyUrl) { + result.error = 'No proxy available (Evomi not configured, DB pool empty)'; + console.log(`[PuppeteerPreflight] FAILED - No proxy available`); + return result; } // Dynamic imports to avoid loading Puppeteer unless needed