A high-performance inference engine for AI models
wasm downloader part 1: kiban (#593)
Aleksandr Golokoz50b1169cc985
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.ymlindex 2a1fbd012..a216905ee 100644--- a/.github/workflows/tests.yml+++ b/.github/workflows/tests.yml@@ -116,6 +116,14 @@ jobs:- name: Build cratesrun: cargo +stable build --no-default-features $FEATURE_FLAGS ${{ matrix.target == 'wasm32-wasip1-threads' && '-p uzu' || '' }} ${{ matrix.host == 'linux' && '-p cli --features backend-cpu,capability-cli' || '' }}+ # Temporary step to check crates that migrated to wasm.+ # New value to matrix was not added intentionally because the whole project is not wasm supported.+ - name: Build wasm supported crates+ if: ${{ matrix.target == 'wasm32-wasip1-threads' }}+ run: |+ rustup +stable target add wasm32-unknown-unknown+ cargo +stable build -p kiban --target wasm32-unknown-unknown+- name: Build benchesif: ${{ matrix.backend != 'cpu' }}run: cargo +nightly bench --no-run --no-default-features $FEATURE_FLAGSdiff --git a/Cargo.lock b/Cargo.lockindex 77c77ad4b..d879c4ab5 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -3311,6 +3311,20 @@ dependencies = ["log",]+[[package]]+name = "kiban"+version = "0.5.12"+dependencies = [+ "async-trait",+ "futures-util",+ "libc",+ "tokio",+ "wasm-bindgen",+ "wasm-bindgen-futures",+ "web-sys",+ "web-time",+]+[[package]]name = "lab"version = "0.11.0"diff --git a/Cargo.toml b/Cargo.tomlindex 95ec3c170..04f93085c 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -19,6 +19,7 @@ members = ["crates/test-runner","crates/keisoku","crates/kanka",+ "crates/kiban",]resolver = "3"@@ -59,6 +60,7 @@ download-manager = { path = "crates/download-manager", default-features = falsemock-registry = { path = "crates/mock-registry", default-features = false }keisoku = { path = "crates/keisoku", default-features = false }kanka = { path = "crates/kanka", default-features = false }+kiban = { path = "crates/kiban", default-features = false }uzu = { path = "crates/uzu", default-features = false }xgrammar = { package = "xgrammar-rs", git = "https://github.com/trymirai/xgrammar-rs.git", tag = "0.2.2", features = ["tokenizers"] }half = { version = "2.7", features = ["bytemuck", "num-traits"] }@@ -108,7 +110,9 @@ clang-ast = "0.1"rangemap = "1.7.1"futures = "0.3"async-stream = "0.3"-tokio = { version = "1.49", features = ["full"] }+tokio = "1.49"+tokio-stream = { version = "0.1.18", features = ["sync"] }+tokio-util = { version = "0.7.17", default-features = false, features = ["rt"] }blake3 = "1.8"quote = "1.0"proc-macro2 = "1.0"@@ -133,7 +137,6 @@ tracing-subscriber = { version = "0.3.22", features = ["env-filter"] }tracing-tree = "0.4.1"tracing-appender = "0.2.4"llm_json = "1.0.2"-tokio-stream = { version = "0.1.18", features = ["sync"] }futures-util = "0.3.31"http = "1.4.0"httparse = "1.10.1"@@ -144,7 +147,7 @@ reqwest = { version = "0.13.2", features = ["blocking","query",] }-tokio-util = { version = "0.7.17", default-features = false, features = ["rt"] }+async-shutdown = "0.2.2"objc-sys = "0.3.5"crc32c = "0.6.8"base64 = "0.22.1"@@ -206,6 +209,8 @@ rodio = { version = "0.22.2", default-features = false, features = ["playback","wav",] }+web-sys = "0.3"+web-time = "1.1"[workspace.lints.clippy]# TODO: add large_stack_frames, large_stack_arrays, trivially_copy_pass_by_ref, needless_pass_by_value, map_err_ignore, use_self, cloned_instead_of_copied, manual_let_else, semicolon_if_nothing_returned, uninlined_format_args, lossy_float_literaldiff --git a/crates/backend-uzu/Cargo.toml b/crates/backend-uzu/Cargo.tomlindex 9835c8502..c986973da 100644--- a/crates/backend-uzu/Cargo.toml+++ b/crates/backend-uzu/Cargo.toml@@ -78,7 +78,7 @@ tempfile.workspace = trueserde.workspace = trueserde_json = { workspace = true, features = ["unbounded_depth"] }futures.workspace = true-tokio.workspace = true+tokio = { workspace = true, features = ["full"] }blake3.workspace = truequote.workspace = trueproc-macro2.workspace = truediff --git a/crates/cli-storage/Cargo.toml b/crates/cli-storage/Cargo.tomlindex 402bc2927..54e3b32d1 100644--- a/crates/cli-storage/Cargo.toml+++ b/crates/cli-storage/Cargo.toml@@ -15,7 +15,7 @@ path = "src/main.rs"[dependencies]shoji.workspace = trueuzu.workspace = true-tokio.workspace = true+tokio = { workspace = true, features = ["full"] }futures-util.workspace = truecrossterm.workspace = trueratatui.workspace = truediff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.tomlindex cb2bc5a5b..6455f2077 100644--- a/crates/cli/Cargo.toml+++ b/crates/cli/Cargo.toml@@ -22,7 +22,7 @@ capability-cli = ["uzu/capability-cli"][dependencies]backend-uzu.workspace = trueuzu = { workspace = true, default-features = false }-tokio.workspace = true+tokio = { workspace = true, features = ["full"] }anyhow.workspace = trueclap.workspace = truecomfy-table.workspace = truediff --git a/crates/download-manager/Cargo.toml b/crates/download-manager/Cargo.tomlindex a1f5fb6d8..dc63bca03 100644--- a/crates/download-manager/Cargo.toml+++ b/crates/download-manager/Cargo.toml@@ -9,7 +9,7 @@ repository.workspace = truelicense.workspace = true[dependencies]-tokio.workspace = true+tokio = { workspace = true, features = ["full"] }tokio-stream.workspace = truefutures-util.workspace = trueserde.workspace = truediff --git a/crates/kiban/Cargo.toml b/crates/kiban/Cargo.tomlnew file mode 100644index 000000000..ab6074e2e--- /dev/null+++ b/crates/kiban/Cargo.toml@@ -0,0 +1,50 @@+[package]+name = "kiban"+description = "Cross-platform OS primitives"+edition.workspace = true+rust-version.workspace = true+version.workspace = true+authors.workspace = true+homepage.workspace = true+repository.workspace = true+license.workspace = true++[dependencies]+async-trait.workspace = true+futures-util.workspace = true++[target.'cfg(unix)'.dependencies]+libc.workspace = true++[target.'cfg(target_family = "wasm")'.dependencies]+tokio = { workspace = true, features = ["sync", "time"] }+wasm-bindgen.workspace = true+wasm-bindgen-futures.workspace = true+web-sys = { workspace = true, features = [+ "Blob",+ "console",+ "File",+ "FileSystemCreateWritableOptions",+ "FileSystemDirectoryHandle",+ "FileSystemFileHandle",+ "FileSystemGetDirectoryOptions",+ "FileSystemGetFileOptions",+ "FileSystemHandle",+ "FileSystemHandleKind",+ "FileSystemRemoveOptions",+ "FileSystemWritableFileStream",+ "Navigator",+ "Storage",+ "StorageManager",+ "Window",+ "WritableStream",+ "WriteParams",+ "WriteCommandType",+] }+web-time.workspace = true++[target.'cfg(not(target_family = "wasm"))'.dependencies]+tokio = { workspace = true, features = ["full"] }++[lints]+workspace = truediff --git a/crates/kiban/README.md b/crates/kiban/README.mdnew file mode 100644index 000000000..ea69ecf28--- /dev/null+++ b/crates/kiban/README.md@@ -0,0 +1,27 @@+# kiban++Cross-platform OS primitives (基盤, "foundation"). One async API that compiles to+[`tokio`](https://tokio.rs) on native targets and to browser primitives — [OPFS][opfs] for+storage, `wasm-bindgen-futures` for tasks — on `wasm`. Downstream crates depend on `kiban`+instead of `tokio` or `web-sys` directly, so the same code builds and runs on both.++[opfs]: https://developer.mozilla.org/en-US/docs/Web/API/File_System_API/Origin_private_file_system++## Modules++- **`fs`** — async filesystem operations backed by `tokio::fs` on native and OPFS on `wasm`.+ `PartFile` provides resumable, append-oriented writes for partial downloads.+- **`rt`** — task runtime.+- **`time`** — time primitives.+- **`process`** — process helpers.+- **`maybe`** — the `MaybeSend` / `MaybeSync` marker traits, which require `Send` / `Sync`+ on native targets and impose no bound on `wasm`, where futures are single-threaded.++The crate also exports the `printf!` and `eprintf!` macros, which route to `println!` /+`eprintln!` on native and to the browser console on `wasm`.++## Platform++`wasm` support targets the browser and relies on OPFS for storage; note that OPFS does not+support hard links, so `fs::hard_link` falls back to copying. On native targets the full+`tokio`-backed implementations are used.diff --git a/crates/kiban/src/fs/asyn.rs b/crates/kiban/src/fs/asyn.rsnew file mode 100644index 000000000..2df60c97f--- /dev/null+++ b/crates/kiban/src/fs/asyn.rs@@ -0,0 +1,133 @@+use std::{io, path::Path};++use crate::time::SystemTime;++pub async fn create_dir_all(path: impl AsRef<Path>) -> Result<(), io::Error> {+ #[cfg(target_family = "wasm")]+ return super::asyn_opfs::dir_create_all(path.as_ref().to_str().unwrap()).await;++ #[cfg(not(target_family = "wasm"))]+ tokio::fs::create_dir_all(path).await+}++pub async fn file_length(path: impl AsRef<Path>) -> Result<u64, io::Error> {+ #[cfg(target_family = "wasm")]+ return super::asyn_opfs::file_length(path.as_ref().to_str().unwrap()).await;++ #[cfg(not(target_family = "wasm"))]+ Ok(tokio::fs::metadata(path.as_ref()).await?.len())+}++pub async fn file_modified(path: impl AsRef<Path>) -> Result<SystemTime, io::Error> {+ #[cfg(target_family = "wasm")]+ return super::asyn_opfs::file_modified(path.as_ref().to_str().unwrap()).await;++ #[cfg(not(target_family = "wasm"))]+ tokio::fs::metadata(path.as_ref()).await?.modified()+}++pub async fn hard_link(+ original: impl AsRef<Path>,+ link: impl AsRef<Path>,+) -> Result<(), io::Error> {+ // OPFS doesn't support linking, that's why just copy+ #[cfg(target_family = "wasm")]+ {+ let original = original.as_ref().to_str().unwrap();+ let link = link.as_ref().to_str().unwrap();+ super::asyn_opfs::file_copy_if_absent(original, link).await+ }++ #[cfg(not(target_family = "wasm"))]+ tokio::fs::hard_link(original.as_ref(), link.as_ref()).await+}++pub async fn is_file(path: impl AsRef<Path>) -> bool {+ #[cfg(target_family = "wasm")]+ return super::asyn_opfs::is_file(path.as_ref().to_str().unwrap()).await;++ #[cfg(not(target_family = "wasm"))]+ tokio::fs::metadata(path).await.map(|m| m.is_file()).unwrap_or(false)+}++pub async fn read(path: impl AsRef<Path>) -> Result<Vec<u8>, io::Error> {+ #[cfg(target_family = "wasm")]+ return super::asyn_opfs::file_read(path.as_ref().to_str().unwrap()).await;++ #[cfg(not(target_family = "wasm"))]+ tokio::fs::read(path.as_ref()).await+}++pub async fn read_to_string(path: impl AsRef<Path>) -> Result<String, io::Error> {+ #[cfg(target_family = "wasm")]+ {+ let content = read(path).await?;+ let string = String::from_utf8(content).map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;+ Ok(string)+ }++ #[cfg(not(target_family = "wasm"))]+ tokio::fs::read_to_string(path.as_ref()).await+}++pub async fn remove_file(path: impl AsRef<Path>) -> Result<(), io::Error> {+ #[cfg(target_family = "wasm")]+ return super::asyn_opfs::file_remove(path.as_ref().to_str().unwrap()).await;++ #[cfg(not(target_family = "wasm"))]+ tokio::fs::remove_file(path.as_ref()).await+}++pub async fn rename(+ src_file: impl AsRef<Path>,+ dst_file: impl AsRef<Path>,+) -> Result<(), io::Error> {+ #[cfg(target_family = "wasm")]+ {+ let src_file_name = src_file.as_ref().to_str().unwrap();+ let dst_file_name = dst_file.as_ref().to_str().unwrap();+ super::asyn_opfs::file_rename(src_file_name, dst_file_name).await+ }++ #[cfg(not(target_family = "wasm"))]+ tokio::fs::rename(src_file, dst_file.as_ref()).await+}++pub async fn try_exists(path: impl AsRef<Path>) -> Result<bool, io::Error> {+ #[cfg(target_family = "wasm")]+ return super::asyn_opfs::exists(path.as_ref().to_str().unwrap()).await;++ #[cfg(not(target_family = "wasm"))]+ tokio::fs::try_exists(path.as_ref()).await+}++pub async fn write(+ path: impl AsRef<Path>,+ contents: impl AsRef<[u8]>,+) -> Result<(), io::Error> {+ #[cfg(target_family = "wasm")]+ return super::asyn_opfs::file_write(path.as_ref().to_str().unwrap(), contents.as_ref()).await;++ #[cfg(not(target_family = "wasm"))]+ tokio::fs::write(path, contents).await+}++pub async fn write_with_sync_all(+ path: impl AsRef<Path>,+ contents: impl AsRef<[u8]>,+) -> Result<(), io::Error> {+ #[cfg(target_family = "wasm")]+ {+ let path = path.as_ref().to_str().unwrap();+ super::asyn_opfs::file_write_if_absent(path, contents.as_ref()).await+ }++ #[cfg(not(target_family = "wasm"))]+ {+ use tokio::io::AsyncWriteExt;++ let mut file = tokio::fs::OpenOptions::new().write(true).create_new(true).open(&path).await?;+ file.write_all(contents.as_ref()).await?;+ file.sync_all().await+ }+}diff --git a/crates/kiban/src/fs/asyn_opfs.rs b/crates/kiban/src/fs/asyn_opfs.rsnew file mode 100644index 000000000..42a708ba1--- /dev/null+++ b/crates/kiban/src/fs/asyn_opfs.rs@@ -0,0 +1,270 @@+use std::{+ io,+ io::ErrorKind,+ path::{Component, Path},+ sync::OnceLock,+};++use tokio::sync::Mutex;+use web_sys::{js_sys::Reflect, wasm_bindgen::JsValue};++use super::opfs::{self, WriteCommandType, WriteParams};+use crate::time::SystemTime;++pub(crate) async fn dir_create_all(path: &str) -> Result<(), io::Error> {+ let mut root = get_root_dir().await?;+ for segment in normalized_segments(path)? {+ root = get_dir_handle(&root, &segment, true).await?;+ }+ Ok(())+}++pub(crate) async fn exists(path: &str) -> Result<bool, io::Error> {+ let root = get_root_dir().await?;+ let (parent, name) = match resolve_parent(&root, path).await {+ Ok(path) => path,+ Err(err) if err.kind() == ErrorKind::NotFound => return Ok(false),+ Err(err) => return Err(err),+ };++ let file_err = match get_file_handle(parent.clone(), &name, false).await {+ Ok(_) => return Ok(true),+ Err(err) => err,+ };+ match get_dir_handle(&parent, &name, false).await {+ Ok(_) => Ok(true),+ Err(dir_err) if file_err.kind() == ErrorKind::NotFound && dir_err.kind() == ErrorKind::NotFound => Ok(false),+ Err(dir_err) if dir_err.kind() == ErrorKind::NotFound => Err(file_err),+ Err(dir_err) => Err(dir_err),+ }+}++pub(crate) async fn file_copy(+ src_path: &str,+ dst_path: &str,+) -> Result<(), io::Error> {+ if src_path == dst_path {+ return Err(io::Error::new(+ ErrorKind::InvalidFilename,+ "source and destination file paths are equal".to_string(),+ ));+ }++ let src_file = get_file_handle_root(src_path, false).await?;+ let total = src_file.size().await.map_err(|err| js_value_to_io_error(&err, ErrorKind::Other))?;++ let dst_file = get_file_handle_root(dst_path, true).await?;+ let writer = dst_file.create_writable(false).await.map_err(|err| js_value_to_io_error(&err, ErrorKind::Other))?;++ let chunk_size = 16 * 1024;+ let mut offset = 0u64;+ while offset < total {+ let end = (offset + chunk_size).min(total);+ let chunk =+ src_file.read_range(offset..end).await.map_err(|err| js_value_to_io_error(&err, ErrorKind::Other))?;+ if chunk.is_empty() {+ break;+ }++ let chunk_length = chunk.len();+ let params = &WriteParams {+ command_type: WriteCommandType::Write,+ data: Some(chunk),+ position: Some(offset),+ size: None,+ };+ match writer.write_with_params(params).await {+ Ok(_) => {},+ Err(err) => {+ let _ = writer.abort().await;+ return Err(js_value_to_io_error(&err, ErrorKind::Other));+ },+ }+ offset += chunk_length as u64;+ }++ writer.close().await.map_err(|err| js_value_to_io_error(&err, ErrorKind::Other))?;++ Ok(())+}++pub(crate) async fn file_copy_if_absent(+ src_path: &str,+ dst_path: &str,+) -> Result<(), io::Error> {+ let _guard = create_if_absent_lock().lock().await;+ if exists(dst_path).await? {+ return Err(io::Error::new(ErrorKind::AlreadyExists, "file already exists"));+ }+ file_copy(src_path, dst_path).await+}++pub(crate) async fn file_length(path: &str) -> Result<u64, io::Error> {+ let file = get_file_handle_root(path, false).await?;+ let size = file.size().await.map_err(|err| js_value_to_io_error(&err, ErrorKind::Other))?;+ Ok(size)+}++pub(crate) async fn file_modified(path: &str) -> Result<SystemTime, io::Error> {+ let file_handle = get_file_handle_root(path, false).await?;+ let file = file_handle.get_file().await.map_err(|err| js_value_to_io_error(&err, ErrorKind::InvalidInput))?;+ Ok(file.last_modified())+}++pub(crate) async fn file_read(path: &str) -> Result<Vec<u8>, io::Error> {+ let file = get_file_handle_root(path, false).await?;+ let result = file.read().await.map_err(|err| js_value_to_io_error(&err, ErrorKind::Other))?;+ Ok(result)+}++pub(crate) async fn file_remove(path: &str) -> Result<(), io::Error> {+ let root = get_root_dir().await?;+ let (parent, name) = resolve_parent(&root, path).await?;+ let _ = get_file_handle(parent.clone(), &name, false).await?;+ parent.remove_entry(&name).await.map_err(|err| js_value_to_io_error(&err, ErrorKind::Other))?;+ Ok(())+}++pub(crate) async fn file_rename(+ src_path: &str,+ dst_path: &str,+) -> Result<(), io::Error> {+ if same_opfs_entry(src_path, dst_path)? {+ let _ = get_file_handle_root(src_path, false).await?;+ return Ok(());+ }++ file_copy(src_path, dst_path).await?;+ file_remove(src_path).await+}++pub(crate) async fn file_write(+ path: &str,+ contents: &[u8],+) -> Result<(), io::Error> {+ let file = get_file_handle_root(path, true).await?;+ let writer = file.create_writable(false).await.map_err(|err| js_value_to_io_error(&err, ErrorKind::Other))?;+ writer.write(contents).await.map_err(|err| js_value_to_io_error(&err, ErrorKind::Other))?;+ writer.close().await.map_err(|err| js_value_to_io_error(&err, ErrorKind::Other))?;+ Ok(())+}++pub(crate) async fn file_write_if_absent(+ path: &str,+ contents: &[u8],+) -> Result<(), io::Error> {+ let _guard = create_if_absent_lock().lock().await;+ if exists(path).await? {+ return Err(io::Error::new(ErrorKind::AlreadyExists, "file already exists"));+ }+ file_write(path, contents).await+}++pub(crate) async fn is_file(path: &str) -> bool {+ get_file_handle_root(path, false).await.is_ok()+}++pub(crate) async fn get_dir_handle(+ dir: &opfs::DirectoryHandle,+ path: &str,+ create: bool,+) -> Result<opfs::DirectoryHandle, io::Error> {+ dir.get_directory_handle(path, create).await.map_err(|err| js_value_to_io_error(&err, ErrorKind::Other))+}++pub(crate) async fn get_file_handle(+ dir_handle: opfs::DirectoryHandle,+ path: &str,+ create: bool,+) -> Result<opfs::FileHandle, io::Error> {+ dir_handle.get_file_handle(path, create).await.map_err(|err| js_value_to_io_error(&err, ErrorKind::Other))+}++pub(crate) async fn get_file_handle_root(+ path: &str,+ create: bool,+) -> Result<opfs::FileHandle, io::Error> {+ let root = get_root_dir().await?;+ let (parent, name) = resolve_parent(&root, path).await?;+ get_file_handle(parent, &name, create).await+}++pub(crate) async fn get_root_dir() -> Result<opfs::DirectoryHandle, io::Error> {+ opfs::get_storage_root().await.map_err(|err| js_value_to_io_error(&err, ErrorKind::Other))+}++pub(crate) async fn resolve_parent(+ root: &opfs::DirectoryHandle,+ path: &str,+) -> Result<(opfs::DirectoryHandle, String), io::Error> {+ let mut parts = normalized_segments(path)?;+ let name = parts.pop().ok_or(io::Error::new(ErrorKind::InvalidInput, "empty path"))?;+ let mut dir = root.clone();+ for segment in parts {+ dir = get_dir_handle(&dir, &segment, false).await?;+ }+ Ok((dir, name))+}++fn normalized_segments(path: &str) -> Result<Vec<String>, io::Error> {+ let mut segments = Vec::new();+ let mut rooted = false;++ for component in Path::new(path).components() {+ match component {+ Component::Prefix(_) => {+ return Err(io::Error::new(ErrorKind::InvalidInput, "OPFS paths do not support prefixes"));+ },+ Component::RootDir => {+ rooted = true;+ segments.clear();+ },+ Component::CurDir => {},+ Component::ParentDir => {+ if segments.pop().is_none() && !rooted {+ return Err(io::Error::new(ErrorKind::InvalidInput, "OPFS path escapes storage root"));+ }+ },+ Component::Normal(segment) => {+ let segment = segment+ .to_str()+ .ok_or_else(|| io::Error::new(ErrorKind::InvalidInput, "OPFS path is not valid UTF-8"))?;+ segments.push(segment.to_string());+ },+ }+ }++ Ok(segments)+}++fn same_opfs_entry(+ src_path: &str,+ dst_path: &str,+) -> Result<bool, io::Error> {+ Ok(normalized_segments(src_path)? == normalized_segments(dst_path)?)+}++fn create_if_absent_lock() -> &'static Mutex<()> {+ static CREATE_IF_ABSENT_LOCK: OnceLock<Mutex<()>> = OnceLock::new();+ CREATE_IF_ABSENT_LOCK.get_or_init(|| Mutex::new(()))+}++pub(crate) fn js_value_to_io_error(+ value: &JsValue,+ kind: ErrorKind,+) -> io::Error {+ io::Error::new(js_value_to_io_error_kind(value, kind), format!("{:?}", value))+}++fn js_value_to_io_error_kind(+ value: &JsValue,+ fallback: ErrorKind,+) -> ErrorKind {+ let name = Reflect::get(value, &JsValue::from_str("name")).ok().and_then(|name| name.as_string());+ match name.as_deref() {+ Some("NotFoundError") => ErrorKind::NotFound,+ Some("NotAllowedError") => ErrorKind::PermissionDenied,+ Some("TypeMismatchError") => ErrorKind::NotADirectory,+ _ => fallback,+ }+}diff --git a/crates/kiban/src/fs/mod.rs b/crates/kiban/src/fs/mod.rsnew file mode 100644index 000000000..6273abbaf--- /dev/null+++ b/crates/kiban/src/fs/mod.rs@@ -0,0 +1,9 @@+#[cfg(target_family = "wasm")]+mod asyn_opfs;+#[cfg(target_family = "wasm")]+mod opfs;+mod part_file;++pub mod asyn;++pub use part_file::PartFile;diff --git a/crates/kiban/src/fs/opfs/directory_handle.rs b/crates/kiban/src/fs/opfs/directory_handle.rsnew file mode 100644index 000000000..bfa77d359--- /dev/null+++ b/crates/kiban/src/fs/opfs/directory_handle.rs@@ -0,0 +1,108 @@+use wasm_bindgen::{JsCast, JsValue};+use wasm_bindgen_futures::JsFuture;+use web_sys::{+ FileSystemDirectoryHandle, FileSystemFileHandle, FileSystemGetDirectoryOptions, FileSystemGetFileOptions,+ FileSystemHandle, FileSystemHandleKind, FileSystemRemoveOptions,+ js_sys::{Array, Reflect},+};++use super::{FileHandle, Result};++#[derive(Debug, Clone)]+pub enum DirectoryEntry {+ File(FileHandle),+ Directory(DirectoryHandle),+}++/// Mirrors [`FileSystemDirectoryHandle`](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle).+#[derive(Debug, Clone)]+pub struct DirectoryHandle(FileSystemDirectoryHandle);++impl From<FileSystemDirectoryHandle> for DirectoryHandle {+ fn from(handle: FileSystemDirectoryHandle) -> Self {+ Self(handle)+ }+}++impl DirectoryHandle {+ pub fn name(&self) -> String {+ self.0.name()+ }++ pub fn kind(&self) -> FileSystemHandleKind {+ self.0.kind()+ }++ pub async fn is_same_entry(+ &self,+ other: &FileSystemHandle,+ ) -> Result<bool> {+ let same = JsFuture::from(self.0.is_same_entry(other)).await?;+ Ok(same.as_bool().unwrap_or(false))+ }++ pub async fn get_file_handle(+ &self,+ name: &str,+ create: bool,+ ) -> Result<FileHandle> {+ let web_options = FileSystemGetFileOptions::new();+ web_options.set_create(create);+ let handle = JsFuture::from(self.0.get_file_handle_with_options(name, &web_options)).await?;+ Ok(FileHandle::from(FileSystemFileHandle::from(handle)))+ }++ pub async fn get_directory_handle(+ &self,+ name: &str,+ create: bool,+ ) -> Result<DirectoryHandle> {+ let web_options = FileSystemGetDirectoryOptions::new();+ web_options.set_create(create);+ let handle = JsFuture::from(self.0.get_directory_handle_with_options(name, &web_options)).await?;+ Ok(DirectoryHandle::from(FileSystemDirectoryHandle::from(handle)))+ }++ pub async fn remove_entry(+ &self,+ name: &str,+ ) -> Result<()> {+ JsFuture::from(self.0.remove_entry(name)).await?;+ Ok(())+ }++ pub async fn remove_entry_with_options(+ &self,+ name: &str,+ recursive: bool,+ ) -> Result<()> {+ let web_options = FileSystemRemoveOptions::new();+ web_options.set_recursive(recursive);+ JsFuture::from(self.0.remove_entry_with_options(name, &web_options)).await?;+ Ok(())+ }++ pub async fn entries(&self) -> Result<Vec<(String, DirectoryEntry)>> {+ let iterator = self.0.entries();+ let mut entries = Vec::new();+ loop {+ let step = JsFuture::from(iterator.next()?).await?;+ if Reflect::get(&step, &JsValue::from_str("done"))?.as_bool().unwrap_or(true) {+ break;+ }+ // Each value is a `[name, handle]` pair.+ let pair = Array::from(&Reflect::get(&step, &JsValue::from_str("value"))?);+ let name = pair.get(0).as_string().ok_or_else(|| JsValue::from_str("directory entry has no name"))?;+ let handle = pair.get(1);+ let entry = if handle.has_type::<FileSystemFileHandle>() {+ DirectoryEntry::File(FileHandle::from(FileSystemFileHandle::from(handle)))+ } else if handle.has_type::<FileSystemDirectoryHandle>() {+ DirectoryEntry::Directory(DirectoryHandle::from(FileSystemDirectoryHandle::from(handle)))+ } else {+ return Err(JsValue::from_str("unknown directory entry handle type"));+ };+ entries.push((name, entry));+ }+ Ok(entries)+ }+}diff --git a/crates/kiban/src/fs/opfs/file.rs b/crates/kiban/src/fs/opfs/file.rsnew file mode 100644index 000000000..6290c1f35--- /dev/null+++ b/crates/kiban/src/fs/opfs/file.rs@@ -0,0 +1,84 @@+use std::{+ ops::{Bound, RangeBounds},+ time::Duration,+};++use wasm_bindgen::JsValue;+use wasm_bindgen_futures::JsFuture;+use web_sys::{Blob, js_sys::Uint8Array};++use super::Result;+use crate::time::SystemTime;++/// Mirrors [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File), which+/// inherits the read methods of [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).+#[derive(Debug, Clone)]+pub struct File(web_sys::File);++impl From<web_sys::File> for File {+ fn from(file: web_sys::File) -> Self {+ Self(file)+ }+}++impl File {+ pub fn last_modified(&self) -> SystemTime {+ SystemTime::UNIX_EPOCH + Duration::from_millis(self.0.last_modified() as u64)+ }++ pub async fn read(&self) -> Result<Vec<u8>> {+ let buffer = JsFuture::from(self.0.array_buffer()).await?;+ Ok(bytes_from_array_buffer(+ &buffer,+ usize::try_from(self.size()).map_err(|_| JsValue::from_str("file is too large to read into memory"))?,+ ))+ }++ pub async fn read_range<R: RangeBounds<u64>>(+ &self,+ range: R,+ ) -> Result<Vec<u8>> {+ let size = self.size();+ let start = match range.start_bound() {+ Bound::Included(&n) => n,+ Bound::Excluded(&n) => n + 1,+ Bound::Unbounded => 0,+ };+ let end = match range.end_bound() {+ Bound::Included(&n) => n + 1,+ Bound::Excluded(&n) => n,+ Bound::Unbounded => size,+ }+ .min(size);++ if start >= end {+ return Ok(Vec::new());+ }++ let blob: Blob = self.0.slice_with_f64_and_f64(start as f64, end as f64)?;+ let buffer = JsFuture::from(blob.array_buffer()).await?;+ Ok(bytes_from_array_buffer(+ &buffer,+ usize::try_from(end - start)+ .map_err(|_| JsValue::from_str("file range is too large to read into memory"))?,+ ))+ }++ pub fn size(&self) -> u64 {+ self.0.size() as u64+ }++ pub async fn text(&self) -> Result<String> {+ JsFuture::from(self.0.text()).await?.as_string().ok_or_else(|| JsValue::from_str("file is not valid UTF-8"))+ }+}++fn bytes_from_array_buffer(+ buffer: &JsValue,+ len: usize,+) -> Vec<u8> {+ let array = Uint8Array::new(buffer);+ let mut bytes = vec![0u8; len];+ array.copy_to(&mut bytes);+ bytes+}diff --git a/crates/kiban/src/fs/opfs/file_handle.rs b/crates/kiban/src/fs/opfs/file_handle.rsnew file mode 100644index 000000000..24db84e93--- /dev/null+++ b/crates/kiban/src/fs/opfs/file_handle.rs@@ -0,0 +1,68 @@+use std::ops::RangeBounds;++use wasm_bindgen::JsCast;+use wasm_bindgen_futures::JsFuture;+use web_sys::{+ FileSystemCreateWritableOptions, FileSystemFileHandle, FileSystemHandle, FileSystemHandleKind,+ FileSystemWritableFileStream,+};++use super::{File, Result, WritableFileStream};++/// Mirrors [`FileSystemFileHandle`](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle).+#[derive(Debug, Clone)]+pub struct FileHandle(FileSystemFileHandle);++impl From<FileSystemFileHandle> for FileHandle {+ fn from(handle: FileSystemFileHandle) -> Self {+ Self(handle)+ }+}++impl FileHandle {+ pub fn name(&self) -> String {+ self.0.name()+ }++ pub fn kind(&self) -> FileSystemHandleKind {+ self.0.kind()+ }++ pub async fn is_same_entry(+ &self,+ other: &FileSystemHandle,+ ) -> Result<bool> {+ let same = JsFuture::from(self.0.is_same_entry(other)).await?;+ Ok(same.as_bool().unwrap_or(false))+ }++ pub async fn get_file(&self) -> Result<File> {+ let file = JsFuture::from(self.0.get_file()).await?;+ Ok(File::from(web_sys::File::from(file)))+ }++ pub async fn create_writable(+ &self,+ keep_existing_data: bool,+ ) -> Result<WritableFileStream> {+ let web_options = FileSystemCreateWritableOptions::new();+ web_options.set_keep_existing_data(keep_existing_data);+ let stream = JsFuture::from(self.0.create_writable_with_options(&web_options)).await?;+ Ok(WritableFileStream::from(stream.unchecked_into::<FileSystemWritableFileStream>()))+ }++ pub async fn read(&self) -> Result<Vec<u8>> {+ self.get_file().await?.read().await+ }++ pub async fn read_range<R: RangeBounds<u64>>(+ &self,+ range: R,+ ) -> Result<Vec<u8>> {+ self.get_file().await?.read_range(range).await+ }++ pub async fn size(&self) -> Result<u64> {+ Ok(self.get_file().await?.size())+ }+}diff --git a/crates/kiban/src/fs/opfs/mod.rs b/crates/kiban/src/fs/opfs/mod.rsnew file mode 100644index 000000000..1b7672477--- /dev/null+++ b/crates/kiban/src/fs/opfs/mod.rs@@ -0,0 +1,17 @@+//! Rust wrappers over the browser [Origin Private File System][opfs] (OPFS).++mod directory_handle;+mod file;+mod file_handle;+mod storage;+mod writable_file_stream;++pub use directory_handle::DirectoryHandle;+pub use file::File;+pub use file_handle::FileHandle;+pub use storage::get_storage_root;+use wasm_bindgen::JsValue;+pub use writable_file_stream::{WritableFileStream, WriteCommandType, WriteParams};++pub type Error = JsValue;+pub type Result<T> = std::result::Result<T, Error>;diff --git a/crates/kiban/src/fs/opfs/storage.rs b/crates/kiban/src/fs/opfs/storage.rsnew file mode 100644index 000000000..3be14b066--- /dev/null+++ b/crates/kiban/src/fs/opfs/storage.rs@@ -0,0 +1,12 @@+use wasm_bindgen::JsValue;+use wasm_bindgen_futures::JsFuture;+use web_sys::FileSystemDirectoryHandle;++use super::{DirectoryHandle, Result};++/// Mirrors [`StorageManager.getDirectory()`](https://developer.mozilla.org/en-US/docs/Web/API/StorageManager/getDirectory).+pub async fn get_storage_root() -> Result<DirectoryHandle> {+ let window = web_sys::window().ok_or_else(|| JsValue::from_str("no window object available"))?;+ let directory = JsFuture::from(window.navigator().storage().get_directory()).await?;+ Ok(DirectoryHandle::from(FileSystemDirectoryHandle::from(directory)))+}diff --git a/crates/kiban/src/fs/opfs/writable_file_stream.rs b/crates/kiban/src/fs/opfs/writable_file_stream.rsnew file mode 100644index 000000000..56a113ba5--- /dev/null+++ b/crates/kiban/src/fs/opfs/writable_file_stream.rs@@ -0,0 +1,117 @@+use wasm_bindgen::JsValue;+use wasm_bindgen_futures::{JsFuture, spawn_local};+use web_sys::{+ Blob, FileSystemWritableFileStream,+ js_sys::{Array, Uint8Array},+};++use super::Result;++/// Mirrors the `type` field accepted by+/// [`FileSystemWritableFileStream.write()`](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemWritableFileStream/write#type).+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub enum WriteCommandType {+ /// Write [`data`](WriteParams::data) at [`position`](WriteParams::position).+ Write,+ /// Move the cursor to [`position`](WriteParams::position).+ Seek,+ /// Resize the file to [`size`](WriteParams::size) bytes.+ Truncate,+}++/// Mirrors the [`WriteParams`](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemWritableFileStream/write#parameters)+/// dictionary.+#[derive(Debug, Clone)]+pub struct WriteParams {+ pub command_type: WriteCommandType,+ /// Used by [`WriteCommandType::Write`].+ pub data: Option<Vec<u8>>,+ /// Used by [`WriteCommandType::Write`] and [`WriteCommandType::Seek`].+ pub position: Option<u64>,+ /// Used by [`WriteCommandType::Truncate`].+ pub size: Option<u64>,+}++/// Mirrors [`FileSystemWritableFileStream`](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemWritableFileStream).+#[derive(Debug, Clone)]+pub struct WritableFileStream(FileSystemWritableFileStream);++impl From<FileSystemWritableFileStream> for WritableFileStream {+ fn from(stream: FileSystemWritableFileStream) -> Self {+ Self(stream)+ }+}++impl WritableFileStream {+ pub async fn write(+ &self,+ data: &[u8],+ ) -> Result<()> {+ JsFuture::from(self.0.write_with_blob(&blob_from_bytes(data)?)?).await?;+ Ok(())+ }++ pub async fn write_with_params(+ &self,+ params: &WriteParams,+ ) -> Result<()> {+ let web_params = web_sys::WriteParams::new(match params.command_type {+ WriteCommandType::Write => web_sys::WriteCommandType::Write,+ WriteCommandType::Seek => web_sys::WriteCommandType::Seek,+ WriteCommandType::Truncate => web_sys::WriteCommandType::Truncate,+ });++ if let Some(data) = ¶ms.data {+ web_params.set_data(&JsValue::from(blob_from_bytes(data)?));+ }+ if let Some(position) = params.position {+ web_params.set_position(Some(position as f64));+ }+ if let Some(size) = params.size {+ web_params.set_size(Some(size as f64));+ }++ JsFuture::from(self.0.write_with_write_params(&web_params)?).await?;+ Ok(())+ }++ pub async fn seek(+ &self,+ offset: u64,+ ) -> Result<()> {+ JsFuture::from(self.0.seek_with_f64(offset as f64)?).await?;+ Ok(())+ }++ pub async fn truncate(+ &self,+ size: u64,+ ) -> Result<()> {+ JsFuture::from(self.0.truncate_with_f64(size as f64)?).await?;+ Ok(())+ }++ pub async fn close(&self) -> Result<()> {+ JsFuture::from(self.0.close()).await?;+ Ok(())+ }++ pub async fn abort(&self) -> Result<()> {+ JsFuture::from(self.0.abort()).await?;+ Ok(())+ }++ pub fn close_in_background(self) {+ let close = self.0.close();+ spawn_local(async move {+ let _ = JsFuture::from(close).await;+ });+ }+}++fn blob_from_bytes(data: &[u8]) -> Result<Blob> {+ let bytes = Uint8Array::from(data);+ let parts = Array::new();+ parts.push(&bytes);+ Blob::new_with_u8_array_sequence(&parts)+}diff --git a/crates/kiban/src/fs/part_file/mod.rs b/crates/kiban/src/fs/part_file/mod.rsnew file mode 100644index 000000000..9f8aae01b--- /dev/null+++ b/crates/kiban/src/fs/part_file/mod.rs@@ -0,0 +1,35 @@+use std::{io, path::Path};++#[cfg(target_family = "wasm")]+mod opfs_part_file;+#[cfg(not(target_family = "wasm"))]+mod tokio_part_file;++#[cfg(target_family = "wasm")]+use opfs_part_file::OpfsPartFile;+#[cfg(not(target_family = "wasm"))]+use tokio_part_file::TokioPartFile;++#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]+#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]+pub trait PartFile {+ async fn write_all(+ &mut self,+ buf: &[u8],+ ) -> Result<(), io::Error>;++ async fn flush(&mut self) -> Result<(), io::Error>;+}++impl dyn PartFile {+ pub async fn new(+ path: impl AsRef<Path>,+ resume_from_bytes: u64,+ ) -> Result<impl PartFile, io::Error> {+ #[cfg(target_family = "wasm")]+ return OpfsPartFile::new(path.as_ref().to_str().unwrap(), resume_from_bytes).await;++ #[cfg(not(target_family = "wasm"))]+ TokioPartFile::new(path, resume_from_bytes).await+ }+}diff --git a/crates/kiban/src/fs/part_file/opfs_part_file.rs b/crates/kiban/src/fs/part_file/opfs_part_file.rsnew file mode 100644index 000000000..b6f1a306c--- /dev/null+++ b/crates/kiban/src/fs/part_file/opfs_part_file.rs@@ -0,0 +1,81 @@+use std::{io, io::ErrorKind};++use super::PartFile;+use crate::fs::{+ asyn_opfs::{get_file_handle, get_root_dir, js_value_to_io_error, resolve_parent},+ opfs::{FileHandle, WritableFileStream, WriteCommandType, WriteParams},+};++pub struct OpfsPartFile {+ file: FileHandle,+ writer: Option<WritableFileStream>,+ position: u64,+}++impl OpfsPartFile {+ pub async fn new(+ path: &str,+ resume_from_bytes: u64,+ ) -> Result<Self, io::Error> {+ let root = get_root_dir().await?;+ let (dir, name) = resolve_parent(&root, path).await?;+ let file = get_file_handle(dir, &name, true).await?;+ let writer = file+ .create_writable(resume_from_bytes > 0)+ .await+ .map_err(|err| js_value_to_io_error(&err, ErrorKind::Other))?;+ Ok(Self {+ file,+ writer: Some(writer),+ position: resume_from_bytes,+ })+ }+}++impl Drop for OpfsPartFile {+ fn drop(&mut self) {+ if let Some(writer) = self.writer.take() {+ writer.close_in_background();+ }+ }+}++#[async_trait::async_trait(?Send)]+impl PartFile for OpfsPartFile {+ async fn write_all(+ &mut self,+ buf: &[u8],+ ) -> Result<(), io::Error> {+ if self.writer.is_none() {+ self.writer = Some(+ self.file.create_writable(true).await.map_err(|err| js_value_to_io_error(&err, ErrorKind::Other))?,+ );+ }+ let writer = self.writer.as_ref().expect("OPFS writer must be open");++ if let Err(err) = writer+ .write_with_params(&WriteParams {+ command_type: WriteCommandType::Write,+ data: Some(buf.to_vec()),+ position: Some(self.position),+ size: None,+ })+ .await+ {+ if let Some(writer) = self.writer.take() {+ let _ = writer.close().await;+ }+ return Err(js_value_to_io_error(&err, ErrorKind::Other));+ }++ self.position += buf.len() as u64;+ Ok(())+ }++ async fn flush(&mut self) -> Result<(), io::Error> {+ if let Some(writer) = self.writer.take() {+ writer.close().await.map_err(|err| js_value_to_io_error(&err, ErrorKind::Other))?;+ }+ Ok(())+ }+}diff --git a/crates/kiban/src/fs/part_file/tokio_part_file.rs b/crates/kiban/src/fs/part_file/tokio_part_file.rsnew file mode 100644index 000000000..8c54838bf--- /dev/null+++ b/crates/kiban/src/fs/part_file/tokio_part_file.rs@@ -0,0 +1,42 @@+use std::{io, path::Path};++use tokio::{+ fs::{File, OpenOptions},+ io::AsyncWriteExt,+};++use super::PartFile;++pub struct TokioPartFile {+ file: File,+}++impl TokioPartFile {+ pub async fn new(+ path: impl AsRef<Path>,+ resume_from_bytes: u64,+ ) -> io::Result<Self> {+ let file = if resume_from_bytes > 0 {+ OpenOptions::new().create(true).append(true).open(path.as_ref()).await?+ } else {+ OpenOptions::new().create(true).write(true).truncate(true).open(path.as_ref()).await?+ };+ Ok(Self {+ file,+ })+ }+}++#[async_trait::async_trait]+impl PartFile for TokioPartFile {+ async fn write_all(+ &mut self,+ buf: &[u8],+ ) -> io::Result<()> {+ self.file.write_all(buf).await+ }++ async fn flush(&mut self) -> io::Result<()> {+ self.file.flush().await+ }+}diff --git a/crates/kiban/src/lib.rs b/crates/kiban/src/lib.rsnew file mode 100644index 000000000..c929b2dec--- /dev/null+++ b/crates/kiban/src/lib.rs@@ -0,0 +1,47 @@+#![allow(dead_code)]++pub mod fs;+pub mod maybe;+pub mod process;+pub mod rt;+pub mod time;++#[cfg(target_family = "wasm")]+#[doc(hidden)]+pub mod __console {+ pub fn log(message: &str) {+ web_sys::console::log_1(&message.into());+ }++ pub fn error(message: &str) {+ web_sys::console::error_1(&message.into());+ }+}++#[macro_export]+macro_rules! printf {+ ($($arg:tt)*) => {+ #[cfg(not(target_family = "wasm"))]+ println!($($arg)*);++ #[cfg(target_family = "wasm")]+ {+ let string = format!($($arg)*);+ $crate::__console::log(&string);+ }+ };+}++#[macro_export]+macro_rules! eprintf {+ ($($arg:tt)*) => {+ #[cfg(not(target_family = "wasm"))]+ eprintln!($($arg)*);++ #[cfg(target_family = "wasm")]+ {+ let string = format!($($arg)*);+ $crate::__console::error(&string);+ }+ };+}diff --git a/crates/kiban/src/maybe.rs b/crates/kiban/src/maybe.rsnew file mode 100644index 000000000..154f2a6a0--- /dev/null+++ b/crates/kiban/src/maybe.rs@@ -0,0 +1,21 @@+#![allow(dead_code)]++#[cfg(not(target_family = "wasm"))]+pub trait MaybeSend: std::marker::Send {}+#[cfg(not(target_family = "wasm"))]+impl<T: std::marker::Send + ?Sized> MaybeSend for T {}++#[cfg(target_family = "wasm")]+pub trait MaybeSend {}+#[cfg(target_family = "wasm")]+impl<T: ?Sized> MaybeSend for T {}++#[cfg(not(target_family = "wasm"))]+pub trait MaybeSync: std::marker::Sync {}+#[cfg(not(target_family = "wasm"))]+impl<T: std::marker::Sync + ?Sized> MaybeSync for T {}++#[cfg(target_family = "wasm")]+pub trait MaybeSync {}+#[cfg(target_family = "wasm")]+impl<T: ?Sized> MaybeSync for T {}diff --git a/crates/kiban/src/process.rs b/crates/kiban/src/process.rsnew file mode 100644index 000000000..25da8cbf1--- /dev/null+++ b/crates/kiban/src/process.rs@@ -0,0 +1,45 @@+#[cfg(unix)]+pub async fn is_process_alive(process_id: u32) -> bool {+ // SAFETY: `kill(pid, 0)` is a probe with no signal delivery. Direct syscall is required+ // because sandboxed macOS apps can't exec `/bin/kill`.+ let result = unsafe { libc::kill(process_id as libc::pid_t, 0) };+ if result == 0 {+ return true;+ }+ // EPERM = exists but we can't signal it; ESRCH = gone.+ std::io::Error::last_os_error().raw_os_error() == Some(libc::EPERM)+}++#[cfg(windows)]+pub async fn is_process_alive(process_id: u32) -> bool {+ tokio::task::spawn_blocking(move || {+ std::process::Command::new("tasklist")+ .args(["/FI", &format!("PID eq {process_id}")])+ .output()+ .map(|output| String::from_utf8_lossy(&output.stdout).contains(&process_id.to_string()))+ .unwrap_or(false)+ })+ .await+ .unwrap_or(false)+}++#[cfg(target_family = "wasm")]+pub async fn is_process_alive(_process_id: u32) -> bool {+ false+}++pub fn id() -> u32 {+ if proc_supported() {+ std::process::id()+ } else {+ 0+ }+}++pub fn proc_supported() -> bool {+ #[cfg(not(target_family = "wasm"))]+ return true;++ #[cfg(target_family = "wasm")]+ false+}diff --git a/crates/kiban/src/rt/mod.rs b/crates/kiban/src/rt/mod.rsnew file mode 100644index 000000000..a1300915f--- /dev/null+++ b/crates/kiban/src/rt/mod.rs@@ -0,0 +1,41 @@+#[cfg(not(target_family = "wasm"))]+mod tokio;+#[cfg(target_family = "wasm")]+mod wasm;++#[cfg(not(target_family = "wasm"))]+pub use tokio::TokioRuntimeHandle as RuntimeHandle;+#[cfg(target_family = "wasm")]+pub use wasm::WasmRuntimeHandle as RuntimeHandle;++use crate::maybe::MaybeSend;++#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]+#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]+pub trait TaskJoinHandle<T>: Send + Sync {+ /// Signal the task to stop without waiting for it to wind down.+ fn abort(&self);++ /// Abort the task and wait for it to finish winding down.+ async fn abort_and_join(self: Box<Self>);+}++pub fn spawn<F: Future<Output: MaybeSend + 'static> + MaybeSend + 'static>(+ future: F+) -> Box<dyn TaskJoinHandle<F::Output>> {+ #[cfg(not(target_family = "wasm"))]+ return tokio::spawn(future);++ #[cfg(target_family = "wasm")]+ wasm::spawn(future)+}++#[cfg(not(target_family = "wasm"))]+pub async fn run_blocking<T: Send + 'static>(func: impl FnOnce() -> T + Send + 'static) -> T {+ ::tokio::task::spawn_blocking(func).await.expect("blocking task panicked")+}++#[cfg(target_family = "wasm")]+pub async fn run_blocking<T>(func: impl FnOnce() -> T) -> T {+ func()+}diff --git a/crates/kiban/src/rt/tokio.rs b/crates/kiban/src/rt/tokio.rsnew file mode 100644index 000000000..55c0dedc6--- /dev/null+++ b/crates/kiban/src/rt/tokio.rs@@ -0,0 +1,47 @@+use std::error::Error;++use tokio::{runtime::Handle, task::JoinHandle};++use crate::rt::TaskJoinHandle;++#[derive(Clone, Debug)]+pub struct TokioRuntimeHandle {+ handle: Handle,+}++impl TokioRuntimeHandle {+ pub fn current() -> Self {+ TokioRuntimeHandle {+ handle: Handle::current(),+ }+ }++ pub fn try_current() -> Result<Self, Box<dyn Error>> {+ Ok(TokioRuntimeHandle {+ handle: Handle::try_current()?,+ })+ }++ pub fn spawn<F: Future<Output: Send + 'static> + Send + 'static>(+ &self,+ future: F,+ ) -> Box<dyn TaskJoinHandle<F::Output>> {+ Box::new(self.handle.spawn(future))+ }+}++#[async_trait::async_trait]+impl<T: Send + 'static> TaskJoinHandle<T> for JoinHandle<T> {+ fn abort(&self) {+ self.abort();+ }++ async fn abort_and_join(self: Box<Self>) {+ self.abort();+ let _ = (*self).await;+ }+}++pub fn spawn<F: Future<Output: Send + 'static> + Send + 'static>(future: F) -> Box<dyn TaskJoinHandle<F::Output>> {+ Box::new(tokio::spawn(future))+}diff --git a/crates/kiban/src/rt/wasm.rs b/crates/kiban/src/rt/wasm.rsnew file mode 100644index 000000000..9b0957169--- /dev/null+++ b/crates/kiban/src/rt/wasm.rs@@ -0,0 +1,57 @@+use std::error::Error;++use futures_util::future::{AbortHandle, Abortable};+use tokio::sync::oneshot;++use crate::rt::TaskJoinHandle;++#[derive(Clone, Debug)]+pub struct WasmRuntimeHandle;++impl WasmRuntimeHandle {+ pub fn current() -> Self {+ WasmRuntimeHandle+ }++ pub fn try_current() -> Result<Self, Box<dyn Error>> {+ Ok(WasmRuntimeHandle)+ }++ pub fn spawn<F: Future<Output: 'static> + 'static>(+ &self,+ future: F,+ ) -> Box<dyn TaskJoinHandle<F::Output>> {+ spawn(future)+ }+}++pub struct WasmTaskJoinHandle {+ abort_handle: AbortHandle,+ completion: oneshot::Receiver<()>,+}++#[async_trait::async_trait(?Send)]+impl<T> TaskJoinHandle<T> for WasmTaskJoinHandle {+ fn abort(&self) {+ self.abort_handle.abort();+ }++ async fn abort_and_join(self: Box<Self>) {+ self.abort_handle.abort();+ let _ = self.completion.await;+ }+}++pub fn spawn<F: Future<Output: 'static> + 'static>(future: F) -> Box<dyn TaskJoinHandle<F::Output>> {+ let (abort_handle, abort_registration) = AbortHandle::new_pair();+ let future = Abortable::new(future, abort_registration);+ let (completion_sender, completion) = oneshot::channel();+ wasm_bindgen_futures::spawn_local(async move {+ let _ = future.await;+ let _ = completion_sender.send(());+ });+ Box::new(WasmTaskJoinHandle {+ abort_handle,+ completion,+ })+}diff --git a/crates/kiban/src/time/instant.rs b/crates/kiban/src/time/instant.rsnew file mode 100644index 000000000..31703a5c2--- /dev/null+++ b/crates/kiban/src/time/instant.rs@@ -0,0 +1,5 @@+#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]+pub type Instant = std::time::Instant;++#[cfg(all(target_family = "wasm", target_os = "unknown"))]+pub type Instant = web_time::Instant;diff --git a/crates/kiban/src/time/mod.rs b/crates/kiban/src/time/mod.rsnew file mode 100644index 000000000..eb9242f65--- /dev/null+++ b/crates/kiban/src/time/mod.rs@@ -0,0 +1,8 @@+mod instant;+mod sleep;+mod sleep_wasm;+mod system_time;++pub use instant::Instant;+pub use sleep::sleep;+pub use system_time::SystemTime;diff --git a/crates/kiban/src/time/sleep.rs b/crates/kiban/src/time/sleep.rsnew file mode 100644index 000000000..20e7149ed--- /dev/null+++ b/crates/kiban/src/time/sleep.rs@@ -0,0 +1,11 @@+use std::time::Duration;++#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]+pub async fn sleep(duration: Duration) {+ tokio::time::sleep(duration).await+}++#[cfg(all(target_family = "wasm", target_os = "unknown"))]+pub async fn sleep(duration: Duration) {+ crate::time::sleep_wasm::sleep(duration).await+}diff --git a/crates/kiban/src/time/sleep_wasm.rs b/crates/kiban/src/time/sleep_wasm.rsnew file mode 100644index 000000000..61774fa45--- /dev/null+++ b/crates/kiban/src/time/sleep_wasm.rs@@ -0,0 +1,84 @@+#![cfg(target_family = "wasm")]++use std::{+ cell::RefCell,+ future::Future,+ pin::Pin,+ rc::Rc,+ task::{Context, Poll, Waker},+ time::Duration,+};++use wasm_bindgen::{JsCast, closure::Closure};++struct SleepState {+ done: bool,+ waker: Option<Waker>,+}++pub(crate) struct Sleep {+ state: Rc<RefCell<SleepState>>,+ timeout_id: i32,+ _done: Closure<dyn FnMut()>,+}++impl Sleep {+ pub fn new(duration: Duration) -> Self {+ let state = Rc::new(RefCell::new(SleepState {+ done: false,+ waker: None,+ }));++ let state_clone = state.clone();+ let done = Closure::wrap(Box::new(move || {+ let mut s = state_clone.borrow_mut();+ s.done = true;+ if let Some(waker) = s.waker.take() {+ waker.wake();+ }+ }) as Box<dyn FnMut()>);++ let window = web_sys::window().expect("no global `window` exists");+ let timeout_id = window+ .set_timeout_with_callback_and_timeout_and_arguments_0(+ done.as_ref().unchecked_ref(),+ duration.as_millis() as i32,+ )+ .expect("failed to set timeout");++ Sleep {+ state,+ timeout_id,+ _done: done,+ }+ }+}++impl Drop for Sleep {+ fn drop(&mut self) {+ if let Some(window) = web_sys::window() {+ window.clear_timeout_with_handle(self.timeout_id);+ }+ }+}++impl Future for Sleep {+ type Output = ();++ fn poll(+ self: Pin<&mut Self>,+ cx: &mut Context<'_>,+ ) -> Poll<()> {+ let mut state = self.state.borrow_mut();+ if state.done {+ Poll::Ready(())+ } else {+ state.waker = Some(cx.waker().clone());+ Poll::Pending+ }+ }+}++pub fn sleep(duration: Duration) -> Sleep {+ Sleep::new(duration)+}diff --git a/crates/kiban/src/time/system_time.rs b/crates/kiban/src/time/system_time.rsnew file mode 100644index 000000000..e49e787fe--- /dev/null+++ b/crates/kiban/src/time/system_time.rs@@ -0,0 +1,5 @@+#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]+pub type SystemTime = std::time::SystemTime;++#[cfg(all(target_family = "wasm", target_os = "unknown"))]+pub type SystemTime = web_time::SystemTime;diff --git a/crates/mock-registry/Cargo.toml b/crates/mock-registry/Cargo.tomlindex 182f99e48..f2df078a8 100644--- a/crates/mock-registry/Cargo.toml+++ b/crates/mock-registry/Cargo.toml@@ -19,7 +19,7 @@ serde_json.workspace = trueshoji.workspace = truestream_throttle.workspace = truethiserror.workspace = true-tokio.workspace = true+tokio = { workspace = true, features = ["full"] }tracing.workspace = truewiremock.workspace = truediff --git a/crates/nagare/Cargo.toml b/crates/nagare/Cargo.tomlindex bfa96d1ca..0e106bc69 100644--- a/crates/nagare/Cargo.toml+++ b/crates/nagare/Cargo.toml@@ -63,7 +63,7 @@ indexmap.workspace = truereqwest.workspace = truefutures.workspace = truetokio-util.workspace = true-tokio.workspace = true+tokio = { workspace = true, features = ["full"] }uniffi = { workspace = true, optional = true }napi = { workspace = true, optional = true }napi-derive = { workspace = true, optional = true }diff --git a/crates/uzu/Cargo.toml b/crates/uzu/Cargo.tomlindex c97818f09..1b5b6d99e 100644--- a/crates/uzu/Cargo.toml+++ b/crates/uzu/Cargo.toml@@ -89,7 +89,7 @@ serde.workspace = trueserde_json.workspace = truethiserror.workspace = truefutures.workspace = true-tokio.workspace = true+tokio = { workspace = true, features = ["full"] }tokio-stream.workspace = truedirs.workspace = truesysinfo.workspace = truediff --git a/platforms.toml b/platforms.tomlindex b45c93d41..a410186de 100644--- a/platforms.toml+++ b/platforms.toml@@ -68,7 +68,7 @@ capabilities_default = ["grammar", "cli"][targets."wasm32-wasip1-threads"]backend = "cpu"-aliases = ["wasm"]+aliases = ["wasi"]capabilities_supported = []capabilities_default = []