mini-static 0.21.0

A secure, async static file server with streaming, traversal protection, and connection limits.
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
use std::path::{Path, PathBuf};

use crate::css::{self, CssOptions, CssTool};
use crate::js::{self, JsOptions, JsTool};
use crate::reload::ChangeType;
use crate::watcher::{Broadcaster, ChangeEvent};

/// Routes source-folder file changes to the pipeline that turns them into served output.
///
/// The watcher layer runs over *source folders only*. The designated output dir is never
/// watched and never a trigger: every pipeline below writes its own output, and a pipeline
/// that listened to its own output would re-trigger itself forever (the feedback-loop bug
/// this design fixes). Instead, after a build finishes, the pipeline broadcasts the reload
/// event for the output it wrote, so the browser still hot-swaps/reloads.
///
/// CSS and JS are each independently configured via an optional `(tool, options)` pair.
/// `options.bundle()` selects between two disjoint modes per language: bundle (a single
/// discovered/entry-driven output, rebuilt in full on any relevant change) or per-file
/// (every source file mirrored independently into the output dir).
///
/// `asset_folders` are a third, simpler kind of source: every file under one (any
/// extension, no transformation) is mirrored byte-identical into the output dir,
/// preserving its path relative to the asset folder — for hand-authored static files
/// (`index.html`, images) that should live outside the served/output dir as source,
/// same separation the CSS/JS pipelines already have, without needing a CSS/JS tool.
pub(crate) struct SourcePipeline {
    source_folders: Vec<PathBuf>,
    bundle_roots: Vec<PathBuf>,
    asset_folders: Vec<PathBuf>,
    output_dir: PathBuf,
    css_tool: Option<(CssTool, CssOptions)>,
    js_tool: Option<(JsTool, JsOptions)>,
    prune_output: bool,
    broadcaster: Broadcaster,
}

impl SourcePipeline {
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn new(
        source_folders: Vec<PathBuf>,
        bundle_roots: Vec<PathBuf>,
        asset_folders: Vec<PathBuf>,
        output_dir: PathBuf,
        css_tool: Option<(CssTool, CssOptions)>,
        js_tool: Option<(JsTool, JsOptions)>,
        prune_output: bool,
        broadcaster: Broadcaster,
    ) -> Self {
        SourcePipeline {
            source_folders,
            bundle_roots,
            asset_folders,
            output_dir,
            css_tool,
            js_tool,
            prune_output,
            broadcaster,
        }
    }

    /// Rebuild every enabled output once, then prune stale output if configured.
    ///
    /// Runs at server startup. Never during live-reload — see [`Self::prune_stale_output`].
    pub(crate) async fn full_build(&self) -> Result<(), SourceError> {
        let css_written = self.build_css_bundle().await?;
        self.build_js_bundle().await?;
        self.build_all_per_file().await?;
        self.build_all_assets().await?;

        if self.prune_output {
            self.prune_stale_output(css_written.as_deref()).await?;
        }

        Ok(())
    }

    /// Handle a single change event for `path` (which must be under a watched source
    /// folder or import root), rebuilding output as needed and broadcasting the reload
    /// event for whatever was written.
    pub(crate) async fn process_change(
        &self,
        path: &Path,
        change_type: &ChangeType,
    ) -> Result<(), SourceError> {
        // Asset folders own any extension, so this check runs before the CSS/JS
        // dispatch below (which only fires for `ChangeType::Css`/`Script`) — an
        // asset folder file with one of those extensions is still a plain copy, not
        // a CSS/JS build input.
        if let Some(folder) = self.containing_asset_folder(path) {
            let output = self.mirror_output(folder, path)?;
            copy_asset(path, &output).await?;
            self.broadcast_change(&output);
            return Ok(());
        }

        if change_type == &ChangeType::Css && self.is_input(path) {
            if let Some((_, options)) = &self.css_tool {
                let written = if options.is_bundle() {
                    self.build_css_bundle().await?
                } else {
                    self.rebuild_css_file(path).await?
                };
                if let Some(output) = written {
                    self.broadcast_change(&output);
                }
                return Ok(());
            }
        }

        if change_type == &ChangeType::Script && self.is_input(path) {
            if let Some((_, options)) = &self.js_tool {
                let written = if options.is_bundle() {
                    self.build_js_bundle().await?
                } else {
                    self.rebuild_js_file(path).await?
                };
                if let Some(output) = written {
                    self.broadcast_change(&output);
                }
                return Ok(());
            }
        }

        // No pipeline owns this file kind. Re-broadcast only genuine watched-input
        // changes so the client reloads and re-fetches whatever external builder produced
        // the output. The watcher never watches the output dir, so the ONLY paths that
        // reach here without being under a watched root are this pipeline's own broadcast
        // echoes (e.g. a rebuilt bundle) — re-broadcasting those would make the pipeline
        // loop on its own output forever, flooding the SSE stream (the `_mr` cycling bug).
        if self.is_input(path) {
            self.broadcaster.broadcast(ChangeEvent {
                path: path.to_path_buf(),
                change_type: change_type.clone(),
            });
        }
        Ok(())
    }

