Skip to content

Commit 2a1ff0a

Browse files
committed
fix: rm wasm32 bytes_stream reader impl
1 parent 06ab7b0 commit 2a1ff0a

3 files changed

Lines changed: 103 additions & 30 deletions

File tree

src/net/mod.rs

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,6 @@ impl RemoteAnalyzer {
411411
) -> Result<ProjectAnalysis> {
412412
let mut project_analysis = ProjectAnalysis::new(project_name);
413413

414-
// Use global config to build client for direct downloads
415414
let client = self.build_global_client()?;
416415

417416
let response = client.get(url).send().await.map_err(|e| {
@@ -428,31 +427,56 @@ impl RemoteAnalyzer {
428427
let total_size = response.content_length();
429428
self.progress_hook.on_download_progress(0, total_size);
430429

431-
let stream = response.bytes_stream();
432-
let progress_hook = Arc::clone(&self.progress_hook);
433-
let stream_reader = stream::StreamReader::new(
434-
stream,
435-
Box::new(move |downloaded, total| {
436-
progress_hook.on_download_progress(downloaded, total);
437-
log::debug!(
438-
"Downloaded: {} bytes of {} total",
439-
downloaded,
440-
total
441-
.map(|t| t.to_string())
442-
.unwrap_or_else(|| "unknown".to_string())
443-
);
444-
}),
445-
total_size,
446-
);
447-
448-
self.progress_hook.on_processing_start("Processing...");
449-
stream::process_tarball_stream(
450-
stream_reader,
451-
&mut project_analysis,
452-
&self.filter,
453-
self.progress_hook.as_ref(),
454-
)
455-
.await?;
430+
#[cfg(not(target_arch = "wasm32"))]
431+
{
432+
let stream = response.bytes_stream();
433+
let progress_hook = Arc::clone(&self.progress_hook);
434+
let stream_reader = stream::StreamReader::new(
435+
stream,
436+
Box::new(move |downloaded, total| {
437+
progress_hook.on_download_progress(downloaded, total);
438+
log::debug!(
439+
"Downloaded: {} bytes of {} total",
440+
downloaded,
441+
total
442+
.map(|t| t.to_string())
443+
.unwrap_or_else(|| "unknown".to_string())
444+
);
445+
}),
446+
total_size,
447+
);
448+
449+
self.progress_hook.on_processing_start("Processing...");
450+
stream::process_tarball_stream(
451+
stream_reader,
452+
&mut project_analysis,
453+
&self.filter,
454+
self.progress_hook.as_ref(),
455+
)
456+
.await?;
457+
}
458+
459+
#[cfg(target_arch = "wasm32")]
460+
{
461+
let bytes = response.bytes().await.map_err(|e| {
462+
crate::core::error::AnalysisError::network(format!(
463+
"Failed to read response bytes: {}",
464+
e
465+
))
466+
})?;
467+
468+
self.progress_hook
469+
.on_download_progress(bytes.len() as u64, total_size);
470+
self.progress_hook.on_processing_start("Processing...");
471+
472+
stream::process_tarball(
473+
bytes,
474+
&mut project_analysis,
475+
&self.filter,
476+
self.progress_hook.as_ref(),
477+
)
478+
.await?;
479+
}
456480

457481
Ok(project_analysis)
458482
}

src/net/stream.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,42 @@ impl Read for StreamReader {
173173
}
174174
}
175175

176+
pub async fn process_tarball(
177+
bytes: bytes::Bytes,
178+
project_analysis: &mut ProjectAnalysis,
179+
filter: &IntelligentFilter,
180+
_progress_hook: &dyn ProgressHook,
181+
) -> Result<()> {
182+
let decoder = GzDecoder::new(Cursor::new(bytes));
183+
let mut archive = Archive::new(decoder);
184+
185+
let entries = archive
186+
.entries()
187+
.map_err(|e| AnalysisError::archive(format!("Failed to read tar entries: {}", e)))?;
188+
189+
let mut stats = FilterStats::new();
190+
191+
for entry in entries {
192+
let entry = entry
193+
.map_err(|e| AnalysisError::archive(format!("Failed to read tar entry: {}", e)))?;
194+
195+
if let Ok(metrics) = process_tar_entry_sync(entry, filter, &mut stats) {
196+
project_analysis.add_file_metrics(metrics)?;
197+
}
198+
}
199+
200+
#[cfg(feature = "cli")]
201+
log::info!(
202+
"Filter stats: processed {}/{} files ({:.1}% filtered), saved {}",
203+
stats.processed,
204+
stats.total_entries,
205+
stats.filter_ratio() * 100.0,
206+
stats.format_bytes_saved()
207+
);
208+
209+
Ok(())
210+
}
211+
176212
pub async fn process_tarball_stream(
177213
stream_reader: StreamReader,
178214
project_analysis: &mut ProjectAnalysis,

src/worker.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use crate::{core::filter::IntelligentFilter, net::RemoteAnalyzer};
44
use std::collections::HashMap;
55
use wasm_bindgen::prelude::*;
66

7+
const VERSION: &str = env!("CARGO_PKG_VERSION");
8+
79
fn console(message: &str) {
810
web_sys::console::log_1(&message.into());
911
}
@@ -49,6 +51,8 @@ impl Default for AnalysisOptions {
4951
struct WASMDebugInfo {
5052
total_languages: usize,
5153
total_files: usize,
54+
spend_time: u64,
55+
version: String,
5256
}
5357

5458
#[derive(serde::Serialize)]
@@ -114,19 +118,24 @@ impl AnalysisOptions {
114118
}
115119
}
116120

117-
fn create_wasm_result(analysis: &crate::core::analysis::ProjectAnalysis) -> WASMAnalysisResult {
121+
fn create_wasm_result(
122+
analysis: &crate::core::analysis::ProjectAnalysis,
123+
spend_time: u64,
124+
) -> WASMAnalysisResult {
118125
WASMAnalysisResult {
119126
project_name: analysis.project_name.clone(),
120127
summary: analysis.get_summary(),
121128
language_statistics: analysis.get_language_statistics(),
122129
debug_info: WASMDebugInfo {
123130
total_languages: analysis.language_analyses.len(),
124131
total_files: analysis.global_metrics.file_count,
132+
spend_time,
133+
version: VERSION.to_string(),
125134
},
126135
}
127136
}
128137

129-
fn create_error_result(error: AnalysisError, url: String) -> WASMErrorResult {
138+
fn create_error_result(error: AnalysisError, url: String, spend_time: u64) -> WASMErrorResult {
130139
let error_type = match error {
131140
AnalysisError::NetworkError { .. } => "network_error",
132141
_ => "analysis_error",
@@ -139,6 +148,8 @@ fn create_error_result(error: AnalysisError, url: String) -> WASMErrorResult {
139148
debug_info: WASMDebugInfo {
140149
total_languages: 0,
141150
total_files: 0,
151+
spend_time,
152+
version: VERSION.to_string(),
142153
},
143154
}
144155
}
@@ -148,6 +159,8 @@ pub async fn analyze_url(url: String, options: JsValue) -> Result<JsValue, JsVal
148159
let opts: AnalysisOptions = serde_wasm_bindgen::from_value(options)?;
149160
console(&format!("Starting analysis for URL: {}", url));
150161

162+
let start_time = std::time::Instant::now();
163+
151164
let mut analyzer = RemoteAnalyzer::new();
152165
analyzer.set_global_config(opts.to_provider_config());
153166

@@ -161,7 +174,7 @@ pub async fn analyze_url(url: String, options: JsValue) -> Result<JsValue, JsVal
161174

162175
let result = match analyzer.analyze_url(&url).await {
163176
Ok(analysis) => {
164-
let result = create_wasm_result(&analysis);
177+
let result = create_wasm_result(&analysis, start_time.elapsed().as_secs());
165178
console(&format!(
166179
"Analysis completed successfully for project: {} ({} files, {} languages)",
167180
analysis.project_name,
@@ -173,7 +186,7 @@ pub async fn analyze_url(url: String, options: JsValue) -> Result<JsValue, JsVal
173186
Err(e) => {
174187
console(&format!("Analysis failed: {}", e));
175188
console(&format!("Error details - URL: {}, Error: {:?}", url, e));
176-
let error_result = create_error_result(e, url);
189+
let error_result = create_error_result(e, url, start_time.elapsed().as_secs());
177190
serde_wasm_bindgen::to_value(&error_result)?
178191
}
179192
};

0 commit comments

Comments
 (0)