app store and terminal working!

This commit is contained in:
dr-frmr 2023-11-02 16:42:18 -04:00
parent a1dbed6f26
commit 884fefc4f9
No known key found for this signature in database
18 changed files with 647 additions and 324 deletions

View File

@ -40,18 +40,11 @@ fn build_app(target_path: &str, name: &str, parent_pkg_path: Option<&str>) {
// if and only if module's wit is outdated, re-set-up build environment // if and only if module's wit is outdated, re-set-up build environment
if file_outdated( if file_outdated(
format!("{}/wit/uqbar.wit", pwd.display()), format!("{}/target.wasm", pwd.display()),
format!("{}/wit/uqbar.wit", target_path), format!("{}/target/bindings/{}/target.wasm", target_path, name),
) )
.unwrap_or(true) .unwrap_or(true)
{ {
println!("cargo:warning=wit outdated, rebuilding");
run_command(Command::new("cp").args(&[
"-r",
&format!("{}/wit", pwd.display()),
&format!("{}/wit", target_path),
]))
.unwrap();
// create target/bindings directory // create target/bindings directory
fs::create_dir_all(&format!("{}/target/bindings/{}", target_path, name,)).unwrap(); fs::create_dir_all(&format!("{}/target/bindings/{}", target_path, name,)).unwrap();
// copy newly-made target.wasm into target/bindings // copy newly-made target.wasm into target/bindings
@ -160,7 +153,7 @@ fn main() {
let entry_path = entry.unwrap().path(); let entry_path = entry.unwrap().path();
let package_name = entry_path.file_name().unwrap().to_str().unwrap(); let package_name = entry_path.file_name().unwrap().to_str().unwrap();
if package_name != "terminal" { if package_name != "terminal" && package_name != "app_store" {
continue; continue;
} }

View File

@ -1,3 +0,0 @@
# This file is automatically generated by cargo-component.
# It is not intended for manual editing.
version = 1

View File

@ -14,12 +14,12 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bincode", "bincode",
"cargo-component-bindings",
"rand", "rand",
"serde", "serde",
"serde_json", "serde_json",
"sha2", "sha2",
"wit-bindgen 0.11.0", "uqbar_process_lib",
"wit-bindgen",
] ]
[[package]] [[package]]
@ -46,29 +46,6 @@ dependencies = [
"generic-array", "generic-array",
] ]
[[package]]
name = "cargo-component-bindings"
version = "0.3.0"
source = "git+https://github.com/bytecodealliance/cargo-component#5cddf6df7fd5ab5aa9bfd66f4cf2e2f6cc7d72f9"
dependencies = [
"cargo-component-macro",
"wit-bindgen 0.13.0",
]
[[package]]
name = "cargo-component-macro"
version = "0.3.0"
source = "git+https://github.com/bytecodealliance/cargo-component#5cddf6df7fd5ab5aa9bfd66f4cf2e2f6cc7d72f9"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
"wit-bindgen-core",
"wit-bindgen-rust",
"wit-component",
]
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
@ -77,9 +54,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "cpufeatures" name = "cpufeatures"
version = "0.2.10" version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fbc60abd742b35f2492f808e1abbb83d45f72db402e14c55057edc9c7b1e9e4" checksum = "ce420fe07aecd3e67c5f910618fe65e94158f6dcc0adf44e00d69ce2bdfe0fd0"
dependencies = [ dependencies = [
"libc", "libc",
] ]
@ -154,9 +131,9 @@ checksum = "25a2bc672d1148e28034f176e01fffebb08b35768468cc954630da77a1449005"
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "2.0.2" version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8adf3ddd720272c6ea8bf59463c04e0f93d0bbf7c5439b691bca2987e0270897" checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f"
dependencies = [ dependencies = [
"equivalent", "equivalent",
"hashbrown", "hashbrown",
@ -255,18 +232,18 @@ checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.189" version = "1.0.190"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e422a44e74ad4001bdc8eede9a4570ab52f71190e9c076d14369f38b9200537" checksum = "91d3c334ca1ee894a2c6f6ad698fe8c435b76d504b13d436f0685d648d6d96f7"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.189" version = "1.0.190"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e48d1f918009ce3145511378cf68d613e3b3d9137d67272562080d68a2b32d5" checksum = "67c5609f394e5c2bd7fc51efda478004ea80ef42fee983d5c67a65e34f32c0e3"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -275,9 +252,9 @@ dependencies = [
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.107" version = "1.0.108"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65" checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b"
dependencies = [ dependencies = [
"itoa", "itoa",
"ryu", "ryu",
@ -345,6 +322,17 @@ version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c"
[[package]]
name = "uqbar_process_lib"
version = "0.2.0"
dependencies = [
"anyhow",
"bincode",
"serde",
"serde_json",
"wit-bindgen",
]
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.4" version = "0.9.4"
@ -359,18 +347,18 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]] [[package]]
name = "wasm-encoder" name = "wasm-encoder"
version = "0.35.0" version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ca90ba1b5b0a70d3d49473c5579951f3bddc78d47b59256d2f9d4922b150aca" checksum = "53ae0be20bf87918df4fa831bfbbd0b491d24aee407ed86360eae4c2c5608d38"
dependencies = [ dependencies = [
"leb128", "leb128",
] ]
[[package]] [[package]]
name = "wasm-metadata" name = "wasm-metadata"
version = "0.10.9" version = "0.10.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14abc161bfda5b519aa229758b68f2a52b45a12b993808665c857d1a9a00223c" checksum = "5621910462c61a8efc3248fdfb1739bf649bb335b0df935c27b340418105f9d8"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"indexmap", "indexmap",
@ -384,9 +372,9 @@ dependencies = [
[[package]] [[package]]
name = "wasmparser" name = "wasmparser"
version = "0.115.0" version = "0.116.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e06c0641a4add879ba71ccb3a1e4278fd546f76f1eafb21d8f7b07733b547cd5" checksum = "53290b1276c5c2d47d694fb1a920538c01f51690e7e261acbe1d10c5fc306ea1"
dependencies = [ dependencies = [
"indexmap", "indexmap",
"semver", "semver",
@ -394,18 +382,8 @@ dependencies = [
[[package]] [[package]]
name = "wit-bindgen" name = "wit-bindgen"
version = "0.11.0" version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/bytecodealliance/wit-bindgen#5390bab780733f1660d14c254ec985df2816bf1d"
checksum = "f8a3e8e965dc50e6eb4410d9a11720719fadc6a1713803ea5f3be390b81c8279"
dependencies = [
"bitflags",
]
[[package]]
name = "wit-bindgen"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7d92ce0ca6b6074059413a9581a637550c3a740581c854f9847ec293c8aed71"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"wit-bindgen-rust-macro", "wit-bindgen-rust-macro",
@ -413,9 +391,8 @@ dependencies = [
[[package]] [[package]]
name = "wit-bindgen-core" name = "wit-bindgen-core"
version = "0.13.0" version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/bytecodealliance/wit-bindgen#5390bab780733f1660d14c254ec985df2816bf1d"
checksum = "565b945ae074886071eccf9cdaf8ccd7b959c2b0d624095bea5fe62003e8b3e0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"wit-component", "wit-component",
@ -424,9 +401,8 @@ dependencies = [
[[package]] [[package]]
name = "wit-bindgen-rust" name = "wit-bindgen-rust"
version = "0.13.0" version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/bytecodealliance/wit-bindgen#5390bab780733f1660d14c254ec985df2816bf1d"
checksum = "5695ff4e41873ed9ce56d2787e6b5772bdad9e70e2c1d2d160621d1762257f4f"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"heck", "heck",
@ -437,9 +413,8 @@ dependencies = [
[[package]] [[package]]
name = "wit-bindgen-rust-macro" name = "wit-bindgen-rust-macro"
version = "0.13.0" version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/bytecodealliance/wit-bindgen#5390bab780733f1660d14c254ec985df2816bf1d"
checksum = "a91835ea4231da1fe7971679d505ba14be7826e192b6357f08465866ef482e08"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"proc-macro2", "proc-macro2",
@ -452,9 +427,9 @@ dependencies = [
[[package]] [[package]]
name = "wit-component" name = "wit-component"
version = "0.16.0" version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e87488b57a08e2cbbd076b325acbe7f8666965af174d69d5929cd373bd54547f" checksum = "480cc1a078b305c1b8510f7c455c76cbd008ee49935f3a6c5fd5e937d8d95b1e"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bitflags", "bitflags",
@ -471,9 +446,9 @@ dependencies = [
[[package]] [[package]]
name = "wit-parser" name = "wit-parser"
version = "0.12.1" version = "0.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6ace9943d89bbf3dbbc71b966da0e7302057b311f36a4ac3d65ddfef17b52cf" checksum = "43771ee863a16ec4ecf9da0fc65c3bbd4a1235c8e3da5f094b562894843dfa76"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"id-arena", "id-arena",

View File

@ -13,12 +13,12 @@ lto = true
[dependencies] [dependencies]
anyhow = "1.0" anyhow = "1.0"
bincode = "1.3.3" bincode = "1.3.3"
cargo-component-bindings = { git = "https://github.com/bytecodealliance/cargo-component" }
rand = "0.8.5" rand = "0.8.5"
serde = {version = "1.0", features = ["derive"] } serde = {version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
sha2 = "0.10.8" sha2 = "0.10.8"
wit-bindgen = { version = "0.11.0", default_features = false } wit-bindgen = { git = "https://github.com/bytecodealliance/wit-bindgen", version = "0.13.0" }
uqbar_process_lib = { path = "../../../process_lib" }
[lib] [lib]
crate-type = ["cdylib"] crate-type = ["cdylib"]

View File

@ -1 +0,0 @@
../../../../src/kernel_types.rs

View File

@ -1,20 +1,18 @@
cargo_component_bindings::generate!();
use bindings::{
component::uq_process::types::*, get_capability, get_payload, print_to_terminal, receive,
send_request, send_response, Guest,
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sha2::Digest; use sha2::Digest;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use uqbar_process_lib::component::uq_process::api::*;
use uqbar_process_lib::component::uq_process::types::NodeId;
use uqbar_process_lib::*;
use uqbar_process_lib::kernel_types as kt;
#[allow(dead_code)] wit_bindgen::generate!({
mod kernel_types; path: "../../../wit",
use kernel_types as kt; world: "uq-process",
use kernel_types::{PackageManifestEntry, PackageMetadata, PackageVersion}; exports: {
world: Component,
#[allow(dead_code)] },
mod process_lib; });
use process_lib::PackageId;
#[allow(dead_code)] #[allow(dead_code)]
mod ft_worker_lib; mod ft_worker_lib;
@ -66,7 +64,7 @@ struct PackageListing {
pub publisher: NodeId, pub publisher: NodeId,
pub description: Option<String>, pub description: Option<String>,
pub website: Option<String>, pub website: Option<String>,
pub version: PackageVersion, pub version: kt::PackageVersion,
pub version_hash: String, // sha256 hash of the package zip or whatever pub version_hash: String, // sha256 hash of the package zip or whatever
} }
@ -163,12 +161,13 @@ pub enum InstallResponse {
// //
impl Guest for Component { impl Guest for Component {
fn init(our: Address) { fn init(our: String) {
let our = Address::from_str(&our).unwrap();
assert_eq!(our.process, "main:app_store:uqbar"); assert_eq!(our.process, "main:app_store:uqbar");
// begin by granting messaging capabilities to http_server and terminal, // begin by granting messaging capabilities to http_server and terminal,
// so that they can send us requests. // so that they can send us requests.
process_lib::grant_messaging( grant_messaging(
&our, &our,
&Vec::from([ &Vec::from([
ProcessId::from_str("http_server:sys:uqbar").unwrap(), ProcessId::from_str("http_server:sys:uqbar").unwrap(),
@ -178,7 +177,7 @@ impl Guest for Component {
print_to_terminal(0, &format!("app_store main proc: start")); print_to_terminal(0, &format!("app_store main proc: start"));
// load in our saved state or initalize a new one if none exists // load in our saved state or initalize a new one if none exists
let mut state = process_lib::get_state::<State>().unwrap_or(State { let mut state = get_typed_state::<State>().unwrap_or(State {
packages: HashMap::new(), packages: HashMap::new(),
requested_packages: HashSet::new(), requested_packages: HashSet::new(),
}); });
@ -377,68 +376,68 @@ fn handle_local_request(
hasher.update(&payload.bytes); hasher.update(&payload.bytes);
let version_hash = format!("{:x}", hasher.finalize()); let version_hash = format!("{:x}", hasher.finalize());
let _ = process_lib::send_and_await_response( send_and_await_typed_response(
&vfs_address, &vfs_address,
false, false,
serde_json::to_vec(&kt::VfsRequest { &kt::VfsRequest {
drive: package.to_string(), drive: package.to_string(),
action: kt::VfsAction::New, action: kt::VfsAction::New,
})?, },
None, None,
None, None,
5, 5,
)?; )??;
// add zip bytes // add zip bytes
payload.mime = Some("application/zip".to_string()); payload.mime = Some("application/zip".to_string());
let _ = process_lib::send_and_await_response( send_and_await_typed_response(
&vfs_address, &vfs_address,
true, true,
serde_json::to_vec(&kt::VfsRequest { &kt::VfsRequest {
drive: package.to_string(), drive: package.to_string(),
action: kt::VfsAction::Add { action: kt::VfsAction::Add {
full_path: package.to_string(), full_path: package.to_string(),
entry_type: kt::AddEntryType::ZipArchive, entry_type: kt::AddEntryType::ZipArchive,
}, },
})?, },
None, None,
Some(&payload), Some(&payload),
5, 5,
)?; )??;
// save the zip file itself in VFS for sharing with other nodes // save the zip file itself in VFS for sharing with other nodes
// call it <package>.zip // call it <package>.zip
let _ = process_lib::send_and_await_response( send_and_await_typed_response(
&vfs_address, &vfs_address,
true, true,
serde_json::to_vec(&kt::VfsRequest { &kt::VfsRequest {
drive: package.to_string(), drive: package.to_string(),
action: kt::VfsAction::Add { action: kt::VfsAction::Add {
full_path: format!("/{}.zip", package.to_string()), full_path: format!("/{}.zip", package.to_string()),
entry_type: kt::AddEntryType::NewFile, entry_type: kt::AddEntryType::NewFile,
}, },
})?, },
None, None,
Some(&payload), Some(&payload),
5, 5,
)?; )??;
let _ = process_lib::send_and_await_response( send_and_await_typed_response(
&vfs_address, &vfs_address,
false, false,
serde_json::to_vec(&kt::VfsRequest { &kt::VfsRequest {
drive: package.to_string(), drive: package.to_string(),
action: kt::VfsAction::GetEntry("/metadata.json".into()), action: kt::VfsAction::GetEntry("/metadata.json".into()),
})?, },
None, None,
None, None,
5, 5,
)?; )??;
let Some(payload) = get_payload() else { let Some(payload) = get_payload() else {
return Err(anyhow::anyhow!("no metadata payload")); return Err(anyhow::anyhow!("no metadata payload"));
}; };
let metadata = String::from_utf8(payload.bytes)?; let metadata = String::from_utf8(payload.bytes)?;
let metadata = serde_json::from_str::<PackageMetadata>(&metadata)?; let metadata = serde_json::from_str::<kt::PackageMetadata>(&metadata)?;
let listing_data = PackageListing { let listing_data = PackageListing {
name: metadata.package, name: metadata.package,
@ -455,30 +454,30 @@ fn handle_local_request(
auto_update: true, auto_update: true,
}; };
state.packages.insert(package.clone(), package_state); state.packages.insert(package.clone(), package_state);
process_lib::set_state::<State>(&state); set_typed_state::<State>(&state);
Ok(Some(Resp::NewPackageResponse(NewPackageResponse::Success))) Ok(Some(Resp::NewPackageResponse(NewPackageResponse::Success)))
} }
LocalRequest::Download { LocalRequest::Download {
package, package,
install_from, install_from,
} => Ok(Some(Resp::DownloadResponse( } => Ok(Some(Resp::DownloadResponse(
match process_lib::send_and_await_response( match send_and_await_typed_response(
&Address { &Address {
node: install_from.clone(), node: install_from.clone(),
process: our.process.clone(), process: our.process.clone(),
}, },
true, true,
serde_json::to_vec(&RemoteRequest::Download(package.clone()))?, &RemoteRequest::Download(package.clone()),
None, None,
None, None,
5, 5,
) { ) {
Ok((_source, Message::Response((resp, _context)))) => { Ok(Ok((_source, Message::Response((resp, _context))))) => {
let resp = serde_json::from_slice::<Resp>(&resp.ipc)?; let resp = serde_json::from_slice::<Resp>(&resp.ipc)?;
match resp { match resp {
Resp::RemoteResponse(RemoteResponse::DownloadApproved) => { Resp::RemoteResponse(RemoteResponse::DownloadApproved) => {
state.requested_packages.insert(package.clone()); state.requested_packages.insert(package.clone());
process_lib::set_state::<State>(&state); set_typed_state::<State>(&state);
DownloadResponse::Started DownloadResponse::Started
} }
_ => DownloadResponse::Failure, _ => DownloadResponse::Failure,
@ -492,22 +491,22 @@ fn handle_local_request(
node: our.node.clone(), node: our.node.clone(),
process: ProcessId::from_str("vfs:sys:uqbar")?, process: ProcessId::from_str("vfs:sys:uqbar")?,
}; };
let _ = process_lib::send_and_await_response( send_and_await_typed_response(
&vfs_address, &vfs_address,
false, false,
serde_json::to_vec(&kt::VfsRequest { &kt::VfsRequest {
drive: package.to_string(), drive: package.to_string(),
action: kt::VfsAction::GetEntry("/manifest.json".into()), action: kt::VfsAction::GetEntry("/manifest.json".into()),
})?, },
None, None,
None, None,
5, 5,
)?; )??;
let Some(payload) = get_payload() else { let Some(payload) = get_payload() else {
return Err(anyhow::anyhow!("no payload")); return Err(anyhow::anyhow!("no payload"));
}; };
let manifest = String::from_utf8(payload.bytes)?; let manifest = String::from_utf8(payload.bytes)?;
let manifest = serde_json::from_str::<Vec<PackageManifestEntry>>(&manifest)?; let manifest = serde_json::from_str::<Vec<kt::PackageManifestEntry>>(&manifest)?;
for entry in manifest { for entry in manifest {
let path = if entry.process_wasm_path.starts_with("/") { let path = if entry.process_wasm_path.starts_with("/") {
entry.process_wasm_path entry.process_wasm_path
@ -515,17 +514,17 @@ fn handle_local_request(
format!("/{}", entry.process_wasm_path) format!("/{}", entry.process_wasm_path)
}; };
let (_, hash_response) = process_lib::send_and_await_response( let (_, hash_response) = send_and_await_typed_response(
&vfs_address, &vfs_address,
false, false,
serde_json::to_vec(&kt::VfsRequest { &kt::VfsRequest {
drive: package.to_string(), drive: package.to_string(),
action: kt::VfsAction::GetHash(path.clone()), action: kt::VfsAction::GetHash(path.clone()),
})?, },
None, None,
None, None,
5, 5,
)?; )??;
let Message::Response((Response { ipc, .. }, _)) = hash_response else { let Message::Response((Response { ipc, .. }, _)) = hash_response else {
return Err(anyhow::anyhow!("bad vfs response")); return Err(anyhow::anyhow!("bad vfs response"));
@ -591,15 +590,15 @@ fn handle_local_request(
let Ok(parsed_new_process_id) = ProcessId::from_str(&process_id) else { let Ok(parsed_new_process_id) = ProcessId::from_str(&process_id) else {
return Err(anyhow::anyhow!("app-store: invalid process id!")); return Err(anyhow::anyhow!("app-store: invalid process id!"));
}; };
let _ = process_lib::send_request( send_typed_request(
&Address { &Address {
node: our.node.clone(), node: our.node.clone(),
process: ProcessId::from_str("kernel:sys:uqbar")?, process: ProcessId::from_str("kernel:sys:uqbar")?,
}, },
false, false,
serde_json::to_vec(&kt::KernelCommand::KillProcess(kt::ProcessId::de_wit( &kt::KernelCommand::KillProcess(kt::ProcessId::de_wit(
parsed_new_process_id.clone(), parsed_new_process_id.clone(),
)))?, )),
None, None,
None, None,
None, None,
@ -607,39 +606,39 @@ fn handle_local_request(
// kernel start process takes bytes as payload + wasm_bytes_handle... // kernel start process takes bytes as payload + wasm_bytes_handle...
// reconsider perhaps // reconsider perhaps
let (_, _bytes_response) = process_lib::send_and_await_response( let (_, _bytes_response) = send_and_await_typed_response(
&vfs_address, &vfs_address,
false, false,
serde_json::to_vec(&kt::VfsRequest { &kt::VfsRequest {
drive: package.to_string(), drive: package.to_string(),
action: kt::VfsAction::GetEntry(path), action: kt::VfsAction::GetEntry(path),
})?, },
None, None,
None, None,
5, 5,
)?; )??;
let Some(payload) = get_payload() else { let Some(payload) = get_payload() else {
return Err(anyhow::anyhow!("no wasm bytes payload.")); return Err(anyhow::anyhow!("no wasm bytes payload."));
}; };
let _ = process_lib::send_and_await_response( send_and_await_typed_response(
&Address { &Address {
node: our.node.clone(), node: our.node.clone(),
process: ProcessId::from_str("kernel:sys:uqbar")?, process: ProcessId::from_str("kernel:sys:uqbar")?,
}, },
false, false,
serde_json::to_vec(&kt::KernelCommand::StartProcess { &kt::KernelCommand::StartProcess {
id: kt::ProcessId::de_wit(parsed_new_process_id), id: kt::ProcessId::de_wit(parsed_new_process_id),
wasm_bytes_handle: hash, wasm_bytes_handle: hash,
on_panic: entry.on_panic, on_panic: entry.on_panic,
initial_capabilities, initial_capabilities,
public: entry.public, public: entry.public,
})?, },
None, None,
Some(&payload), Some(&payload),
5, 5,
)?; )??;
} }
Ok(Some(Resp::InstallResponse(InstallResponse::Success))) Ok(Some(Resp::InstallResponse(InstallResponse::Success)))
} }
@ -674,17 +673,17 @@ fn handle_remote_request(
process: ProcessId::from_str("vfs:sys:uqbar")?, process: ProcessId::from_str("vfs:sys:uqbar")?,
}; };
let file_name = format!("/{}.zip", package.to_string()); let file_name = format!("/{}.zip", package.to_string());
let _ = process_lib::send_and_await_response( send_and_await_typed_response(
&vfs_address, &vfs_address,
false, false,
serde_json::to_vec(&kt::VfsRequest { &kt::VfsRequest {
drive: package.to_string(), drive: package.to_string(),
action: kt::VfsAction::GetEntry(file_name.clone()), action: kt::VfsAction::GetEntry(file_name.clone()),
})?, },
None, None,
None, None,
5, 5,
)?; )??;
// transfer will inherit the payload bytes we receive from VFS // transfer will inherit the payload bytes we receive from VFS
spawn_transfer(&our, &file_name, None, &source); spawn_transfer(&our, &file_name, None, &source);
Ok(Some(Resp::RemoteResponse(RemoteResponse::DownloadApproved))) Ok(Some(Resp::RemoteResponse(RemoteResponse::DownloadApproved)))

View File

@ -1 +0,0 @@
../../../../src/process_lib.rs

View File

@ -23,29 +23,6 @@ version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"
[[package]]
name = "cargo-component-bindings"
version = "0.3.0"
source = "git+https://github.com/bytecodealliance/cargo-component#5cddf6df7fd5ab5aa9bfd66f4cf2e2f6cc7d72f9"
dependencies = [
"cargo-component-macro",
"wit-bindgen 0.13.0",
]
[[package]]
name = "cargo-component-macro"
version = "0.3.0"
source = "git+https://github.com/bytecodealliance/cargo-component#5cddf6df7fd5ab5aa9bfd66f4cf2e2f6cc7d72f9"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
"wit-bindgen-core",
"wit-bindgen-rust",
"wit-component",
]
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
@ -64,11 +41,11 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bincode", "bincode",
"cargo-component-bindings",
"rand", "rand",
"serde", "serde",
"serde_json", "serde_json",
"wit-bindgen 0.11.0", "uqbar_process_lib",
"wit-bindgen",
] ]
[[package]] [[package]]
@ -105,9 +82,9 @@ checksum = "25a2bc672d1148e28034f176e01fffebb08b35768468cc954630da77a1449005"
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "2.0.2" version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8adf3ddd720272c6ea8bf59463c04e0f93d0bbf7c5439b691bca2987e0270897" checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f"
dependencies = [ dependencies = [
"equivalent", "equivalent",
"hashbrown", "hashbrown",
@ -206,18 +183,18 @@ checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.189" version = "1.0.190"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e422a44e74ad4001bdc8eede9a4570ab52f71190e9c076d14369f38b9200537" checksum = "91d3c334ca1ee894a2c6f6ad698fe8c435b76d504b13d436f0685d648d6d96f7"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.189" version = "1.0.190"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e48d1f918009ce3145511378cf68d613e3b3d9137d67272562080d68a2b32d5" checksum = "67c5609f394e5c2bd7fc51efda478004ea80ef42fee983d5c67a65e34f32c0e3"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -226,9 +203,9 @@ dependencies = [
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.107" version = "1.0.108"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65" checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b"
dependencies = [ dependencies = [
"itoa", "itoa",
"ryu", "ryu",
@ -279,6 +256,17 @@ version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c"
[[package]]
name = "uqbar_process_lib"
version = "0.2.0"
dependencies = [
"anyhow",
"bincode",
"serde",
"serde_json",
"wit-bindgen",
]
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.11.0+wasi-snapshot-preview1" version = "0.11.0+wasi-snapshot-preview1"
@ -287,18 +275,18 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]] [[package]]
name = "wasm-encoder" name = "wasm-encoder"
version = "0.35.0" version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ca90ba1b5b0a70d3d49473c5579951f3bddc78d47b59256d2f9d4922b150aca" checksum = "53ae0be20bf87918df4fa831bfbbd0b491d24aee407ed86360eae4c2c5608d38"
dependencies = [ dependencies = [
"leb128", "leb128",
] ]
[[package]] [[package]]
name = "wasm-metadata" name = "wasm-metadata"
version = "0.10.9" version = "0.10.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14abc161bfda5b519aa229758b68f2a52b45a12b993808665c857d1a9a00223c" checksum = "5621910462c61a8efc3248fdfb1739bf649bb335b0df935c27b340418105f9d8"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"indexmap", "indexmap",
@ -312,9 +300,9 @@ dependencies = [
[[package]] [[package]]
name = "wasmparser" name = "wasmparser"
version = "0.115.0" version = "0.116.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e06c0641a4add879ba71ccb3a1e4278fd546f76f1eafb21d8f7b07733b547cd5" checksum = "53290b1276c5c2d47d694fb1a920538c01f51690e7e261acbe1d10c5fc306ea1"
dependencies = [ dependencies = [
"indexmap", "indexmap",
"semver", "semver",
@ -322,18 +310,8 @@ dependencies = [
[[package]] [[package]]
name = "wit-bindgen" name = "wit-bindgen"
version = "0.11.0" version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/bytecodealliance/wit-bindgen#5390bab780733f1660d14c254ec985df2816bf1d"
checksum = "f8a3e8e965dc50e6eb4410d9a11720719fadc6a1713803ea5f3be390b81c8279"
dependencies = [
"bitflags",
]
[[package]]
name = "wit-bindgen"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7d92ce0ca6b6074059413a9581a637550c3a740581c854f9847ec293c8aed71"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"wit-bindgen-rust-macro", "wit-bindgen-rust-macro",
@ -341,9 +319,8 @@ dependencies = [
[[package]] [[package]]
name = "wit-bindgen-core" name = "wit-bindgen-core"
version = "0.13.0" version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/bytecodealliance/wit-bindgen#5390bab780733f1660d14c254ec985df2816bf1d"
checksum = "565b945ae074886071eccf9cdaf8ccd7b959c2b0d624095bea5fe62003e8b3e0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"wit-component", "wit-component",
@ -352,9 +329,8 @@ dependencies = [
[[package]] [[package]]
name = "wit-bindgen-rust" name = "wit-bindgen-rust"
version = "0.13.0" version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/bytecodealliance/wit-bindgen#5390bab780733f1660d14c254ec985df2816bf1d"
checksum = "5695ff4e41873ed9ce56d2787e6b5772bdad9e70e2c1d2d160621d1762257f4f"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"heck", "heck",
@ -365,9 +341,8 @@ dependencies = [
[[package]] [[package]]
name = "wit-bindgen-rust-macro" name = "wit-bindgen-rust-macro"
version = "0.13.0" version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/bytecodealliance/wit-bindgen#5390bab780733f1660d14c254ec985df2816bf1d"
checksum = "a91835ea4231da1fe7971679d505ba14be7826e192b6357f08465866ef482e08"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"proc-macro2", "proc-macro2",
@ -380,9 +355,9 @@ dependencies = [
[[package]] [[package]]
name = "wit-component" name = "wit-component"
version = "0.16.0" version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e87488b57a08e2cbbd076b325acbe7f8666965af174d69d5929cd373bd54547f" checksum = "480cc1a078b305c1b8510f7c455c76cbd008ee49935f3a6c5fd5e937d8d95b1e"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bitflags", "bitflags",
@ -399,9 +374,9 @@ dependencies = [
[[package]] [[package]]
name = "wit-parser" name = "wit-parser"
version = "0.12.1" version = "0.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6ace9943d89bbf3dbbc71b966da0e7302057b311f36a4ac3d65ddfef17b52cf" checksum = "43771ee863a16ec4ecf9da0fc65c3bbd4a1235c8e3da5f094b562894843dfa76"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"id-arena", "id-arena",

View File

@ -13,11 +13,11 @@ lto = true
[dependencies] [dependencies]
anyhow = "1.0" anyhow = "1.0"
bincode = "1.3.3" bincode = "1.3.3"
cargo-component-bindings = { git = "https://github.com/bytecodealliance/cargo-component" }
rand = "0.8" rand = "0.8"
serde = {version = "1.0", features = ["derive"] } serde = {version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
wit-bindgen = { version = "0.11.0", default_features = false } wit-bindgen = { git = "https://github.com/bytecodealliance/wit-bindgen", version = "0.13.0" }
uqbar_process_lib = { path = "../../../process_lib" }
[lib] [lib]
crate-type = ["cdylib"] crate-type = ["cdylib"]

View File

@ -1,6 +1,5 @@
use super::bindings::component::uq_process::types::*;
use super::bindings::{print_to_terminal, send_request, spawn, Address, Payload};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use uqbar_process_lib::component::uq_process::api::*;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct FileTransferContext { pub struct FileTransferContext {
@ -84,15 +83,17 @@ pub fn spawn_transfer(
.unwrap(), .unwrap(),
metadata: None, metadata: None,
}, },
Some(&serde_json::to_vec(&FileTransferContext { Some(
file_name: file_name.into(), &serde_json::to_vec(&FileTransferContext {
file_size: match &payload_or_inherit { file_name: file_name.into(),
Some(p) => Some(p.bytes.len() as u64), file_size: match &payload_or_inherit {
None => None, // TODO Some(p) => Some(p.bytes.len() as u64),
}, None => None, // TODO
start_time: std::time::SystemTime::now(), },
}) start_time: std::time::SystemTime::now(),
.unwrap()), })
.unwrap(),
),
payload_or_inherit.as_ref(), payload_or_inherit.as_ref(),
); );
} }

View File

@ -1,15 +1,20 @@
cargo_component_bindings::generate!();
use bindings::component::uq_process::types::*;
use bindings::{get_payload, print_to_terminal, receive, send_request, send_response, Guest};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use uqbar_process_lib::component::uq_process::api::*;
struct Component; use uqbar_process_lib::component::uq_process::types::SendErrorKind;
mod ft_worker_lib; mod ft_worker_lib;
#[allow(dead_code)]
mod process_lib;
use ft_worker_lib::*; use ft_worker_lib::*;
wit_bindgen::generate!({
path: "../../../wit",
world: "uq-process",
exports: {
world: Component,
},
});
struct Component;
/// internal worker protocol /// internal worker protocol
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub enum FTWorkerProtocol { pub enum FTWorkerProtocol {
@ -18,7 +23,8 @@ pub enum FTWorkerProtocol {
} }
impl Guest for Component { impl Guest for Component {
fn init(our: Address) { fn init(our: String) {
let our = Address::from_str(&our).unwrap();
print_to_terminal(1, &format!("{}: start", our.process)); print_to_terminal(1, &format!("{}: start", our.process));
let Ok((parent_process, Message::Request(req))) = receive() else { let Ok((parent_process, Message::Request(req))) = receive() else {
@ -49,7 +55,7 @@ impl Guest for Component {
// then upon reciving affirmative response, // then upon reciving affirmative response,
// send contents in chunks and wait for // send contents in chunks and wait for
// acknowledgement. // acknowledgement.
match bindings::send_and_await_response( match send_and_await_response(
&Address::from_str(&target).unwrap(), &Address::from_str(&target).unwrap(),
&Request { &Request {
inherit: false, inherit: false,

View File

@ -1 +0,0 @@
../../../../src/process_lib.rs

View File

@ -1,3 +0,0 @@
# This file is automatically generated by cargo-component.
# It is not intended for manual editing.
version = 1

View File

@ -19,9 +19,9 @@ dependencies = [
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "2.4.0" version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"
[[package]] [[package]]
name = "equivalent" name = "equivalent"
@ -31,9 +31,9 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.14.0" version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a" checksum = "f93e7192158dbcda357bdec5fb5788eebf8bbac027f3f33e719d29135ae84156"
[[package]] [[package]]
name = "heck" name = "heck"
@ -52,9 +52,9 @@ checksum = "25a2bc672d1148e28034f176e01fffebb08b35768468cc954630da77a1449005"
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "2.0.0" version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5477fe2230a79769d8dc68e0eabf5437907c0457a5614a9e8dddb67f65eb65d" checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f"
dependencies = [ dependencies = [
"equivalent", "equivalent",
"hashbrown", "hashbrown",
@ -81,9 +81,9 @@ checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.66" version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
@ -105,24 +105,24 @@ checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
[[package]] [[package]]
name = "semver" name = "semver"
version = "1.0.18" version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0293b4b29daaf487284529cc2f5675b8e57c61f70167ba415a463651fd6a918" checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.188" version = "1.0.190"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e" checksum = "91d3c334ca1ee894a2c6f6ad698fe8c435b76d504b13d436f0685d648d6d96f7"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.188" version = "1.0.190"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" checksum = "67c5609f394e5c2bd7fc51efda478004ea80ef42fee983d5c67a65e34f32c0e3"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -131,9 +131,9 @@ dependencies = [
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.105" version = "1.0.108"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "693151e1ac27563d6dbcec9dee9fbd5da8539b20fa14ad3752b2e6d363ace360" checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b"
dependencies = [ dependencies = [
"itoa", "itoa",
"ryu", "ryu",
@ -142,9 +142,9 @@ dependencies = [
[[package]] [[package]]
name = "smallvec" name = "smallvec"
version = "1.11.0" version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62bb4feee49fdd9f707ef802e22365a35de4b7b299de4763d44bfea899442ff9" checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a"
[[package]] [[package]]
name = "spdx" name = "spdx"
@ -157,9 +157,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.31" version = "2.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "718fa2415bcb8d8bd775917a1bf12a7931b6dfa890753378538118181e0cb398" checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -180,9 +180,9 @@ dependencies = [
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.11" version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]] [[package]]
name = "unicode-segmentation" name = "unicode-segmentation"

View File

@ -1,6 +1,7 @@
use uqbar_process_lib::component::uq_process::api::*; use uqbar_process_lib::component::uq_process::api::*;
wit_bindgen::generate!({ wit_bindgen::generate!({
path: "../../wit",
world: "uq-process", world: "uq-process",
exports: { exports: {
world: Component, world: Component,

View File

@ -0,0 +1,422 @@
use crate::component::uq_process::api as wit;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
//
// process-facing kernel types, used for process
// management and message-passing
// matches types in uqbar.wit
//
pub type Context = Vec<u8>;
pub type NodeId = String; // QNS domain name
/// process ID is a formatted unique identifier that contains
/// the publishing node's ID, the package name, and finally the process name.
/// the process name can be a random number, or a name chosen by the user.
/// the formatting is as follows:
/// `[process name]:[package name]:[node ID]`
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct ProcessId {
process_name: String,
package_name: String,
publisher_node: NodeId,
}
#[allow(dead_code)]
impl ProcessId {
/// generates a random u64 number if process_name is not declared
pub fn new(process_name: &str, package_name: &str, publisher_node: &str) -> Self {
ProcessId {
process_name: process_name.into(),
package_name: package_name.into(),
publisher_node: publisher_node.into(),
}
}
pub fn from_str(input: &str) -> Result<Self, ProcessIdParseError> {
// split string on colons into 3 segments
let mut segments = input.split(':');
let process_name = segments
.next()
.ok_or(ProcessIdParseError::MissingField)?
.to_string();
let package_name = segments
.next()
.ok_or(ProcessIdParseError::MissingField)?
.to_string();
let publisher_node = segments
.next()
.ok_or(ProcessIdParseError::MissingField)?
.to_string();
if segments.next().is_some() {
return Err(ProcessIdParseError::TooManyColons);
}
Ok(ProcessId {
process_name,
package_name,
publisher_node,
})
}
pub fn to_string(&self) -> String {
[
self.process_name.as_str(),
self.package_name.as_str(),
self.publisher_node.as_str(),
]
.join(":")
}
pub fn process(&self) -> &str {
&self.process_name
}
pub fn package(&self) -> &str {
&self.package_name
}
pub fn publisher_node(&self) -> &str {
&self.publisher_node
}
pub fn en_wit(&self) -> wit::ProcessId {
wit::ProcessId {
process_name: self.process_name.clone(),
package_name: self.package_name.clone(),
publisher_node: self.publisher_node.clone(),
}
}
pub fn de_wit(wit: wit::ProcessId) -> ProcessId {
ProcessId {
process_name: wit.process_name,
package_name: wit.package_name,
publisher_node: wit.publisher_node,
}
}
}
#[derive(Debug)]
pub enum ProcessIdParseError {
TooManyColons,
MissingField,
}
#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct Address {
pub node: NodeId,
pub process: ProcessId,
}
impl Address {
pub fn en_wit(&self) -> wit::Address {
wit::Address {
node: self.node.clone(),
process: self.process.en_wit(),
}
}
pub fn de_wit(wit: wit::Address) -> Address {
Address {
node: wit.node,
process: ProcessId {
process_name: wit.process.process_name,
package_name: wit.process.package_name,
publisher_node: wit.process.publisher_node,
},
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Payload {
pub mime: Option<String>, // MIME type
pub bytes: Vec<u8>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Request {
pub inherit: bool,
pub expects_response: Option<u64>, // number of seconds until timeout
pub ipc: Vec<u8>,
pub metadata: Option<String>, // JSON-string
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Response {
pub inherit: bool,
pub ipc: Vec<u8>,
pub metadata: Option<String>, // JSON-string
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Message {
Request(Request),
Response((Response, Option<Context>)),
}
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct Capability {
pub issuer: Address,
pub params: String, // JSON-string
}
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct SignedCapability {
pub issuer: Address,
pub params: String, // JSON-string
pub signature: Vec<u8>, // signed by the kernel, so we can verify that the kernel issued it
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SendError {
pub kind: SendErrorKind,
pub target: Address,
pub message: Message,
pub payload: Option<Payload>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum SendErrorKind {
Offline,
Timeout,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum OnPanic {
None,
Restart,
Requests(Vec<(Address, Request, Option<Payload>)>),
}
impl OnPanic {
pub fn is_restart(&self) -> bool {
match self {
OnPanic::None => false,
OnPanic::Restart => true,
OnPanic::Requests(_) => false,
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub enum KernelCommand {
StartProcess {
id: ProcessId,
wasm_bytes_handle: u128,
on_panic: OnPanic,
initial_capabilities: HashSet<SignedCapability>,
public: bool,
},
KillProcess(ProcessId), // this is extrajudicial killing: we might lose messages!
// kernel only
RebootProcess {
process_id: ProcessId,
persisted: PersistedProcess,
},
Shutdown,
}
#[derive(Debug, Serialize, Deserialize)]
pub enum KernelResponse {
StartedProcess,
StartProcessError,
KilledProcess(ProcessId),
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PersistedProcess {
pub wasm_bytes_handle: u128,
// pub drive: String,
// pub full_path: String,
pub on_panic: OnPanic,
pub capabilities: HashSet<Capability>,
pub public: bool, // marks if a process allows messages from any process
}
#[derive(Debug, Serialize, Deserialize)]
pub struct VfsRequest {
pub drive: String,
pub action: VfsAction,
}
#[derive(Debug, Serialize, Deserialize)]
pub enum VfsAction {
New,
Add {
full_path: String,
entry_type: AddEntryType,
},
Rename {
full_path: String,
new_full_path: String,
},
Delete(String),
WriteOffset {
full_path: String,
offset: u64,
},
SetSize {
full_path: String,
size: u64,
},
GetPath(u128),
GetHash(String),
GetEntry(String),
GetFileChunk {
full_path: String,
offset: u64,
length: u64,
},
GetEntryLength(String),
}
#[derive(Debug, Serialize, Deserialize)]
pub enum AddEntryType {
Dir,
NewFile, // add a new file to fs and add name in vfs
ExistingFile { hash: u128 }, // link an existing file in fs to a new name in vfs
ZipArchive,
}
#[derive(Debug, Serialize, Deserialize)]
pub enum GetEntryType {
Dir,
File,
}
#[derive(Debug, Serialize, Deserialize)]
pub enum VfsResponse {
Ok,
Err(VfsError),
GetPath(Option<String>),
GetHash(Option<u128>),
GetEntry {
// file bytes in payload, if entry was a file
is_file: bool,
children: Vec<String>,
},
GetFileChunk, // chunk in payload
GetEntryLength(u64),
}
#[derive(Debug, Serialize, Deserialize)]
pub enum VfsError {
BadDriveName,
BadDescriptor,
NoCap,
EntryNotFound,
}
#[allow(dead_code)]
impl VfsError {
pub fn kind(&self) -> &str {
match *self {
VfsError::BadDriveName => "BadDriveName",
VfsError::BadDescriptor => "BadDescriptor",
VfsError::NoCap => "NoCap",
VfsError::EntryNotFound => "EntryNotFound",
}
}
}
//
// package types
//
pub type PackageVersion = (u32, u32, u32);
/// the type that gets deserialized from `metadata.json` in a package
#[derive(Debug, Serialize, Deserialize)]
pub struct PackageMetadata {
pub package: String,
pub publisher: String,
pub version: PackageVersion,
pub description: Option<String>,
pub website: Option<String>,
}
/// the type that gets deserialized from each entry in the array in `manifest.json`
#[derive(Debug, Serialize, Deserialize)]
pub struct PackageManifestEntry {
pub process_name: String,
pub process_wasm_path: String,
pub on_panic: OnPanic,
pub request_networking: bool,
pub request_messaging: Vec<String>,
pub public: bool,
}
//
// conversions between wit types and kernel types (annoying!)
//
pub fn de_wit_request(wit: wit::Request) -> Request {
Request {
inherit: wit.inherit,
expects_response: wit.expects_response,
ipc: wit.ipc,
metadata: wit.metadata,
}
}
pub fn en_wit_request(request: Request) -> wit::Request {
wit::Request {
inherit: request.inherit,
expects_response: request.expects_response,
ipc: request.ipc,
metadata: request.metadata,
}
}
pub fn de_wit_response(wit: wit::Response) -> Response {
Response {
inherit: wit.inherit,
ipc: wit.ipc,
metadata: wit.metadata,
}
}
pub fn en_wit_response(response: Response) -> wit::Response {
wit::Response {
inherit: response.inherit,
ipc: response.ipc,
metadata: response.metadata,
}
}
pub fn de_wit_payload(wit: Option<wit::Payload>) -> Option<Payload> {
match wit {
None => None,
Some(wit) => Some(Payload {
mime: wit.mime,
bytes: wit.bytes,
}),
}
}
pub fn en_wit_payload(load: Option<Payload>) -> Option<wit::Payload> {
match load {
None => None,
Some(load) => Some(wit::Payload {
mime: load.mime,
bytes: load.bytes,
}),
}
}
pub fn de_wit_signed_capability(wit: wit::SignedCapability) -> SignedCapability {
SignedCapability {
issuer: Address {
node: wit.issuer.node,
process: ProcessId {
process_name: wit.issuer.process.process_name,
package_name: wit.issuer.process.package_name,
publisher_node: wit.issuer.process.publisher_node,
},
},
params: wit.params,
signature: wit.signature,
}
}
pub fn en_wit_signed_capability(cap: SignedCapability) -> wit::SignedCapability {
wit::SignedCapability {
issuer: cap.issuer.en_wit(),
params: cap.params,
signature: cap.signature,
}
}

View File

@ -1,13 +1,17 @@
#![allow(dead_code)]
use crate::component::uq_process::api::*;
/// Uqbar process standard library for Rust compiled to WASM /// Uqbar process standard library for Rust compiled to WASM
/// Must be used in context of bindings generated by uqbar.wit /// Must be used in context of bindings generated by uqbar.wit
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::component::uq_process::api::*;
wit_bindgen::generate!({ wit_bindgen::generate!({
path: "../wit", path: "../wit",
world: "uq-process-lib", world: "uq-process-lib",
}); });
pub mod kernel_types;
/// Override the println! macro to print to the terminal /// Override the println! macro to print to the terminal
// macro_rules! println { // macro_rules! println {
// () => { // () => {
@ -233,113 +237,71 @@ pub enum AddressParseError {
/// For payloads, we use bincode to serialize and deserialize to bytes. /// For payloads, we use bincode to serialize and deserialize to bytes.
/// ///
pub fn send_typed_request<T1, T2, T3, T4>( pub fn send_typed_request<T1>(
target: &Address, target: &Address,
inherit_payload_and_context: bool, inherit_payload_and_context: bool,
ipc: &T1, ipc: &T1,
metadata: Option<&T2>, metadata: Option<String>,
context: Option<&T3>, payload: Option<&Payload>,
payload: Option<&T4>,
timeout: Option<u64>, timeout: Option<u64>,
) -> anyhow::Result<()> ) -> anyhow::Result<()>
where where
T1: serde::Serialize, T1: serde::Serialize,
T2: serde::Serialize,
T3: serde::Serialize,
T4: serde::Serialize,
{ {
let payload = match payload {
Some(payload) => Some(Payload {
mime: None,
bytes: bincode::serialize(payload)?,
}),
None => None,
};
let context = match context {
Some(context) => Some(serde_json::to_vec(context)?),
None => None,
};
crate::send_request( crate::send_request(
target, target,
&Request { &Request {
inherit: inherit_payload_and_context, inherit: inherit_payload_and_context,
expects_response: timeout, expects_response: timeout,
ipc: serde_json::to_vec(ipc)?, ipc: serde_json::to_vec(ipc)?,
metadata: match metadata { metadata,
Some(metadata) => Some(serde_json::to_string(metadata)?),
None => None,
},
}, },
context.as_ref(), None,
payload.as_ref(), payload,
); );
Ok(()) Ok(())
} }
pub fn send_typed_response<T1, T2, T3>( pub fn send_typed_response<T1>(
inherit_payload: bool, inherit_payload: bool,
ipc: &T1, ipc: &T1,
metadata: Option<&T2>, metadata: Option<String>,
payload: Option<&T3>, // will overwrite inherit flag if both are set payload: Option<&Payload>, // will overwrite inherit flag if both are set
) -> anyhow::Result<()> ) -> anyhow::Result<()>
where where
T1: serde::Serialize, T1: serde::Serialize,
T2: serde::Serialize,
T3: serde::Serialize,
{ {
let payload = match payload {
Some(payload) => Some(Payload {
mime: None,
bytes: bincode::serialize(payload)?,
}),
None => None,
};
crate::send_response( crate::send_response(
&Response { &Response {
inherit: inherit_payload, inherit: inherit_payload,
ipc: serde_json::to_vec(ipc)?, ipc: serde_json::to_vec(ipc)?,
metadata: match metadata { metadata,
Some(metadata) => Some(serde_json::to_string(metadata)?),
None => None,
},
}, },
payload.as_ref(), payload,
); );
Ok(()) Ok(())
} }
pub fn send_and_await_typed_response<T1, T2, T3>( pub fn send_and_await_typed_response<T1>(
target: &Address, target: &Address,
inherit_payload_and_context: bool, inherit_payload_and_context: bool,
ipc: &T1, ipc: &T1,
metadata: Option<&T2>, metadata: Option<String>,
payload: Option<&T3>, payload: Option<&Payload>,
timeout: u64, timeout: u64,
) -> anyhow::Result<Result<(Address, Message), SendError>> ) -> anyhow::Result<Result<(Address, Message), SendError>>
where where
T1: serde::Serialize, T1: serde::Serialize,
T2: serde::Serialize,
T3: serde::Serialize,
{ {
let payload = match payload {
Some(payload) => Some(Payload {
mime: None,
bytes: bincode::serialize(payload)?,
}),
None => None,
};
let res = crate::send_and_await_response( let res = crate::send_and_await_response(
target, target,
&Request { &Request {
inherit: inherit_payload_and_context, inherit: inherit_payload_and_context,
expects_response: Some(timeout), expects_response: Some(timeout),
ipc: serde_json::to_vec(ipc)?, ipc: serde_json::to_vec(ipc)?,
metadata: match metadata { metadata,
Some(metadata) => Some(serde_json::to_string(metadata)?),
None => None,
},
}, },
payload.as_ref(), payload,
); );
Ok(res) Ok(res)
} }
@ -373,12 +335,11 @@ where
} }
pub fn grant_messaging(our: &Address, grant_to: &Vec<ProcessId>) -> anyhow::Result<()> { pub fn grant_messaging(our: &Address, grant_to: &Vec<ProcessId>) -> anyhow::Result<()> {
let Some(our_messaging_cap) = crate::get_capability( let Some(our_messaging_cap) = crate::get_capability(our, &"\"messaging\"".into()) else {
our,
&"\"messaging\"".into()
) else {
// the kernel will always give us this capability, so this should never happen // the kernel will always give us this capability, so this should never happen
return Err(anyhow::anyhow!("failed to get our own messaging capability!")) return Err(anyhow::anyhow!(
"failed to get our own messaging capability!"
));
}; };
for process in grant_to { for process in grant_to {
crate::share_capability(&process, &our_messaging_cap); crate::share_capability(&process, &our_messaging_cap);

View File

@ -1,3 +1,4 @@
use crate::types::*;
use anyhow::Result; use anyhow::Result;
use dotenv; use dotenv;
use ethers::prelude::namehash; use ethers::prelude::namehash;
@ -6,8 +7,6 @@ use std::sync::Arc;
use tokio::sync::{mpsc, oneshot}; use tokio::sync::{mpsc, oneshot};
use tokio::{fs, time::timeout}; use tokio::{fs, time::timeout};
use crate::types::*;
mod encryptor; mod encryptor;
mod eth_rpc; mod eth_rpc;
mod filesystem; mod filesystem;