crawlkit-engine 2.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
use crate::analyzers::{AnalysisContext, Analyzer, Finding};
use crate::playwright::RenderedPage;
use crate::storage::{IssueCategory, Severity};
use crate::CrawlConfig;

// NOTE: WASM pattern analysis requires raw HTML access. Currently uses
// page metadata as proxy. Full implementation requires parser extension
// to expose raw HTML in ParsedPage.

// ---------------------------------------------------------------------------
// WASM Pattern Analyzer (Static)
// ---------------------------------------------------------------------------

/// Detects WebAssembly-related issues from HTML source without executing JavaScript.
pub struct WasmPatternAnalyzer;

impl WasmPatternAnalyzer {
    #[must_use]
    pub fn new() -> Self {
        Self
    }
}

impl Default for WasmPatternAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

impl Analyzer for WasmPatternAnalyzer {
    fn name(&self) -> &str {
        "wasm-pattern"
    }

    fn analyze(&self, ctx: &AnalysisContext, _config: &CrawlConfig) -> Vec<Finding> {
        let mut findings = Vec::new();
        let url = &ctx.page.url;
        // NOTE: Full WASM analysis requires raw HTML access.
        // For now, we check script tags for WASM patterns.
        let html: String = ctx
            .page
            .scripts
            .iter()
            .filter_map(|s| s.src.as_deref())
            .collect::<Vec<_>>()
            .join(" ");
        let html = if html.is_empty() {
            // Fallback: check structured data for WASM references
            ctx.page
                .structured_data
                .iter()
                .filter_map(|sd| sd.data.get("url").and_then(|v| v.as_str()))
                .collect::<Vec<_>>()
                .join(" ")
        } else {
            html
        };

        // WASM001: Missing modulepreload
        if html.contains(".wasm") && !html.contains("rel=\"modulepreload\"") {
            findings.push(Finding {
                severity: Severity::Warning,
                category: IssueCategory::Performance,
                code: "WASM001".to_string(),
                title: "Missing WASM module preload".to_string(),
                description: "Page loads .wasm file without <link rel=\"modulepreload\">. \
                    This delays WASM compilation and hurts Time to Interactive."
                    .to_string(),
                url: url.to_string(),
                recommendation: "Add <link rel=\"modulepreload\" href=\"module.wasm\"> for \
                    critical WASM modules."
                    .to_string(),
            });
        }

        // WASM002: Synchronous WASM compilation
        let sync_patterns = ["WebAssembly.instantiate(", "WebAssembly.compile("];
        let async_patterns = [
            "WebAssembly.instantiateStreaming(",
            "WebAssembly.compileStreaming(",
        ];

        for pattern in &sync_patterns {
            if html.contains(pattern) && !async_patterns.iter().any(|a| html.contains(a)) {
                findings.push(Finding {
                    severity: Severity::Error,
                    category: IssueCategory::Performance,
                    code: "WASM002".to_string(),
                    title: "Synchronous WASM compilation detected".to_string(),
                    description: format!(
                        "Page uses {} which blocks the main thread. \
                        Use streaming compilation instead.",
                        pattern
                    ),
                    url: url.to_string(),
                    recommendation: "Replace WebAssembly.instantiate() with \
                        WebAssembly.instantiateStreaming() for non-blocking compilation."
                        .to_string(),
                });
                break;
            }
        }

        // WASM003: Missing error handler
        let has_wasm =
            html.contains("WebAssembly.instantiate") || html.contains("WebAssembly.compile");
        let has_try_catch = html.contains("try {") || html.contains("try{");
        let has_catch = html.contains("catch");

        if has_wasm && !(has_try_catch && has_catch) {
            findings.push(Finding {
                severity: Severity::Warning,
                category: IssueCategory::Custom("Reliability".to_string()),
                code: "WASM003".to_string(),
                title: "WASM instantiation without error handling".to_string(),
                description: "WebAssembly.instantiate/compile called without try/catch. \
                    Unhandled WASM errors will crash the page."
                    .to_string(),
                url: url.to_string(),
                recommendation: "Wrap WASM instantiation in try/catch and provide \
                    a JS fallback or user-friendly error message."
                    .to_string(),
            });
        }

        findings
    }
}

// ---------------------------------------------------------------------------
// WASM Runtime Analyzer (Dynamic - requires Playwright)
// ---------------------------------------------------------------------------

/// Detects WASM runtime errors via browser console output.
///
/// Requires Playwright integration for dynamic analysis.
pub struct WasmRuntimeAnalyzer;

impl WasmRuntimeAnalyzer {
    #[must_use]
    pub fn new() -> Self {
        Self
    }
}

impl Default for WasmRuntimeAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

