crawlkit-engine 1.0.0

High-performance Rust web crawler and SEO analysis toolkit with 28 analyzers, WASM plugin system, and enterprise features
Documentation
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
use std::path::PathBuf;
use std::process::Command;
use std::sync::Arc;
use std::time::{Duration, Instant};

use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use thiserror::Error;

/// Errors from Playwright operations.
#[derive(Debug, Error)]
pub enum PlaywrightError {
    /// Playwright binary is not installed or not found.
    #[error("playwright not available: {0}")]
    NotAvailable(String),

    /// Page navigation failed.
    #[error("page navigation failed: {0}")]
    NavigationFailed(String),

    /// Page rendering timed out.
    #[error("page timeout after {0:?}")]
    Timeout(Duration),

    /// JavaScript evaluation on the page failed.
    #[error("JavaScript evaluation failed: {0}")]
    JsEvaluationFailed(String),

    /// Browser launch failed.
    #[error("browser launch failed: {0}")]
    BrowserLaunchFailed(String),

    /// Browser context creation failed.
    #[error("context creation failed: {0}")]
    ContextCreationFailed(String),

    /// Resource limit exceeded during rendering.
    #[error("resource limit exceeded: {0}")]
    ResourceLimitExceeded(String),
}

/// Configuration for Playwright browser rendering.
///
/// Controls browser type, timeouts, concurrency limits, and resource
/// constraints. Default is disabled with Chromium headless mode.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlaywrightConfig {
    /// Enable JavaScript rendering.
    pub enabled: bool,
    /// Browser type.
    pub browser_type: BrowserType,
    /// Page load timeout.
    pub timeout: Duration,
    /// Maximum concurrent browser contexts.
    pub max_concurrent: usize,
    /// Headless mode.
    pub headless: bool,
    /// Extra arguments to pass to the browser.
    pub args: Vec<String>,
    /// Maximum memory per context (bytes).
    pub max_memory_per_context: u64,
    /// Maximum CPU time per page (seconds).
    pub max_cpu_seconds: u64,
}

impl Default for PlaywrightConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            browser_type: BrowserType::Chromium,
            timeout: Duration::from_secs(30),
            max_concurrent: 5,
            headless: true,
            args: vec![
                "--no-sandbox".to_string(),
                "--disable-setuid-sandbox".to_string(),
                "--disable-dev-shm-usage".to_string(),
                "--disable-gpu".to_string(),
                "--disable-extensions".to_string(),
                "--disable-background-networking".to_string(),
            ],
            max_memory_per_context: 512 * 1024 * 1024, // 512 MB
            max_cpu_seconds: 30,
        }
    }
}

/// Browser type for rendering.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum BrowserType {
    /// Google Chrome / Chromium.
    Chromium,
    /// Mozilla Firefox.
    Firefox,
    /// Apple WebKit (Safari engine).
    WebKit,
}

impl BrowserType {
    /// Get the Playwright browser name.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            BrowserType::Chromium => "chromium",
            BrowserType::Firefox => "firefox",
            BrowserType::WebKit => "webkit",
        }
    }
}

/// Result of JavaScript rendering.
///
/// Contains the rendered HTML, console messages, network requests,
/// and WASM errors detected during page rendering.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RenderedPage {
    /// Final URL after JS rendering and redirects.
    pub final_url: String,
    /// Rendered HTML content.
    pub html: String,
    /// Console messages from the page.
    pub console_messages: Vec<ConsoleMessage>,
    /// Network requests made during rendering.
    pub network_requests: Vec<NetworkRequest>,
    /// WASM-related errors detected.
    pub wasm_errors: Vec<WasmError>,
    /// Time taken to render the page.
    pub render_time: Duration,
    /// Memory used during render (bytes).
    pub memory_used: u64,
}

/// Console message from browser.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsoleMessage {
    /// Message level (log, warn, error, etc.).
    pub level: String,
    /// Message text.
    pub text: String,
    /// Source file URL (if available).
    pub source: Option<String>,
    /// Line number in source (if available).
    pub line: Option<u32>,
}

/// Network request during rendering.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkRequest {
    /// Request URL.
    pub url: String,
    /// HTTP method (GET, POST, etc.).
    pub method: String,
    /// Response status code (if received).
    pub status: Option<u16>,
    /// Resource type (document, script, stylesheet, etc.).
    pub resource_type: String,
    /// Response size in bytes (if available).
    pub size: Option<u64>,
}

/// WASM error detected during rendering.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WasmError {
    /// Error type (runtime, compilation, etc.).
    pub error_type: String,
    /// Error message.
    pub message: String,
    /// Source file (if available).
    pub source: Option<String>,
    /// Timestamp when error occurred.
    pub timestamp: u64,
}