    /// Rebuild the single CSS bundle from every source folder, if CSS bundle mode is
    /// configured. Returns the output path it wrote, or `None` when bundle mode isn't
    /// configured or no CSS sources exist (nothing to produce).
    async fn build_css_bundle(&self) -> Result<Option<PathBuf>, SourceError> {
        let Some((css_tool, options)) = &self.css_tool else {
            return Ok(None);
        };
        if !options.is_bundle() || !self.has_css_sources() {
            return Ok(None);
        }

        let output = self.output_dir.join(options.output_file_name());
        css::build_css_bundle(*css_tool, options, &self.source_folders, &output)
            .await
            .map_err(SourceError::Css)?;

        Ok(Some(output))
    }

    /// Rebuild the JS bundle from its configured entry point, if JS bundle mode is
    /// configured. Returns the output path it wrote, or `None` when bundle mode isn't
    /// configured.
    async fn build_js_bundle(&self) -> Result<Option<PathBuf>, SourceError> {
        let Some((js_tool, options)) = &self.js_tool else {
            return Ok(None);
        };
        let (true, Some(entry)) = (options.is_bundle(), options.entry()) else {
            return Ok(None);
        };

        let name = options.output_file_name().unwrap_or("bundle.js");
        let output = self.output_dir.join(name);
        js::build_js_bundle(*js_tool, options, entry, &output)
            .await
            .map_err(SourceError::Js)?;

        Ok(Some(output))
    }

    /// Process every file under every source folder through whichever per-file (i.e.
    /// non-bundle) pipeline claims its extension. Bundle-mode CSS/JS is handled
    /// separately by [`Self::build_css_bundle`]/[`Self::build_js_bundle`].
    async fn build_all_per_file(&self) -> Result<(), SourceError> {
        for folder in &self.source_folders {
            let files = list_files(folder).await.map_err(SourceError::Io)?;
            for file in files {
                if is_css(&file) {
                    if let Some((css_tool, options)) = &self.css_tool {
                        if !options.is_bundle() {
                            let output = self.mirror_output(folder, &file)?;
                            css::build_css_file(*css_tool, options, &file, &output)
                                .await
                                .map_err(SourceError::Css)?;
                        }
                    }
                } else if is_script(&file) {
                    if let Some((js_tool, options)) = &self.js_tool {
                        if !options.is_bundle() {
                            let output = self.mirror_output(folder, &file)?;
                            js::build_js_file(*js_tool, options, &file, &output)
                                .await
                                .map_err(SourceError::Js)?;
                        }
                    }
                }
            }
        }
        Ok(())
    }

    /// Rebuild a single changed CSS file into its mirrored output path (per-file mode
    /// only — bundle mode always rebuilds the whole bundle via [`Self::build_css_bundle`]).
    async fn rebuild_css_file(&self, source: &Path) -> Result<Option<PathBuf>, SourceError> {
        let Some(folder) = self.containing_source_folder(source) else {
            return Ok(None);
        };
        if !is_css(source) {
            return Ok(None);
        }
        let Some((css_tool, options)) = &self.css_tool else {
            return Ok(None);
        };

        let output = self.mirror_output(folder, source)?;
        css::build_css_file(*css_tool, options, source, &output)
            .await
            .map_err(SourceError::Css)?;
        Ok(Some(output))
    }

