skyzen 0.1.1

A fast, ergonomic HTTP framework that works everywhere
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! WebSocket types for Durable Object hibernation.

#[cfg(all(feature = "ws", target_arch = "wasm32"))]
use std::sync::Arc;

#[cfg(all(feature = "ws", target_arch = "wasm32"))]
use http_kit::error::BoxHttpError;
#[cfg(all(feature = "ws", target_arch = "wasm32"))]
use http_kit::http_error;
use http_kit::ws::WebSocketMessage;
use serde::{de::DeserializeOwned, Serialize};
#[cfg(all(feature = "ws", target_arch = "wasm32"))]
use skyzen_core::Responder;
#[cfg(all(feature = "ws", target_arch = "wasm32"))]
use wasm_bindgen::JsCast;

use super::error::DurableObjectError;

/// A hibernation WebSocket event delivered to [`DurableObject::websocket`].
///
/// [`DurableObject::websocket`]: super::DurableObject::websocket
#[derive(Debug)]
pub enum WebSocketEvent {
    /// A message was received from the client.
    Message(WebSocketMessage),
    /// The connection was closed by the client.
    Close {
        /// The close code.
        code: u16,
        /// The close reason.
        reason: String,
        /// Whether the close was clean.
        was_clean: bool,
    },
    /// An error occurred on the connection.
    Error(String),
}

/// Opaque handle to a hibernating WebSocket connection.
///
/// This wraps the platform-specific WebSocket object and provides
/// methods to send messages, manage tags, and handle attachments.
///
/// The inner representation is platform-specific and set by the runtime glue.
pub struct WebSocketConnection {
    /// Platform-specific inner handle, set by the cloudflare glue layer.
    inner: Box<dyn WebSocketConnectionInner>,
}

impl std::fmt::Debug for WebSocketConnection {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WebSocketConnection")
            .finish_non_exhaustive()
    }
}

/// Platform-specific WebSocket connection operations.
///
/// Implemented by the cloudflare glue layer (or test mocks).
pub trait WebSocketConnectionInner: Send + Sync {
    /// Send a text message.
    ///
    /// # Errors
    ///
    /// Returns [`DurableObjectError`] when the runtime send fails.
    fn send_text(&self, text: &str) -> Result<(), DurableObjectError>;
    /// Send binary data.
    ///
    /// # Errors
    ///
    /// Returns [`DurableObjectError`] when the runtime send fails.
    fn send_binary(&self, data: &[u8]) -> Result<(), DurableObjectError>;
    /// Close the connection.
    ///
    /// # Errors
    ///
    /// Returns [`DurableObjectError`] when the runtime close fails.
    fn close(&self, code: u16, reason: &str) -> Result<(), DurableObjectError>;
    /// Get tags for this connection.
    ///
    /// # Errors
    ///
    /// Returns [`DurableObjectError`] when tag lookup fails.
    fn tags(&self) -> Result<Vec<String>, DurableObjectError>;
    /// Get the typed attachment.
    ///
    /// # Errors
    ///
    /// Returns [`DurableObjectError`] when attachment retrieval fails.
    fn get_attachment_raw(&self) -> Result<Option<Vec<u8>>, DurableObjectError>;
    /// Set the typed attachment.
    ///
    /// # Errors
    ///
    /// Returns [`DurableObjectError`] when attachment persistence fails.
    fn set_attachment_raw(&self, data: &[u8]) -> Result<(), DurableObjectError>;
}

impl WebSocketConnection {
    /// Create from a platform-specific inner handle.
    #[must_use]
    pub fn new(inner: Box<dyn WebSocketConnectionInner>) -> Self {
        Self { inner }
    }

    /// Send a text message to this connection.
    ///
    /// # Errors
    ///
    /// Returns [`DurableObjectError`] if the send fails.
    pub fn send_text(&self, text: &str) -> Result<(), DurableObjectError> {
        self.inner.send_text(text)
    }

    /// Send binary data to this connection.
    ///
    /// # Errors
    ///
    /// Returns [`DurableObjectError`] if the send fails.
    pub fn send_binary(&self, data: &[u8]) -> Result<(), DurableObjectError> {
        self.inner.send_binary(data)
    }

    /// Serialize a value to JSON and send it as text.
    ///
    /// # Errors
    ///
    /// Returns [`DurableObjectError`] if serialization or send fails.
    pub fn send_json<T: Serialize>(&self, value: &T) -> Result<(), DurableObjectError> {
        let json = serde_json::to_string(value)
            .map_err(|e| DurableObjectError::Serialization(e.to_string()))?;
        self.send_text(&json)
    }

