ssg-core 0.0.56

Core compilation pipeline for SSG — no system dependencies, WASM-compatible.
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
// Copyright © 2023 - 2026 Static Site Generator (SSG). All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! ISR build manifest — `dist/.ssg/manifest.json`.
//!
//! The manifest maps every output URL to its exact source dependency
//! set (markdown file + layout + partials + data files) plus a content
//! hash of those dependencies. The Edge renderer consults this manifest
//! to (a) find which sources to fetch from KV / Edge Config, and (b)
//! detect cache invalidation when sources change.
//!
//! Shape (canonical, stable):
//!
//! ```json
//! {
//!   "version": 1,
//!   "generated_at": "<rfc3339 timestamp or build-id>",
//!   "default_cache": { "s_maxage": 60, "swr": 86400 },
//!   "entries": {
//!     "/posts/foo/index.html": {
//!       "sources": ["content/posts/foo.md", "templates/post.html"],
//!       "hash": "<sha256-hex>",
//!       "cache": { "s_maxage": 600, "swr": 3600 }
//!     }
//!   }
//! }
//! ```
//!
//! `cache` is omitted at the entry level when the page wants the
//! site-wide default (`default_cache`). Per-route overrides come from
//! frontmatter (`isr.s_maxage`, `isr.swr`).
//!
//! ## Determinism
//!
//! Entries are written in lexicographic URL order so the manifest is
//! byte-stable for a given input set — critical for CDN cache keys
//! and reproducible builds.

use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::BTreeMap;

/// Schema version of the manifest. Bump when the on-disk shape changes
/// in a way edge adapters need to detect.
pub const MANIFEST_VERSION: u32 = 1;

/// Default `s-maxage` (seconds) the manifest emits when a page does
/// not override via frontmatter. 60 s tracks the per-route SLA quoted
/// in issue #546.
pub const DEFAULT_S_MAXAGE: u32 = 60;

/// Default `stale-while-revalidate` (seconds). 24 h matches the
/// `Cache-Control: stale-while-revalidate=86400` snippet in the
/// architecture doc.
pub const DEFAULT_SWR: u32 = 86_400;

/// Per-page `Cache-Control` knobs.
///
/// Both fields are seconds, both optional at the entry level — when
/// absent we fall back to the manifest-wide [`Manifest::default_cache`].
///
/// # Examples
///
/// ```
/// use ssg_core::CachePolicy;
///
/// let policy = CachePolicy { s_maxage: 60, swr: 86_400 };
/// assert_eq!(
///     policy.to_cache_control(),
///     "s-maxage=60, stale-while-revalidate=86400",
/// );
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct CachePolicy {
    /// `s-maxage` — how long the CDN may serve the cached response
    /// before considering it stale.
    pub s_maxage: u32,
    /// `stale-while-revalidate` — how long the CDN may continue
    /// serving the stale response while it revalidates in the
    /// background.
    pub swr: u32,
}

impl Default for CachePolicy {
    fn default() -> Self {
        Self {
            s_maxage: DEFAULT_S_MAXAGE,
            swr: DEFAULT_SWR,
        }
    }
}

impl CachePolicy {
    /// Renders the policy as a `Cache-Control` header value.
    ///
    /// # Examples
    ///
    /// ```
    /// use ssg_core::CachePolicy;
    ///
    /// let policy = CachePolicy { s_maxage: 120, swr: 600 };
    /// assert_eq!(
    ///     policy.to_cache_control(),
    ///     "s-maxage=120, stale-while-revalidate=600",
    /// );
    /// ```
    #[must_use]
    pub fn to_cache_control(&self) -> String {
        format!(
            "s-maxage={}, stale-while-revalidate={}",
            self.s_maxage, self.swr
        )
    }
}

/// One entry in the ISR manifest — describes how one URL renders.
///
/// # Examples
///
/// ```
/// use ssg_core::{build_entry, ManifestEntry};
///
/// let entry: ManifestEntry =
///     build_entry(vec!["a.md".into()], &[b"# A"], None);
/// assert_eq!(entry.sources, vec!["a.md"]);
/// assert_eq!(entry.hash.len(), 64);
/// assert!(entry.cache.is_none());
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ManifestEntry {
    /// Source dependency keys, in deterministic order. Each entry is
    /// a path-shaped key that resolves through a `ContentProvider`.
    pub sources: Vec<String>,
    /// SHA-256 hex digest of the concatenated dependency bytes.
    /// Used by the Edge runtime to detect that a re-fetch is needed.
    pub hash: String,
    /// Per-page cache override. Omitted from JSON when `None` so the
    /// adapter falls back to the manifest-wide default.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache: Option<CachePolicy>,
}