    /// Rebuild a single changed JS file into its mirrored output path (per-file mode
    /// only — bundle mode always rebuilds the whole bundle via [`Self::build_js_bundle`]).
    async fn rebuild_js_file(&self, source: &Path) -> Result<Option<PathBuf>, SourceError> {
        let Some(folder) = self.containing_source_folder(source) else {
            return Ok(None);
        };
        if !is_script(source) {
            return Ok(None);
        }
        let Some((js_tool, options)) = &self.js_tool else {
            return Ok(None);
        };

        let output = self.mirror_output(folder, source)?;
        js::build_js_file(*js_tool, options, source, &output)
            .await
            .map_err(SourceError::Js)?;
        Ok(Some(output))
    }

    /// Mirror every file under every asset folder into the output dir, byte-identical,
    /// preserving each file's path relative to its asset folder.
    async fn build_all_assets(&self) -> Result<(), SourceError> {
        for folder in &self.asset_folders {
            let files = list_files(folder).await.map_err(SourceError::Io)?;
            for file in files {
                let output = self.mirror_output(folder, &file)?;
                copy_asset(&file, &output).await?;
            }
        }
        Ok(())
    }

    /// The asset folder containing `path`, if any.
    fn containing_asset_folder(&self, path: &Path) -> Option<&PathBuf> {
        self.asset_folders
            .iter()
            .find(|folder| path.starts_with(folder))
    }

    /// The output path for `source`, mirroring its path relative to `folder` under the
    /// output dir.
    fn mirror_output(&self, folder: &Path, source: &Path) -> Result<PathBuf, SourceError> {
        let relative = source
            .strip_prefix(folder)
            .map_err(|_| SourceError::NotUnderSource(source.to_path_buf()))?;
        Ok(self.output_dir.join(relative))
    }

    /// Broadcast a reload event for `output`, deriving its change type from the output's
    /// own extension so the browser hot-swaps/reloads exactly as it would for that file.
    fn broadcast_change(&self, output: &Path) {
        self.broadcaster.broadcast(ChangeEvent {
            path: output.to_path_buf(),
            change_type: ChangeType::from_path(output),
        });
    }

    /// True when `path` lives under any watched root (a source folder, a CSS `@import`
    /// root, or an asset folder). These are the only paths the watcher emits, so an
    /// event whose path fails this check must be the pipeline's own output echo and
    /// must not be re-broadcast.
    fn is_input(&self, path: &Path) -> bool {
        self.source_folders
            .iter()
            .chain(self.bundle_roots.iter())
            .chain(self.asset_folders.iter())
            .any(|root| path.starts_with(root))
    }

    /// The source folder containing `path`, if any.
    fn containing_source_folder(&self, path: &Path) -> Option<&PathBuf> {
        self.source_folders
            .iter()
            .find(|folder| path.starts_with(folder))
    }

    /// True when at least one `.css` file exists under the source folders.
    fn has_css_sources(&self) -> bool {
        for folder in &self.source_folders {
            if walk_dir(folder).any(|path| is_css(&path)) {
                return true;
            }
        }
        false
    }

    /// The CSS bundle's output path, if CSS bundle mode is configured.
    fn css_bundle_output_path(&self) -> Option<PathBuf> {
        let (_, options) = self.css_tool.as_ref()?;
        if !options.is_bundle() {
            return None;
        }
        Some(self.output_dir.join(options.output_file_name()))
    }

