use std::io::Read;
use std::path::PathBuf;
pub const MAX_BUNDLE_BYTES: u64 = 100 * 1024 * 1024;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Fetched {
pub path: PathBuf,
pub digest: String,
pub bytes: usize,
}
pub fn bundle_dir() -> Option<PathBuf> {
let cache = crate::script_cache::default_cache_path();
Some(cache.parent()?.join("bundles"))
}
pub fn path_for(digest: &str) -> Option<PathBuf> {
Some(bundle_dir()?.join(format!("{digest}.zip")))
}
pub fn fetched() -> Vec<String> {
let Some(dir) = bundle_dir() else {
return Vec::new();
};
let mut out: Vec<String> = std::fs::read_dir(dir)
.into_iter()
.flatten()
.filter_map(|entry| entry.ok())
.filter_map(|entry| {
let name = entry.file_name().to_string_lossy().into_owned();
name.strip_suffix(".zip").map(str::to_string)
})
.collect();
out.sort();
out
}
pub fn store(bytes: &[u8]) -> Result<Fetched, String> {
let digest = sha256_hex(bytes);
let path = path_for(&digest).ok_or("no cache directory")?;
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)
.map_err(|e| format!("cannot make {}: {e}", parent.display()))?;
}
if !path.exists() {
let tmp = path.with_extension(format!("tmp.{}", std::process::id()));
std::fs::write(&tmp, bytes).map_err(|e| format!("cannot write {}: {e}", tmp.display()))?;
std::fs::rename(&tmp, &path)
.map_err(|e| format!("cannot rename into {}: {e}", path.display()))?;
}
Ok(Fetched {
path,
digest,
bytes: bytes.len(),
})
}
fn sha256_hex(bytes: &[u8]) -> String {
use sha2::Digest;
let mut hasher = sha2::Sha256::new();
hasher.update(bytes);
hasher
.finalize()
.iter()
.map(|b| format!("{b:02x}"))
.collect()
}
pub fn fetch(url: &str) -> Result<Fetched, String> {
if !(url.starts_with("https://") || url.starts_with("http://")) {
return Err(format!("{url}: only http and https are fetched"));
}
let mut response = ureq::get(url).call().map_err(|e| format!("{url}: {e}"))?;
let mut body = Vec::new();
let mut reader = response.body_mut().as_reader().take(MAX_BUNDLE_BYTES + 1);
reader
.read_to_end(&mut body)
.map_err(|e| format!("{url}: {e}"))?;
if body.len() as u64 > MAX_BUNDLE_BYTES {
return Err(format!(
"{url}: larger than the {MAX_BUNDLE_BYTES}-byte limit for a bundle"
));
}
store(&body)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn only_http_urls_are_fetched() {
for url in ["/tmp/bundle.zip", "file:///tmp/bundle.zip", "ftp://x/y.zip"] {
let err = fetch(url).unwrap_err();
assert!(err.contains("only http and https"), "{url}: {err}");
}
}
#[test]
fn what_is_stored_is_filed_under_the_digest_of_its_bytes() {
let one = store(b"a bundle's bytes").expect("stores");
let same = store(b"a bundle's bytes").expect("stores");
let other = store(b"different bytes").expect("stores");
assert_eq!(
one.digest, same.digest,
"the same bytes are the same bundle"
);
assert_eq!(one.path, same.path, "and share one file");
assert_ne!(one.digest, other.digest);
assert_eq!(one.digest.len(), 64, "sha-256, hex");
assert!(one.path.ends_with(format!("{}.zip", one.digest)));
assert_eq!(one.bytes, 16);
assert!(fetched().contains(&one.digest));
for f in [one, other] {
let _ = std::fs::remove_file(f.path);
}
}
#[test]
fn a_digest_is_of_the_bytes_and_not_of_their_lossy_text() {
let a = store(&[0xff, 0xfe, 0x00]).expect("stores");
let b = store(&[0xff, 0xfd, 0x00]).expect("stores");
assert_ne!(a.digest, b.digest, "different bytes, different bundles");
for f in [a, b] {
let _ = std::fs::remove_file(f.path);
}
}
#[test]
fn the_cache_path_is_beside_the_bytecode_cache() {
let dir = bundle_dir().expect("a cache directory");
assert!(dir.ends_with("texrs/bundles"), "{dir:?}");
let path = path_for("abc").unwrap();
assert!(path.ends_with("abc.zip"), "{path:?}");
}
}