krab_core 0.2.0

Shared runtime for the Krab framework: config, HTTP, database, telemetry, resilience, signals, and protocol governance
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
531
532
533
534
535
536
537
538
539
use std::rc::Rc;
use std::sync::atomic::{AtomicU64, Ordering};

pub mod action;
pub mod config;
pub mod control_flow;
#[cfg(feature = "auth")]
pub mod credentials;
// Compiled unconditionally: the CSRF wire-contract names are shared between
// the `rest` server half and the `web` wasm client half, which never build
// together, so the constants cannot live behind either feature gate.
pub mod csrf;
pub mod error_boundary;
#[cfg(feature = "graphql")]
pub mod graphql;
#[cfg(feature = "grpc-semantics")]
pub mod grpc_semantics;
pub mod resource;

/// Deprecated alias for [`grpc_semantics`].
///
/// The name promised a transport this crate does not have. Kept for one minor
/// version per the breaking-change policy in `RELEASE_POLICY.md`; deprecated in
/// `0.2.0`, so removable no earlier than `0.3.0`. See ADR 0007.
#[cfg(feature = "grpc-semantics")]
#[deprecated(
    since = "0.2.0",
    note = "renamed to `grpc_semantics`: this module provides gRPC status-code and \
            timeout-header semantics for a gateway, not a gRPC transport. \
            Update the feature name too: `grpc` -> `grpc-semantics`."
)]
pub use grpc_semantics as grpc;
pub mod head;
#[cfg(feature = "rest")]
pub mod http_error;
pub mod i18n;
pub mod image;
// Server-side: ISR caches rendered pages, and now does so through
// `store::DistributedStore`, which needs `tokio`. Neither is meaningful in a
// browser bundle.
#[cfg(not(target_arch = "wasm32"))]
pub mod isr;
pub mod layout;
pub mod protocol;
pub mod render_policy;
pub mod render_stream;
pub mod resilience;
pub mod service_contract;
pub mod signal;
pub mod style_scope;

#[cfg(not(target_arch = "wasm32"))]
pub mod ws;

// Available to both halves of a server function, not just the server half.
//
// This was `#[cfg(feature = "rest")]`, which gated out the entire module for a
// browser build — including `call_server_fn`, which is what the `#[server]`
// macro's wasm32 stub calls. The module's own internals already branch on
// `not(feature = "rest")` and `target_arch = "wasm32"`, so it was written to
// compile without `rest`; the module declaration made that code unreachable and
// the client half of `#[server]` could never build.
#[cfg(any(feature = "rest", feature = "web"))]
pub mod server_fn;

#[cfg(not(target_arch = "wasm32"))]
pub mod telemetry;

