| 1 | /* rastrillo.js — the fragment shim. First-party, dependency-free, and |
| 2 | inert: the polling section answers only to an opt-in data attribute, |
| 3 | and everything it enhances also works with scripts disabled (a |
| 4 | status page's <noscript> meta refresh). This file is app-owned from |
| 5 | the moment it is scaffolded — edit it like any other static file. |
| 6 | |
| 7 | Menu dismiss, the switch's drag, the touch listener and the colour |
| 8 | scheme menu's in-place apply are Fiesta's fiesta.js, which every |
| 9 | page loads beside this file. An older rastrillo.js held them too; |
| 10 | running that one beside fiesta.js is not supported. |
| 11 | |
| 12 | Vocabulary: |
| 13 | data-poll="URL" fetch URL for an HTML fragment, replace this |
| 14 | element with it, and keep going only if the |
| 15 | replacement element itself carries data-poll |
| 16 | data-poll-every="2" seconds between polls (default 2) |
| 17 | data-poll-push="URL" optional, beside data-poll: open an |
| 18 | EventSource to URL and re-fetch the fragment |
| 19 | when the server says so, instead of on a |
| 20 | timer. Any EventSource error downgrades this |
| 21 | element to timer polling for good (once, |
| 22 | no flapping); a browser without EventSource |
| 23 | never leaves the timer path |
| 24 | |
| 25 | Every poll carries the request header Rastrillo-Fragment: 1, which |
| 26 | marks the request as a shim poll so a handler can tell it apart from |
| 27 | direct navigation. Reserved: the framework's own handlers do not |
| 28 | read it today. |
| 29 | |
| 30 | A polled response may answer 204 with a Rastrillo-Location header |
| 31 | instead of a fragment; the shim navigates there, but only to a local |
| 32 | path. 403 and 404 end the poll for good — the job was swept, or was |
| 33 | never yours. Any other fetch error backs off (doubling to a 30s cap) |
| 34 | and keeps trying — a network blip must not strand a status page. |
| 35 | |
| 36 | The scheme cookie. fiesta.js applies a choice from the colour-scheme |
| 37 | menu in place, so its form never reaches the server; this file |
| 38 | writes the cookie that form would have set, from the rst-scheme |
| 39 | event fiesta.js dispatches, so the next render paints the same |
| 40 | scheme first. */ |
| 41 | (function () { |
| 42 | "use strict"; |
| 43 | |
| 44 | // The same rule sessions.SafeReturn enforces server-side: a same-site |
| 45 | // absolute path — starts with exactly one "/", no scheme, no |
| 46 | // backslash, no control characters (browsers strip tab/CR/LF before |
| 47 | // parsing, so "/\t/evil.example" would resolve scheme-relative). |
| 48 | // Anything laxer is an open redirect, here driven by a response |
| 49 | // header instead of a form field. |
| 50 | function localPath(to) { |
| 51 | return to.charAt(0) === "/" && to.charAt(1) !== "/" && |
| 52 | to.indexOf("\\") === -1 && !/[\u0000-\u001f\u007f]/.test(to); |
| 53 | } |
| 54 | |
| 55 | function poll(el) { |
| 56 | var base = (parseFloat(el.getAttribute("data-poll-every")) || 2) * 1000; |
| 57 | var wait = base; |
| 58 | var src = null; // open EventSource while pushing; null on the timer path |
| 59 | var busy = false; // a fetch is in flight |
| 60 | var queued = false; // an update landed mid-fetch; tick again after it |
| 61 | function stop() { |
| 62 | if (src) { src.close(); src = null; } |
| 63 | } |
| 64 | // One downgrade, never back: push hands this element to the timer. |
| 65 | function fallback() { |
| 66 | stop(); |
| 67 | schedule(); |
| 68 | } |
| 69 | function tick() { |
| 70 | if (busy) { queued = true; return; } |
| 71 | busy = true; |
| 72 | fetch(el.getAttribute("data-poll"), { headers: { "Rastrillo-Fragment": "1" } }) |
| 73 | .then(function (res) { |
| 74 | // Terminal, not an error to retry: retrying a 404 forever is |
| 75 | // noise, and following a signed-out redirect would swap a |
| 76 | // sign-in page into the fragment's slot. |
| 77 | if (res.status === 403 || res.status === 404) return null; |
| 78 | if (!res.ok && res.status !== 204) throw new Error("status " + res.status); |
| 79 | var to = res.headers.get("Rastrillo-Location"); |
| 80 | if (to) { |
| 81 | if (localPath(to)) window.location.assign(to); |
| 82 | return null; // navigating, or refusing to — either way, done |
| 83 | } |
| 84 | return res.text(); |
| 85 | }) |
| 86 | .then(function (html) { |
| 87 | busy = false; |
| 88 | if (html === null) { stop(); return; } // stopped |
| 89 | wait = base; // a healthy response resets the backoff |
| 90 | var tpl = document.createElement("template"); |
| 91 | tpl.innerHTML = html; |
| 92 | var next = tpl.content.firstElementChild; |
| 93 | if (!next) { stop(); return; } // fragment with no element: stop politely |
| 94 | el.replaceWith(next); |
| 95 | el = next; |
| 96 | if (!el.hasAttribute("data-poll")) { stop(); return; } |
| 97 | if (!src) { schedule(); return; } |
| 98 | // Pushing: a fragment that dropped data-poll-push falls back |
| 99 | // to the timer; otherwise catch up if an update was queued. |
| 100 | if (!el.hasAttribute("data-poll-push")) { fallback(); return; } |
| 101 | if (queued) { queued = false; tick(); } |
| 102 | }) |
| 103 | .catch(function () { |
| 104 | busy = false; |
| 105 | wait = Math.min(wait * 2, 30000); |
| 106 | if (!src) schedule(); // pushing: the next event retries instead |
| 107 | }); |
| 108 | } |
| 109 | function schedule() { setTimeout(tick, wait); } |
| 110 | var push = el.getAttribute("data-poll-push"); |
| 111 | if (push && window.EventSource) { |
| 112 | src = new EventSource(push); |
| 113 | src.addEventListener("update", function () { tick(); }); |
| 114 | src.addEventListener("done", function () { stop(); tick(); }); |
| 115 | src.addEventListener("gone", stop); |
| 116 | src.onerror = fallback; |
| 117 | return; |
| 118 | } |
| 119 | schedule(); |
| 120 | } |
| 121 | |
| 122 | // The cookie is the server's own (name, path, age, SameSite; see |
| 123 | // scheme.go), or a reload paints the scheme the reader just left. |
| 124 | // System is no cookie, as on the server. |
| 125 | document.addEventListener("rst-scheme", function (e) { |
| 126 | var s = e.detail && e.detail.scheme, on = s === "light" || s === "dark"; |
| 127 | document.cookie = "rastrillo_scheme=" + (on ? s + "; Max-Age=31536000" : "; Max-Age=0") + "; Path=/; SameSite=Lax" + (location.protocol === "https:" ? "; Secure" : ""); |
| 128 | }); |
| 129 | |
| 130 | function scan() { |
| 131 | document.querySelectorAll("[data-poll]").forEach(poll); |
| 132 | } |
| 133 | |
| 134 | // A prerendered page runs this before anyone is looking at it, and |
| 135 | // may never be shown: polling there would fetch on a timer, or hold an |
| 136 | // EventSource open, for a page the reader only hovered a link to. It |
| 137 | // starts when the page is activated instead. |
| 138 | function start() { |
| 139 | if (document.prerendering) document.addEventListener("prerenderingchange", scan, { once: true }); |
| 140 | else scan(); |
| 141 | } |
| 142 | |
| 143 | if (document.readyState === "loading") { |
| 144 | document.addEventListener("DOMContentLoaded", start); |
| 145 | } else { |
| 146 | start(); |
| 147 | } |
| 148 | })(); |
| 149 | |