    /// Close this connection with a code and reason.
    ///
    /// # Errors
    ///
    /// Returns [`DurableObjectError`] if the close fails.
    pub fn close(&self, code: u16, reason: &str) -> Result<(), DurableObjectError> {
        self.inner.close(code, reason)
    }

    /// Get the tags associated with this connection.
    ///
    /// # Errors
    ///
    /// Returns [`DurableObjectError`] if the operation fails.
    pub fn tags(&self) -> Result<Vec<String>, DurableObjectError> {
        self.inner.tags()
    }

    /// Get the per-connection typed attachment (survives hibernation).
    ///
    /// # Errors
    ///
    /// Returns [`DurableObjectError`] if deserialization fails.
    pub fn attachment<T: DeserializeOwned>(&self) -> Result<Option<T>, DurableObjectError> {
        self.inner
            .get_attachment_raw()?
            .map(|bytes| {
                serde_json::from_slice(&bytes)
                    .map_err(|e| DurableObjectError::Serialization(e.to_string()))
            })
            .transpose()
    }

    /// Set the per-connection typed attachment (survives hibernation).
    ///
    /// # Errors
    ///
    /// Returns [`DurableObjectError`] if serialization fails.
    pub fn set_attachment<T: Serialize>(&self, value: &T) -> Result<(), DurableObjectError> {
        let bytes = serde_json::to_vec(value)
            .map_err(|e| DurableObjectError::Serialization(e.to_string()))?;
        self.inner.set_attachment_raw(&bytes)
    }
}

/// Responder that accepts a WebSocket for Hibernation.
///
/// Construct this in a handler, add tags, and return it.
/// During `respond_to()`, the runtime reads `DurableObjectState` from
/// request extensions to call `acceptWebSocket(ws, tags)`.
#[derive(Debug, Default)]
pub struct HibernationWebSocketUpgrade {
    tags: Vec<String>,
}

impl HibernationWebSocketUpgrade {
    /// Create a new hibernation WebSocket upgrade.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a tag to categorize this connection.
    #[must_use]
    pub fn tag(mut self, tag: impl Into<String>) -> Self {
        self.tags.push(tag.into());
        self
    }

    /// Get the tags that will be applied to this connection.
    #[must_use]
    pub fn tags(&self) -> &[String] {
        &self.tags
    }

    /// Consume and return the tags.
    #[must_use]
    pub fn into_tags(self) -> Vec<String> {
        self.tags
    }
}

#[cfg(all(feature = "ws", target_arch = "wasm32"))]
type DurableObjectWebSocketAcceptFn =
    dyn Fn(&web_sys::WebSocket, &[String]) -> Result<(), DurableObjectError> + Send + Sync;

/// Wrapper for client websocket stored in response extensions.
#[cfg(all(feature = "ws", target_arch = "wasm32"))]
#[derive(Clone)]
pub struct DurableClientWebSocket(pub web_sys::WebSocket);

#[cfg(all(feature = "ws", target_arch = "wasm32"))]
impl std::fmt::Debug for DurableClientWebSocket {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DurableClientWebSocket")
            .finish_non_exhaustive()
    }
}

// SAFETY: WebAssembly workers are single-threaded; JS handles are safe to mark Send/Sync.
#[cfg(all(feature = "ws", target_arch = "wasm32"))]
unsafe impl Send for DurableClientWebSocket {}
#[cfg(all(feature = "ws", target_arch = "wasm32"))]
unsafe impl Sync for DurableClientWebSocket {}

/// Internal extension value injected by Durable Object runtime glue.
///
/// `HibernationWebSocketUpgrade` reads this from request extensions and
/// calls `acceptWebSocket(server, tags)` through it.
#[cfg(all(feature = "ws", target_arch = "wasm32"))]
#[derive(Clone)]
pub struct DurableObjectState {
    accept_websocket: Arc<DurableObjectWebSocketAcceptFn>,
}

#[cfg(all(feature = "ws", target_arch = "wasm32"))]
impl std::fmt::Debug for DurableObjectState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DurableObjectState").finish_non_exhaustive()
    }
}