fn escape_html_attr(s: &str) -> String {
    s.replace('&', "&")
        .replace('"', """)
        .replace('<', "&lt;")
        .replace('>', "&gt;")
}

fn escape_html_text(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
}

#[cfg(not(target_arch = "wasm32"))]
pub mod service;

// Driver selection is available under either driver; the Postgres runtime
// inside is gated on `db-postgres`.
#[cfg(any(feature = "db-postgres", feature = "db-sqlite"))]
pub mod db;

// `UserRepository` is the port applications implement per driver, so it is
// not Postgres-specific.
#[cfg(any(feature = "db-postgres", feature = "db-sqlite"))]
pub mod repository;

#[cfg(feature = "rest")]
pub mod http;
#[cfg(feature = "rest")]
pub mod http_auth;
#[cfg(feature = "rest")]
pub mod http_headers;
#[cfg(feature = "rest")]
mod http_observability;
#[cfg(feature = "rest")]
mod http_protocol;
#[cfg(feature = "rest")]
pub mod http_runtime;
#[cfg(feature = "rest")]
pub mod http_security;

#[cfg(feature = "rest")]
pub mod static_assets;
// Gated on the target, not on `rest`. A shared key-value store has nothing to
// do with having an HTTP surface, and `isr` — which is not feature-gated —
// depends on it. It needs `tokio`, which is a non-wasm dependency, hence the
// target gate rather than no gate at all.
#[cfg(not(target_arch = "wasm32"))]
pub mod store;

#[cfg(all(feature = "rest", test))]
mod auth_tests;

#[cfg(all(feature = "db-postgres", test))]
mod db_tests;

#[cfg(all(feature = "rest", test))]
mod api_tests;

#[cfg(all(feature = "rest", test))]
mod server_fn_tests;

#[cfg(all(feature = "rest", test))]
mod protocol_tests;

static NEXT_HYDRATION_BOUNDARY_ID: AtomicU64 = AtomicU64::new(1);

pub const HYDRATION_NODE_ID_ATTR: &str = "data-krab-node-id";
const ISLAND_NAME_ATTR: &str = "data-island";

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HydrationNodeMarker {
    pub boundary_id: String,
    pub path: String,
}

impl HydrationNodeMarker {
    pub fn new(boundary_id: impl Into<String>, path: impl Into<String>) -> Self {
        Self {
            boundary_id: boundary_id.into(),
            path: path.into(),
        }
    }

    pub fn root(boundary_id: &str) -> Self {
        Self::new(boundary_id, "0")
    }

    pub fn child(&self, child_index: usize) -> Self {
        Self::new(
            &self.boundary_id,
            child_hydration_path(&self.path, child_index),
        )
    }

    pub fn parse(input: &str) -> Option<Self> {
        let (boundary_id, path) = input.split_once('/')?;
        let boundary_id = boundary_id.trim();
        let path = path.trim();
        if boundary_id.is_empty() || path.is_empty() {
            return None;
        }
        Some(Self::new(boundary_id, path))
    }

    pub fn as_attr_value(&self) -> String {
        format!("{}/{}", self.boundary_id, self.path)
    }
}

pub fn next_hydration_boundary_id(scope: &str) -> String {
    let id = NEXT_HYDRATION_BOUNDARY_ID.fetch_add(1, Ordering::Relaxed);
    format!("{scope}:{id}")
}

pub fn annotate_hydration_tree(node: Node, boundary_id: &str) -> Node {
    annotate_hydration_tree_with_marker(node, &HydrationNodeMarker::root(boundary_id))
}

fn annotate_hydration_tree_with_marker(node: Node, marker: &HydrationNodeMarker) -> Node {
    match node {
        Node::Element(mut element) => {
            stamp_hydration_marker_if_absent(&mut element, || marker.as_attr_value());

            if !element
                .attributes
                .iter()
                .any(|attr| attr.name == ISLAND_NAME_ATTR)
            {
                element.children = annotate_hydration_children(element.children, marker);
            }

            Node::Element(element)
        }
        Node::Fragment(children) => Node::Fragment(annotate_hydration_children(children, marker)),
        Node::Dynamic(factory) => {
            let marker = marker.clone();
            Node::Dynamic(Rc::new(move || {
                annotate_hydration_tree_with_marker(factory(), &marker)
            }))
        }
        Node::Text(text) => Node::Text(text),
    }
}

fn annotate_hydration_children(
    children: Vec<Node>,
    parent_marker: &HydrationNodeMarker,
) -> Vec<Node> {
    children
        .into_iter()
        .enumerate()
        .map(|(index, child)| {
            annotate_hydration_tree_with_marker(child, &parent_marker.child(index))
        })
        .collect()
}

/// The `{parent}.{index}` path scheme shared by hydration annotation and
/// `<For>`'s fragment-row keying (`control_flow::with_key`). One definition, so
/// the two walkers cannot drift apart.
pub(crate) fn child_hydration_path(parent_path: &str, child_index: usize) -> String {
    if parent_path.is_empty() {
        child_index.to_string()
    } else {
        format!("{parent_path}.{child_index}")
    }
}

/// Stamp `value()` onto `element` as [`HYDRATION_NODE_ID_ATTR`] unless the
/// marker is already present — an existing marker always wins. Both
/// `annotate_hydration_tree` and `<For>`'s key stamping go through here, which
/// is what lets a key set by `with_key` survive hydration annotation instead of
/// being overwritten by a positional path (and vice versa).
///
/// `value` is a closure so callers that build the attribute value on demand pay
/// for it only when the stamp is actually applied.
pub(crate) fn stamp_hydration_marker_if_absent(
    element: &mut Element,
    value: impl FnOnce() -> String,
) {
    if !element
        .attributes
        .iter()
        .any(|attr| attr.name == HYDRATION_NODE_ID_ATTR)
    {
        element
            .attributes
            .push(Attribute::new(HYDRATION_NODE_ID_ATTR.to_string(), value()));
    }
}

pub trait Render {
    fn render(&self) -> String;
}

impl Render for String {
    fn render(&self) -> String {
        self.clone()
    }
}

impl Render for &str {
    fn render(&self) -> String {
        self.to_string()
    }
}

impl<T: std::fmt::Display> Render for &T {
    fn render(&self) -> String {
        self.to_string()
    }
}

#[derive(Clone)]
pub enum Node {
    Element(Element),
    Text(String),
    Fragment(Vec<Node>),
    Dynamic(Rc<dyn Fn() -> Node>),
}

// Remove Debug derive from Node because Fn is not Debug
impl std::fmt::Debug for Node {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Node::Element(e) => f.debug_tuple("Element").field(e).finish(),
            Node::Text(t) => f.debug_tuple("Text").field(t).finish(),
            Node::Fragment(nodes) => f.debug_tuple("Fragment").field(nodes).finish(),
            Node::Dynamic(_) => f.debug_tuple("Dynamic").finish(),
        }
    }
}

impl Render for Node {
    fn render(&self) -> String {
        match self {
            Node::Element(el) => el.render(),
            Node::Text(text) => escape_html_text(text),
            Node::Fragment(nodes) => nodes.iter().map(|n| n.render()).collect(),
            Node::Dynamic(f) => f().render(),
        }
    }
}

#[derive(Debug, Clone)]
pub struct Element {
    pub tag: String,
    pub attributes: Vec<Attribute>,
    pub children: Vec<Node>,
    pub events: Vec<EventListener>,
}

#[derive(Clone)]
pub struct EventListener {
    pub name: String,
    #[cfg(feature = "web")]
    pub callback: Rc<dyn Fn(web_sys::Event)>,
    #[cfg(not(feature = "web"))]
    pub callback: (),
}

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

impl Render for Element {
    fn render(&self) -> String {
        let attrs = self
            .attributes
            .iter()
            .map(|a| format!(" {}=\"{}\"", a.name, escape_html_attr(&a.value)))
            .collect::<String>();
        let children = self.children.iter().map(|c| c.render()).collect::<String>();

        // Note: events are not rendered to HTML string

        if self.children.is_empty() {
            match self.tag.as_str() {
                "area" | "base" | "br" | "col" | "embed" | "hr" | "img" | "input" | "link"
                | "meta" | "param" | "source" | "track" | "wbr" => {
                    format!("<{}{}/>", self.tag, attrs)
                }
                _ => format!("<{}{}></{}>", self.tag, attrs, self.tag),
            }
        } else {
            format!("<{}{}>{}</{}>", self.tag, attrs, children, self.tag)
        }
    }
}

#[derive(Debug, Clone)]
pub struct Attribute {
    pub name: String,
    pub value: String,
}

impl Attribute {
    pub fn new(name: String, value: String) -> Self {
        Self { name, value }
    }
}

pub trait IntoNode {
    fn into_node(self) -> Node;
}

impl IntoNode for Node {
    fn into_node(self) -> Node {
        self
    }
}

impl IntoNode for String {
    fn into_node(self) -> Node {
        Node::Text(self)
    }
}

impl IntoNode for &str {
    fn into_node(self) -> Node {
        Node::Text(self.to_string())
    }
}

impl IntoNode for i32 {
    fn into_node(self) -> Node {
        Node::Text(self.to_string())
    }
}

impl IntoNode for &i32 {
    fn into_node(self) -> Node {
        Node::Text(self.to_string())
    }
}

// Generic implementation for Closures?
// impl<F> IntoNode for F where F: Fn() -> Node + 'static { ... }
// This might conflict or requires boxing.
// Since we use Rc<dyn Fn() -> Node>, we can impl it.
impl<F> IntoNode for F
where
    F: Fn() -> Node + 'static,
{
    fn into_node(self) -> Node {
        Node::Dynamic(Rc::new(self))
    }
}

// Also support closures returning things that can be nodes?
// e.g. Fn() -> String.
// Rust doesn't support specialization well, so F: Fn() -> Node is safer.
// If the user returns String from closure, they might need to wrap it.

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

    fn attr_value<'a>(node: &'a Node, name: &str) -> Option<&'a str> {
        let Node::Element(element) = node else {
            return None;
        };

        element
            .attributes
            .iter()
            .find(|attr| attr.name == name)
            .map(|attr| attr.value.as_str())
    }

    #[test]
    fn annotate_hydration_tree_assigns_stable_element_paths() {
        let node = Node::Element(Element {
            tag: "div".to_string(),
            attributes: vec![],
            children: vec![Node::Element(Element {
                tag: "span".to_string(),
                attributes: vec![],
                children: vec![Node::Text("hello".to_string())],
                events: vec![],
            })],
            events: vec![],
        });

        let annotated = annotate_hydration_tree(node, "boundary:1");

        let Node::Element(root) = annotated else {
            panic!("expected root element");
        };
        assert_eq!(
            root.attributes
                .iter()
                .find(|attr| attr.name == HYDRATION_NODE_ID_ATTR)
                .map(|attr| attr.value.as_str()),
            Some("boundary:1/0")
        );
        assert_eq!(
            root.children
                .first()
                .and_then(|child| attr_value(child, HYDRATION_NODE_ID_ATTR)),
            Some("boundary:1/0.0")
        );
    }

    #[test]
    fn hydration_node_marker_round_trips_and_builds_child_paths() {
        let marker = HydrationNodeMarker::root("profile:7");
        assert_eq!(marker.as_attr_value(), "profile:7/0");

        let child = marker.child(2);
        assert_eq!(child.as_attr_value(), "profile:7/0.2");

        let parsed = HydrationNodeMarker::parse("profile:7/0.2").expect("marker should parse");
        assert_eq!(parsed, child);
    }

    #[test]
    fn annotate_hydration_tree_does_not_descend_into_nested_island_wrappers() {
        let node = Node::Element(Element {
            tag: "section".to_string(),
            attributes: vec![],
            children: vec![Node::Element(Element {
                tag: "div".to_string(),
                attributes: vec![Attribute::new(
                    ISLAND_NAME_ATTR.to_string(),
                    "NestedIsland".to_string(),
                )],
                children: vec![Node::Element(Element {
                    tag: "span".to_string(),
                    attributes: vec![Attribute::new(
                        HYDRATION_NODE_ID_ATTR.to_string(),
                        "nested-boundary/0".to_string(),
                    )],
                    children: vec![],
                    events: vec![],
                })],
                events: vec![],
            })],
            events: vec![],
        });

        let annotated = annotate_hydration_tree(node, "outer:1");
        let Node::Element(root) = annotated else {
            panic!("expected root element");
        };
        let Some(Node::Element(nested_wrapper)) = root.children.first() else {
            panic!("expected nested island wrapper");
        };

        assert_eq!(
            nested_wrapper
                .attributes
                .iter()
                .find(|attr| attr.name == HYDRATION_NODE_ID_ATTR)
                .map(|attr| attr.value.as_str()),
            Some("outer:1/0.0")
        );
        assert_eq!(
            nested_wrapper
                .children
                .first()
                .and_then(|child| attr_value(child, HYDRATION_NODE_ID_ATTR)),
            Some("nested-boundary/0")
        );
    }
}