/// Complete ISR build manifest — emitted as `dist/.ssg/manifest.json`.
///
/// # Examples
///
/// ```
/// use ssg_core::{build_entry, Manifest};
///
/// let mut m = Manifest::new("build-1");
/// m.insert("/a.html", build_entry(vec!["a.md".into()], &[b"a"], None));
/// assert_eq!(m.len(), 1);
/// assert!(m.get("/a.html").is_some());
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Manifest {
    /// Schema version (currently always [`MANIFEST_VERSION`]).
    pub version: u32,
    /// Identifier for the build that produced this manifest. Adapters
    /// use this to detect a stale KV namespace after a deploy.
    pub generated_at: String,
    /// Site-wide cache policy applied when an entry lacks its own
    /// `cache` field.
    pub default_cache: CachePolicy,
    /// URL → entry map. Iteration order is lexicographic.
    pub entries: BTreeMap<String, ManifestEntry>,
}

impl Default for Manifest {
    fn default() -> Self {
        Self::new("unspecified")
    }
}

impl Manifest {
    /// Constructs an empty manifest stamped with `build_id`.
    ///
    /// # Examples
    ///
    /// ```
    /// use ssg_core::{Manifest, MANIFEST_VERSION};
    ///
    /// let m = Manifest::new("build-42");
    /// assert_eq!(m.version, MANIFEST_VERSION);
    /// assert_eq!(m.generated_at, "build-42");
    /// assert!(m.is_empty());
    /// ```
    #[must_use]
    pub fn new(build_id: impl Into<String>) -> Self {
        Self {
            version: MANIFEST_VERSION,
            generated_at: build_id.into(),
            default_cache: CachePolicy::default(),
            entries: BTreeMap::new(),
        }
    }

    /// Inserts (or replaces) an entry.
    ///
    /// # Examples
    ///
    /// ```
    /// use ssg_core::{build_entry, Manifest};
    ///
    /// let mut m = Manifest::new("b");
    /// m.insert("/a.html", build_entry(vec!["a.md".into()], &[b"a"], None));
    /// assert_eq!(m.len(), 1);
    /// ```
    pub fn insert(&mut self, url: impl Into<String>, entry: ManifestEntry) {
        let _ = self.entries.insert(url.into(), entry);
    }

    /// Returns the entry for `url`, if any.
    ///
    /// # Examples
    ///
    /// ```
    /// use ssg_core::{build_entry, Manifest};
    ///
    /// let mut m = Manifest::new("b");
    /// m.insert("/a.html", build_entry(vec!["a.md".into()], &[b"a"], None));
    /// assert!(m.get("/a.html").is_some());
    /// assert!(m.get("/missing").is_none());
    /// ```
    #[must_use]
    pub fn get(&self, url: &str) -> Option<&ManifestEntry> {
        self.entries.get(url)
    }

