dioxus-fullstack-core 0.7.7

Hooks for serializing futures, values in dioxus-fullstack and other utilities
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
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
#![warn(missing_docs)]
#![doc = include_str!("../README.md")]

use base64::Engine;
use dioxus_core::CapturedError;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{cell::RefCell, io::Cursor, rc::Rc};

#[cfg(feature = "web")]
thread_local! {
    static CONTEXT: RefCell<Option<HydrationContext>> = const { RefCell::new(None) };
}

/// Data shared between the frontend and the backend for hydration
/// of server functions.
#[derive(Default, Clone)]
pub struct HydrationContext {
    #[cfg(feature = "web")]
    /// Is resolving suspense done on the client
    suspense_finished: bool,
    data: Rc<RefCell<HTMLData>>,
}

impl HydrationContext {
    /// Create a new serialize context from the serialized data
    pub fn from_serialized(
        data: &[u8],
        debug_types: Option<Vec<String>>,
        debug_locations: Option<Vec<String>>,
    ) -> Self {
        Self {
            #[cfg(feature = "web")]
            suspense_finished: false,
            data: Rc::new(RefCell::new(HTMLData::from_serialized(
                data,
                debug_types,
                debug_locations,
            ))),
        }
    }

    /// Serialize the data in the context to be sent to the client
    pub fn serialized(&self) -> SerializedHydrationData {
        self.data.borrow().serialized()
    }

    /// Create a new entry in the data that will be sent to the client without inserting any data. Returns an id that can be used to insert data into the entry once it is ready.
    pub fn create_entry<T>(&self) -> SerializeContextEntry<T> {
        let entry_index = self.data.borrow_mut().create_entry();

        SerializeContextEntry {
            index: entry_index,
            context: self.clone(),
            phantom: std::marker::PhantomData,
        }
    }

    /// Get the entry for the error in the suspense boundary
    pub fn error_entry(&self) -> SerializeContextEntry<Option<CapturedError>> {
        // The first entry is reserved for the error
        let entry_index = self.data.borrow_mut().create_entry_with_id(0);

        SerializeContextEntry {
            index: entry_index,
            context: self.clone(),
            phantom: std::marker::PhantomData,
        }
    }

    /// Extend this data with the data from another [`HydrationContext`]
    pub fn extend(&self, other: &Self) {
        self.data.borrow_mut().extend(&other.data.borrow());
    }

    #[cfg(feature = "web")]
    /// Run a closure inside of this context
    pub fn in_context<T>(&self, f: impl FnOnce() -> T) -> T {
        CONTEXT.with(|context| {
            let old = context.borrow().clone();
            *context.borrow_mut() = Some(self.clone());
            let result = f();
            *context.borrow_mut() = old;
            result
        })
    }

    pub(crate) fn insert<T: Transportable<M>, M: 'static>(
        &self,
        id: usize,
        value: &T,
        location: &'static std::panic::Location<'static>,
    ) {
        self.data.borrow_mut().insert(id, value, location);
    }

    pub(crate) fn get<T: Transportable<M>, M: 'static>(
        &self,
        id: usize,
    ) -> Result<T, TakeDataError> {
        // If suspense is finished on the client, we can assume that the data is available
        #[cfg(feature = "web")]
        if self.suspense_finished {
            return Err(TakeDataError::DataNotAvailable);
        }
        self.data.borrow().get(id)
    }
}

/// An entry into the serialized context. The order entries are created in must be consistent
/// between the server and the client.
pub struct SerializeContextEntry<T> {
    /// The index this context will be inserted into inside the serialize context
    index: usize,
    /// The context this entry is associated with
    context: HydrationContext,
    phantom: std::marker::PhantomData<T>,
}

impl<T> Clone for SerializeContextEntry<T> {
    fn clone(&self) -> Self {
        Self {
            index: self.index,
            context: self.context.clone(),
            phantom: std::marker::PhantomData,
        }
    }
}

impl<T> SerializeContextEntry<T> {
    /// Insert data into an entry that was created with [`HydrationContext::create_entry`]
    pub fn insert<M: 'static>(self, value: &T, location: &'static std::panic::Location<'static>)
    where
        T: Transportable<M>,
    {
        self.context.insert(self.index, value, location);
    }

