use std::collections::{BTreeMap, HashMap};
use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use anyhow::{Result, anyhow};
use sha2::{Digest, Sha256};
use super::compile::{compile, relative_imports};
use super::{SYSTEM, system};
use crate::addresses::{Owned, address};
use crate::folder::{ServedFolder, lexical};
#[derive(Clone, Default)]
pub struct Modules {
inner: Arc<Caches>,
}
#[derive(Default)]
struct Caches {
read: Mutex<HashMap<PathBuf, Read>>,
compiled: Mutex<HashMap<u64, Result<String, String>>>,
}
impl std::fmt::Debug for Modules {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Modules")
}
}
#[derive(Clone, Debug)]
struct Read {
changed: Option<std::time::SystemTime>,
len: u64,
source: Arc<str>,
imports: Vec<(String, PathBuf)>,
specified: Vec<PathBuf>,
}
pub(super) const IMPORT_EXTENSIONS: [&str; 3] = ["", ".tsx", ".ts"];
impl Modules {
pub fn snapshot(&self, served: &ServedFolder, file: &Path) -> Snapshot {
let mut reads = BTreeMap::new();
let mut queue = vec![file.to_path_buf()];
while let Some(next) = queue.pop() {
if reads.contains_key(&next) || served.file(&next).is_none() {
continue;
}
let Some(read) = self.read(&next) else {
continue;
};
queue.extend(read.imports.iter().map(|(_, p)| p.clone()));
reads.insert(next, read);
}
Snapshot {
served: served.clone(),
reads,
modules: self.clone(),
}
}
pub fn forget(&self, file: &Path) {
let file = lexical(file);
if let Ok(mut cache) = self.inner.read.lock() {
cache.remove(&file);
cache.retain(|_, read| {
!read.specified.iter().any(|base| {
IMPORT_EXTENSIONS
.iter()
.any(|ext| file.as_os_str() == format!("{}{ext}", base.display()).as_str())
})
});
}
}
pub fn compile(
&self,
path: &Path,
source: &str,
known: &[String],
imports: &BTreeMap<String, String>,
) -> Result<String> {
let mut hasher = std::collections::hash_map::DefaultHasher::new();
(path, source, known, imports).hash(&mut hasher);
let key = hasher.finish();
if let Some(found) = self
.inner
.compiled
.lock()
.ok()
.and_then(|cache| cache.get(&key).cloned())
{
return found.map_err(|e| anyhow!(e));
}
let result = compile(path, source, known, imports).map_err(|e| e.to_string());
if let Ok(mut cache) = self.inner.compiled.lock() {
if cache.len() > 512 {
cache.clear();
}
cache.insert(key, result.clone());
}
result.map_err(|e| anyhow!(e))
}
pub fn system(&self, name: &str) -> Option<Result<String>> {
let source = system(name)?;
let known: Vec<String> = SYSTEM.iter().map(|(name, _)| name.to_string()).collect();
Some(self.compile(
Path::new(&format!("{name}.tsx")),
source,
&known,
&Default::default(),
))
}
fn read(&self, file: &Path) -> Option<Read> {
let meta = std::fs::metadata(file).ok().filter(|m| m.is_file())?;
let (changed, len) = (meta.modified().ok(), meta.len());
if let Some(found) = self.inner.read.lock().ok().and_then(|cache| {
cache
.get(file)
.filter(|r| r.changed == changed && r.len == len)
.cloned()
}) {
return Some(found);
}
let source: Arc<str> = std::fs::read_to_string(file).ok()?.into();
let folder = file.parent().unwrap_or(file);
let specifiers = relative_imports(file, &source);
let specified = specifiers
.iter()
.map(|specifier| lexical(&folder.join(specifier)))
.collect();
let imports = specifiers
.into_iter()
.filter_map(|specifier| {
let base = folder.join(&specifier);
IMPORT_EXTENSIONS
.iter()
.map(|ext| PathBuf::from(format!("{}{ext}", base.display())))
.find(|path| path.is_file())
.and_then(|path| path.canonicalize().ok())
.map(|path| (specifier, path))
})
.collect();
let read = Read {
changed,
len,
source,
imports,
specified,
};
if let Ok(mut cache) = self.inner.read.lock() {
if cache.len() > 4096 {
cache.clear();
}
cache.insert(file.to_path_buf(), read.clone());
}
Some(read)
}
}
#[derive(Debug)]
pub struct Snapshot {
served: ServedFolder,
reads: BTreeMap<PathBuf, Read>,
modules: Modules,
}
impl Snapshot {
pub fn served(&self) -> &ServedFolder {
&self.served
}
pub fn closure(&self, file: &Path) -> Vec<(PathBuf, Arc<str>)> {
let Some(root) = self.reads.get(file) else {
return Vec::new();
};
let mut found: BTreeMap<PathBuf, Arc<str>> = BTreeMap::new();
let mut queue: Vec<PathBuf> = root.imports.iter().map(|(_, p)| p.clone()).collect();
while let Some(next) = queue.pop() {
if next == file || found.contains_key(&next) || !self.served.contains(&next) {
continue;
}
let Some(read) = self.reads.get(&next) else {
continue;
};
queue.extend(read.imports.iter().map(|(_, p)| p.clone()));
found.insert(next, read.source.clone());
}
std::iter::once((file.to_path_buf(), root.source.clone()))
.chain(found)
.collect()
}
pub fn hash(&self, file: &Path, known: &[String]) -> String {
let mut hash = Sha256::new();
hash.update(crate::addresses::BUILD);
hash.update(known.join("\0"));
for (path, source) in self.closure(file) {
hash.update([0]);
hash.update(self.name(&path).as_bytes());
hash.update([0]);
hash.update(source.as_bytes());
}
crate::addresses::short(&hash.finalize())
}
pub fn address(&self, file: &Path, known: &[String]) -> String {
let name = self.name(file);
address(Owned::Module {
mount: self.served.mount(),
name: &name,
hash: &self.hash(file, known),
})
}
fn name(&self, file: &Path) -> String {
self.served
.name(file)
.unwrap_or_else(|| panic!("{} is outside the served folder", file.display()))
}
pub fn compile(&self, file: &Path, known: &[String]) -> Result<String> {
let read = self
.reads
.get(file)
.ok_or_else(|| anyhow!("{} can't be read", file.display()))?;
let imports = read
.imports
.iter()
.filter(|(_, path)| self.served.contains(path))
.map(|(specifier, path)| (specifier.clone(), self.address(path, known)))
.collect();
self.modules.compile(file, &read.source, known, &imports)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::widgets::is_widget_file;
fn module_hash(modules: &Modules, served: &Path, file: &Path, known: &[String]) -> String {
modules
.snapshot(&ServedFolder::new(served).unwrap(), file)
.hash(file, known)
}
#[test]
fn compiling_is_remembered_until_the_source_or_an_import_changes() {
let modules = Modules::default();
let compile_cached =
|path: &Path, source: &str, known: &[String], imports: &BTreeMap<String, String>| {
modules.compile(path, source, known, imports)
};
let path = Path::new("Same.tsx");
let none = BTreeMap::new();
let first = compile_cached(path, "export default () => <b>1</b>;", &[], &none).unwrap();
let again = compile_cached(path, "export default () => <b>1</b>;", &[], &none).unwrap();
assert_eq!(first, again);
let edited = compile_cached(path, "export default () => <b>2</b>;", &[], &none).unwrap();
assert!(edited.contains('2'), "{edited}");
let source = "export { x } from \"./x\";";
let imports = |v: &str| BTreeMap::from([("./x".to_string(), format!("/x.ts?v={v}"))]);
let one = compile_cached(path, source, &[], &imports("1")).unwrap();
let two = compile_cached(path, source, &[], &imports("2")).unwrap();
assert!(
one.contains("/x.ts?v=1") && two.contains("/x.ts?v=2"),
"{two}"
);
let error = compile_cached(path, "export default (", &[], &none).unwrap_err();
assert!(!error.to_string().is_empty());
}
#[test]
fn editing_a_helper_changes_its_importers_addresses_only() {
let modules = Modules::default();
let dir = std::env::temp_dir().join(format!("devpage-wversion-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("w")).unwrap();
let dir = dir.canonicalize().unwrap();
let w = dir.join("w");
std::fs::write(
w.join("Card.tsx"),
"import { label } from \"./label\";\nexport default () => label;",
)
.unwrap();
std::fs::write(
w.join("Deep.tsx"),
"export * from \"./Card\";\nexport default () => null;",
)
.unwrap();
std::fs::write(w.join("Other.tsx"), "export default () => null;").unwrap();
std::fs::write(w.join("label.ts"), "export const label = 1;").unwrap();
let known = vec!["Card".to_string(), "Deep".to_string(), "Other".to_string()];
let hashes = || {
["Card.tsx", "Deep.tsx", "Other.tsx", "label.ts"]
.map(|name| module_hash(&modules, &dir, &w.join(name), &known))
};
let before = hashes();
std::fs::write(w.join("label.ts"), "export const label = 2;").unwrap();
modules.forget(&w.join("label.ts"));
let after = hashes();
assert_ne!(before[0], after[0], "the widget importing it");
assert_ne!(before[1], after[1], "a widget importing that widget");
assert_eq!(before[2], after[2], "a widget that doesn't import it");
assert_ne!(before[3], after[3], "the helper itself");
let card = modules
.snapshot(&ServedFolder::new(&dir).unwrap(), &w.join("Card.tsx"))
.compile(&w.join("Card.tsx"), &known)
.unwrap();
let label = modules
.snapshot(&ServedFolder::new(&dir).unwrap(), &w.join("label.ts"))
.address(&w.join("label.ts"), &known);
assert!(card.contains(&format!("\"{label}\"")), "{card}");
assert!(label.ends_with(&format!("?v={}", after[3])), "{label}");
assert_ne!(
module_hash(&modules, &dir, &w.join("Other.tsx"), &known[..2]),
after[2]
);
assert!(is_widget_file(&w.join("label.ts"), &w));
assert!(!is_widget_file(&w.join("notes.md"), &w));
}
#[test]
fn a_helper_created_after_its_importer_moves_the_importers_address() {
let modules = Modules::default();
let dir = std::env::temp_dir().join(format!("devpage-wlate-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("w")).unwrap();
let dir = dir.canonicalize().unwrap();
let w = dir.join("w");
let other = w.join("Other.tsx");
std::fs::write(
&other,
"import { label } from \"./label\";\nexport default () => label;",
)
.unwrap();
let known = vec!["Other".to_string()];
let missing = module_hash(&modules, &dir, &other, &known);
std::fs::write(w.join("label.ts"), "export const label = 1;").unwrap();
modules.forget(&w.join("label.ts"));
let created = module_hash(&modules, &dir, &other, &known);
assert_ne!(missing, created, "creating the helper");
let label = modules
.snapshot(&ServedFolder::new(&dir).unwrap(), &w.join("label.ts"))
.address(&w.join("label.ts"), &known);
let compiled = modules
.snapshot(&ServedFolder::new(&dir).unwrap(), &other)
.compile(&other, &known)
.unwrap();
assert!(compiled.contains(&format!("\"{label}\"")), "{compiled}");
std::fs::write(w.join("label.ts"), "export const label = 2;").unwrap();
modules.forget(&w.join("label.ts"));
assert_ne!(
created,
module_hash(&modules, &dir, &other, &known),
"editing the helper"
);
let edited = module_hash(&modules, &dir, &other, &known);
std::fs::remove_file(w.join("label.ts")).unwrap();
modules.forget(&w.join("label.ts"));
assert_ne!(
edited,
module_hash(&modules, &dir, &other, &known),
"removing it"
);
}
#[test]
fn a_snapshot_hashes_and_compiles_what_it_read_whatever_is_saved_since() {
let modules = Modules::default();
let dir = std::env::temp_dir().join(format!("devpage-wsnap-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("w")).unwrap();
let dir = dir.canonicalize().unwrap();
let w = dir.join("w");
let card = w.join("Card.tsx");
std::fs::write(
&card,
"import { label } from \"./label\";\nexport default () => \"old\" + label;",
)
.unwrap();
std::fs::write(w.join("label.ts"), "export const label = 1;").unwrap();
let known = vec!["Card".to_string()];
let snapshot = modules.snapshot(&ServedFolder::new(&dir).unwrap(), &card);
let hash = snapshot.hash(&card, &known);
std::fs::write(
&card,
"import { label } from \"./label\";\nexport default () => \"new\" + label;",
)
.unwrap();
std::fs::write(w.join("label.ts"), "export const label = 2;").unwrap();
modules.forget(&card);
modules.forget(&w.join("label.ts"));
assert_ne!(
hash,
module_hash(&modules, &dir, &card, &known),
"the files did change"
);
assert_eq!(hash, snapshot.hash(&card, &known));
let compiled = snapshot.compile(&card, &known).unwrap();
assert!(compiled.contains("\"old\""), "{compiled}");
let label = snapshot.address(&w.join("label.ts"), &known);
assert!(compiled.contains(&format!("\"{label}\"")), "{compiled}");
assert_ne!(
label,
modules
.snapshot(&ServedFolder::new(&dir).unwrap(), &w.join("label.ts"))
.address(&w.join("label.ts"), &known)
);
}
#[cfg(unix)]
#[test]
fn a_module_or_import_that_links_out_of_the_served_folder_is_never_read() {
let top = std::env::temp_dir().join(format!("devpage-wlinkout-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&top);
std::fs::create_dir_all(top.join("served/w")).unwrap();
std::fs::create_dir_all(top.join("outside")).unwrap();
let top = top.canonicalize().unwrap();
let (served, w) = (top.join("served"), top.join("served/w"));
std::fs::write(
top.join("outside/secret.ts"),
"export const secret = \"SECRET\";",
)
.unwrap();
std::fs::write(
top.join("outside/Evil.tsx"),
"export default () => \"EVIL\";",
)
.unwrap();
std::os::unix::fs::symlink(top.join("outside/secret.ts"), w.join("leak.ts")).unwrap();
std::os::unix::fs::symlink(top.join("outside/Evil.tsx"), w.join("Evil.tsx")).unwrap();
std::fs::write(
w.join("Card.tsx"),
"import { secret } from \"./leak\";\nexport default () => secret;",
)
.unwrap();
let modules = Modules::default();
let card = modules.snapshot(&ServedFolder::new(&served).unwrap(), &w.join("Card.tsx"));
let closure: Vec<PathBuf> = card
.closure(&w.join("Card.tsx"))
.into_iter()
.map(|(p, _)| p)
.collect();
assert_eq!(closure, vec![w.join("Card.tsx")]);
let compiled = card.compile(&w.join("Card.tsx"), &[]).unwrap();
assert!(!compiled.contains("SECRET"), "{compiled}");
let evil = modules.snapshot(&ServedFolder::new(&served).unwrap(), &w.join("Evil.tsx"));
assert!(evil.closure(&w.join("Evil.tsx")).is_empty());
assert!(evil.compile(&w.join("Evil.tsx"), &[]).is_err());
}
}