|
| 1 | +use std::path::PathBuf; |
| 2 | +use std::str::FromStr; |
| 3 | + |
| 4 | +use anyhow::{anyhow, Result}; |
| 5 | +use extism::*; |
| 6 | +use futures::stream::StreamExt; |
| 7 | +use iroh::net::key::SecretKey; |
| 8 | +use iroh::node::Node; |
| 9 | +use iroh::rpc_protocol::{DownloadLocation, SetTagOption}; |
| 10 | +use iroh::util::path::IrohPaths; |
| 11 | + |
| 12 | +type IrohNode = Node<iroh::bytes::store::flat::Store>; |
| 13 | + |
| 14 | +const IROH_EXTISM_DATA_DIR: &str = "iroh-extism"; |
| 15 | + |
| 16 | +pub async fn default_iroh_extism_data_root() -> Result<PathBuf> { |
| 17 | + if let Some(val) = std::env::var_os("IROH_EXTISM_DATA_DIR") { |
| 18 | + return Ok(PathBuf::from(val)); |
| 19 | + } |
| 20 | + let path = dirs_next::data_dir().ok_or_else(|| { |
| 21 | + anyhow!("operating environment provides no directory for application data") |
| 22 | + })?; |
| 23 | + |
| 24 | + Ok(path.join(IROH_EXTISM_DATA_DIR)) |
| 25 | +} |
| 26 | + |
| 27 | +pub async fn create_iroh(path: PathBuf) -> Result<IrohNode> { |
| 28 | + tokio::fs::create_dir_all(&path).await?; |
| 29 | + |
| 30 | + let blob_dir = path.join(IrohPaths::BaoFlatStoreDir); |
| 31 | + tokio::fs::create_dir_all(&blob_dir).await?; |
| 32 | + |
| 33 | + let db = iroh::bytes::store::flat::Store::load(blob_dir).await?; |
| 34 | + let doc_store = iroh::sync::store::fs::Store::new(path.join(IrohPaths::DocsDatabase))?; |
| 35 | + let node = iroh::node::Node::builder(db, doc_store) |
| 36 | + .secret_key(SecretKey::generate()) |
| 37 | + .spawn() |
| 38 | + .await?; |
| 39 | + |
| 40 | + Ok(node) |
| 41 | +} |
| 42 | + |
| 43 | +// host_fn!(iroh_blob_get_node_id(user_data: Node<iroh::bytes::store::flat::Store>) -> String { |
| 44 | +// Ok(user_data.node_id().to_string()) |
| 45 | +// }); |
| 46 | + |
| 47 | +struct Context { |
| 48 | + rt: tokio::runtime::Handle, |
| 49 | + iroh: IrohNode, |
| 50 | +} |
| 51 | + |
| 52 | +host_fn!(iroh_blob_get_ticket(user_data: Context; ticket: &str) -> Vec<u8> { |
| 53 | + let ctx = user_data.get()?; |
| 54 | + let ctx = ctx.lock().unwrap(); |
| 55 | + |
| 56 | + let (node_addr, hash, format) = iroh::ticket::BlobTicket::from_str(ticket).map_err(|_| anyhow!("invalid ticket"))?.into_parts(); |
| 57 | + |
| 58 | + if format != iroh::rpc_protocol::BlobFormat::Raw { |
| 59 | + return Err(anyhow!("can only get raw bytes for now, not HashSequences (collections)")); |
| 60 | + } |
| 61 | + let client = ctx.iroh.client(); |
| 62 | + let buf = ctx.rt.block_on(async move { |
| 63 | + let mut stream = client.blobs.download(iroh::rpc_protocol::BlobDownloadRequest { |
| 64 | + hash, |
| 65 | + format, |
| 66 | + peer: node_addr, |
| 67 | + out: DownloadLocation::Internal, |
| 68 | + tag: SetTagOption::Auto, |
| 69 | + }).await?; |
| 70 | + while stream.next().await.is_some() {} |
| 71 | + |
| 72 | + let buffer = client.blobs.read(hash).await?.read_to_bytes().await?; |
| 73 | + anyhow::Ok(buffer.to_vec()) |
| 74 | + })?; |
| 75 | + |
| 76 | + Ok(buf) |
| 77 | +}); |
| 78 | + |
| 79 | +pub fn add_all_host_functions( |
| 80 | + rt: tokio::runtime::Handle, |
| 81 | + b: PluginBuilder, |
| 82 | + iroh: IrohNode, |
| 83 | +) -> PluginBuilder { |
| 84 | + let ctx = UserData::new(Context { |
| 85 | + rt, |
| 86 | + iroh, |
| 87 | + }); |
| 88 | + |
| 89 | + b.with_function( |
| 90 | + "iroh_blob_get_ticket", |
| 91 | + [PTR], |
| 92 | + [PTR], |
| 93 | + ctx, |
| 94 | + iroh_blob_get_ticket, |
| 95 | + ) |
| 96 | +} |
0 commit comments