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
use crate::Render;
use std::collections::HashMap;
use std::time::Instant;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SuspenseState {
    Pending,
    Resolved,
    Error,
}

impl SuspenseState {
    fn as_str(self) -> &'static str {
        match self {
            SuspenseState::Pending => "pending",
            SuspenseState::Resolved => "resolved",
            SuspenseState::Error => "error",
        }
    }

    fn parse(value: &str) -> Option<Self> {
        match value {
            "pending" => Some(Self::Pending),
            "resolved" => Some(Self::Resolved),
            "error" => Some(Self::Error),
            _ => None,
        }
    }
}

/// Parsed suspense marker emitted by SSR streaming output.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SuspenseMarker {
    pub boundary_id: String,
    pub state: SuspenseState,
}

impl SuspenseMarker {
    /// Parse from either full marker comment (`<!--krab:suspense:...-->`) or raw body (`krab:suspense:...`).
    pub fn parse(input: &str) -> Option<Self> {
        let trimmed = input.trim();
        let body = trimmed
            .strip_prefix("<!--")
            .and_then(|v| v.strip_suffix("-->"))
            .unwrap_or(trimmed);

        let mut parts = body.split(':');
        let prefix = parts.next()?;
        let kind = parts.next()?;
        let boundary_id = parts.next()?.trim();
        let state_raw = parts.next()?.trim();

        if prefix != "krab" || kind != "suspense" || boundary_id.is_empty() {
            return None;
        }

        let state = SuspenseState::parse(state_raw)?;
        Some(Self {
            boundary_id: boundary_id.to_string(),
            state,
        })
    }
}

/// Streaming SSR telemetry snapshot for performance budgets and regressions.
#[derive(Debug, Clone)]
pub struct StreamTelemetry {
    pub ttfb_ms: Option<u128>,
    pub first_visible_chunk_ms: Option<u128>,
    pub full_stream_complete_ms: Option<u128>,
    pub emitted_bytes: usize,
    pub flush_count: usize,
    pub suspense_marker_count: usize,
    /// Number of boundary transitions by `boundary_id`.
    pub boundary_events: HashMap<String, usize>,
    pub budget_limit_bytes: Option<usize>,
    pub budget_exceeded: bool,
    pub stream_cancelled: bool,
    pub cancel_reason: Option<String>,
}

#[derive(Debug, Clone)]
pub struct ChunkedStreamWriter {
    chunk_size: usize,
    flush_threshold: usize,
    pending: String,
    chunks: Vec<String>,
    flush_count: usize,
    created_at: Instant,
    first_write_at: Option<Instant>,
    first_flush_at: Option<Instant>,
    emitted_bytes: usize,
    suspense_marker_count: usize,
    boundary_events: HashMap<String, usize>,
    max_total_bytes: Option<usize>,
    budget_exceeded: bool,
    stream_cancelled: bool,
    cancel_reason: Option<String>,
}

impl Default for ChunkedStreamWriter {
    fn default() -> Self {
        Self::new(1024, 4096)
    }
}

impl ChunkedStreamWriter {
    pub fn new(chunk_size: usize, flush_threshold: usize) -> Self {
        Self {
            chunk_size: chunk_size.max(128),
            flush_threshold: flush_threshold.max(chunk_size.max(128)),
            pending: String::new(),
            chunks: Vec::new(),
            flush_count: 0,
            created_at: Instant::now(),
            first_write_at: None,
            first_flush_at: None,
            emitted_bytes: 0,
            suspense_marker_count: 0,
            boundary_events: HashMap::new(),
            max_total_bytes: None,
            budget_exceeded: false,
            stream_cancelled: false,
            cancel_reason: None,
        }
    }

    /// Set a maximum per-request render budget in bytes.
    ///
    /// Once the budget is exceeded, subsequent writes are ignored and
    /// `budget_exceeded` is surfaced via [`telemetry_snapshot`](Self::telemetry_snapshot).
    pub fn with_max_total_bytes(mut self, max_total_bytes: usize) -> Self {
        self.max_total_bytes = Some(max_total_bytes.max(1));
        self
    }

    /// Configure/override the maximum per-request render budget in bytes.
    pub fn set_max_total_bytes(&mut self, max_total_bytes: usize) {
        self.max_total_bytes = Some(max_total_bytes.max(1));
    }