    /// Grab the data from the serialize context
    pub fn get<M: 'static>(&self) -> Result<T, TakeDataError>
    where
        T: Transportable<M>,
    {
        self.context.get(self.index)
    }
}

/// Check if the client is currently rendering a component for hydration. Always returns true on the server.
pub fn is_hydrating() -> bool {
    #[cfg(feature = "web")]
    {
        // On the client, we can check if the context is set
        CONTEXT.with(|context| context.borrow().is_some())
    }
    #[cfg(not(feature = "web"))]
    {
        true
    }
}

/// Get or insert the current serialize context. On the client, the hydration context this returns
/// will always return `TakeDataError::DataNotAvailable` if hydration of the current chunk is finished.
pub fn serialize_context() -> HydrationContext {
    #[cfg(feature = "web")]
    // On the client, the hydration logic provides the context in a global
    if let Some(current_context) = CONTEXT.with(|context| context.borrow().clone()) {
        current_context
    } else {
        // If the context is not set, then suspense is not active
        HydrationContext {
            suspense_finished: true,
            ..Default::default()
        }
    }
    #[cfg(not(feature = "web"))]
    {
        // On the server each scope creates the context lazily
        dioxus_core::has_context()
            .unwrap_or_else(|| dioxus_core::provide_context(HydrationContext::default()))
    }
}

pub(crate) struct HTMLData {
    /// The position of the cursor in the data. This is only used on the client
    pub(crate) cursor: usize,
    /// The data required for hydration
    pub data: Vec<Option<Vec<u8>>>,
    /// The types of each serialized data
    ///
    /// NOTE: we don't store this in the main data vec because we don't want to include it in
    /// release mode and we can't assume both the client and server are built with debug assertions
    /// matching
    #[cfg(debug_assertions)]
    pub debug_types: Vec<Option<String>>,
    /// The locations of each serialized data
    #[cfg(debug_assertions)]
    pub debug_locations: Vec<Option<String>>,
}

impl Default for HTMLData {
    fn default() -> Self {
        Self {
            cursor: 1,
            data: Vec::new(),
            #[cfg(debug_assertions)]
            debug_types: Vec::new(),
            #[cfg(debug_assertions)]
            debug_locations: Vec::new(),
        }
    }
}

impl HTMLData {
    #[allow(unused)]
    fn from_serialized(
        data: &[u8],
        debug_types: Option<Vec<String>>,
        debug_locations: Option<Vec<String>>,
    ) -> Self {
        let data = ciborium::from_reader(Cursor::new(data)).unwrap();
        Self {
            cursor: 1,
            data,
            #[cfg(debug_assertions)]
            debug_types: debug_types
                .unwrap_or_default()
                .into_iter()
                .map(Some)
                .collect(),
            #[cfg(debug_assertions)]
            debug_locations: debug_locations
                .unwrap_or_default()
                .into_iter()
                .map(Some)
                .collect(),
        }
    }

    /// Create a new entry in the data that will be sent to the client without inserting any data. Returns an id that can be used to insert data into the entry once it is ready.
    fn create_entry(&mut self) -> usize {
        let id = self.cursor;
        self.cursor += 1;
        self.create_entry_with_id(id)
    }

    fn create_entry_with_id(&mut self, id: usize) -> usize {
        while id + 1 > self.data.len() {
            self.data.push(None);
            #[cfg(debug_assertions)]
            {
                self.debug_types.push(None);
                self.debug_locations.push(None);
            }
        }
        id
    }