#[cfg(all(feature = "ws", target_arch = "wasm32"))]
impl DurableObjectState {
    /// Create a state adapter used by `HibernationWebSocketUpgrade`.
    #[must_use]
    pub fn new(
        accept_websocket: impl Fn(&web_sys::WebSocket, &[String]) -> Result<(), DurableObjectError>
            + Send
            + Sync
            + 'static,
    ) -> Self {
        Self {
            accept_websocket: Arc::new(accept_websocket),
        }
    }

    /// Accept a server websocket with optional tags.
    ///
    /// # Errors
    ///
    /// Returns [`DurableObjectError`] when runtime websocket acceptance fails.
    pub fn accept_websocket(
        &self,
        websocket: &web_sys::WebSocket,
        tags: &[String],
    ) -> Result<(), DurableObjectError> {
        (self.accept_websocket)(websocket, tags)
    }
}

#[cfg(all(feature = "ws", target_arch = "wasm32"))]
http_error!(
    HibernationDurableStateMissing,
    http_kit::StatusCode::INTERNAL_SERVER_ERROR,
    "DurableObjectState missing in request extensions for hibernation websocket upgrade."
);

#[cfg(all(feature = "ws", target_arch = "wasm32"))]
http_error!(
    HibernationWebSocketAcceptFailed,
    http_kit::StatusCode::INTERNAL_SERVER_ERROR,
    "Failed to accept hibernation websocket connection."
);

#[cfg(all(feature = "ws", target_arch = "wasm32"))]
impl Responder for HibernationWebSocketUpgrade {
    type Error = BoxHttpError;