    pub fn write(&mut self, input: &str) {
        if self.budget_exceeded || self.stream_cancelled {
            return;
        }

        if let Some(limit) = self.max_total_bytes {
            let projected = self.emitted_bytes + self.pending.len() + input.len();
            if projected > limit {
                self.budget_exceeded = true;
                return;
            }
        }

        if self.first_write_at.is_none() {
            self.first_write_at = Some(Instant::now());
        }

        self.pending.push_str(input);
        self.flush_if_ready();
    }

    pub fn write_suspense_marker(&mut self, boundary_id: &str, state: SuspenseState) {
        self.suspense_marker_count += 1;
        *self
            .boundary_events
            .entry(boundary_id.to_string())
            .or_insert(0) += 1;
        self.write(&format!(
            "<!--krab:suspense:{}:{}-->",
            boundary_id,
            state.as_str()
        ));
    }

    pub fn flush(&mut self) {
        if self.pending.is_empty() || self.stream_cancelled {
            return;
        }

        while self.pending.len() > self.chunk_size {
            let split_at = nearest_char_boundary(&self.pending, self.chunk_size);
            let chunk = self.pending[..split_at].to_string();
            self.emitted_bytes += chunk.len();
            self.chunks.push(chunk);
            self.pending = self.pending[split_at..].to_string();
        }

        if !self.pending.is_empty() {
            let chunk = std::mem::take(&mut self.pending);
            self.emitted_bytes += chunk.len();
            self.chunks.push(chunk);
        }

        if self.first_flush_at.is_none() {
            self.first_flush_at = Some(Instant::now());
        }

        self.flush_count += 1;
    }

    pub fn finish(mut self) -> Vec<String> {
        self.flush();
        self.chunks
    }

    pub fn flush_count(&self) -> usize {
        self.flush_count
    }

    pub fn telemetry_snapshot(&self) -> StreamTelemetry {
        let now = Instant::now();
        StreamTelemetry {
            ttfb_ms: self
                .first_flush_at
                .map(|t| t.duration_since(self.created_at).as_millis()),
            first_visible_chunk_ms: self
                .first_flush_at
                .map(|t| t.duration_since(self.created_at).as_millis()),
            full_stream_complete_ms: if self.flush_count > 0 {
                Some(now.duration_since(self.created_at).as_millis())
            } else {
                None
            },
            emitted_bytes: self.emitted_bytes,
            flush_count: self.flush_count,
            suspense_marker_count: self.suspense_marker_count,
            boundary_events: self.boundary_events.clone(),
            budget_limit_bytes: self.max_total_bytes,
            budget_exceeded: self.budget_exceeded,
            stream_cancelled: self.stream_cancelled,
            cancel_reason: self.cancel_reason.clone(),
        }
    }

    /// Cancels stream emission for current request.
    /// Further writes/flushes are ignored.
    pub fn cancel(&mut self, reason: impl Into<String>) {
        if !self.pending.is_empty() {
            self.flush();
        }
        self.stream_cancelled = true;
        self.cancel_reason = Some(reason.into());
    }

    pub fn is_cancelled(&self) -> bool {
        self.stream_cancelled
    }

    fn flush_if_ready(&mut self) {
        if self.pending.len() >= self.flush_threshold {
            self.flush();
        }
    }
}

pub fn render_to_chunk_stream(renderable: &impl Render, writer: &mut ChunkedStreamWriter) {
    writer.write(&renderable.render());
}