/// Browser context with resource isolation.
#[derive(Clone)]
pub struct BrowserContext {
    /// Context ID.
    pub id: String,
    /// Memory limit.
    pub memory_limit: u64,
    /// CPU time limit.
    pub cpu_limit: Duration,
    /// Creation time.
    pub created_at: Instant,
    /// Whether context is active.
    pub active: bool,
}

impl BrowserContext {
    /// Create a new browser context.
    #[must_use]
    pub fn new(id: String, memory_limit: u64, cpu_limit: Duration) -> Self {
        Self {
            id,
            memory_limit,
            cpu_limit,
            created_at: Instant::now(),
            active: true,
        }
    }

    /// Check if context has exceeded resource limits.
    #[must_use]
    pub fn is_over_limit(&self, memory_used: u64) -> bool {
        let elapsed = self.created_at.elapsed();
        memory_used > self.memory_limit || elapsed > self.cpu_limit
    }
}

/// Playwright binary detection and management.
pub struct PlaywrightDetector {
    /// Path to Playwright binary.
    binary_path: Option<PathBuf>,
    /// Playwright version.
    version: Option<String>,
    /// Available browsers.
    available_browsers: Vec<BrowserType>,
}

impl PlaywrightDetector {
    /// Detect Playwright installation.
    #[must_use]
    pub fn detect() -> Self {
        let binary_path = Self::find_binary();
        let version = binary_path.as_ref().and_then(Self::get_version);
        let available_browsers = binary_path
            .as_ref()
            .map(Self::get_browsers)
            .unwrap_or_default();

        Self {
            binary_path,
            version,
            available_browsers,
        }
    }

    /// Find Playwright binary in PATH.
    fn find_binary() -> Option<PathBuf> {
        // Check common locations
        let candidates = [
            "npx playwright",
            "playwright",
            "/usr/local/bin/playwright",
            "/usr/bin/playwright",
        ];

        for candidate in &candidates {
            if let Ok(output) = Command::new("which").arg(candidate).output() {
                if output.status.success() {
                    let path = String::from_utf8_lossy(&output.stdout).trim().to_string();
                    if !path.is_empty() {
                        return Some(PathBuf::from(path));
                    }
                }
            }
        }

        None
    }

    /// Get Playwright version.
    fn get_version(binary: &PathBuf) -> Option<String> {
        Command::new(binary)
            .arg("--version")
            .output()
            .ok()
            .and_then(|o| {
                if o.status.success() {
                    String::from_utf8(o.stdout)
                        .ok()
                        .map(|s| s.trim().to_string())
                } else {
                    None
                }
            })
    }

    /// Get available browsers.
    fn get_browsers(binary: &PathBuf) -> Vec<BrowserType> {
        Command::new(binary)
            .arg("install")
            .arg("--dry-run")
            .output()
            .ok()
            .map(|o| {
                let output = String::from_utf8_lossy(&o.stdout);
                let mut browsers = Vec::new();
                if output.contains("chromium") {
                    browsers.push(BrowserType::Chromium);
                }
                if output.contains("firefox") {
                    browsers.push(BrowserType::Firefox);
                }
                if output.contains("webkit") {
                    browsers.push(BrowserType::WebKit);
                }
                browsers
            })
            .unwrap_or_default()
    }

    /// Check if Playwright is available.
    #[must_use]
    pub fn is_available(&self) -> bool {
        self.binary_path.is_some()
    }

    /// Get binary path.
    #[must_use]
    pub fn binary_path(&self) -> Option<&PathBuf> {
        self.binary_path.as_ref()
    }

    /// Get version.
    #[must_use]
    pub fn version(&self) -> Option<&str> {
        self.version.as_deref()
    }

    /// Check if browser is available.
    #[must_use]
    pub fn has_browser(&self, browser: BrowserType) -> bool {
        self.available_browsers.contains(&browser)
    }
}

/// Playwright renderer with browser context isolation.
///
/// Manages browser contexts with resource limits (memory, CPU) and
/// provides page rendering via Playwright CLI subprocess. Each render
/// creates an isolated browser context that is destroyed after use.
///
/// # Examples
///
/// ```rust
/// use crawlkit_engine::{PlaywrightRenderer, PlaywrightConfig};
///
/// let renderer = PlaywrightRenderer::new(PlaywrightConfig::default());
/// assert!(!renderer.is_available()); // disabled by default
/// ```
pub struct PlaywrightRenderer {
    config: PlaywrightConfig,
    detector: PlaywrightDetector,
    contexts: Arc<RwLock<Vec<BrowserContext>>>,
}

