brrrty 0.0.22

Brrrty, a fast static site generator compatible with Eleventy (11ty) projects.
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
//! Live reload: a script injected into every HTML page opens a WebSocket back to the server,
//! and each rebuild tells the open pages what to do, the way `@11ty/eleventy-dev-server`
//! 2.1.2 does:
//!
//! - a change to stylesheets only, when the page links one of them, swaps the stylesheets in
//!   place;
//! - a change to the page's own template patches the new HTML into the page (morphdom);
//! - any other change reloads the page;
//! - a failed build logs `Build error: …` to the browser console, and the page keeps the
//!   last good build until a fix arrives.
//!
//! Brrrty also logs a build error to pages that connect while it stands, and reports
//! problems writing the output folder the same way.

use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc::{self, RecvTimeoutError, SyncSender};
use std::sync::{Arc, Mutex};
use std::time::Duration;

use brrrty_core::{Problem, Severity};
use brrrty_serve::{Handled, HtmlPage, HtmlRewrite, Incoming, Reply, RunningServer, WebSocket};

use super::Rebuild;

// The paths Brrrty answers itself while serving, as the dev server answers `/.11ty/…`. A file
// of the same name in the output folder is hidden; other paths under `/.brrrty/` are not.
const CLIENT_PATH: &str = "/.brrrty/reload-client.js";
const MORPHDOM_PATH: &str = "/.brrrty/morphdom.js";
const SOCKET_PATH: &str = "/.brrrty/live";

const CLIENT_JS: &str = include_str!("reload-client.js");
const MORPHDOM_JS: &str = include_str!("morphdom.js");

/// The tag injected into HTML pages.
pub const SCRIPT_TAG: &str = r#"<script type="module" src="/.brrrty/reload-client.js"></script>"#;

/// How many messages may wait for one page. A page that falls this far behind has stopped
/// reading, and is dropped.
const QUEUE: usize = 16;

/// How often an idle writer checks that its page is still there. A page that closes is
/// noticed by the server's reader at once; its writer ends within this.
const WRITER_CHECK: Duration = Duration::from_millis(100);

/// One connected page. Its messages go through a queue that a thread of its own writes out,
/// so a page that reads slowly or not at all holds up only itself, never a rebuild. The thread
/// ends within [`WRITER_CHECK`] of the page's socket closing, so the writers running are the
/// open sockets, which the server caps, plus those closed in the last moment.
struct Page {
    ws: WebSocket,
    queue: SyncSender<Arc<str>>,
}

impl Page {
    fn new(ws: WebSocket, writers: Arc<AtomicUsize>) -> Option<Page> {
        let (queue, rx) = mpsc::sync_channel::<Arc<str>>(QUEUE);
        let writer = ws.clone();
        writers.fetch_add(1, Ordering::SeqCst);
        let counted = Arc::clone(&writers);
        let spawned = std::thread::Builder::new()
            .name("brrrty-live-reload".into())
            .spawn(move || {
                loop {
                    match rx.recv_timeout(WRITER_CHECK) {
                        Ok(message) => {
                            if writer.send_text(&message).is_err() {
                                break;
                            }
                        }
                        Err(RecvTimeoutError::Timeout) if writer.is_open() => {}
                        Err(_) => break,
                    }
                }
                writer.close();
                counted.fetch_sub(1, Ordering::SeqCst);
            });
        if spawned.is_err() {
            writers.fetch_sub(1, Ordering::SeqCst);
            return None;
        }
        Some(Page { ws, queue })
    }

    /// Queue `message`; false when the page is gone or too far behind.
    fn send(&self, message: &Arc<str>) -> bool {
        if self.ws.is_open() && self.queue.try_send(Arc::clone(message)).is_ok() {
            return true;
        }
        self.ws.close();
        false
    }
}

#[derive(Default)]
struct State {
    pages: Vec<Page>,
    /// Writer threads still running.
    writers: Arc<AtomicUsize>,
    /// The `error` message for the problems that stand, sent to each page that connects.
    error: Option<Arc<str>>,
}

impl State {
    fn broadcast(&mut self, message: &Arc<str>) {
        self.pages.retain(|p| p.send(message));
    }
}