fn nearest_char_boundary(s: &str, target: usize) -> usize {
    if target >= s.len() {
        return s.len();
    }
    let mut i = target;
    while !s.is_char_boundary(i) {
        i -= 1;
    }
    i
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{annotate_hydration_tree, Element, Node};

    #[test]
    fn chunk_writer_splits_and_flushes() {
        let mut writer = ChunkedStreamWriter::new(128, 256);
        writer.write(&"a".repeat(300));

        let chunks = writer.finish();
        assert!(chunks.len() >= 3);
        assert_eq!(chunks.concat(), "a".repeat(300));
    }

    #[test]
    fn suspense_markers_are_hydration_compatible_comments() {
        let mut writer = ChunkedStreamWriter::new(32, 64);
        writer.write_suspense_marker("home-data", SuspenseState::Pending);
        writer.write("<div data-krab-hydration=\"home-data\">fallback</div>");
        writer.write_suspense_marker("home-data", SuspenseState::Resolved);
        let html = writer.finish().concat();

        assert!(html.contains("<!--krab:suspense:home-data:pending-->"));
        assert!(html.contains("data-krab-hydration=\"home-data\""));
        assert!(html.contains("<!--krab:suspense:home-data:resolved-->"));
    }

    #[test]
    fn suspense_marker_parser_handles_comment_and_raw_body() {
        let parsed_comment = SuspenseMarker::parse("<!--krab:suspense:home:pending-->")
            .expect("comment marker should parse");
        assert_eq!(parsed_comment.boundary_id, "home");
        assert_eq!(parsed_comment.state, SuspenseState::Pending);

        let parsed_raw = SuspenseMarker::parse("krab:suspense:profile:resolved")
            .expect("raw marker should parse");
        assert_eq!(parsed_raw.boundary_id, "profile");
        assert_eq!(parsed_raw.state, SuspenseState::Resolved);
    }

    #[test]
    fn hydration_node_markers_survive_chunk_stream_rendering() {
        let renderable = annotate_hydration_tree(
            Node::Element(Element {
                tag: "section".to_string(),
                attributes: vec![],
                children: vec![Node::Element(Element {
                    tag: "div".to_string(),
                    attributes: vec![],
                    children: vec![Node::Text("streamed".to_string())],
                    events: vec![],
                })],
                events: vec![],
            }),
            "stream-home:1",
        );
        let mut writer = ChunkedStreamWriter::new(16, 16);

        render_to_chunk_stream(&renderable, &mut writer);

        let html = writer.finish().concat();
        assert!(html.contains("data-krab-node-id=\"stream-home:1/0\""));
        assert!(html.contains("data-krab-node-id=\"stream-home:1/0.0\""));
        assert!(html.contains(">streamed</div>"));
    }

    #[test]
    fn backpressure_flushes_when_threshold_reached() {
        let mut writer = ChunkedStreamWriter::new(128, 128);
        writer.write("hello");
        assert_eq!(writer.flush_count(), 0);
        writer.write(&"x".repeat(130));
        assert!(writer.flush_count() >= 1);
    }

    #[test]
    fn telemetry_snapshot_tracks_stream_events() {
        let mut writer = ChunkedStreamWriter::new(64, 64);
        writer.write("<!DOCTYPE html>");
        writer.write_suspense_marker("home", SuspenseState::Pending);
        writer.write("<div>hello</div>");
        writer.write_suspense_marker("home", SuspenseState::Resolved);
        writer.flush();

        let telemetry = writer.telemetry_snapshot();
        assert!(telemetry.flush_count >= 1);
        assert_eq!(telemetry.suspense_marker_count, 2);
        assert_eq!(telemetry.boundary_events.get("home"), Some(&2));
        assert!(telemetry.ttfb_ms.is_some());
        assert_eq!(telemetry.budget_limit_bytes, None);
        assert!(!telemetry.budget_exceeded);
    }

    #[test]
    fn budget_guard_blocks_writes_once_exceeded() {
        let mut writer = ChunkedStreamWriter::new(128, 256).with_max_total_bytes(10);
        writer.write("12345");
        writer.write("67890");
        writer.write("EXTRA"); // must be blocked by budget
        writer.flush();
        let telemetry = writer.telemetry_snapshot();
        let html = writer.finish().concat();
        assert_eq!(html, "1234567890");

        assert_eq!(telemetry.budget_limit_bytes, Some(10));
        assert!(telemetry.budget_exceeded);
        assert!(!telemetry.stream_cancelled);
    }

    #[test]
    fn budget_guard_can_be_set_after_initialization() {
        let mut writer = ChunkedStreamWriter::new(128, 256);
        writer.set_max_total_bytes(4);
        writer.write("abcd");
        writer.write("e"); // exceeds budget
        writer.flush();
        let telemetry = writer.telemetry_snapshot();
        let html = writer.finish().concat();
        assert_eq!(html, "abcd");

        assert_eq!(telemetry.budget_limit_bytes, Some(4));
        assert!(telemetry.budget_exceeded);
        assert!(!telemetry.stream_cancelled);
    }

    #[test]
    fn cancellation_stops_future_writes_and_exposes_reason() {
        let mut writer = ChunkedStreamWriter::new(64, 64);
        writer.write("<html>");
        writer.cancel("client disconnected");
        writer.write("<body>should-not-appear</body>");
        writer.flush();

        let telemetry = writer.telemetry_snapshot();
        assert!(writer.is_cancelled());
        assert!(telemetry.stream_cancelled);
        assert_eq!(
            telemetry.cancel_reason.as_deref(),
            Some("client disconnected")
        );

        let html = writer.finish().concat();
        assert_eq!(html, "<html>");
    }
}