    /// Insert data into an entry that was created with [`Self::create_entry`]
    fn insert<T: Transportable<M>, M: 'static>(
        &mut self,
        id: usize,
        value: &T,
        #[allow(unused)] location: &'static std::panic::Location<'static>,
    ) {
        let serialized = value.transport_to_bytes();
        self.data[id] = Some(serialized);
        #[cfg(debug_assertions)]
        {
            self.debug_types[id] = Some(std::any::type_name::<T>().to_string());
            self.debug_locations[id] = Some(location.to_string());
        }
    }

    /// Get the data from the serialize context
    fn get<T: Transportable<M>, M: 'static>(&self, index: usize) -> Result<T, TakeDataError> {
        if index >= self.data.len() {
            tracing::trace!(
                "Tried to take more data than was available, len: {}, index: {}; This is normal if the server function was started on the client, but may indicate a bug if the server function result should be deserialized from the server",
                self.data.len(),
                index
            );
            return Err(TakeDataError::DataNotAvailable);
        }
        let bytes = self.data[index].as_ref();
        match bytes {
            Some(bytes) => match T::transport_from_bytes(bytes) {
                Ok(x) => Ok(x),
                Err(err) => {
                    #[cfg(debug_assertions)]
                    {
                        let debug_type = self.debug_types.get(index);
                        let debug_locations = self.debug_locations.get(index);

                        if let (Some(Some(debug_type)), Some(Some(debug_locations))) =
                            (debug_type, debug_locations)
                        {
                            let client_type = std::any::type_name::<T>();
                            let client_location = std::panic::Location::caller();
                            // We we have debug types and a location, we can provide a more helpful error message
                            tracing::error!(
                                "Error deserializing data: {err:?}\n\nThis type was serialized on the server at {debug_locations} with the type name {debug_type}. The client failed to deserialize the type {client_type} at {client_location}.",
                            );
                            return Err(TakeDataError::DeserializationError(err));
                        }
                    }
                    // Otherwise, just log the generic deserialization error
                    tracing::error!("Error deserializing data: {:?}", err);
                    Err(TakeDataError::DeserializationError(err))
                }
            },
            None => Err(TakeDataError::DataPending),
        }
    }

    /// Extend this data with the data from another [`HTMLData`]
    pub(crate) fn extend(&mut self, other: &Self) {
        // Make sure this vectors error entry exists even if it is empty
        if self.data.is_empty() {
            self.data.push(None);
            #[cfg(debug_assertions)]
            {
                self.debug_types.push(None);
                self.debug_locations.push(None);
            }
        }

        let mut other_data_iter = other.data.iter().cloned();
        #[cfg(debug_assertions)]
        let mut other_debug_types_iter = other.debug_types.iter().cloned();
        #[cfg(debug_assertions)]
        let mut other_debug_locations_iter = other.debug_locations.iter().cloned();

        // Merge the error entry from the other context
        if let Some(Some(other_error)) = other_data_iter.next() {
            self.data[0] = Some(other_error.clone());
            #[cfg(debug_assertions)]
            {
                self.debug_types[0] = other_debug_types_iter.next().unwrap_or(None);
                self.debug_locations[0] = other_debug_locations_iter.next().unwrap_or(None);
            }
        }

        // Don't copy the error from the other context
        self.data.extend(other_data_iter);
        #[cfg(debug_assertions)]
        {
            self.debug_types.extend(other_debug_types_iter);
            self.debug_locations.extend(other_debug_locations_iter);
        }
    }

    /// Encode data as base64. This is intended to be used in the server to send data to the client.
    pub(crate) fn serialized(&self) -> SerializedHydrationData {
        let mut serialized = Vec::new();
        ciborium::into_writer(&self.data, &mut serialized).unwrap();

        let data = base64::engine::general_purpose::STANDARD.encode(serialized);

        #[cfg(debug_assertions)]
        let format_js_list_of_strings = |list: &[Option<String>]| {
            let body = list
                .iter()
                .map(|s| match s {
                    Some(s) => {
                        // Escape backslashes, quotes, and newlines
                        let escaped = s
                            .replace(r#"\"#, r#"\\"#)
                            .replace("\n", r#"\n"#)
                            .replace(r#"""#, r#"\""#);

                        format!(r#""{escaped}""#)
                    }
                    None => r#""unknown""#.to_string(),
                })
                .collect::<Vec<_>>()
                .join(",");
            format!("[{}]", body)
        };

        SerializedHydrationData {
            data,
            #[cfg(debug_assertions)]
            debug_types: format_js_list_of_strings(&self.debug_types),
            #[cfg(debug_assertions)]
            debug_locations: format_js_list_of_strings(&self.debug_locations),
        }
    }
}

/// Data that was serialized on the server for hydration on the client. This includes
/// extra information about the types and sources of the serialized data in debug mode
pub struct SerializedHydrationData {
    /// The base64 encoded serialized data
    pub data: String,
    /// A list of the types of each serialized data
    #[cfg(debug_assertions)]
    pub debug_types: String,
    /// A list of the locations of each serialized data
    #[cfg(debug_assertions)]
    pub debug_locations: String,
}

/// An error that can occur when trying to take data from the server
#[derive(Debug)]
pub enum TakeDataError {
    /// Deserializing the data failed
    DeserializationError(ciborium::de::Error<std::io::Error>),
    /// No data was available
    DataNotAvailable,
    /// The server serialized a placeholder for the data, but it isn't available yet
    DataPending,
}

impl std::fmt::Display for TakeDataError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::DeserializationError(e) => write!(f, "DeserializationError: {}", e),
            Self::DataNotAvailable => write!(f, "DataNotAvailable"),
            Self::DataPending => write!(f, "DataPending"),
        }
    }
}