    /// Returns the number of entries.
    ///
    /// # Examples
    ///
    /// ```
    /// use ssg_core::{build_entry, Manifest};
    ///
    /// let mut m = Manifest::new("b");
    /// assert_eq!(m.len(), 0);
    /// m.insert("/a.html", build_entry(vec!["a.md".into()], &[b"a"], None));
    /// assert_eq!(m.len(), 1);
    /// ```
    #[must_use]
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Reports whether the manifest holds no entries.
    ///
    /// # Examples
    ///
    /// ```
    /// use ssg_core::{build_entry, Manifest};
    ///
    /// let mut m = Manifest::new("b");
    /// assert!(m.is_empty());
    /// m.insert("/a.html", build_entry(vec!["a.md".into()], &[b"a"], None));
    /// assert!(!m.is_empty());
    /// ```
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Serialises to canonical pretty JSON (stable key order, 2-space
    /// indent). Suitable for direct write to `dist/.ssg/manifest.json`.
    ///
    /// # Errors
    /// Returns the underlying `serde_json::Error` if any entry is not
    /// representable (in practice this is unreachable — every field
    /// is a primitive or a `String`).
    ///
    /// # Examples
    ///
    /// ```
    /// use ssg_core::Manifest;
    ///
    /// let m = Manifest::new("build-1");
    /// let json = m.to_pretty_json().unwrap();
    /// assert!(json.contains("\"version\""));
    /// assert!(json.contains("\"generated_at\": \"build-1\""));
    /// ```
    pub fn to_pretty_json(&self) -> serde_json::Result<String> {
        serde_json::to_string_pretty(self)
    }

    /// Returns every URL that depends on the given source key.
    ///
    /// Used by the invalidation webhook (AC8): given
    /// `content/posts/foo.md`, find every URL whose manifest entry
    /// lists it as a source — typically the post itself plus any
    /// tag/archive index that includes it.
    ///
    /// # Examples
    ///
    /// ```
    /// use ssg_core::{build_entry, Manifest};
    ///
    /// let mut m = Manifest::new("b");
    /// m.insert(
    ///     "/a.html",
    ///     build_entry(vec!["c/a.md".into()], &[b"A"], None),
    /// );
    /// m.insert(
    ///     "/b.html",
    ///     build_entry(vec!["c/b.md".into()], &[b"B"], None),
    /// );
    /// let deps = m.urls_for_source("c/a.md");
    /// assert_eq!(deps, vec!["/a.html"]);
    /// ```
    #[must_use]
    pub fn urls_for_source(&self, source_key: &str) -> Vec<String> {
        self.entries
            .iter()
            .filter_map(|(url, entry)| {
                if entry.sources.iter().any(|s| s == source_key) {
                    Some(url.clone())
                } else {
                    None
                }
            })
            .collect()
    }
}

/// Computes the canonical SHA-256 hex digest for a set of source bytes.
///
/// The digest covers every source's *full bytes* in the order they
/// appear in `sources`, with a `0x00` separator between sources so
/// `["ab", "c"]` and `["a", "bc"]` produce distinct hashes. Sources
/// are NOT sorted — callers must hand them in deterministic order.
///
/// # Examples
///
/// ```
/// use ssg_core::hash_sources;
///
/// let a = hash_sources(&[b"hello", b"world"]);
/// let b = hash_sources(&[b"world", b"hello"]);
/// assert_eq!(a.len(), 64);
/// assert_ne!(a, b, "hash is order-sensitive");
/// ```
#[must_use]
pub fn hash_sources(sources: &[&[u8]]) -> String {
    let mut hasher = Sha256::new();
    for (i, src) in sources.iter().enumerate() {
        if i > 0 {
            hasher.update([0u8]);
        }
        hasher.update(src);
    }
    let digest = hasher.finalize();
    let mut out = String::with_capacity(digest.len() * 2);
    for byte in digest {
        let _ =
            std::fmt::Write::write_fmt(&mut out, format_args!("{byte:02x}"));
    }
    out
}

/// Builds a [`ManifestEntry`] from sources + their bytes.
///
/// `sources` and `bytes` MUST be the same length and in matching
/// order. The resulting entry is hashed via [`hash_sources`].
///
/// # Panics
/// Panics in debug builds if the slice lengths disagree.
///
/// # Examples
///
/// ```
/// use ssg_core::build_entry;
///
/// let entry = build_entry(
///     vec!["a".into(), "b".into()],
///     &[b"alpha", b"beta"],
///     None,
/// );
/// assert_eq!(entry.sources, vec!["a", "b"]);
/// assert_eq!(entry.hash.len(), 64);
/// ```
#[must_use]
pub fn build_entry(
    sources: Vec<String>,
    bytes: &[&[u8]],
    cache: Option<CachePolicy>,
) -> ManifestEntry {
    debug_assert_eq!(
        sources.len(),
        bytes.len(),
        "sources and bytes must align"
    );
    let hash = hash_sources(bytes);
    ManifestEntry {
        sources,
        hash,
        cache,
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn manifest_round_trip_json() {
        let mut m = Manifest::new("build-42");
        m.insert(
            "/index.html",
            ManifestEntry {
                sources: vec![
                    "content/index.md".into(),
                    "templates/base.html".into(),
                ],
                hash: hash_sources(&[b"# Home", b"<html></html>"]),
                cache: None,
            },
        );
        m.insert(
            "/posts/foo/index.html",
            ManifestEntry {
                sources: vec![
                    "content/posts/foo.md".into(),
                    "templates/post.html".into(),
                ],
                hash: hash_sources(&[b"# Foo", b"<html><body/></html>"]),
                cache: Some(CachePolicy {
                    s_maxage: 600,
                    swr: 3600,
                }),
            },
        );

        let json = m.to_pretty_json().unwrap();
        // Stable order — /index.html before /posts/foo/index.html.
        let i_idx = json.find("/index.html").unwrap();
        let p_idx = json.find("/posts/foo/index.html").unwrap();
        assert!(i_idx < p_idx, "URLs must be lexicographically ordered");

        let parsed: Manifest = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, m);
    }

    #[test]
    fn cache_policy_to_cache_control() {
        let p = CachePolicy {
            s_maxage: 120,
            swr: 600,
        };
        assert_eq!(
            p.to_cache_control(),
            "s-maxage=120, stale-while-revalidate=600"
        );
    }

    #[test]
    fn cache_policy_default_matches_constants() {
        let d = CachePolicy::default();
        assert_eq!(d.s_maxage, DEFAULT_S_MAXAGE);
        assert_eq!(d.swr, DEFAULT_SWR);
    }

    #[test]
    fn hash_sources_is_order_sensitive() {
        let a = hash_sources(&[b"hello", b"world"]);
        let b = hash_sources(&[b"world", b"hello"]);
        assert_ne!(a, b);
    }

    #[test]
    fn hash_sources_avoids_concat_collision() {
        // ["ab", "c"] vs ["a", "bc"] must hash differently.
        let a = hash_sources(&[b"ab", b"c"]);
        let b = hash_sources(&[b"a", b"bc"]);
        assert_ne!(a, b);
    }

    #[test]
    fn hash_sources_stable_for_same_input() {
        let a = hash_sources(&[b"foo", b"bar"]);
        let b = hash_sources(&[b"foo", b"bar"]);
        assert_eq!(a, b);
        // SHA-256 hex is 64 chars.
        assert_eq!(a.len(), 64);
        assert!(a.chars().all(|c| c.is_ascii_hexdigit()));
    }

    #[test]
    fn build_entry_populates_hash() {
        let entry = build_entry(
            vec!["a".into(), "b".into()],
            &[b"alpha", b"beta"],
            None,
        );
        assert_eq!(entry.sources, vec!["a", "b"]);
        assert!(entry.cache.is_none());
        assert_eq!(entry.hash.len(), 64);
    }

    #[test]
    fn manifest_get_returns_entry() {
        let mut m = Manifest::default();
        let entry = build_entry(vec!["x".into()], &[b"xxx"], None);
        m.insert("/x.html", entry.clone());
        assert_eq!(m.get("/x.html"), Some(&entry));
        assert!(m.get("/missing").is_none());
    }

    #[test]
    fn manifest_len_and_is_empty() {
        let mut m = Manifest::new("b");
        assert!(m.is_empty());
        assert_eq!(m.len(), 0);
        m.insert("/a.html", build_entry(vec!["a".into()], &[b"a"], None));
        assert!(!m.is_empty());
        assert_eq!(m.len(), 1);
    }

    #[test]
    fn urls_for_source_finds_dependents() {
        let mut m = Manifest::default();
        m.insert(
            "/a.html",
            build_entry(
                vec!["c/a.md".into(), "t/base.html".into()],
                &[b"A", b"T"],
                None,
            ),
        );
        m.insert(
            "/b.html",
            build_entry(
                vec!["c/b.md".into(), "t/base.html".into()],
                &[b"B", b"T"],
                None,
            ),
        );
        m.insert(
            "/tags/foo.html",
            build_entry(
                vec!["c/a.md".into(), "c/b.md".into(), "t/tag.html".into()],
                &[b"A", b"B", b"TAG"],
                None,
            ),
        );

        let mut deps = m.urls_for_source("c/a.md");
        deps.sort();
        assert_eq!(deps, vec!["/a.html", "/tags/foo.html"]);

        let mut tdeps = m.urls_for_source("t/base.html");
        tdeps.sort();
        assert_eq!(tdeps, vec!["/a.html", "/b.html"]);

        let none = m.urls_for_source("unknown.md");
        assert!(none.is_empty());
    }

    #[test]
    fn entry_cache_skipped_when_none() {
        let mut m = Manifest::default();
        m.insert("/a.html", build_entry(vec!["a.md".into()], &[b"a"], None));
        let json = m.to_pretty_json().unwrap();
        // entry has no "cache" key when None
        assert!(!json.contains("\"cache\""));
    }

    #[test]
    fn entry_cache_emitted_when_some() {
        let mut m = Manifest::default();
        m.insert(
            "/a.html",
            build_entry(
                vec!["a.md".into()],
                &[b"a"],
                Some(CachePolicy {
                    s_maxage: 10,
                    swr: 20,
                }),
            ),
        );
        let json = m.to_pretty_json().unwrap();
        assert!(json.contains("\"cache\""));
        assert!(json.contains("\"s_maxage\": 10"));
        assert!(json.contains("\"swr\": 20"));
    }

    #[test]
    fn manifest_version_is_one() {
        let m = Manifest::new("x");
        assert_eq!(m.version, 1);
    }
}