    fn respond_to(
        self,
        request: &crate::Request,
        response: &mut crate::Response,
    ) -> Result<(), Self::Error> {
        let durable_state = request
            .extensions()
            .get::<DurableObjectState>()
            .cloned()
            .ok_or_else(|| Box::new(HibernationDurableStateMissing::new()) as BoxHttpError)?;

        let pair = crate::websocket::ffi::WebSocketPair::new();
        let client = pair.client();
        let server = pair.server();
        let server_socket: web_sys::WebSocket = server.unchecked_into();

        durable_state
            .accept_websocket(&server_socket, self.tags())
            .map_err(|error| {
                tracing::error!(%error, "failed to accept hibernation websocket");
                Box::new(HibernationWebSocketAcceptFailed::new()) as BoxHttpError
            })?;

        *response.status_mut() = http_kit::StatusCode::SWITCHING_PROTOCOLS;
        let client_socket: web_sys::WebSocket = client.unchecked_into();
        response
            .extensions_mut()
            .insert(DurableClientWebSocket(client_socket));

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use std::sync::{Arc, Mutex};

    use serde::{Deserialize, Serialize};

    use super::{
        DurableObjectError, HibernationWebSocketUpgrade, WebSocketConnection,
        WebSocketConnectionInner,
    };

    #[derive(Debug, Default)]
    struct MockSocketState {
        text_messages: Vec<String>,
        binary_messages: Vec<Vec<u8>>,
        close_calls: Vec<(u16, String)>,
        tags: Vec<String>,
        attachment: Option<Vec<u8>>,
        fail_text: Option<DurableObjectError>,
        fail_binary: Option<DurableObjectError>,
        fail_close: Option<DurableObjectError>,
        fail_tags: Option<DurableObjectError>,
        fail_get_attachment: Option<DurableObjectError>,
        fail_set_attachment: Option<DurableObjectError>,
    }

    #[derive(Debug, Clone)]
    struct MockSocketInner {
        state: Arc<Mutex<MockSocketState>>,
    }

    impl MockSocketInner {
        fn error_clone(error: &DurableObjectError) -> DurableObjectError {
            match error {
                DurableObjectError::Runtime(message) => {
                    DurableObjectError::Runtime(message.clone())
                }
                DurableObjectError::Serialization(message) => {
                    DurableObjectError::Serialization(message.clone())
                }
                DurableObjectError::WebSocket(message) => {
                    DurableObjectError::WebSocket(message.clone())
                }
            }
        }
    }

    impl WebSocketConnectionInner for MockSocketInner {
        fn send_text(&self, text: &str) -> Result<(), DurableObjectError> {
            {
                let mut state = self.state.lock().unwrap();
                if let Some(error) = &state.fail_text {
                    return Err(Self::error_clone(error));
                }
                state.text_messages.push(text.to_owned());
            }
            Ok(())
        }

        fn send_binary(&self, data: &[u8]) -> Result<(), DurableObjectError> {
            {
                let mut state = self.state.lock().unwrap();
                if let Some(error) = &state.fail_binary {
                    return Err(Self::error_clone(error));
                }
                state.binary_messages.push(data.to_vec());
            }
            Ok(())
        }

        fn close(&self, code: u16, reason: &str) -> Result<(), DurableObjectError> {
            {
                let mut state = self.state.lock().unwrap();
                if let Some(error) = &state.fail_close {
                    return Err(Self::error_clone(error));
                }
                state.close_calls.push((code, reason.to_owned()));
            }
            Ok(())
        }

        fn tags(&self) -> Result<Vec<String>, DurableObjectError> {
            let state = self.state.lock().unwrap();
            if let Some(error) = &state.fail_tags {
                return Err(Self::error_clone(error));
            }
            Ok(state.tags.clone())
        }

        fn get_attachment_raw(&self) -> Result<Option<Vec<u8>>, DurableObjectError> {
            let state = self.state.lock().unwrap();
            if let Some(error) = &state.fail_get_attachment {
                return Err(Self::error_clone(error));
            }
            Ok(state.attachment.clone())
        }

        fn set_attachment_raw(&self, data: &[u8]) -> Result<(), DurableObjectError> {
            {
                let mut state = self.state.lock().unwrap();
                if let Some(error) = &state.fail_set_attachment {
                    return Err(Self::error_clone(error));
                }
                state.attachment = Some(data.to_vec());
            }
            Ok(())
        }
    }

    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct Attachment {
        room: String,
        revision: u32,
    }

    #[derive(Debug)]
    struct AlwaysFails;

    impl Serialize for AlwaysFails {
        fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
        where
            S: serde::Serializer,
        {
            Err(serde::ser::Error::custom("expected serialization failure"))
        }
    }

    fn connection_with_state(state: Arc<Mutex<MockSocketState>>) -> WebSocketConnection {
        WebSocketConnection::new(Box::new(MockSocketInner { state }))
    }

    #[test]
    fn send_json_serializes_payload_as_text_message() {
        let state = Arc::new(Mutex::new(MockSocketState::default()));
        let connection = connection_with_state(Arc::clone(&state));

        connection
            .send_json(&Attachment {
                room: "room-1".to_owned(),
                revision: 7,
            })
            .unwrap();

        assert_eq!(
            state.lock().unwrap().text_messages,
            vec![r#"{"room":"room-1","revision":7}"#.to_owned()]
        );
    }

    #[test]
    fn send_json_rejects_non_finite_numbers() {
        let connection = connection_with_state(Arc::new(Mutex::new(MockSocketState::default())));

        let error = connection.send_json(&AlwaysFails).unwrap_err();

        assert!(matches!(error, DurableObjectError::Serialization(_)));
    }

    #[test]
    fn attachment_round_trips_through_raw_storage() {
        let state = Arc::new(Mutex::new(MockSocketState::default()));
        let connection = connection_with_state(Arc::clone(&state));
        let attachment = Attachment {
            room: "general".to_owned(),
            revision: 3,
        };

        connection.set_attachment(&attachment).unwrap();
        let restored = connection.attachment::<Attachment>().unwrap().unwrap();

        assert_eq!(restored, attachment);
        assert!(state.lock().unwrap().attachment.is_some());
    }

    #[test]
    fn attachment_reports_deserialization_errors_for_invalid_bytes() {
        let state = Arc::new(Mutex::new(MockSocketState {
            attachment: Some(b"{".to_vec()),
            ..MockSocketState::default()
        }));
        let connection = connection_with_state(state);

        let error = connection.attachment::<Attachment>().unwrap_err();

        assert!(matches!(error, DurableObjectError::Serialization(_)));
    }

    #[test]
    fn close_and_tags_delegate_to_inner_handle() {
        let state = Arc::new(Mutex::new(MockSocketState {
            tags: vec!["room-1".to_owned(), "admin".to_owned()],
            ..MockSocketState::default()
        }));
        let connection = connection_with_state(Arc::clone(&state));

        assert_eq!(
            connection.tags().unwrap(),
            vec!["room-1".to_owned(), "admin".to_owned()]
        );
        connection.close(1000, "done").unwrap();

        assert_eq!(
            state.lock().unwrap().close_calls,
            vec![(1000, "done".to_owned())]
        );
    }

    #[test]
    fn hibernation_upgrade_preserves_tag_order() {
        let tags = HibernationWebSocketUpgrade::new()
            .tag("room-1")
            .tag("presence")
            .into_tags();

        assert_eq!(tags, vec!["room-1".to_owned(), "presence".to_owned()]);
    }
}