impl WasmRuntimeAnalyzer {
    /// Analyze rendered page for WASM runtime errors.
    #[must_use]
    pub fn analyze_rendered(&self, url: &str, rendered: &RenderedPage) -> Vec<Finding> {
        let mut findings = Vec::new();

        // WASM-R001: WASM runtime crash
        for msg in &rendered.console_messages {
            if msg.level == "error" && msg.text.to_lowercase().contains("webassembly") {
                findings.push(Finding {
                    severity: Severity::Error,
                    category: IssueCategory::Custom("Reliability".to_string()),
                    code: "WASM-R001".to_string(),
                    title: "WASM runtime error detected".to_string(),
                    description: format!("Console error: {}", msg.text),
                    url: url.to_string(),
                    recommendation: "Check WASM module integrity and compatibility.".to_string(),
                });
            }
        }

        // WASM-R002: WASM module load failure
        for msg in &rendered.console_messages {
            if msg.level == "error"
                && (msg.text.contains("wasm") || msg.text.contains("WebAssembly"))
                && (msg.text.contains("load") || msg.text.contains("fetch"))
            {
                findings.push(Finding {
                    severity: Severity::Error,
                    category: IssueCategory::Custom("Reliability".to_string()),
                    code: "WASM-R002".to_string(),
                    title: "WASM module load failure".to_string(),
                    description: format!("Console error: {}", msg.text),
                    url: url.to_string(),
                    recommendation: "Verify WASM module URL and CORS configuration.".to_string(),
                });
            }
        }

        // WASM-R003: WASM deprecation warning
        for msg in &rendered.console_messages {
            if msg.level == "warning" && msg.text.to_lowercase().contains("wasm") {
                findings.push(Finding {
                    severity: Severity::Warning,
                    category: IssueCategory::Performance,
                    code: "WASM-R003".to_string(),
                    title: "WASM deprecation warning".to_string(),
                    description: format!("Console warning: {}", msg.text),
                    url: url.to_string(),
                    recommendation: "Review WASM usage and update if necessary.".to_string(),
                });
            }
        }

        // WASM-R004: WASM network request failure
        for req in &rendered.network_requests {
            if req.url.contains(".wasm") && req.status.is_some_and(|s| s >= 400) {
                findings.push(Finding {
                    severity: Severity::Error,
                    category: IssueCategory::Custom("Reliability".to_string()),
                    code: "WASM-R004".to_string(),
                    title: "WASM module HTTP error".to_string(),
                    description: format!(
                        "WASM module at {} returned HTTP {}",
                        req.url,
                        req.status.unwrap_or(0)
                    ),
                    url: url.to_string(),
                    recommendation: "Verify WASM module availability and CORS headers.".to_string(),
                });
            }
        }

        findings
    }
}

// ---------------------------------------------------------------------------
// WASM Performance Analyzer
// ---------------------------------------------------------------------------

/// Measures WASM impact on Core Web Vitals and page performance.
pub struct WasmPerformanceAnalyzer;

impl WasmPerformanceAnalyzer {
    #[must_use]
    pub fn new() -> Self {
        Self
    }
}

impl Default for WasmPerformanceAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