impl PlaywrightRenderer {
    /// Create a new Playwright renderer.
    #[must_use]
    pub fn new(config: PlaywrightConfig) -> Self {
        let detector = PlaywrightDetector::detect();
        Self {
            config,
            detector,
            contexts: Arc::new(RwLock::new(Vec::new())),
        }
    }

    /// Create with default config.
    #[must_use]
    pub fn with_default_config() -> Self {
        Self::new(PlaywrightConfig::default())
    }

    /// Check if Playwright is available.
    #[must_use]
    pub fn is_available(&self) -> bool {
        self.config.enabled && self.detector.is_available()
    }

    /// Get detector reference.
    #[must_use]
    pub fn detector(&self) -> &PlaywrightDetector {
        &self.detector
    }

    /// Create a new browser context with resource isolation.
    ///
    /// # Errors
    /// Returns error if context creation fails.
    pub fn create_context(&self) -> Result<BrowserContext, PlaywrightError> {
        if !self.is_available() {
            return Err(PlaywrightError::NotAvailable(
                "Playwright not available".to_string(),
            ));
        }

        let contexts = self.contexts.read();
        if contexts.len() >= self.config.max_concurrent {
            return Err(PlaywrightError::ResourceLimitExceeded(
                "Maximum concurrent contexts reached".to_string(),
            ));
        }

        let context = BrowserContext::new(
            uuid::Uuid::new_v4().to_string(),
            self.config.max_memory_per_context,
            Duration::from_secs(self.config.max_cpu_seconds),
        );

        drop(contexts);
        self.contexts.write().push(context.clone());

        Ok(context)
    }

    /// Render a page with JavaScript.
    ///
    /// # Errors
    /// Returns error if rendering fails.
    pub async fn render(&self, url: &str) -> Result<RenderedPage, PlaywrightError> {
        if !self.config.enabled {
            return Err(PlaywrightError::NotAvailable(
                "JavaScript rendering is disabled".to_string(),
            ));
        }

        if !self.is_available() {
            return Err(PlaywrightError::NotAvailable(
                "Playwright binary not found".to_string(),
            ));
        }

        let start = Instant::now();

        // Create isolated context
        let context = self.create_context()?;

        // Launch browser and render page via Playwright CLI
        let rendered = self.render_via_cli(url).await?;

        let render_time = start.elapsed();

        // Remove context
        self.contexts.write().retain(|c| c.id != context.id);

        Ok(RenderedPage {
            render_time,
            ..rendered
        })
    }

    /// Render page via Playwright CLI subprocess.
    ///
    /// The URL is passed as a separate argument to the script via
    /// `process.argv[2]` to prevent JavaScript injection.
    async fn render_via_cli(&self, url: &str) -> Result<RenderedPage, PlaywrightError> {
        let _binary = self.detector.binary_path().ok_or_else(|| {
            PlaywrightError::NotAvailable("Playwright binary not found".to_string())
        })?;

        // SECURITY: URL is NOT interpolated into the JS string.
        // It is passed via process.argv to prevent code injection.
        let script = format!(
            r#"
const {{ chromium }} = require('playwright');
const targetUrl = process.argv[2];

(async () => {{
    const browser = await chromium.launch({{
        headless: {},
        args: {}
    }});
    
    const context = await browser.newContext({{
        viewport: {{ width: 1920, height: 1080 }},
        userAgent: 'crawlkit/0.4.0'
    }});
    
    const page = await context.newPage();
    
    const consoleMessages = [];
    const networkRequests = [];
    
    page.on('console', msg => {{
        consoleMessages.push({{
            level: msg.type(),
            text: msg.text(),
            source: msg.location().url || null,
            line: msg.location().lineNumber || null
        }});
    }});
    
    page.on('request', req => {{
        networkRequests.push({{
            url: req.url(),
            method: req.method(),
            status: null,
            resourceType: req.resourceType(),
            size: null
        }});
    }});
    
    page.on('response', res => {{
        const req = networkRequests.find(r => r.url === res.url());
        if (req) {{
            req.status = res.status();
        }}
    }});
    
    try {{
        await page.goto(targetUrl, {{ waitUntil: 'networkidle', timeout: {} }});
    }} catch (e) {{
        console.error('Navigation error:', e.message);
    }}
    
    const html = await page.content();
    const finalUrl = page.url();
    
    // Detect WASM errors
    const wasmErrors = [];
    for (const msg of consoleMessages) {{
        if (msg.level === 'error' && 
            (msg.text.toLowerCase().includes('webassembly') || 
             msg.text.toLowerCase().includes('wasm'))) {{
            wasmErrors.push({{
                error_type: 'runtime',
                message: msg.text,
                source: msg.source,
                timestamp: Date.now()
            }});
        }}
    }}
    
    const result = {{
        final_url: finalUrl,
        html: html,
        console_messages: consoleMessages,
        network_requests: networkRequests,
        wasm_errors: wasmErrors,
        memory_used: process.memoryUsage().heapUsed
    }};
    
    console.log(JSON.stringify(result));
    
    await browser.close();
}})();
"#,
            self.config.headless,
            serde_json::to_string(&self.config.args).unwrap_or_else(|_| "[]".to_string()),
            self.config.timeout.as_millis()
        );

        // Write script to temporary file
        let temp_dir = std::env::temp_dir();
        let script_path = temp_dir.join(format!("crawlkit_playwright_{}.js", uuid::Uuid::new_v4()));
        std::fs::write(&script_path, &script)
            .map_err(|e| PlaywrightError::BrowserLaunchFailed(e.to_string()))?;

        // Execute Playwright script. Pass URL as argument to prevent injection.
        // Allow NODE_PATH override via environment variable.
        let node_path = std::env::var("NODE_PATH").unwrap_or_default();
        let mut cmd = tokio::process::Command::new("node");
        cmd.arg(&script_path);
        cmd.arg(url); // URL passed as argv[2], accessed via process.argv[2] in JS
        if !node_path.is_empty() {
            cmd.env("NODE_PATH", &node_path);
        }
        let output = cmd
            .output()
            .await
            .map_err(|e| PlaywrightError::BrowserLaunchFailed(e.to_string()))?;

        // Clean up temporary script
        let _ = std::fs::remove_file(&script_path);

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(PlaywrightError::BrowserLaunchFailed(stderr.to_string()));
        }

