| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
use std::convert::Infallible; |
| 11 |
use std::time::Duration; |
| 12 |
|
| 13 |
use axum::response::sse::{Event, KeepAlive, Sse}; |
| 14 |
use tokio_stream::Stream; |
| 15 |
|
| 16 |
use crate::event::ChatEvent; |
| 17 |
use crate::stream::ChatStream; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
pub const KEEPALIVE_INTERVAL: Duration = Duration::from_secs(30); |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
fn frame(event: &ChatEvent) -> Event { |
| 32 |
let data = serde_json::to_string(event).unwrap_or_else(|_| { |
| 33 |
|
| 34 |
|
| 35 |
tracing::error!("chat event failed to serialize"); |
| 36 |
r#"{"type":"gap"}"#.to_owned() |
| 37 |
}); |
| 38 |
Event::default().event(event.name()).data(data) |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
pub fn sse_response( |
| 48 |
stream: ChatStream, |
| 49 |
) -> Sse<impl Stream<Item = Result<Event, Infallible>> + Send> { |
| 50 |
let events = async_stream::stream! { |
| 51 |
let mut stream = stream; |
| 52 |
while let Some(event) = stream.next().await { |
| 53 |
yield Ok(frame(&event)); |
| 54 |
} |
| 55 |
}; |
| 56 |
|
| 57 |
Sse::new(events).keep_alive( |
| 58 |
KeepAlive::new() |
| 59 |
.interval(KEEPALIVE_INTERVAL) |
| 60 |
.text("keepalive"), |
| 61 |
) |
| 62 |
} |
| 63 |
|
| 64 |
#[cfg(test)] |
| 65 |
mod tests { |
| 66 |
use super::*; |
| 67 |
use crate::ids::{MessageId, RoomId, UserId}; |
| 68 |
use crate::message::Message; |
| 69 |
use uuid::Uuid; |
| 70 |
|
| 71 |
fn message(id: i64) -> Message { |
| 72 |
Message { |
| 73 |
id: MessageId(id), |
| 74 |
room_id: RoomId(Uuid::nil()), |
| 75 |
author_id: UserId(Uuid::nil()), |
| 76 |
body_html: "hi".into(), |
| 77 |
created_at: 0, |
| 78 |
nonce: None, |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
fn wire(event: &ChatEvent) -> String { |
| 90 |
format!("{:?}", frame(event)) |
| 91 |
} |
| 92 |
|
| 93 |
#[test] |
| 94 |
fn debug_output_still_exposes_the_buffer() { |
| 95 |
assert!( |
| 96 |
wire(&ChatEvent::Gap).contains("event: gap\\n"), |
| 97 |
"Event's Debug no longer embeds the encoded frame, so the wire-format \ |
| 98 |
assertions in this module have stopped checking anything" |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
#[test] |
| 103 |
fn every_variant_frames_with_its_own_event_name() { |
| 104 |
for (event, name) in [ |
| 105 |
(ChatEvent::Message(message(1)), "message"), |
| 106 |
(ChatEvent::Delete { id: MessageId(1) }, "delete"), |
| 107 |
( |
| 108 |
ChatEvent::Purge { |
| 109 |
author_id: UserId(Uuid::nil()), |
| 110 |
}, |
| 111 |
"purge", |
| 112 |
), |
| 113 |
(ChatEvent::Gap, "gap"), |
| 114 |
] { |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
assert!( |
| 119 |
wire(&event).contains(&format!("event: {name}\\n")), |
| 120 |
"expected an SSE event field named {name}, got: {}", |
| 121 |
wire(&event) |
| 122 |
); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
#[test] |
| 127 |
fn body_html_is_carried_verbatim() { |
| 128 |
|
| 129 |
|
| 130 |
let mut m = message(1); |
| 131 |
m.body_html = "<em>hi</em>".into(); |
| 132 |
let json = serde_json::to_string(&ChatEvent::Message(m)).unwrap(); |
| 133 |
assert!(json.contains(r#"<em>hi</em>"#), "{json}"); |
| 134 |
} |
| 135 |
|
| 136 |
#[test] |
| 137 |
fn a_frame_never_contains_a_bare_newline() { |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
let mut m = message(1); |
| 142 |
m.body_html = "line one\nline two".into(); |
| 143 |
let json = serde_json::to_string(&ChatEvent::Message(m)).unwrap(); |
| 144 |
assert!( |
| 145 |
!json.contains('\n'), |
| 146 |
"raw newline reached the frame: {json}" |
| 147 |
); |
| 148 |
assert!( |
| 149 |
json.contains(r"\n"), |
| 150 |
"newline should survive escaped: {json}" |
| 151 |
); |
| 152 |
} |
| 153 |
} |
| 154 |
|