Spaces:
Runtime error
Runtime error
File size: 12,456 Bytes
fb38ec5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | # Steel Browser Architecture
This document provides a comprehensive overview of Steel Browser's architecture, design decisions, and how the various components work together.
## ποΈ High-Level Architecture
Steel Browser follows a modular, plugin-based architecture designed for extensibility and maintainability:
```
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Steel Browser β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Frontend (React UI) β Backend (Fastify API) β
β βββ Session Management β βββ CDP Service β
β βββ Real-time Viewing β βββ Session Management β
β βββ DevTools Integration β βββ File Storage β
β βββ Configuration UI β βββ Plugin System β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Chrome/Chromium Browser β
β βββ Chrome DevTools Protocol (CDP) β
β βββ Browser Extensions β
β βββ Page Contexts β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
```
## π§ Core Components
### 1. CDP Service (`api/src/services/cdp/cdp.service.ts`)
The Chrome DevTools Protocol (CDP) Service is the heart of Steel Browser, managing all browser interactions:
**Responsibilities:**
- Browser lifecycle management (launch, close, restart)
- Page creation and navigation
- WebSocket proxy for CDP connections
- Plugin system coordination
- Session state management
- Context isolation and fingerprinting
**Key Features:**
```typescript
class CDPService extends EventEmitter {
// Browser management
async launch(options?: BrowserLauncherOptions): Promise<Browser>
async shutdown(): Promise<void>
async refreshPrimaryPage(): Promise<void>
// Plugin system
registerPlugin(plugin: BasePlugin): void
unregisterPlugin(pluginName: string): boolean
// Page management
async createPage(): Promise<Page>
async getPages(): Promise<Page[]>
}
```
### 2. Plugin System
Steel Browser's plugin architecture allows for extensible functionality without modifying core code.
#### Base Plugin (`api/src/services/cdp/plugins/core/base-plugin.ts`)
```typescript
abstract class BasePlugin {
// Lifecycle hooks
async onBrowserLaunch(browser: Browser): Promise<void>
async onPageCreated(page: Page): Promise<void>
async onPageNavigate(page: Page): Promise<void>
async onPageUnload(page: Page): Promise<void>
async onBrowserClose(browser: Browser): Promise<void>
async onBeforePageClose(page: Page): Promise<void>
async onShutdown(): Promise<void>
}
```
#### Plugin Manager (`api/src/services/cdp/plugins/core/plugin-manager.ts`)
Coordinates plugin lifecycle and ensures error isolation:
- **Registration**: Manages plugin registration and dependency injection
- **Event Distribution**: Notifies all plugins of browser events
- **Error Handling**: Isolates plugin errors to prevent system crashes
- **Lifecycle Management**: Coordinates plugin startup and shutdown
### 3. Session Management (`api/src/services/session.service.ts`)
Manages browser sessions with isolated contexts:
**Features:**
- Session creation with custom configurations
- Context isolation (cookies, localStorage, sessionStorage)
- Resource cleanup and garbage collection
- Session persistence and restoration
- Concurrent session management
```typescript
interface SessionConfig {
proxy?: ProxyConfig;
userAgent?: string;
viewport?: { width: number; height: number };
extensions?: string[];
fingerprint?: FingerprintOptions;
}
```
### 4. File Storage Service (`api/src/services/file.service.ts`)
Handles file operations with session-scoped storage:
- **Upload Management**: Handles multipart file uploads
- **Download Coordination**: Manages browser downloads
- **Storage Isolation**: Session-scoped file storage
- **Cleanup**: Automatic file cleanup on session end
## π Plugin Architecture Deep Dive
### Plugin Lifecycle
1. **Registration**: Plugins register with the PluginManager
2. **Initialization**: Service dependency injection
3. **Event Handling**: Respond to browser lifecycle events
4. **Cleanup**: Graceful shutdown and resource cleanup
### Event Flow
```
Browser Launch β Plugin.onBrowserLaunch()
β
Page Created β Plugin.onPageCreated()
β
Page Navigate β Plugin.onPageNavigate()
β
Page Unload β Plugin.onPageUnload()
β
Page Close β Plugin.onBeforePageClose()
β
Browser Close β Plugin.onBrowserClose()
β
System Shutdown β Plugin.onShutdown()
```
### Example Plugin Implementation
```typescript
import { BasePlugin, PluginOptions } from '@steel-browser/api/cdp-plugin';
import { Browser, Page } from 'puppeteer-core';
export class AdBlockPlugin extends BasePlugin {
private blockedDomains: Set<string>;
constructor(options: PluginOptions & { blockedDomains?: string[] }) {
super({ name: 'ad-blocker', ...options });
this.blockedDomains = new Set(options.blockedDomains || []);
}
async onPageCreated(page: Page): Promise<void> {
await page.setRequestInterception(true);
page.on('request', (request) => {
const url = new URL(request.url());
if (this.blockedDomains.has(url.hostname)) {
request.abort();
} else {
request.continue();
}
});
}
}
```
## π API Architecture
### Fastify Plugin System
Steel Browser uses Fastify's plugin architecture for modular API design:
```typescript
// Main plugin registration
await fastify.register(steelBrowserPlugin, {
fileStorage: { maxSizePerSession: 100 * MB }
});
// Individual plugins
await fastify.register(browserInstancePlugin);
await fastify.register(sessionPlugin);
await fastify.register(fileStoragePlugin);
```
### Route Organization
Routes are organized by functionality:
- **Actions** (`/v1/*`): Browser automation actions (scrape, screenshot, PDF)
- **Sessions** (`/v1/sessions/*`): Session management
- **CDP** (`/v1/cdp/*`): Direct CDP access
- **Files** (`/v1/files/*`): File upload/download
- **Selenium** (`/selenium/*`): Selenium WebDriver compatibility
### Schema Validation
All API endpoints use Zod schemas for validation:
```typescript
const ScrapeRequestSchema = z.object({
url: z.string().url().optional(),
delay: z.number().optional(),
format: z.array(z.enum(['html','cleaned_html','markdown','readability'])).optional(),
screenshot: z.boolean().optional(),
pdf: z.boolean().optional(),
});
```
## π¨ Frontend Architecture
### React Component Structure
```
src/
βββ components/ # Reusable UI components
β βββ ui/ # Base UI components (buttons, inputs)
β βββ badges/ # Status badges
β βββ icons/ # Icon components
β βββ sessions/ # Session-specific components
βββ containers/ # Page-level containers
βββ contexts/ # React contexts for state management
βββ hooks/ # Custom React hooks
βββ steel-client/ # Auto-generated API client
```
### State Management
- **React Query**: Server state management and caching
- **React Context**: Global application state
- **Local State**: Component-specific state with hooks
### Real-time Updates
WebSocket connections provide real-time updates:
```typescript
// Session monitoring
const { data: sessions } = useQuery({
queryKey: ['sessions'],
queryFn: () => steelClient.sessions.getSessions(),
refetchInterval: 1000 // Real-time updates
});
```
## π Security Architecture
### Input Validation
- **API Level**: Zod schema validation for all inputs
- **Browser Level**: Content Security Policy (CSP) headers
- **File Level**: File type validation and size limits
### Context Isolation
Each session runs in an isolated browser context:
```typescript
const context = await browser.createIncognitoBrowserContext();
context.setDefaultNavigationTimeout(30000);
context.setDefaultTimeout(30000);
```
### Resource Limits
- **Memory**: Browser process memory limits
- **CPU**: Process CPU throttling
- **Storage**: Session-scoped file storage limits
- **Network**: Request rate limiting and proxy support
## π Performance Considerations
### Browser Resource Management
- **Process Isolation**: Each session in separate browser context
- **Memory Cleanup**: Automatic page and context cleanup
- **Connection Pooling**: Reuse CDP connections where possible
### Caching Strategy
- **Static Assets**: Long-term caching for UI assets
- **API Responses**: Short-term caching for session data
- **Browser Cache**: Configurable per-session browser caching
### Scaling Considerations
Current architecture supports:
- **Vertical Scaling**: Multi-core CPU utilization
- **Session Concurrency**: Multiple simultaneous sessions
- **Resource Monitoring**: Memory and CPU usage tracking
Future scaling options:
- **Horizontal Scaling**: Multiple Steel instances
- **Load Balancing**: Session distribution
- **Distributed Storage**: Shared file storage
## π§ͺ Testing Architecture
### Test Structure (Planned)
```
tests/
βββ unit/ # Unit tests for individual components
βββ integration/ # API endpoint integration tests
βββ e2e/ # End-to-end browser automation tests
βββ performance/ # Load and performance tests
```
### Testing Strategy
- **Unit Tests**: Core services and utilities
- **Integration Tests**: API endpoints and database interactions
- **E2E Tests**: Full browser automation workflows
- **Performance Tests**: Load testing and benchmarking
## π§ Configuration Management
### Environment Variables
Configuration through environment variables:
```typescript
const envSchema = z.object({
NODE_ENV: z.enum(['development', 'production', 'test']),
HOST: z.string().default('0.0.0.0'),
PORT: z.string().default('3000'),
CHROME_EXECUTABLE_PATH: z.string().optional(),
CHROME_HEADLESS: z.boolean().default(true),
// ... more configuration options
});
```
### Runtime Configuration
- **Browser Options**: Per-session browser configuration
- **Plugin Configuration**: Dynamic plugin options
- **Feature Flags**: Runtime feature toggling
## π Deployment Architecture
### Containerization
Multi-stage Docker builds for optimization:
```dockerfile
# Build stage
FROM node:22-slim AS build
# ... build steps
# Production stage
FROM node:22-slim AS production
# ... production setup
```
### Service Dependencies
- **Chrome/Chromium**: Browser engine
- **Node.js**: Runtime environment
- **Nginx**: Reverse proxy (in containers)
- **File System**: Session storage
## π Development Workflow
### Hot Reloading
Development environment supports hot reloading:
```bash
npm run dev # Starts both API and UI with hot reload
```
### Debug Configuration
Built-in debugging support:
```bash
# API debugging
node --inspect ./api/build/index.js
# Enable verbose logging
ENABLE_VERBOSE_LOGGING=true npm run dev -w api
```
## π Monitoring and Observability
### Logging
Structured logging with Pino:
```typescript
fastify.log.info({
sessionId,
action: 'page_created',
url: page.url()
}, 'New page created');
```
### Metrics (Planned)
- **Session Metrics**: Creation, duration, success rates
- **Performance Metrics**: Response times, resource usage
- **Error Tracking**: Error rates and categorization
### Health Checks
Built-in health check endpoints:
```typescript
// Basic health check
GET /health
// Detailed readiness check
GET /ready
```
---
This architecture provides a solid foundation for browser automation while maintaining flexibility for future enhancements and scaling requirements. |