oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1// The editor bundle (spec §3.1, §8: the one surface that needs
2// scripts) is a committed, prebuilt artifact — never built by this app
3// or by CI — produced by fixtures/build-bundles.mjs and committed under
4// internal/slides/static/ as editor.<hash>.js. Docs' assets.go
5// pattern, adopted: the bundle's name is resolved once at package init
6// and the process refuses to start without it.
7//
8// It lives in static/ beside the vendored stylesheets rather than in a
9// tree of its own because render.go's assets already embeds and
10// fingerprints everything under static/. The bundle carries a content
11// hash in its own name (so a build that changed the bytes changes the
12// URL) and assets.Path adds a second, which is harmless: the asset
13// handler strips one hash to find the file and serves it immutable
14// when the hash it stripped matches the content, which it always does.
15package slides
16
17import (
18 "fmt"
19 "io/fs"
20 "strings"
21)
22
23// bundleKinds is every bundle that must exist. Listed rather than
24// discovered from the directory: a build that produced no bundle at all
25// would otherwise start cleanly and serve an editor page whose script
26// tag 404s, which is the same silent-failure shape the exactly-one rule
27// below exists to prevent. The editor, and the font gate's two
28// (fontgate.go): the hold, a classic script for <head>, and the gate.
29var bundleKinds = []string{"editor", "fontsgate", "fontshold", "presenter"}
30
31// bundleNames maps each committed bundle's kind to its filename under
32// static/ — "editor" to "editor.f13f5e0ce6fa7467.js" — resolved once,
33// at package init, from the embedded tree.
34//
35// Exactly one file must ever be present per kind.
36// fixtures/build-bundles.mjs deletes every previous "<kind>.<hash>.js"
37// before writing the new one, so a tree with two (a script edited to
38// skip that step, a hand-added file, a bad merge) is a mistake — this
39// panics at init rather than silently picking one, because a wrong
40// bundle silently served is indistinguishable from a correct one until
41// it disagrees with the server at runtime.
42var bundleNames = mustResolveBundleNames()
43
44// isBundleFile reports whether name is the shape build-bundles.mjs
45// writes: the kind, a dot, sixteen hex digits, ".js".
46func isBundleFile(kind, name string) bool {
47 rest, ok := strings.CutPrefix(name, kind+".")
48 if !ok {
49 return false
50 }
51 h, ok := strings.CutSuffix(rest, ".js")
52 if !ok || len(h) != 16 {
53 return false
54 }
55 for _, c := range h {
56 if (c < '0' || c > '9') && (c < 'a' || c > 'f') {
57 return false
58 }
59 }
60 return true
61}
62
63func mustResolveBundleNames() map[string]string {
64 entries, err := fs.ReadDir(appFS, "static")
65 if err != nil {
66 panic(fmt.Sprintf("slides: read embedded static/: %v", err))
67 }
68 out := make(map[string]string, len(bundleKinds))
69 for _, kind := range bundleKinds {
70 var found []string
71 for _, e := range entries {
72 if !e.IsDir() && isBundleFile(kind, e.Name()) {
73 found = append(found, e.Name())
74 }
75 }
76 if len(found) != 1 {
77 panic(fmt.Sprintf("slides: want exactly one %q bundle in internal/slides/static/, found %v — run `node fixtures/build-bundles.mjs` and commit its output", kind, found))
78 }
79 out[kind] = found[0]
80 }
81 return out
82}
83
84// bundlePath is the URL a committed bundle is served at, through the
85// same fingerprinting handler every stylesheet goes through. The
86// template's <script> tag uses this rather than a literal, so it can
87// never drift from what the handler actually serves.
88func bundlePath(kind string) string {
89 name, ok := bundleNames[kind]
90 if !ok {
91 // Unreachable while every caller passes a member of
92 // bundleKinds, which init has already proved resolvable.
93 panic(fmt.Sprintf("slides: no committed bundle named %q", kind))
94 }
95 return assets.Path("static/" + name)
96}
97
98// editorBundlePath is the editor's bundle — templates/editor.html's
99// <script type="module"> tag.
100func editorBundlePath() string { return bundlePath("editor") }
101
102// presenterBundlePath is the presenting windows' bundle: the presenter
103// window and the slide window of a session with a position, moving at
104// once (fixtures/presenter-entry.mjs).
105func presenterBundlePath() string { return bundlePath("presenter") }
106