refactor: make shared path and cache utilities wasm-safe

Move native-only path and Tokio dependencies behind cfg gates so shared crates can compile for wasm32. Replace the absolute-path implementation with a lexical normalization fallback on wasm, use a std mutex for the cache helper on wasm, and degrade git symlink creation to an unsupported-platform error instead of a compile-time failure.
This commit is contained in:
Jeremy Lewi
2026-04-13 09:58:43 -07:00
parent 5bc4c5c997
commit 609b6168b3
6 changed files with 99 additions and 3 deletions
+16
View File
@@ -5,7 +5,13 @@ use std::num::NonZeroUsize;
use lru::LruCache;
use sha1::Digest;
use sha1::Sha1;
#[cfg(target_arch = "wasm32")]
use std::sync::Mutex;
#[cfg(target_arch = "wasm32")]
use std::sync::MutexGuard;
#[cfg(not(target_arch = "wasm32"))]
use tokio::sync::Mutex;
#[cfg(not(target_arch = "wasm32"))]
use tokio::sync::MutexGuard;
/// A minimal LRU cache protected by a Tokio mutex.
@@ -119,6 +125,7 @@ where
}
}
#[cfg(not(target_arch = "wasm32"))]
fn lock_if_runtime<K, V>(m: &Mutex<LruCache<K, V>>) -> Option<MutexGuard<'_, LruCache<K, V>>>
where
K: Eq + Hash,
@@ -127,6 +134,14 @@ where
Some(tokio::task::block_in_place(|| m.blocking_lock()))
}
#[cfg(target_arch = "wasm32")]
fn lock_if_runtime<K, V>(m: &Mutex<LruCache<K, V>>) -> Option<MutexGuard<'_, LruCache<K, V>>>
where
K: Eq + Hash,
{
m.lock().ok()
}
/// Computes the SHA-1 digest of `bytes`.
///
/// Useful for content-based cache keys when you want to avoid staleness
@@ -141,6 +156,7 @@ pub fn sha1_digest(bytes: &[u8]) -> [u8; 20] {
out
}
#[cfg(not(target_arch = "wasm32"))]
#[cfg(test)]
mod tests {
use super::BlockingLruCache;