cross-stream 0.15.1

An event stream store for personal, local-first use, specializing in event sourcing.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use futures::StreamExt;

use base64::Engine;
use ssri::Integrity;

use http_body_util::{combinators::BoxBody, BodyExt, Empty, StreamBody};
use hyper::body::Bytes;
use hyper::Method;
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
use tokio::sync::mpsc::Receiver;
use tokio_util::io::ReaderStream;

use super::request;
use crate::store::{ReadError, ReadOptions, TTL};

pub async fn cat(
    addr: &str,
    options: ReadOptions,
    sse: bool,
    with_timestamp: bool,
) -> Result<Receiver<Result<Bytes, crate::error::Error>>, crate::error::Error> {
    // Convert any usize limit to u64
    let mut query_parts = Vec::new();
    if options != ReadOptions::default() {
        query_parts.push(options.to_query_string());
    }
    if with_timestamp {
        query_parts.push("with-timestamp".to_string());
    }
    let query = if query_parts.is_empty() {
        None
    } else {
        Some(query_parts.join("&"))
    };

    let mut headers = vec![trailers()];
    if sse {
        headers.push(("Accept".to_string(), "text/event-stream".to_string()));
    }

    let res = request::request(
        addr,
        Method::GET,
        "",
        query.as_deref(),
        empty(),
        Some(headers),
    )
    .await?;

    let (_parts, mut body) = res.into_parts();
    let (tx, rx) = tokio::sync::mpsc::channel(100);

    // The server's bytes, as they arrive; a read that fell behind ends with
    // its error, from the `xs-lagged` trailer (ADR 0008), and a broken
    // connection with its own.
    tokio::spawn(async move {
        while let Some(frame) = body.frame().await {
            let item = match frame {
                Ok(frame) => match frame.into_data() {
                    Ok(bytes) => Ok(bytes),
                    Err(frame) => match lagged(frame) {
                        Some(err) => Err(err.into()),
                        None => continue,
                    },
                },
                // A broken connection ends the read with its error too.
                Err(e) => Err(e.into()),
            };
            let end = item.is_err();
            if tx.send(item).await.is_err() || end {
                return;
            }
        }
    });

    Ok(rx)
}

pub async fn append<R>(
    addr: &str,
    topic: &str,
    data: R,
    meta: Option<&serde_json::Value>,
    ttl: Option<TTL>,
    with_timestamp: bool,
) -> Result<Bytes, crate::Error>
where
    R: AsyncRead + Unpin + Send + 'static,
{
    let mut query_parts = Vec::new();
    if let Some(t) = ttl {
        query_parts.push(t.to_query());
    }
    if with_timestamp {
        query_parts.push("with-timestamp".to_string());
    }
    let query = if query_parts.is_empty() {
        None
    } else {
        Some(query_parts.join("&"))
    };

    let reader_stream = ReaderStream::new(data);
    let mapped_stream = reader_stream.map(|result| {
        result
            .map(hyper::body::Frame::data)
            .map_err(|e| Box::new(e) as crate::Error)
    });
    let body = StreamBody::new(mapped_stream);

    let headers = meta.map(|meta_value| {
        let json_string = serde_json::to_string(meta_value).unwrap();
        let encoded = base64::prelude::BASE64_STANDARD.encode(json_string);
        vec![("xs-meta".to_string(), encoded)]
    });

    let res = request::request(
        addr,
        Method::POST,
        &format!("append/{topic}"),
        query.as_deref(),
        body,
        headers,
    )
    .await?;
    let body = res.collect().await?.to_bytes();
    Ok(body)
}

pub async fn cas_get<W>(
    addr: &str,
    integrity: Integrity,
    writer: &mut W,
) -> Result<(), crate::Error>
where
    W: AsyncWrite + Unpin,
{
    let parts = super::types::RequestParts::parse(addr, &format!("cas/{integrity}"), None)?;

    match parts.connection {
        super::types::ConnectionKind::Unix(path) => {
            // Direct CAS access for local path
            let store_path = path.parent().unwrap_or(&path).to_path_buf();
            let cas_path = store_path.join("cacache");
            match cacache::Reader::open_hash(&cas_path, integrity).await {
                Ok(mut reader) => {
                    tokio::io::copy(&mut reader, writer).await?;
                    writer.flush().await?;
                    Ok(())
                }
                Err(e) => {
                    // Check if this is an entry not found error
                    if matches!(e, cacache::Error::EntryNotFound(_, _)) {
                        return Err(Box::new(crate::error::NotFound));
                    }
                    // Also check for IO not found errors in the chain
                    let boxed_err: crate::Error = Box::new(e);
                    if crate::error::has_not_found_io_error(&boxed_err) {
                        return Err(Box::new(crate::error::NotFound));
                    }
                    Err(boxed_err)
                }
            }
        }
        _ => {
            // Remote HTTP access
            let res = request::request(
                addr,
                Method::GET,
                &format!("cas/{integrity}"),
                None,
                empty(),
                None,
            )
            .await?;
            let mut body = res.into_body();

            while let Some(frame) = body.frame().await {
                let frame = frame?;
                if let Ok(chunk) = frame.into_data() {
                    writer.write_all(&chunk).await?;
                }
            }

            writer.flush().await?;
            Ok(())
        }
    }
}

pub async fn cas_post<R>(addr: &str, data: R) -> Result<Bytes, crate::Error>
where
    R: AsyncRead + Unpin + Send + 'static,
{
    let reader_stream = ReaderStream::new(data);
    let mapped_stream = reader_stream.map(|result| {
        result
            .map(hyper::body::Frame::data)
            .map_err(|e| Box::new(e) as crate::Error)
    });
    let body = StreamBody::new(mapped_stream);

    let res = request::request(addr, Method::POST, "cas", None, body, None).await?;
    let body = res.collect().await?.to_bytes();
    Ok(body)
}