impl std::error::Error for TakeDataError {}

/// Create a new entry in the serialize context for the head element hydration
pub fn head_element_hydration_entry() -> SerializeContextEntry<bool> {
    serialize_context().create_entry()
}

/// A `Transportable` type can be safely transported from the server to the client, and be used for
/// hydration. Not all types can sensibly be transported, but many can. This trait makes it possible
/// to customize how types are transported which helps for non-serializable types like `dioxus_core::CapturedError`.
///
/// By default, all types that implement `Serialize` and `DeserializeOwned` are transportable.
///
/// You can also implement `Transportable` for `Result<T, dioxus_core::CapturedError>` where `T` is
/// `Serialize` and `DeserializeOwned` to allow transporting results that may contain errors.
///
/// Note that transporting a `Result<T, dioxus_core::CapturedError>` will lose various aspects of the original
/// `dioxus_core::CapturedError` such as backtraces and source errors, but will preserve the error message.
pub trait Transportable<M = ()>: 'static {
    /// Serialize the type to a byte vector for transport
    fn transport_to_bytes(&self) -> Vec<u8>;

    /// Deserialize the type from a byte slice
    fn transport_from_bytes(bytes: &[u8]) -> Result<Self, ciborium::de::Error<std::io::Error>>
    where
        Self: Sized;
}

impl<T> Transportable<()> for T
where
    T: Serialize + DeserializeOwned + 'static,
{
    fn transport_to_bytes(&self) -> Vec<u8> {
        let mut serialized = Vec::new();
        ciborium::into_writer(self, &mut serialized).unwrap();
        serialized
    }

    fn transport_from_bytes(bytes: &[u8]) -> Result<Self, ciborium::de::Error<std::io::Error>>
    where
        Self: Sized,
    {
        ciborium::from_reader(Cursor::new(bytes))
    }
}

#[derive(Serialize, Deserialize)]
struct TransportResultErr<T> {
    error: Result<T, CapturedError>,
}

#[doc(hidden)]
pub struct TransportViaErrMarker;

impl<T> Transportable<TransportViaErrMarker> for Result<T, anyhow::Error>
where
    T: Serialize + DeserializeOwned + 'static,
{
    fn transport_to_bytes(&self) -> Vec<u8> {
        let err = TransportResultErr {
            error: self
                .as_ref()
                .map_err(|e| CapturedError::from_display(e.to_string())),
        };

        let mut serialized = Vec::new();
        ciborium::into_writer(&err, &mut serialized).unwrap();
        serialized
    }

    fn transport_from_bytes(bytes: &[u8]) -> Result<Self, ciborium::de::Error<std::io::Error>>
    where
        Self: Sized,
    {
        let err: TransportResultErr<T> = ciborium::from_reader(Cursor::new(bytes))?;
        match err.error {
            Ok(value) => Ok(Ok(value)),
            Err(captured) => Ok(Err(anyhow::Error::msg(captured.to_string()))),
        }
    }
}

#[doc(hidden)]
pub struct TransportCapturedError;
#[derive(Serialize, Deserialize)]
struct TransportError {
    error: String,
}

impl Transportable<TransportCapturedError> for CapturedError {
    fn transport_to_bytes(&self) -> Vec<u8> {
        let err = TransportError {
            error: self.to_string(),
        };

        let mut serialized = Vec::new();
        ciborium::into_writer(&err, &mut serialized).unwrap();
        serialized
    }

    fn transport_from_bytes(bytes: &[u8]) -> Result<Self, ciborium::de::Error<std::io::Error>>
    where
        Self: Sized,
    {
        let err: TransportError = ciborium::from_reader(Cursor::new(bytes))?;
        Ok(dioxus_core::CapturedError::msg::<String>(err.error))
    }
}