xs/client/
commands.rs

1use futures::StreamExt;
2
3use base64::Engine;
4use ssri::Integrity;
5
6use http_body_util::{combinators::BoxBody, BodyExt, Empty, StreamBody};
7use hyper::body::Bytes;
8use hyper::Method;
9use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
10use tokio::sync::mpsc::Receiver;
11use tokio_util::io::ReaderStream;
12
13use super::request;
14use crate::store::{ReadOptions, TTL};
15
16pub async fn cat(
17    addr: &str,
18    options: ReadOptions,
19    sse: bool,
20) -> Result<Receiver<Bytes>, Box<dyn std::error::Error + Send + Sync>> {
21    // Convert any usize limit to u64
22    let query = if options == ReadOptions::default() {
23        None
24    } else {
25        Some(options.to_query_string())
26    };
27
28    let headers = if sse {
29        Some(vec![(
30            "Accept".to_string(),
31            "text/event-stream".to_string(),
32        )])
33    } else {
34        None
35    };
36
37    let res = request::request(addr, Method::GET, "", query.as_deref(), empty(), headers).await?;
38
39    let (_parts, mut body) = res.into_parts();
40    let (tx, rx) = tokio::sync::mpsc::channel(100);
41
42    tokio::spawn(async move {
43        while let Some(frame_result) = body.frame().await {
44            match frame_result {
45                Ok(frame) => {
46                    if let Ok(bytes) = frame.into_data() {
47                        if tx.send(bytes).await.is_err() {
48                            break;
49                        }
50                    }
51                }
52                Err(e) => {
53                    eprintln!("Error reading body: {e}");
54                    break;
55                }
56            }
57        }
58    });
59
60    Ok(rx)
61}
62
63pub async fn append<R>(
64    addr: &str,
65    topic: &str,
66    data: R,
67    meta: Option<&serde_json::Value>,
68    ttl: Option<TTL>,
69) -> Result<Bytes, Box<dyn std::error::Error + Send + Sync>>
70where
71    R: AsyncRead + Unpin + Send + 'static,
72{
73    let query = ttl.map(|t| t.to_query());
74
75    let reader_stream = ReaderStream::new(data);
76    let mapped_stream = reader_stream.map(|result| {
77        result
78            .map(hyper::body::Frame::data)
79            .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)
80    });
81    let body = StreamBody::new(mapped_stream);
82
83    let headers = meta.map(|meta_value| {
84        let json_string = serde_json::to_string(meta_value).unwrap();
85        let encoded = base64::prelude::BASE64_STANDARD.encode(json_string);
86        vec![("xs-meta".to_string(), encoded)]
87    });
88
89    let res = request::request(
90        addr,
91        Method::POST,
92        &format!("append/{topic}"),
93        query.as_deref(),
94        body,
95        headers,
96    )
97    .await?;
98    let body = res.collect().await?.to_bytes();
99    Ok(body)
100}
101
102pub async fn cas_get<W>(
103    addr: &str,
104    integrity: Integrity,
105    writer: &mut W,
106) -> Result<(), Box<dyn std::error::Error + Send + Sync>>
107where
108    W: AsyncWrite + Unpin,
109{
110    let parts = super::types::RequestParts::parse(addr, &format!("cas/{integrity}"), None)?;
111
112    match parts.connection {
113        super::types::ConnectionKind::Unix(path) => {
114            // Direct CAS access for local path
115            let store_path = path.parent().unwrap_or(&path).to_path_buf();
116            let cas_path = store_path.join("cacache");
117            match cacache::Reader::open_hash(&cas_path, integrity).await {
118                Ok(mut reader) => {
119                    tokio::io::copy(&mut reader, writer).await?;
120                    writer.flush().await?;
121                    Ok(())
122                }
123                Err(e) => {
124                    // Check if this is an entry not found error
125                    if matches!(e, cacache::Error::EntryNotFound(_, _)) {
126                        return Err(Box::new(crate::error::NotFound));
127                    }
128                    // Also check for IO not found errors in the chain
129                    let boxed_err: Box<dyn std::error::Error + Send + Sync> = Box::new(e);
130                    if crate::error::has_not_found_io_error(&boxed_err) {
131                        return Err(Box::new(crate::error::NotFound));
132                    }
133                    Err(boxed_err)
134                }
135            }
136        }
137        _ => {
138            // Remote HTTP access
139            let res = request::request(
140                addr,
141                Method::GET,
142                &format!("cas/{integrity}"),
143                None,
144                empty(),
145                None,
146            )
147            .await?;
148            let mut body = res.into_body();
149
150            while let Some(frame) = body.frame().await {
151                let frame = frame?;
152                if let Ok(chunk) = frame.into_data() {
153                    writer.write_all(&chunk).await?;
154                }
155            }
156
157            writer.flush().await?;
158            Ok(())
159        }
160    }
161}
162
163pub async fn cas_post<R>(
164    addr: &str,
165    data: R,
166) -> Result<Bytes, Box<dyn std::error::Error + Send + Sync>>
167where
168    R: AsyncRead + Unpin + Send + 'static,
169{
170    let reader_stream = ReaderStream::new(data);
171    let mapped_stream = reader_stream.map(|result| {
172        result
173            .map(hyper::body::Frame::data)
174            .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)
175    });
176    let body = StreamBody::new(mapped_stream);
177
178    let res = request::request(addr, Method::POST, "cas", None, body, None).await?;
179    let body = res.collect().await?.to_bytes();
180    Ok(body)
181}
182
183pub async fn get(addr: &str, id: &str) -> Result<Bytes, Box<dyn std::error::Error + Send + Sync>> {
184    let res = request::request(addr, Method::GET, id, None, empty(), None).await?;
185    let body = res.collect().await?.to_bytes();
186    Ok(body)
187}
188
189pub async fn remove(addr: &str, id: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
190    let _ = request::request(addr, Method::DELETE, id, None, empty(), None).await?;
191    Ok(())
192}
193
194pub async fn last(
195    addr: &str,
196    topic: &str,
197    follow: bool,
198) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
199    let query = if follow { Some("follow=true") } else { None };
200
201    let res = request::request(
202        addr,
203        Method::GET,
204        &format!("last/{topic}"),
205        query,
206        empty(),
207        None,
208    )
209    .await?;
210
211    let mut body = res.into_body();
212    let mut stdout = tokio::io::stdout();
213
214    while let Some(frame) = body.frame().await {
215        let frame = frame?;
216        if let Ok(chunk) = frame.into_data() {
217            stdout.write_all(&chunk).await?;
218        }
219    }
220    stdout.flush().await?;
221    Ok(())
222}
223
224pub async fn import<R>(
225    addr: &str,
226    data: R,
227) -> Result<Bytes, Box<dyn std::error::Error + Send + Sync>>
228where
229    R: AsyncRead + Unpin + Send + 'static,
230{
231    let reader_stream = ReaderStream::new(data);
232    let mapped_stream = reader_stream.map(|result| {
233        result
234            .map(hyper::body::Frame::data)
235            .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)
236    });
237    let body = StreamBody::new(mapped_stream);
238
239    let res = request::request(addr, Method::POST, "import", None, body, None).await?;
240    let body = res.collect().await?.to_bytes();
241    Ok(body)
242}
243
244pub async fn version(addr: &str) -> Result<Bytes, Box<dyn std::error::Error + Send + Sync>> {
245    match request::request(addr, Method::GET, "version", None, empty(), None).await {
246        Ok(res) => {
247            let body = res.collect().await?.to_bytes();
248            Ok(body)
249        }
250        Err(e) => {
251            // this was the version before the /version endpoint was added
252            if crate::error::NotFound::is_not_found(&e) {
253                Ok(Bytes::from(r#"{"version":"0.0.9"}"#))
254            } else {
255                Err(e)
256            }
257        }
258    }
259}
260
261fn empty() -> BoxBody<Bytes, Box<dyn std::error::Error + Send + Sync>> {
262    Empty::<Bytes>::new()
263        .map_err(|never| match never {})
264        .boxed()
265}
266
267pub async fn eval(
268    addr: &str,
269    script: String,
270) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
271    let res = request::request(addr, Method::POST, "eval", None, script, None).await?;
272
273    let mut body = res.into_body();
274    let mut stdout = tokio::io::stdout();
275
276    while let Some(frame) = body.frame().await {
277        let frame = frame?;
278        if let Ok(chunk) = frame.into_data() {
279            stdout.write_all(&chunk).await?;
280        }
281    }
282    stdout.flush().await?;
283    Ok(())
284}
285
286#[cfg(test)]
287mod tests {
288    use super::*;
289    use std::str::FromStr;
290    use tempfile::TempDir;
291
292    #[tokio::test]
293    async fn test_cas_get_not_found_local() {
294        let temp_dir = TempDir::new().unwrap();
295        let store_path = temp_dir.path().to_str().unwrap();
296
297        // Create a fake hash that doesn't exist
298        let fake_hash = "sha256-fakehashnotfound0000000000000000000000000000000=";
299        let integrity = Integrity::from_str(fake_hash).unwrap();
300
301        let mut output = Vec::new();
302        let result = cas_get(store_path, integrity, &mut output).await;
303
304        // Should return NotFound error
305        assert!(result.is_err());
306        let err = result.unwrap_err();
307        assert!(crate::error::NotFound::is_not_found(&err));
308    }
309}