    /// Remove stale output at build time, never during live-reload.
    ///
    /// The only output this server can own *by identity* is the CSS bundle file — a single
    /// exact path that no hand-written file shares. If bundling is enabled but no CSS
    /// sources remain, the leftover bundle is removed. Per-file outputs are deliberately
    /// NOT auto-pruned: their mirrored paths can coincide with hand-written files, and
    /// deleting files the server doesn't own is a surprise (A1) the caller can't opt into
    /// by accident.
    async fn prune_stale_output(&self, css_written: Option<&Path>) -> Result<(), SourceError> {
        let Some(bundle) = self.css_bundle_output_path() else {
            return Ok(());
        };

        let wrote_bundle = css_written.is_some_and(|written| written == bundle);
        if wrote_bundle {
            return Ok(());
        }

        if tokio::fs::metadata(&bundle).await.is_err() {
            return Ok(());
        }

        tokio::fs::remove_file(&bundle)
            .await
            .map_err(SourceError::Io)?;
        eprintln!("pruned stale css bundle output: {}", bundle.display());
        Ok(())
    }
}

/// Why [`SourcePipeline`] could not produce output for a change or build.
#[derive(Debug)]
pub(crate) enum SourceError {
    /// The CSS tool step failed.
    Css(css::CssError),
    /// The JS tool step failed.
    Js(js::JsError),
    /// A filesystem operation failed.
    Io(std::io::Error),
    /// A changed path was not under the source folder claimed to contain it.
    NotUnderSource(PathBuf),
}

impl std::fmt::Display for SourceError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SourceError::Css(e) => write!(f, "css tool step failed: {e}"),
            SourceError::Js(e) => write!(f, "js tool step failed: {e}"),
            SourceError::Io(e) => write!(f, "io error: {e}"),
            SourceError::NotUnderSource(p) => {
                write!(f, "path not under any source folder: {}", p.display())
            }
        }
    }
}

impl std::error::Error for SourceError {}

/// True if `path` names a CSS file (by extension).
fn is_css(path: &Path) -> bool {
    path.extension().and_then(|e| e.to_str()) == Some("css")
}

/// True if `path` names a script the tool accepts (`js`/`mjs`).
fn is_script(path: &Path) -> bool {
    matches!(
        path.extension().and_then(|e| e.to_str()),
        Some("js" | "mjs")
    )
}

/// Copy `source` to `output` byte-identical, creating `output`'s parent directory if
/// needed — the flat-mirror operation asset folders use, with no transformation.
async fn copy_asset(source: &Path, output: &Path) -> Result<(), SourceError> {
    if let Some(parent) = output.parent() {
        tokio::fs::create_dir_all(parent)
            .await
            .map_err(SourceError::Io)?;
    }
    tokio::fs::copy(source, output)
        .await
        .map_err(SourceError::Io)?;
    Ok(())
}

/// Recursively list every file under `dir`.
///
/// Bounded by the filesystem: `walk_dir` pushes directories onto a stack and terminates
/// when none remain — a directory tree is finite, so this loop always ends.
async fn list_files(dir: &Path) -> std::io::Result<Vec<PathBuf>> {
    let mut files = Vec::new();
    let mut dirs = vec![dir.to_path_buf()];

    while let Some(current) = dirs.pop() {
        let mut entries = tokio::fs::read_dir(&current).await?;
        while let Some(entry) = entries.next_entry().await? {
            let path = entry.path();
            if entry.file_type().await?.is_dir() {
                dirs.push(path);
            } else {
                files.push(path);
            }
        }
    }

    Ok(files)
}

/// Synchronously walk `dir` and yield every file path. Used by the cheap existence check
/// in [`SourcePipeline::has_css_sources`] (runs at build boundaries, not per request).
fn walk_dir(dir: &Path) -> impl Iterator<Item = PathBuf> {
    let mut dirs = vec![dir.to_path_buf()];
    std::iter::from_fn(move || {
        while let Some(current) = dirs.pop() {
            let Ok(entries) = std::fs::read_dir(&current) else {
                continue;
            };
            for entry in entries.flatten() {
                let path = entry.path();
                if entry.file_type().is_ok_and(|t| t.is_dir()) {
                    dirs.push(path);
                } else {
                    return Some(path);
                }
            }
        }
        None
    })
}

#[cfg(test)]
#[path = "../tests/unit/source.rs"]
mod tests;