feat: implement safeFetch utility for enhanced HTTP requests handling - #23
Conversation
- Added safeFetch utility in `packages/backend/convex/utils/safeFetch.ts` to handle HTTP requests with robust error handling, including support for timeouts, redirects, and content type validation. - Introduced `fetchMetadataHtml` and `fetchSmallJson` functions for specific use cases. - Implemented IP address blocking logic to prevent requests to private or blocked IPs. - Created comprehensive tests for safeFetch functionality in `packages/backend/tests/safeFetch.test.ts`, covering various scenarios including blocked IPs, unsupported content types, and redirect handling. - Added utility functions to detect blocked pages in `packages/common/utils/blockedPage.ts` to identify common patterns in blocked responses.
ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Free Run ID: ⛔ Files ignored due to path filters (2)
📒 Files selected for processing (16)
📝 WalkthroughWalkthroughThis PR introduces a comprehensive safe-fetching and anti-bot detection system for article extraction. It adds DNS-guarded HTTP fetching with IP blocking, timeout enforcement, and response validation; deploys shared anti-bot page detection across extraction and metadata fetching; hardens the browser launcher with stealth plugins; adds structured logging throughout; and refactors OpenGraph and extractor handling to use the new safe-fetch infrastructure and block-detection helpers. ChangesSafe Fetching and Anti-Bot Infrastructure
Extractor Service Hardening
Metadata and Link Handling Integration
Sequence DiagramsequenceDiagram
participant Client
participant Handler as OpenGraph Handler
participant SafeFetch
participant DNS as DNS Lookup
participant Extractor as Extractor Service
participant Readability as Readability Parser
Client->>Handler: handleOpenGraph(linkId)
Handler->>SafeFetch: fetchMetadataHtml(url)
SafeFetch->>DNS: resolve hostname
DNS-->>SafeFetch: public IP or blocked error
SafeFetch->>SafeFetch: validate response content-type & size
SafeFetch-->>Handler: {ok, text} or {ok: false, reason}
alt Direct fetch succeeded
Handler->>Handler: parse HTML, sanitize, detect blocked page
alt Not blocked & content usable
Handler-->>Client: OpenGraph fields updated
else Still looks blocked or no content
Handler->>Extractor: callExtractor(url)
Extractor->>Readability: extractReadableArticle
Readability-->>Extractor: article or null
Extractor-->>Handler: extraction result
Handler->>Handler: validate extracted content not blocked
Handler-->>Client: OpenGraph with fallback content or error
end
else Direct fetch failed
Handler->>Handler: update embed with fallback favicon/title
Handler->>Extractor: tryExtractorFallback
Extractor->>Readability: extractReadableArticle
Readability-->>Extractor: article or null
Extractor-->>Handler: extraction result
alt Extractor succeeded & content usable
Handler-->>Client: OpenGraph with extractor fields
else Extractor failed or looks blocked
Handler-->>Client: OpenGraph error or minimal data
end
end
Estimated Code Review Effort🎯 4 (Complex) | ⏱️ ~60 minutes Poem
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. 👉 Get your free trial and get 200 agent minutes per Slack user (a $50 value). Note 🎁 Summarized by CodeRabbit FreeYour organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login. Review rate limit: 2/3 reviews remaining, refill in 20 minutes. Comment |
packages/backend/convex/utils/safeFetch.tsto handle HTTP requests with robust error handling, including support for timeouts, redirects, and content type validation.fetchMetadataHtmlandfetchSmallJsonfunctions for specific use cases.packages/backend/tests/safeFetch.test.ts, covering various scenarios including blocked IPs, unsupported content types, and redirect handling.packages/common/utils/blockedPage.tsto identify common patterns in blocked responses.Summary by CodeRabbit
Release Notes
New Features
Improvements