pub async fn get(addr: &str, id: &str, with_timestamp: bool) -> Result<Bytes, crate::Error> {
    let query = if with_timestamp {
        Some("with-timestamp")
    } else {
        None
    };
    let res = request::request(addr, Method::GET, id, query, empty(), None).await?;
    let body = res.collect().await?.to_bytes();
    Ok(body)
}

pub async fn remove(addr: &str, id: &str) -> Result<(), crate::Error> {
    let _ = request::request(addr, Method::DELETE, id, None, empty(), None).await?;
    Ok(())
}

pub async fn last(
    addr: &str,
    topic: Option<&str>,
    last: usize,
    follow: bool,
    with_timestamp: bool,
) -> Result<(), crate::Error> {
    let mut query_parts = Vec::new();
    if last != 1 {
        query_parts.push(format!("last={last}"));
    }
    if follow {
        query_parts.push("follow=true".to_string());
    }
    if with_timestamp {
        query_parts.push("with-timestamp".to_string());
    }
    let query = if query_parts.is_empty() {
        None
    } else {
        Some(query_parts.join("&"))
    };

    let path = match topic {
        Some(t) => format!("last/{t}"),
        None => "last".to_string(),
    };

    let res = request::request(
        addr,
        Method::GET,
        &path,
        query.as_deref(),
        empty(),
        Some(vec![trailers()]),
    )
    .await?;

    let mut body = res.into_body();
    let mut stdout = tokio::io::stdout();

    while let Some(frame) = body.frame().await {
        match frame?.into_data() {
            Ok(chunk) => stdout.write_all(&chunk).await?,
            Err(frame) => {
                if let Some(err) = lagged(frame) {
                    stdout.flush().await?;
                    return Err(err.into());
                }
            }
        }
    }
    stdout.flush().await?;
    Ok(())
}

pub async fn import<R>(addr: &str, data: R) -> Result<Bytes, crate::Error>
where
    R: AsyncRead + Unpin + Send + 'static,
{
    let reader_stream = ReaderStream::new(data);
    let mapped_stream = reader_stream.map(|result| {
        result
            .map(hyper::body::Frame::data)
            .map_err(|e| Box::new(e) as crate::Error)
    });
    let body = StreamBody::new(mapped_stream);

    let res = request::request(addr, Method::POST, "import", None, body, None).await?;
    let body = res.collect().await?.to_bytes();
    Ok(body)
}

pub async fn version(addr: &str) -> Result<Bytes, crate::Error> {
    match request::request(addr, Method::GET, "version", None, empty(), None).await {
        Ok(res) => {
            let body = res.collect().await?.to_bytes();
            Ok(body)
        }
        Err(e) => {
            // this was the version before the /version endpoint was added
            if crate::error::NotFound::is_not_found(&e) {
                Ok(Bytes::from(r#"{"version":"0.0.9"}"#))
            } else {
                Err(e)
            }
        }
    }
}

/// Asks the server for trailers: it sends them only when asked.
fn trailers() -> (String, String) {
    ("TE".to_string(), "trailers".to_string())
}

/// The read's error, if `frame` is the `xs-lagged` trailer (ADR 0008).
fn lagged(frame: hyper::body::Frame<Bytes>) -> Option<ReadError> {
    let trailers = frame.into_trailers().ok()?;
    let skipped = trailers
        .get(crate::api::LAGGED_TRAILER)?
        .to_str()
        .ok()?
        .parse()
        .ok()?;
    Some(ReadError::Lagged { skipped })
}

fn empty() -> BoxBody<Bytes, crate::Error> {
    Empty::<Bytes>::new()
        .map_err(|never| match never {})
        .boxed()
}

pub async fn eval(addr: &str, script: String) -> Result<(), crate::Error> {
    let res = request::request(
        addr,
        Method::POST,
        "eval",
        None,
        script,
        Some(vec![trailers()]),
    )
    .await?;

    let mut body = res.into_body();
    let mut stdout = tokio::io::stdout();

    while let Some(frame) = body.frame().await {
        match frame?.into_data() {
            Ok(chunk) => stdout.write_all(&chunk).await?,
            Err(frame) => {
                // The script's error, sent after its output (api.rs).
                let error = frame.into_trailers().ok().and_then(|t| {
                    let v = t.get(crate::api::ERROR_TRAILER)?;
                    Some(v.to_str().ok()?.to_string())
                });
                if let Some(error) = error {
                    stdout.flush().await?;
                    return Err(error.into());
                }
            }
        }
    }
    stdout.flush().await?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn the_lagged_trailer_is_the_read_error() {
        let mut trailers = hyper::HeaderMap::new();
        trailers.insert(crate::api::LAGGED_TRAILER, 506.into());
        assert!(matches!(
            lagged(hyper::body::Frame::trailers(trailers)),
            Some(ReadError::Lagged { skipped: 506 })
        ));
        assert!(lagged(hyper::body::Frame::data(Bytes::from("{}"))).is_none());
    }
    use std::str::FromStr;
    use tempfile::TempDir;

    #[tokio::test]
    async fn test_cas_get_not_found_local() {
        let temp_dir = TempDir::new().unwrap();
        let store_path = temp_dir.path().to_str().unwrap();

        // Create a fake hash that doesn't exist
        let fake_hash = "sha256-fakehashnotfound0000000000000000000000000000000=";
        let integrity = Integrity::from_str(fake_hash).unwrap();

        let mut output = Vec::new();
        let result = cas_get(store_path, integrity, &mut output).await;

        // Should return NotFound error
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(crate::error::NotFound::is_not_found(&err));
    }
}