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
//! # Asset Path Resolution for WASM and Native Platforms
//!
//! This module provides async asset resolution that works in both native
//! and WASM environments through the Runtime trait abstraction.
//!
//! ## Overview
//!
//! When bundling JavaScript/TypeScript code, modules often reference static assets
//! using patterns like `new URL('./asset.wasm', import.meta.url)`. This module
//! resolves these asset specifiers to their actual filesystem locations, handling:
//!
//! - Relative paths (`./file.wasm`, `../assets/logo.png`)
//! - Node modules resolution (`@pkg/file.wasm`, `pkg/assets/file.wasm`)
//! - Bare filenames (wasm-bindgen pattern: `file.wasm` without `./`)
//! - Monorepo support (cross-package references)
//! - Security validation (directory traversal prevention)
//!
//! ## WASM Compatibility
//!
//! All filesystem operations are abstracted through the [`Runtime`](crate::Runtime) trait:
//!
//! - **Native**: Uses `std::fs` via [`NativeRuntime`](crate::NativeRuntime)
//! - **WASM**: Bridges to JavaScript via `BrowserRuntime`
//!
//! This allows the same asset resolution logic to run in both environments.
//!
//! ## Path Canonicalization
//!
//! Platform-specific path handling ensures consistent behavior:
//!
//! - **Native**: Attempts `std::fs::canonicalize()` for real symlink resolution,
//! falls back to path normalization if that fails (e.g., path doesn't exist yet)
//! - **WASM**: Uses manual normalization via the `path-clean` crate (no symlinks
//! exist in the browser's virtual filesystem)
//!
//! The `path-clean` crate correctly handles `.` and `..` components on all platforms.
//!
//! ## Security
//!
//! The module implements multiple security layers:
//!
//! 1. **Directory Traversal Prevention**: Validates that resolved paths don't
//! escape the project directory using `..` tricks
//! 2. **Allowed Directories**: Assets must be within:
//! - The project directory (cwd)
//! - `node_modules` directories
//! - Monorepo workspace root (if detected)
//! 3. **Size Validation**: Optional size limits prevent DoS attacks via huge files
//!
//! ## Resolution Algorithm
//!
//! The resolution process follows this logic:
//!
//! 1. **Relative Paths** (`./` or `../`): Resolve relative to the referrer's directory
//! 2. **Absolute Paths** (`/`): Resolve directly, validate security
//! 3. **Package Paths** (contains `/` or `@scope/`): Search up directory tree for
//! `node_modules/specifier`
//! 4. **Bare Filenames** (no path separators): Try relative to referrer first
//! (wasm-bindgen pattern), fall back to node_modules
//!
//! ## Usage Examples
//!
//! ### Basic Resolution
//!
//! ```rust
//! use fob_bundler::builders::asset_resolver;
//! use fob_bundler::NativeRuntime;
//! use std::path::Path;
//!
//! # async fn example() -> fob_bundler::Result<()> {
//! let runtime = NativeRuntime;
//! let cwd = std::env::current_dir()?;
//! let referrer = cwd.join("src/module.js");
//!
//! // Resolve relative asset
//! let resolved = asset_resolver::resolve_asset(
//! "./worker.wasm",
//! &referrer,
//! &cwd,
//! &runtime,
//! ).await?;
//!
//! println!("Resolved to: {}", resolved.display());
//! # Ok(())
//! # }
//! ```
//!
//! ### Size Validation
//!
//! ```rust
//! use fob_bundler::builders::asset_resolver;
//! use fob_bundler::NativeRuntime;
//! use std::path::Path;
//!
//! # async fn example() -> fob_bundler::Result<()> {
//! let runtime = NativeRuntime;
//! let asset_path = Path::new("large_file.wasm");
//!
//! // Validate with 10MB limit
//! let size = asset_resolver::validate_asset_size(
//! asset_path,
//! Some(10 * 1024 * 1024),
//! &runtime,
//! ).await?;
//!
//! println!("Asset size: {} bytes", size);
//! # Ok(())
//! # }
//! ```
//!
//! ## Educational Notes
//!
//! ### Why Async?
//!
//! Even though native filesystem operations are synchronous, we use async for:
//!
//! 1. **WASM Compatibility**: Browser APIs (fetch, FileSystem Access API) are async
//! 2. **Future Proofing**: Enables potential remote asset fetching
//! 3. **Uniform Interface**: Same API works everywhere
//!
//! On native platforms, the async overhead is negligible (just a future wrapper).
//!
//! ### Why Runtime Trait?
//!
//! The Runtime trait abstracts filesystem operations, allowing:
//!
//! 1. **Platform Independence**: Same code runs on native and WASM
//! 2. **Testability**: Easy to inject mock filesystem for testing
//! 3. **Flexibility**: Can swap implementations (e.g., in-memory FS for tests)
//!
//! This is a core pattern in Rust for cross-platform code.
use crate::;
use ;
pub use validate_asset_size;
use canonicalize_path;
use validate_asset_security;
/// Resolve an asset specifier to an absolute filesystem path.
///
/// # Arguments
///
/// * `specifier` - The asset specifier (e.g., "./file.wasm", "../assets/logo.png")
/// * `referrer` - Absolute path to the module that references the asset
/// * `cwd` - Current working directory (project root)
/// * `runtime` - Runtime for filesystem operations
///
/// # Returns
///
/// Absolute, canonicalized path to the asset file
///
/// # Errors
///
/// Returns error if the asset cannot be resolved or doesn't exist
pub async
/// Resolve a relative path from the referrer's location.
async
/// Resolve an asset from node_modules using Node.js resolution algorithm.
///
/// Walks up the directory tree looking for node_modules/specifier
async
/// Validate and canonicalize an asset path.
///
/// # Security
///
/// - Prevents directory traversal attacks
/// - Ensures path is within project or node_modules
/// - Validates file exists
async