|
| 1 | +// The stable entry point a reader can bookmark. |
| 2 | +// |
| 3 | +// A release directory is content-addressed, so its URL changes whenever the |
| 4 | +// release does — including when only the viewer bundle changed, since the |
| 5 | +// bundle is part of the digest. That is what makes a release immutable, and |
| 6 | +// it is also why a link to one rots. `latest.json` never moves but serves |
| 7 | +// JSON, so it cannot be the link you hand someone. |
| 8 | +// |
| 9 | +// This page sits beside `latest.json` at the publish root and forwards to |
| 10 | +// whichever release that pointer names, so `https://host/<prefix>/index.html` |
| 11 | +// renders the current dashboard and keeps doing so across republishes. |
| 12 | +// |
| 13 | +// It reads the pointer in the browser rather than carrying a release baked |
| 14 | +// into it, and that is the whole design: the bytes depend only on the |
| 15 | +// target's prefix, so every publish writes the same page and there is no |
| 16 | +// state to keep in step with the pointer. A page that named a release would |
| 17 | +// have to be rewritten on every publish and could fall behind — two |
| 18 | +// publishers racing, or a publish that promoted and then failed before |
| 19 | +// rewriting the page — and a dashboard silently showing an older release is |
| 20 | +// the worst way for this to break. Nothing here can drift, because nothing |
| 21 | +// here remembers anything. |
| 22 | +// |
| 23 | +// Reading the pointer needs script. So does the release it forwards to: the |
| 24 | +// viewer renders nothing without it. That costs no reader anything, and the |
| 25 | +// no-script path says so rather than linking somewhere equally unusable. |
| 26 | + |
| 27 | +/** |
| 28 | + * Marks the page as ours. `promoteEntryPoint` refuses to overwrite an |
| 29 | + * index.html without it, so publishing into a bucket that already serves a |
| 30 | + * site of its own does not clobber that site's front page. |
| 31 | + */ |
| 32 | +export const ENTRY_POINT_MARKER = "<!-- chainplot:entry-point -->"; |
| 33 | + |
| 34 | +/** How long the page stays blank before admitting something went wrong. */ |
| 35 | +const FALLBACK_DELAY_SECONDS = 4; |
| 36 | + |
| 37 | +/** |
| 38 | + * Embed a string in a script as a JSON literal. |
| 39 | + * |
| 40 | + * `</script>` inside a string ends the element early whatever JSON thinks, so |
| 41 | + * the slash is escaped too. |
| 42 | + */ |
| 43 | +function scriptLiteral(value: string): string { |
| 44 | + return JSON.stringify(value).replaceAll("/", "\\/"); |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * The path from the publish root to a release, which is what the page |
| 49 | + * forwards to. Relative on purpose: the same bytes work under a bucket root, |
| 50 | + * a custom domain, or a host that serves the bucket under some path of its |
| 51 | + * own. |
| 52 | + * |
| 53 | + * The result goes into a URL without percent-encoding, which is safe because |
| 54 | + * neither half can carry a character that would change the URL's shape: a |
| 55 | + * target prefix is `^[A-Za-z0-9][A-Za-z0-9._-]*(/[A-Za-z0-9._-]+)*$` in the |
| 56 | + * project schema, and a release id is hex. No `#`, `?`, `%` or space can |
| 57 | + * reach here. Widen that pattern and this needs encoding. |
| 58 | + */ |
| 59 | +export function relativeReleasePath( |
| 60 | + releasePrefix: string, |
| 61 | + keyPrefix: string | null, |
| 62 | +): string { |
| 63 | + if (!keyPrefix) return releasePrefix; |
| 64 | + const base = `${keyPrefix}/`; |
| 65 | + if (!releasePrefix.startsWith(base)) { |
| 66 | + throw new Error( |
| 67 | + `release prefix ${releasePrefix} is not under the target prefix ${keyPrefix}`, |
| 68 | + ); |
| 69 | + } |
| 70 | + return releasePrefix.slice(base.length); |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * The forwarding page for a target. |
| 75 | + * |
| 76 | + * Depends only on the prefix, so republishing a project writes identical |
| 77 | + * bytes however many times it runs. |
| 78 | + */ |
| 79 | +export function entryPointHtml(keyPrefix: string | null): string { |
| 80 | + const prefix = scriptLiteral(keyPrefix ?? ""); |
| 81 | + const delay = FALLBACK_DELAY_SECONDS; |
| 82 | + // `replace` rather than `assign`: the forwarding page must not become a |
| 83 | + // history entry, or Back from the dashboard lands here and bounces forward |
| 84 | + // again. `no-store` because a cached pointer is the staleness this page |
| 85 | + // exists to avoid. |
| 86 | + return `<!doctype html> |
| 87 | +${ENTRY_POINT_MARKER} |
| 88 | +<html lang="en"> |
| 89 | +<head> |
| 90 | +<meta charset="utf-8"> |
| 91 | +<meta name="viewport" content="width=device-width, initial-scale=1"> |
| 92 | +<title>Chainplot</title> |
| 93 | +<meta name="robots" content="noindex"> |
| 94 | +<style> |
| 95 | +body { margin: 0; font: 14px/1.5 system-ui, sans-serif; color: #33383d; } |
| 96 | +/* Hidden until something has gone wrong, so a redirect that works shows |
| 97 | + nothing at all rather than a message the reader cannot act on. */ |
| 98 | +#fallback { visibility: hidden; animation: reveal 0s ${delay}s forwards; padding: 2rem; } |
| 99 | +@keyframes reveal { to { visibility: visible; } } |
| 100 | +</style> |
| 101 | +<script> |
| 102 | +(function () { |
| 103 | + var PREFIX = ${prefix}; |
| 104 | + function relative(releasePrefix) { |
| 105 | + var base = PREFIX ? PREFIX + "/" : ""; |
| 106 | + if (base && releasePrefix.lastIndexOf(base, 0) !== 0) return null; |
| 107 | + return releasePrefix.slice(base.length); |
| 108 | + } |
| 109 | + function fail(message) { |
| 110 | + var note = document.getElementById("reason"); |
| 111 | + if (note) note.textContent = message; |
| 112 | + } |
| 113 | + if (typeof fetch !== "function") return; |
| 114 | + fetch("latest.json", { cache: "no-store" }) |
| 115 | + .then(function (response) { |
| 116 | + if (!response.ok) throw new Error("latest.json returned " + response.status); |
| 117 | + return response.json(); |
| 118 | + }) |
| 119 | + .then(function (pointer) { |
| 120 | + var path = pointer && pointer.release_prefix && relative(pointer.release_prefix); |
| 121 | + if (!path) throw new Error("latest.json names no release under this prefix"); |
| 122 | + location.replace(path + "/index.html"); |
| 123 | + }) |
| 124 | + .catch(function (err) { |
| 125 | + fail(String((err && err.message) || err)); |
| 126 | + }); |
| 127 | +})(); |
| 128 | +</script> |
| 129 | +</head> |
| 130 | +<body> |
| 131 | +<div id="fallback"> |
| 132 | +<p><strong>This page forwards to the current release.</strong></p> |
| 133 | +<p>It did not, which means <a href="latest.json">latest.json</a> could not be read. |
| 134 | +<span id="reason"></span></p> |
| 135 | +<noscript><p>It needs JavaScript, as does the dashboard it forwards to.</p></noscript> |
| 136 | +</div> |
| 137 | +</body> |
| 138 | +</html> |
| 139 | +`; |
| 140 | +} |
0 commit comments