/// The live-reload channel of one server. Clones share it.
#[derive(Clone, Default)]
pub struct LiveReload(Arc<Mutex<State>>);

impl LiveReload {
    /// Inject the client into `server`'s HTML pages and answer its requests.
    pub fn attach(server: &RunningServer) -> LiveReload {
        let live = LiveReload::default();
        server.set_html_hook(Some(Arc::new(inject_hook)));
        let handler = live.clone();
        server.set_handler(Some(Arc::new(move |req: &Incoming| handler.handle(req))));
        live
    }

    fn handle(&self, req: &Incoming) -> Option<Handled> {
        let js = |body: &str| {
            Some(Handled::Reply(Reply {
                status: 200,
                headers: vec![("Content-Type".into(), "application/javascript".into())],
                body: body.as_bytes().to_vec(),
            }))
        };
        match req.path()? {
            CLIENT_PATH => js(CLIENT_JS),
            MORPHDOM_PATH => js(MORPHDOM_JS),
            SOCKET_PATH => {
                let live = self.clone();
                Some(Handled::WebSocket(Box::new(move |ws| live.connected(ws))))
            }
            _ => None,
        }
    }

    fn connected(&self, ws: WebSocket) {
        let writers = Arc::clone(&self.lock().writers);
        let Some(page) = Page::new(ws.clone(), writers) else {
            ws.close();
            return;
        };
        // Queued under the lock, so no rebuild's message can come before these.
        let mut state = self.lock();
        state.pages.retain(|p| p.ws.is_open());
        let mut ok = page.send(&Arc::from(r#"{"type":"connected"}"#));
        if let Some(error) = &state.error {
            ok = ok && page.send(error);
        }
        if ok {
            state.pages.push(page);
        }
    }

    fn lock(&self) -> std::sync::MutexGuard<'_, State> {
        self.0.lock().unwrap_or_else(|e| e.into_inner())
    }

    /// How many pages are connected.
    pub fn clients(&self) -> usize {
        let mut state = self.lock();
        state.pages.retain(|p| p.ws.is_open());
        state.pages.len()
    }

    /// How many writer threads are running: one per open socket, plus any whose socket closed
    /// in the last 100 ms.
    #[doc(hidden)]
    pub fn writer_threads(&self) -> usize {
        self.lock().writers.load(Ordering::SeqCst)
    }

    /// Tell the open pages about a rebuild. Never waits on a page.
    pub fn rebuilt(&self, r: &Rebuild) {
        // Built before taking the lock: a reload can carry whole pages.
        // Nothing new to show when every changed template was refused: its page keeps the
        // file it had, as 11ty leaves a page whose edit broke the build.
        let only_refused =
            !r.changed_paths.is_empty() && r.changed_paths.iter().all(|c| r.refused.contains(c));
        let reload: Option<Arc<str>> =
            (r.served && !only_refused).then(|| reload_message(r).into());
        let error: Option<Arc<str>> = if !r.ok {
            Some(error_message(&r.problems).into())
        } else if !r.output_problems.is_empty() {
            Some(error_message(&r.output_problems).into())
        } else {
            None
        };
        let mut state = self.lock();
        if let Some(reload) = &reload {
            state.broadcast(reload);
        }
        if let Some(error) = &error {
            state.broadcast(error);
        }
        state.error = error;
    }

    /// Log `problems` in the open pages, and in each page that connects until the next good
    /// build. For a first build that failed, before any rebuild.
    pub fn show_problems(&self, problems: &[Problem]) {
        let message: Arc<str> = error_message(problems).into();
        let mut state = self.lock();
        state.broadcast(&message);
        state.error = Some(message);
    }
}

fn inject_hook(page: &HtmlPage) -> HtmlRewrite {
    if !page.is_navigation {
        return HtmlRewrite {
            body: page.body.to_string(),
            headers: Vec::new(),
        };
    }
    // The server's own error page forbids every script; allow the client from this server.
    let headers = if page.is_builtin_error {
        vec![(
            "Content-Security-Policy".to_string(),
            "script-src 'self'".to_string(),
        )]
    } else {
        Vec::new()
    };
    HtmlRewrite {
        body: inject(page.body, SCRIPT_TAG),
        headers,
    }
}

/// Put `script` into `html` where the dev server does: before the first `</head>`, else after
/// an import map, else after the first `</title>`, else before the first `</body>`, else
/// before the first `</html>`, else after the first `<!doctype html>`, else at the end. The
/// tags are matched as written in lowercase, as the dev server matches them.
pub fn inject(html: &str, script: &str) -> String {
    let insert_at = |i: usize| format!("{}{script}{}", &html[..i], &html[i..]);
    if let Some(i) = html.find("</head>") {
        return insert_at(i);
    }
    if let Some(end) = import_map_end(html) {
        return insert_at(end);
    }
    if let Some(i) = html.find("</title>") {
        return insert_at(i + "</title>".len());
    }
    if let Some(i) = html.find("</body>") {
        return insert_at(i);
    }
    if let Some(i) = html.find("</html>") {
        return insert_at(i);
    }
    if let Some(i) = html.find("<!doctype html>") {
        return insert_at(i + "<!doctype html>".len());
    }
    format!("{html}{script}")
}

/// The end of the first import map, as the dev server's
/// `/<script type=\\?importmap\\?[^>]*>(\n|.)*?<\/script>/i` finds it: `type=importmap`
/// unquoted (or with backslashes), in any case, up to the nearest `</script>` with no
/// carriage return or line separator before it.
fn import_map_end(html: &str) -> Option<usize> {
    let lower = html.to_ascii_lowercase();
    let mut from = 0;
    while let Some(found) = lower[from..].find("<script type=") {
        let start = from + found;
        from = start + 1;
        let mut rest = &lower[start + "<script type=".len()..];
        rest = rest.strip_prefix('\\').unwrap_or(rest);
        let Some(after) = rest.strip_prefix("importmap") else {
            continue;
        };
        let after = after.strip_prefix('\\').unwrap_or(after);
        let Some(close) = after.find('>') else {
            continue;
        };
        let body_start = lower.len() - after.len() + close + 1;
        let Some(end) = lower[body_start..].find("</script>") else {
            continue;
        };
        let body = &html[body_start..body_start + end];
        if body.contains(['\r', '\u{2028}', '\u{2029}']) {
            continue;
        }
        return Some(body_start + end + "</script>".len());
    }
    None
}

fn reload_message(r: &Rebuild) -> String {
    let mut out = String::from(r#"{"type":"reload","css":"#);
    out.push_str(if r.css_only { "true" } else { "false" });
    out.push_str(r#","stylesheets":["#);
    for (i, s) in r.stylesheets.iter().enumerate() {
        if i > 0 {
            out.push(',');
        }
        json_string(&mut out, s);
    }
    out.push_str(r#"],"pages":["#);
    for (i, p) in r.pages.iter().enumerate() {
        if i > 0 {
            out.push(',');
        }
        out.push_str(r#"{"url":"#);
        json_string(&mut out, &p.url);
        out.push_str(r#","content":"#);
        json_string(&mut out, &p.content);
        out.push('}');
    }
    out.push_str("]}");
    out
}

fn error_message(problems: &[Problem]) -> String {
    let errors: Vec<&Problem> = problems
        .iter()
        .filter(|p| matches!(p.severity, Severity::Error))
        .collect();
    let shown: Vec<&Problem> = if errors.is_empty() {
        problems.iter().collect()
    } else {
        errors
    };
    let text = if shown.is_empty() {
        "the build failed".to_string()
    } else {
        shown
            .iter()
            .map(|p| describe(p))
            .collect::<Vec<_>>()
            .join("\n")
    };
    let mut out = String::from(r#"{"type":"error","message":"#);
    json_string(&mut out, &text);
    out.push('}');
    out
}

/// `path:line:column: message`, as the terminal shows it.
fn describe(p: &Problem) -> String {
    let location = match (&p.path, p.line, p.column) {
        (Some(path), Some(l), Some(c)) => format!("{}:{l}:{c}: ", path.display()),
        (Some(path), Some(l), None) => format!("{}:{l}: ", path.display()),
        (Some(path), None, _) => format!("{}: ", path.display()),
        (None, ..) => String::new(),
    };
    format!("{location}{}", p.message)
}

fn json_string(out: &mut String, s: &str) {
    out.push('"');
    for c in s.chars() {
        match c {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
            c => out.push(c),
        }
    }
    out.push('"');
}

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

    const S: &str = "<S>";

    #[test]
    fn the_script_goes_where_the_dev_server_puts_it() {
        let cases = [
            // Before </head>, even when </title> and </body> are there too.
            (
                "<html><head><title>t</title></head><body>x</body></html>",
                "<html><head><title>t</title><S></head><body>x</body></html>",
            ),
            // Only the first </head>.
            ("a</head>b</head>", "a<S></head>b</head>"),
            // After an unquoted import map when there is no </head>.
            (
                "<SCRIPT TYPE=importmap>{}</script><title>t</title>",
                "<SCRIPT TYPE=importmap>{}</script><S><title>t</title>",
            ),
            // A quoted type is not what the dev server's pattern matches.
            (
                "<script type=\"importmap\">{}</script><title>t</title>x",
                "<script type=\"importmap\">{}</script><title>t</title><S>x",
            ),
            // After </title>.
            (
                "<!doctype html><title>x</title><p>title only</p>\n",
                "<!doctype html><title>x</title><S><p>title only</p>\n",
            ),
            // Before </body>, then </html>.
            (
                "<html><body><p>b</p></body></html>",
                "<html><body><p>b</p><S></body></html>",
            ),
            ("<p>a</p></html>", "<p>a</p><S></html>"),
            // After a lowercase doctype only.
            ("<!doctype html><p>d", "<!doctype html><S><p>d"),
            ("<!DOCTYPE html><p>d", "<!DOCTYPE html><p>d<S>"),
            // Uppercase closing tags are not matched.
            (
                "<HEAD></HEAD><BODY>x</BODY>",
                "<HEAD></HEAD><BODY>x</BODY><S>",
            ),
            // Otherwise at the end, even for an empty page.
            ("<p>no head no body</p>\n", "<p>no head no body</p>\n<S>"),
            ("", "<S>"),
        ];
        for (html, want) in cases {
            assert_eq!(inject(html, S), want, "{html}");
        }
    }

    #[test]
    fn an_import_map_with_a_carriage_return_is_not_matched() {
        assert_eq!(
            inject("<script type=importmap>{\r}</script>x", S),
            "<script type=importmap>{\r}</script>x<S>"
        );
        assert_eq!(
            inject("<script type=importmap>{\n}</script>x", S),
            "<script type=importmap>{\n}</script><S>x"
        );
    }

    #[test]
    fn messages_are_json() {
        let r = Rebuild {
            ok: true,
            served: true,
            refused: Vec::new(),
            kept: 0,
            changed_paths: Vec::new(),
            css_only: false,
            problems: Vec::new(),
            pages: vec![super::super::ChangedPage {
                url: "/a/".into(),
                content: "<p class=\"x\">é\n\u{1}</p>".into(),
            }],
            stylesheets: vec!["/s.css".into()],
            output_problems: Vec::new(),
        };
        assert_eq!(
            reload_message(&r),
            r#"{"type":"reload","css":false,"stylesheets":["/s.css"],"pages":[{"url":"/a/","content":"<p class=\"x\">é\n\u0001</p>"}]}"#
        );
        let p = Problem::error("x", "bad \"thing\"")
            .with_path("src/a.md")
            .at(3, 7);
        let q = Problem::error("y", "no path");
        assert_eq!(
            error_message(&[Problem::warning("w", "just a warning"), p, q]),
            r#"{"type":"error","message":"src/a.md:3:7: bad \"thing\"\nno path"}"#
        );
        // Warnings alone, such as a failed write to the output folder, are shown.
        assert_eq!(
            error_message(&[Problem::warning("w", "cannot write _site/a")]),
            r#"{"type":"error","message":"cannot write _site/a"}"#
        );
        assert_eq!(
            error_message(&[]),
            r#"{"type":"error","message":"the build failed"}"#
        );
    }
}