larastvel-core 0.2.2

Larastvel framework core - inspired by Laravel
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
use std::collections::HashMap;
use std::sync::Arc;

use async_trait::async_trait;
use axum::extract::ws::{Message, WebSocket, WebSocketUpgrade};
use axum::response::IntoResponse;
use axum::Extension;
use futures_util::{SinkExt, StreamExt};
use tokio::sync::{mpsc, RwLock};

use super::{
    BroadcastError, BroadcastMessage, Broadcaster, ChannelAuthCallback, PresenceChannelData,
};

// ---------------------------------------------------------------------------
// Shared subscriber registry
// ---------------------------------------------------------------------------

type Sender = mpsc::UnboundedSender<String>;

/// Maps channel names to connected WebSocket senders.
#[derive(Debug, Clone)]
pub struct SubscriberRegistry {
    channels: Arc<RwLock<HashMap<String, Vec<Sender>>>>,
}

impl SubscriberRegistry {
    pub fn new() -> Self {
        Self {
            channels: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Subscribe a sender to a channel.
    pub async fn subscribe(&self, channel: &str, sender: Sender) {
        let mut map = self.channels.write().await;
        map.entry(channel.to_string()).or_default().push(sender);
    }

    /// Unsubscribe a sender from a channel.
    pub async fn unsubscribe(&self, channel: &str, sender: &Sender) {
        let mut map = self.channels.write().await;
        if let Some(senders) = map.get_mut(channel) {
            senders.retain(|s| !s.same_channel(sender));
            if senders.is_empty() {
                map.remove(channel);
            }
        }
    }

    /// Remove a sender from all channels it belongs to.
    pub async fn remove_sender(&self, sender: &Sender) {
        let mut map = self.channels.write().await;
        map.retain(|_, senders| {
            senders.retain(|s| !s.same_channel(sender));
            !senders.is_empty()
        });
    }

    /// Send a message to all subscribers of the given channels.
    pub async fn broadcast_to_channels(&self, channels: &[String], message: &str) {
        let map = self.channels.read().await;
        for channel in channels {
            if let Some(senders) = map.get(channel) {
                for sender in senders {
                    let _ = sender.send(message.to_string());
                }
            }
        }
    }

    /// Number of channels with subscribers.
    pub async fn channel_count(&self) -> usize {
        self.channels.read().await.len()
    }

    /// Total number of subscribers across all channels.
    pub async fn subscriber_count(&self) -> usize {
        self.channels.read().await.values().map(|v| v.len()).sum()
    }
}

impl Default for SubscriberRegistry {
    fn default() -> Self {
        Self::new()
    }
}

// ---------------------------------------------------------------------------
// NativeBroadcaster
// ---------------------------------------------------------------------------

/// A self-hosted WebSocket broadcaster.
///
/// Clients connect via WebSocket to a provided endpoint, subscribe to
/// channels, and receive real-time events when the application broadcasts.
///
/// This replaces the need for third-party services like Pusher or Ably
/// in development and small-scale deployments.
#[derive(Debug, Clone)]
pub struct NativeBroadcaster {
    name: String,
    registry: SubscriberRegistry,
}

impl NativeBroadcaster {
    /// Create a new native broadcaster.
    pub fn new(name: &str, registry: SubscriberRegistry) -> Self {
        Self {
            name: name.to_string(),
            registry,
        }
    }

    /// Returns a reference to the shared subscriber registry.
    ///
    /// Used to mount the WebSocket upgrade handler.
    pub fn registry(&self) -> &SubscriberRegistry {
        &self.registry
    }
}

#[async_trait]
impl Broadcaster for NativeBroadcaster {
    async fn broadcast(&self, message: BroadcastMessage) -> Result<(), BroadcastError> {
        let channels = message.channels.clone();
        let payload = serde_json::json!({
            "event": message.event,
            "data": message.data,
            "channel": channels.first().map(|s| s.as_str()),
        });
        let text = serde_json::to_string(&payload)
            .map_err(|e| BroadcastError::Failed(format!("Serialization error: {}", e)))?;
        self.registry.broadcast_to_channels(&channels, &text).await;
        Ok(())
    }

    async fn authenticate(
        &self,
        _channel_name: &str,
        _socket_id: &str,
        _callback: Option<&ChannelAuthCallback>,
    ) -> Result<String, BroadcastError> {
        let response = serde_json::json!({
            "auth": format!("{}:native-auth", self.name),
        });
        serde_json::to_string(&response)
            .map_err(|e| BroadcastError::AuthError(format!("Serialization error: {}", e)))
    }

    async fn authenticate_presence(
        &self,
        _channel_name: &str,
        _socket_id: &str,
        channel_data: &PresenceChannelData,
        _callback: Option<&ChannelAuthCallback>,
    ) -> Result<String, BroadcastError> {
        let channel_data_json = serde_json::to_string(channel_data)
            .map_err(|e| BroadcastError::AuthError(format!("Serialization error: {}", e)))?;
        let response = serde_json::json!({
            "auth": format!("{}:native-auth", self.name),
            "channel_data": channel_data_json,
        });
        serde_json::to_string(&response)
            .map_err(|e| BroadcastError::AuthError(format!("Serialization error: {}", e)))
    }

    fn name(&self) -> &str {
        &self.name
    }
}

// ---------------------------------------------------------------------------
// WebSocket handler
// ---------------------------------------------------------------------------

/// Axum handler that upgrades an HTTP connection to a WebSocket.
///
/// Clients connect to this endpoint, then send JSON control messages
/// to subscribe/unsubscribe from channels:
///
/// ```json
/// {"type": "subscribe", "channel": "chat"}
/// {"type": "unsubscribe", "channel": "chat"}
/// {"type": "ping"}
/// ```
///
/// The server responds with:
/// ```json
/// {"type": "subscribed", "channel": "chat"}
/// {"type": "unsubscribed", "channel": "chat"}
/// {"type": "pong"}
/// ```
///
/// Broadcast events are pushed as:
/// ```json
/// {"event": "new-message", "data": {...}, "channel": "chat"}
/// ```
pub async fn ws_handler(
    ws: WebSocketUpgrade,
    Extension(registry): Extension<SubscriberRegistry>,
) -> impl IntoResponse {
    ws.on_upgrade(move |socket| handle_socket(socket, registry))
}

async fn handle_socket(socket: WebSocket, registry: SubscriberRegistry) {
    let (mut sender, mut receiver) = socket.split();
    let (tx, mut rx) = mpsc::unbounded_channel::<String>();

    // Spawn a task to forward messages from rx to the WebSocket sender.
    let send_task = tokio::spawn(async move {
        while let Some(msg) = rx.recv().await {
            if sender.send(Message::Text(msg.into())).await.is_err() {
                break;
            }
        }
    });

    let mut subscribed_channels: Vec<String> = Vec::new();

    while let Some(Ok(msg)) = receiver.next().await {
        match msg {
            Message::Text(text) => {
                if let Ok(cmd) = serde_json::from_str::<ClientCommand>(&text) {
                    match cmd.r#type.as_str() {
                        "subscribe" => {
                            if let Some(ch) = cmd.channel {
                                registry.subscribe(&ch, tx.clone()).await;
                                subscribed_channels.push(ch.clone());
                                let _ = tx
                                    .send(format!(r#"{{"type":"subscribed","channel":"{}"}}"#, ch));
                            }
                        }
                        "unsubscribe" => {
                            if let Some(ch) = cmd.channel {
                                registry.unsubscribe(&ch, &tx).await;
                                subscribed_channels.retain(|c| c != &ch);
                                let _ = tx.send(format!(
                                    r#"{{"type":"unsubscribed","channel":"{}"}}"#,
                                    ch
                                ));
                            }
                        }
                        "ping" => {
                            let _ = tx.send(r#"{"type":"pong"}"#.to_string());
                        }
                        _ => {}
                    }
                }
            }
            Message::Close(_) => break,
            _ => {}
        }
    }

    // Clean up: remove this sender from all subscribed channels.
    for ch in &subscribed_channels {
        registry.unsubscribe(ch, &tx).await;
    }
    registry.remove_sender(&tx).await;
    send_task.abort();
}

#[derive(serde::Deserialize)]
struct ClientCommand {
    r#type: String,
    channel: Option<String>,
}

// ============================================================================
// TESTS
// ============================================================================

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

    #[tokio::test]
    async fn test_subscriber_registry_subscribe() {
        let registry = SubscriberRegistry::new();
        let (tx, _rx) = mpsc::unbounded_channel();

        registry.subscribe("chat", tx).await;
        assert_eq!(registry.channel_count().await, 1);
        assert_eq!(registry.subscriber_count().await, 1);
    }

    #[tokio::test]
    async fn test_subscriber_registry_unsubscribe() {
        let registry = SubscriberRegistry::new();
        let (tx, _rx) = mpsc::unbounded_channel();

        registry.subscribe("chat", tx.clone()).await;
        registry.subscribe("chat", tx.clone()).await;
        assert_eq!(registry.subscriber_count().await, 2);

        registry.unsubscribe("chat", &tx).await;
        // Both senders are the same channel, so both get removed
        assert_eq!(registry.channel_count().await, 0);
    }

    #[tokio::test]
    async fn test_subscriber_registry_remove_sender() {
        let registry = SubscriberRegistry::new();
        let (tx, _rx) = mpsc::unbounded_channel();

        registry.subscribe("chat", tx.clone()).await;
        registry.subscribe("alerts", tx.clone()).await;
        assert_eq!(registry.channel_count().await, 2);

        registry.remove_sender(&tx).await;
        assert_eq!(registry.channel_count().await, 0);
    }

    #[tokio::test]
    async fn test_subscriber_registry_broadcast_to_channels() {
        let registry = SubscriberRegistry::new();
        let (tx, mut rx) = mpsc::unbounded_channel::<String>();

        registry.subscribe("chat", tx.clone()).await;
        registry
            .broadcast_to_channels(&["chat".to_string()], "hello")
            .await;

        let received = rx.recv().await;
        assert_eq!(received, Some("hello".to_string()));
    }

    #[tokio::test]
    async fn test_subscriber_registry_broadcast_no_subscribers() {
        let registry = SubscriberRegistry::new();
        // Should not panic
        registry
            .broadcast_to_channels(&["empty".to_string()], "msg")
            .await;
    }

    #[tokio::test]
    async fn test_native_broadcaster_broadcast() {
        let registry = SubscriberRegistry::new();
        let broadcaster = NativeBroadcaster::new("native", registry.clone());

        let (tx, mut rx) = mpsc::unbounded_channel::<String>();
        registry.subscribe("chat", tx).await;

        let message = BroadcastMessage::new(
            "test-event",
            serde_json::json!({"key": "value"}),
            vec!["chat".to_string()],
        );
        broadcaster.broadcast(message).await.unwrap();

        let received = rx.recv().await;
        assert!(received.is_some());
        let parsed: serde_json::Value = serde_json::from_str(&received.unwrap()).unwrap();
        assert_eq!(parsed["event"], "test-event");
        assert_eq!(parsed["data"]["key"], "value");
    }

    #[tokio::test]
    async fn test_native_broadcaster_broadcast_multiple_channels() {
        let registry = SubscriberRegistry::new();
        let broadcaster = NativeBroadcaster::new("native", registry.clone());

        let (tx1, mut rx1) = mpsc::unbounded_channel();
        let (tx2, mut rx2) = mpsc::unbounded_channel();
        registry.subscribe("chat", tx1).await;
        registry.subscribe("alerts", tx2).await;

        let message = BroadcastMessage::new(
            "multi",
            serde_json::json!({"n": 1}),
            vec!["chat".to_string(), "alerts".to_string()],
        );
        broadcaster.broadcast(message).await.unwrap();

        assert!(rx1.recv().await.is_some());
        assert!(rx2.recv().await.is_some());
    }

    #[tokio::test]
    async fn test_native_broadcaster_authenticate() {
        let registry = SubscriberRegistry::new();
        let broadcaster = NativeBroadcaster::new("native", registry);

        let result = broadcaster
            .authenticate("private-chat", "123.456", None)
            .await
            .unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&result).unwrap();
        assert!(parsed["auth"].is_string());
    }

    #[tokio::test]
    async fn test_native_broadcaster_authenticate_presence() {
        let registry = SubscriberRegistry::new();
        let broadcaster = NativeBroadcaster::new("native", registry);

        let channel_data = PresenceChannelData {
            user_id: "user-1".to_string(),
            user_info: None,
        };
        let result = broadcaster
            .authenticate_presence("presence-chat", "123.456", &channel_data, None)
            .await
            .unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&result).unwrap();
        assert!(parsed["auth"].is_string());
        assert!(parsed["channel_data"].is_string());
    }

    #[test]
    fn test_native_broadcaster_name() {
        let registry = SubscriberRegistry::new();
        let broadcaster = NativeBroadcaster::new("my-native", registry);
        assert_eq!(broadcaster.name(), "my-native");
    }

    #[tokio::test]
    async fn test_registry_default_is_empty() {
        let registry = SubscriberRegistry::default();
        assert_eq!(registry.channel_count().await, 0);
        assert_eq!(registry.subscriber_count().await, 0);
    }

    #[tokio::test]
    async fn test_registry_multiple_channels() {
        let registry = SubscriberRegistry::new();
        let (tx, _rx) = mpsc::unbounded_channel();

        registry.subscribe("chat", tx.clone()).await;
        registry.subscribe("alerts", tx.clone()).await;
        registry.subscribe("system", tx.clone()).await;

        assert_eq!(registry.channel_count().await, 3);
        assert_eq!(registry.subscriber_count().await, 3);
    }
}