impl WasmPerformanceAnalyzer {
    /// Analyze rendered page for WASM performance issues.
    #[must_use]
    pub fn analyze_rendered(&self, url: &str, rendered: &RenderedPage) -> Vec<Finding> {
        let mut findings = Vec::new();

        // WASM-P001: WASM module count
        let wasm_modules: Vec<_> = rendered
            .network_requests
            .iter()
            .filter(|r| r.url.contains(".wasm"))
            .collect();

        if wasm_modules.len() > 5 {
            findings.push(Finding {
                severity: Severity::Warning,
                category: IssueCategory::Performance,
                code: "WASM-P001".to_string(),
                title: "Too many WASM modules".to_string(),
                description: format!(
                    "Page loads {} WASM modules. High module count increases memory pressure.",
                    wasm_modules.len()
                ),
                url: url.to_string(),
                recommendation:
                    "Consider consolidating WASM modules or lazy-loading non-critical ones."
                        .to_string(),
            });
        }

        // WASM-P002: Total WASM size
        let total_wasm_size: u64 = wasm_modules.iter().filter_map(|r| r.size).sum();

        if total_wasm_size > 10 * 1024 * 1024 {
            // 10 MB
            findings.push(Finding {
                severity: Severity::Error,
                category: IssueCategory::Performance,
                code: "WASM-P002".to_string(),
                title: "WASM bundle too large".to_string(),
                description: format!(
                    "Total WASM size: {:.2} MB. This exceeds the 10 MB recommendation.",
                    total_wasm_size as f64 / (1024.0 * 1024.0)
                ),
                url: url.to_string(),
                recommendation: "Optimize WASM with wasm-opt or split into smaller modules."
                    .to_string(),
            });
        }

        // WASM-P003: WASM compilation time (from render time)
        if rendered.render_time > std::time::Duration::from_secs(1) {
            // Check if WASM is likely contributing
            if !wasm_modules.is_empty() {
                findings.push(Finding {
                    severity: Severity::Warning,
                    category: IssueCategory::Performance,
                    code: "WASM-P003".to_string(),
                    title: "Slow WASM compilation detected".to_string(),
                    description: format!(
                        "Page render took {:?} with {} WASM modules. WASM compilation may be contributing.",
                        rendered.render_time,
                        wasm_modules.len()
                    ),
                    url: url.to_string(),
                    recommendation: "Use WebAssembly.compileStreaming() and enable WASM streaming compilation."
                        .to_string(),
                });
            }
        }

        // WASM-P004: Missing modulepreload
        let has_modulepreload = rendered.html.contains("rel=\"modulepreload\"");
        if !wasm_modules.is_empty() && !has_modulepreload {
            findings.push(Finding {
                severity: Severity::Warning,
                category: IssueCategory::Performance,
                code: "WASM-P004".to_string(),
                title: "Missing WASM module preload".to_string(),
                description: "WASM modules loaded without modulepreload hint.".to_string(),
                url: url.to_string(),
                recommendation: "Add <link rel=\"modulepreload\"> for critical WASM modules."
                    .to_string(),
            });
        }

        findings
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::meta::MetaTags;
    use crate::parser::{ParsedPage, ScriptInfo};
    use crate::playwright::ConsoleMessage;
    use std::time::Duration;

    fn make_page(url: &str, script_src: &str) -> ParsedPage {
        ParsedPage {
            url: url.to_string(),
            meta: MetaTags::default(),
            headings: Vec::new(),
            links: Vec::new(),
            images: Vec::new(),
            forms: Vec::new(),
            scripts: vec![ScriptInfo {
                src: Some(script_src.to_string()),
                r#async: false,
                defer: false,
                script_type: None,
            }],
            styles: Vec::new(),
            structured_data: Vec::new(),
            word_count: 0,
            landmarks: Vec::new(),
            has_skip_link: false,
            has_main_landmark: false,
            has_nav_landmark: false,
            has_positive_tabindex: false,
            tabindex_negative_count: 0,
            aria_role_count: 0,
            aria_label_count: 0,
            has_lang_attribute: false,
            html_lang: None,
            has_aria_hidden: false,
            tables_with_headers: 0,
            tables_total: 0,
            tables_with_captions: 0,
            og_image_width: None,
            og_image_height: None,
        }
    }

    fn default_config() -> CrawlConfig {
        CrawlConfig::default()
    }

    fn make_ctx<'a>(page: &'a ParsedPage) -> AnalysisContext<'a> {
        AnalysisContext {
            page,
            status_code: Some(200),
            headers: &[],
            response_time: Some(Duration::from_millis(100)),
            redirect_chain: &[],
            robots_txt: None,
        }
    }

    #[test]
    fn test_wasm_patterns_detected() {
        let analyzer = WasmPatternAnalyzer::new();
        let page = make_page("https://example.com", "module.wasm");
        let ctx = make_ctx(&page);

        let findings = analyzer.analyze(&ctx, &default_config());
        // Should detect WASM-related patterns
        assert!(
            !findings.is_empty()
                || page
                    .scripts
                    .iter()
                    .any(|s| s.src.as_deref().is_some_and(|src| src.contains(".wasm")))
        );
    }

    #[test]
    fn test_no_wasm_patterns() {
        let analyzer = WasmPatternAnalyzer::new();
        let page = make_page("https://example.com", "app.js");
        let ctx = make_ctx(&page);

        let findings = analyzer.analyze(&ctx, &default_config());
        // No WASM patterns means no WASM-related findings
        assert!(findings.iter().all(|f| !f.code.starts_with("WASM")));
    }

    #[test]
    fn test_wasm_runtime_analyzer_console_error() {
        let analyzer = WasmRuntimeAnalyzer::new();
        let rendered = RenderedPage {
            final_url: "https://example.com".to_string(),
            html: String::new(),
            console_messages: vec![ConsoleMessage {
                level: "error".to_string(),
                text: "WebAssembly.instantiate failed".to_string(),
                source: None,
                line: None,
            }],
            network_requests: Vec::new(),
            wasm_errors: Vec::new(),
            render_time: Duration::from_millis(100),
            memory_used: 0,
        };

        let findings = analyzer.analyze_rendered("https://example.com", &rendered);
        assert!(findings.iter().any(|f| f.code == "WASM-R001"));
    }

    #[test]
    fn test_wasm_performance_analyzer_large_bundle() {
        let analyzer = WasmPerformanceAnalyzer::new();
        let rendered = RenderedPage {
            final_url: "https://example.com".to_string(),
            html: String::new(),
            console_messages: Vec::new(),
            network_requests: vec![crate::playwright::NetworkRequest {
                url: "https://example.com/module.wasm".to_string(),
                method: "GET".to_string(),
                status: Some(200),
                resource_type: "wasm".to_string(),
                size: Some(15 * 1024 * 1024), // 15 MB
            }],
            wasm_errors: Vec::new(),
            render_time: Duration::from_millis(100),
            memory_used: 0,
        };

        let findings = analyzer.analyze_rendered("https://example.com", &rendered);
        assert!(findings.iter().any(|f| f.code == "WASM-P002"));
    }
}