Library/Fn/Worker/
Detect.rs1use std::path::Path;
6
7use walkdir::WalkDir;
8
9use super::{WorkerConfig, WorkerInfo, WorkerType};
10
11pub struct WorkerDetector {
13 config:WorkerConfig,
14}
15
16impl WorkerDetector {
17 pub fn new(config:WorkerConfig) -> Self { Self { config } }
18
19 pub fn detect_workers(&self, root_dir:&Path) -> Vec<WorkerInfo> {
21 let mut workers = Vec::new();
22
23 for entry in WalkDir::new(root_dir).follow_links(true).into_iter().filter_map(|e| e.ok()) {
24 let path = entry.path();
25
26 if self.is_worker_file(path) {
27 if let Some(worker_info) = self.create_worker_info(path) {
28 workers.push(worker_info);
29 }
30 }
31 }
32
33 workers
34 }
35
36 pub fn is_worker_file(&self, path:&Path) -> bool {
38 if !path.is_file() {
39 return false;
40 }
41
42 let file_name = path.file_name().and_then(|s| s.to_str()).unwrap_or("");
43
44 let worker_patterns = [
46 ".worker.ts",
47 ".worker.js",
48 ".worker.tsx",
49 ".worker.jsx",
50 "-worker.ts",
51 "-worker.js",
52 "worker.ts",
53 "worker.js",
54 "SharedWorker.ts",
55 "SharedWorker.js",
56 ];
57
58 for pattern in worker_patterns {
59 if file_name.ends_with(pattern) {
60 return true;
61 }
62 }
63
64 if let Ok(content) = std::fs::read_to_string(path) {
66 return self.contains_worker_marker(&content);
67 }
68
69 false
70 }
71
72 fn create_worker_info(&self, path:&Path) -> Option<WorkerInfo> {
74 let file_name = path.file_name()?.to_str()?;
75
76 let worker_type = if file_name.contains("SharedWorker") || file_name.contains("shared") {
77 WorkerType::Classic
79 } else {
80 self.config.worker_type
81 };
82
83 let is_shared = file_name.contains("SharedWorker") || file_name.contains("shared");
84
85 let source_path = path.to_string_lossy().to_string();
86
87 let name = path.file_stem()?.to_str()?.replace(".worker", "").replace("-worker", "");
88
89 let output_path = Path::new(&self.config.output_dir)
90 .join(format!("{}.js", name))
91 .to_string_lossy()
92 .to_string();
93
94 Some(WorkerInfo { source_path, output_path, name, worker_type, dependencies:Vec::new(), is_shared })
95 }
96
97 fn contains_worker_marker(&self, content:&str) -> bool {
99 let markers = [
100 "new Worker(",
101 "new SharedWorker(",
102 "self.onmessage",
103 "self.postMessage",
104 "importScripts(",
105 "// @worker",
106 "//worker",
107 ];
108
109 for marker in markers {
110 if content.contains(marker) {
111 return true;
112 }
113 }
114
115 false
116 }
117
118 pub fn extract_dependencies(&self, path:&Path) -> Vec<String> {
120 let mut deps = Vec::new();
121
122 if let Ok(content) = std::fs::read_to_string(path) {
123 for line in content.lines() {
125 let trimmed = line.trim();
126
127 if trimmed.starts_with("import ") {
129 if let Some(from_start) = trimmed.find("from") {
130 let import_part = &trimmed[from_start + 4..];
131 if let Some(path_start) = import_part.find('"') {
132 let path_end = import_part[path_start + 1..].find('"');
133 if let Some(end) = path_end {
134 let import_path = &import_part[path_start + 1..path_start + 1 + end];
135 deps.push(import_path.to_string());
136 }
137 }
138 }
139 }
140
141 if trimmed.starts_with("importScripts(") {
143 if let Some(paren_start) = trimmed.find('(') {
144 let paren_content = &trimmed[paren_start + 1..];
145 if let Some(paren_end) = paren_content.find(')') {
146 let scripts = &paren_content[..paren_end];
147 for script in scripts.split(',') {
148 let script = script.trim().trim_matches('"').trim_matches('\'');
149 if !script.is_empty() {
150 deps.push(script.to_string());
151 }
152 }
153 }
154 }
155 }
156 }
157 }
158
159 deps
160 }
161}
162
163#[cfg(test)]
164mod tests {
165 use super::*;
166
167 #[test]
168 fn test_worker_detection_by_name() {
169 let config = WorkerConfig::new();
170 let detector = WorkerDetector::new(config);
171
172 assert!(detector.is_worker_file(Path::new("test.worker.ts")));
173 assert!(detector.is_worker_file(Path::new("my-worker.js")));
174 assert!(detector.is_worker_file(Path::new("SharedWorker.ts")));
175 assert!(!detector.is_worker_file(Path::new("regular.ts")));
176 }
177
178 #[test]
179 fn test_worker_detection_by_content() {
180 let config = WorkerConfig::new();
181 let detector = WorkerDetector::new(config);
182
183 let content = r#"
184 self.onmessage = function(e) {
185 self.postMessage(e.data);
186 };
187 "#;
188
189 assert!(detector.contains_worker_marker(content));
190 }
191
192 #[test]
193 fn test_dependency_extraction() {
194 let config = WorkerConfig::new();
195 let detector = WorkerDetector::new(config);
196
197 let path = Path::new("test.worker.ts");
198 let _deps = detector.extract_dependencies(path);
200 }
201}