haps 0.1.22

Hashtree Package System: signed packages, content-addressed distribution, social discovery
Documentation
//! A catalog operation reads one immutable Hashtree directory throughout.
use anyhow::Result;
use hashtree_client::Cid;
use hashtree_client::{Client, ClientConfig, Reference};
use std::{path::Path, time::Duration};
use tokio::sync::OnceCell;

pub struct CatalogTransport {
    client: Client,
    reference: Option<Reference>,
    root: OnceCell<Cid>,
}

impl CatalogTransport {
    pub fn new(location: &str, cache: &Path) -> Result<Self> {
        let mut config = ClientConfig::from_env()?;
        // Allow slow cold connections, but return immediately when a signed
        // root is available. This is a deadline, not an observation window.
        config.resolve_window = Duration::from_secs(10);
        Self::with_config(location, cache, config)
    }

    pub fn with_config(location: &str, cache: &Path, config: ClientConfig) -> Result<Self> {
        let (reference, root) = if let Some(hash) = location.strip_prefix("htree://nhash") {
            let decoded = hashtree_core::nhash_decode(&format!("nhash{hash}"))?;
            (
                None,
                OnceCell::new_with(Some(Cid {
                    hash: decoded.hash,
                    key: decoded.decrypt_key,
                })),
            )
        } else {
            (Some(Reference::parse(location)?), OnceCell::new())
        };
        Ok(Self {
            client: Client::new(config, &cache.join("transport"))?,
            reference,
            root,
        })
    }

    pub async fn read(&self, path: &str, limit: usize) -> Result<Vec<u8>> {
        let root = self
            .root
            .get_or_try_init(|| {
                // Keep the concurrent resolver state off callers' native stacks.
                Box::pin(
                    self.client
                        .resolve_ready(self.reference.as_ref().expect("unresolved named root")),
                )
            })
            .await?;
        self.client.read_file(root, path, limit).await
    }
}