        let stdout = String::from_utf8_lossy(&output.stdout);
        let result: serde_json::Value = serde_json::from_str(&stdout)
            .map_err(|e| PlaywrightError::JsEvaluationFailed(e.to_string()))?;

        Ok(RenderedPage {
            final_url: result["final_url"].as_str().unwrap_or(url).to_string(),
            html: result["html"].as_str().unwrap_or("").to_string(),
            console_messages: serde_json::from_value(result["console_messages"].clone())
                .unwrap_or_default(),
            network_requests: serde_json::from_value(result["network_requests"].clone())
                .unwrap_or_default(),
            wasm_errors: serde_json::from_value(result["wasm_errors"].clone()).unwrap_or_default(),
            render_time: Duration::from_millis(0), // Will be set by caller
            memory_used: result["memory_used"].as_u64().unwrap_or(0),
        })
    }

    /// Get active context count.
    #[must_use]
    pub fn active_contexts(&self) -> usize {
        self.contexts.read().len()
    }

    /// Get configuration.
    #[must_use]
    pub fn config(&self) -> &PlaywrightConfig {
        &self.config
    }
}

impl Default for PlaywrightRenderer {
    fn default() -> Self {
        Self::with_default_config()
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_playwright_config_default() {
        let config = PlaywrightConfig::default();
        assert!(!config.enabled);
        assert_eq!(config.max_concurrent, 5);
        assert!(config.headless);
        assert_eq!(config.max_memory_per_context, 512 * 1024 * 1024);
    }

    #[test]
    fn test_browser_type_as_str() {
        assert_eq!(BrowserType::Chromium.as_str(), "chromium");
        assert_eq!(BrowserType::Firefox.as_str(), "firefox");
        assert_eq!(BrowserType::WebKit.as_str(), "webkit");
    }

    #[test]
    fn test_playwright_detector() {
        let detector = PlaywrightDetector::detect();
        // May or may not be available depending on environment
        let _ = detector.is_available();
    }

    #[test]
    fn test_browser_context_resource_limits() {
        let context = BrowserContext::new(
            "test".to_string(),
            1024 * 1024, // 1 MB
            Duration::from_secs(10),
        );

        assert!(!context.is_over_limit(512 * 1024)); // 512 KB - OK
        assert!(context.is_over_limit(2 * 1024 * 1024)); // 2 MB - Over limit
    }

    #[tokio::test]
    async fn test_playwright_render_disabled() {
        let renderer = PlaywrightRenderer::with_default_config();
        let result = renderer.render("https://example.com").await;
        assert!(result.is_err());
    }

    #[test]
    fn test_playwright_renderer_not_available() {
        let renderer = PlaywrightRenderer::with_default_config();
        assert!(!renderer.is_available());
    }
}