Tech Stack
Every experiment uses a consistent, modern tech stack optimized for edge computing:Core Technologies
TypeScript
Strictly typed codebase with full type safety.All experiments use TypeScript with:
- Strict mode enabled
- No
anytypes - Full type coverage
tsconfig.jsonper experiment
src/types/env.d.ts.Hono
Fast, lightweight web framework for Cloudflare Workers.Benefits:
- Ultra-small bundle size (~12KB)
- Type-safe routing
- Middleware support
- Web standard APIs
- Built for edge runtime
Cloudflare Workers
Serverless runtime at the edge.Features:
- V8 isolate-based execution
- Standard Web APIs
- Sub-millisecond cold starts
- Global deployment
- Zero configuration scaling
Wrangler
CLI tool for Workers development and deployment.Used for:
- Local development (
wrangler dev) - Deployment (
wrangler deploy) - Resource management (D1, KV, R2)
- Secret management
Project Structure
Every experiment follows an identical directory structure for consistency:Key Principles
- Single Responsibility: One experiment demonstrates one Cloudflare feature
- No Shared Code: Each experiment is completely independent
- Consistent Structure: Same directory layout across all experiments
- Type Safety: Full TypeScript coverage with strict mode
Common Patterns
Experiments implement several recurring architectural patterns:1. Fetch + Parse Pattern
Fetch external content and extract structured data. Used in: Dependency Analyzer, Website Metadata Extractor, Website DevTools Inspector- Validate user input (URL)
- Fetch external content
- Parse/extract data with regex or HTMLRewriter
- Return JSON response
- Handle errors consistently
2. AI Integration Pattern
Fetch content, extract text, and process with Workers AI. Used in: AI Website Summary, GitHub Repo Explainer- Fetch external content
- Extract relevant text (limit to model context)
- Send to Workers AI with structured prompt
- Return AI response
3. Browser Automation Pattern
Use headless browser to interact with pages. Used in: Screenshot API- Launch browser session
- Configure viewport and navigation options
- Navigate to URL and wait for load
- Perform action (screenshot, scraping, etc.)
- Always close browser (cleanup)
- Return binary or JSON response
4. Storage Pattern (D1 + KV)
Combine D1 (primary) with KV (cache) for optimal performance. Used in: Link Shortener- D1: Source of truth, handles writes and cache misses
- KV: Read-through cache, optimized for redirects
- Pattern: Read from KV → Miss → Read D1 → Cache in KV → Return
5. Object Storage Pattern
Public and private R2 buckets with direct access. Used in: R2 Storage- Private bucket: Objects only accessible via Worker
- Public bucket: Direct URLs for CDN delivery
- Pattern: Choose bucket → Upload → Return URL (public) or key (private)
Error Handling
Consistent error handling across all experiments:Response Utilities
Global Error Handler
Every experiment registers a global error handler:Error Codes
Standardized error codes used across experiments:| Code | Status | Description |
|---|---|---|
INVALID_URL | 400 | Missing or malformed URL parameter |
INVALID_INPUT | 400 | Invalid request body or parameters |
NOT_FOUND | 404 | Resource not found (links, objects) |
FETCH_ERROR | 502 | Failed to fetch external resource |
PARSE_ERROR | 502 | Failed to parse HTML or response |
AI_ERROR | 502 | Workers AI inference failed |
SCREENSHOT_ERROR | 502 | Browser Rendering failed |
DATABASE_ERROR | 500 | D1 query failed |
STORAGE_ERROR | 500 | KV or R2 operation failed |
INTERNAL_ERROR | 500 | Uncaught/unexpected error |
Validation
Input validation is critical for security and reliability:URL Validation
Request Body Validation
HTML Parsing
Experiments use regex-based parsing for lightweight HTML extraction:- Lower memory overhead
- Faster for simple extraction
- No need for full DOM representation
- Works well at the edge with streaming
Configuration
Configuration is managed viawrangler.json and environment variables:
wrangler.json Structure
Environment Bindings
Type bindings insrc/types/env.d.ts:
Performance Optimization
Experiments follow edge-optimized performance patterns:1. Minimize Latency
- Use edge-native APIs (fetch, KV)
- Stream responses when possible
- Avoid buffering large payloads
2. Optimize Cold Starts
- Keep bundle size small (Hono ~12KB)
- Minimal dependencies
- Tree-shaking via esbuild
3. Cache Effectively
- Use KV for read-heavy data
- Set appropriate Cache-Control headers
- Leverage Cloudflare’s CDN
4. Handle Timeouts
- Set reasonable fetch timeouts
- Browser navigation timeouts
- Graceful degradation
Security Best Practices
Input Validation
Always validate user input:
- URL format and protocol
- JSON structure
- Parameter types and ranges
Error Messages
Never expose internal details:
- Generic error messages for users
- Specific errors logged internally
- No stack traces in responses
Rate Limiting
Protect against abuse:
- Cloudflare Rate Limiting rules
- Per-IP limits
- Per-endpoint limits
Secret Management
Store secrets securely:
- Use Wrangler secrets (encrypted)
- Never commit secrets to git
- Rotate keys regularly