gitoria
All repositories: gitoria
5.2 KB
// lib/homepage.hl — THE REPO HOMEPAGE (ticket gitoria#8; the creator, ticket #1): the repo's `/` shows README.md as a page;// if there is a `$docs` folder, all Markdown files inside it form the homepage instead. Read from the repo's main// branch (the owner's setting, git.hl defaultBranch) while the page is built on the server (homepageNow).import { NL } from './util.hl'import { git, pickDefault, maxTree } from './git.hl'import { safePath, safeName } from './git-helpers.hl'import { markdownBlocks } from './markdown.hl'import { sha256 } from 'hl:crypto'static maxFiles = 30 // Markdown files of a $docs folderstatic maxLines = 5000 // lines of one file// ---- the homepage cache (gitoria#25) -------------------------------------------------------------// A README request used to run git ls-tree and git cat-file and parse the Markdown on every single request (about 21 ms of// the server's time even for a small README). The homepage is a pure function of the repo's commit, so it is kept under// "slug + commit id of the main branch": a push gives the branch a new commit id, hence a new key — nothing to invalidate,// a stale entry can only be unused. The hit costs one git call (for-each-ref, which also gives the commit id). BOUNDED: at// most hpMaxEntries homepages and hpMaxChars characters of source in total (the cost of an entry is its text length); the// least recently used goes first; a homepage over hpMaxOne characters is built but not kept. Only a found homepage is kept// (a failed git call must not stick). Flat statics written key by key, like lib/markdown.hl.static hpMaxEntries = 64static hpMaxChars = 8000000static hpMaxOne = 2000000static hpCache = {} // "slug commit" → the homepage resultstatic hpSize = {} // "slug commit" → characters of its textsstatic hpOrder = [] // keys, least recently used firststatic hpChars = [0] // the sum of hpSize (one slot: a static number cannot be written from a lambda)static hpStats = [0, 0] // [hits, builds]: the gates read it through homepageStats// how often the cache answered and how often the homepage was builtstatic homepageStats = () => { return { hits = hpStats[0] builds = hpStats[1] } }static hpTouch = (key) => {rest = []for (k of hpOrder) { if (k != key) { rest.push(k) } }rest.push(key)hpOrder = restreturn null}static hpKeep = (key, result, chars) => {if (chars > hpMaxOne) { return null }while (hpOrder.length > 0 && (hpOrder.length >= hpMaxEntries || hpChars[0] + chars > hpMaxChars)) {old = hpOrder[0]hpChars[0] = hpChars[0] - hpSize[old]delete hpCache[old]delete hpSize[old]hpOrder = hpOrder.slice(1)}hpCache[key] = resulthpSize[key] = charshpChars[0] = hpChars[0] + charshpOrder.push(key)return null}// the homepage of a repo, SYNCHRONOUSLY (git() calls back before it returns): what the Readme page's root usesstatic homepageNow = (slug, setting) => {let out = { files = [] found = false truncated = false }// one git call: every branch with its commit idnames = []ids = {}git(slug, ['for-each-ref', '--format=%(objectname) %(refname:short)', 'refs/heads'], 500, (r) => {if (r.ok) {for (l of r.lines) {sp = l.indexOf(' ')if (sp > 0) {n = l.slice(sp + 1)if (safeName(n)) {names.push(n)ids[n] = l.slice(0, sp)}}}}return null})ref = pickDefault(names, setting)if (ref == null) { return out }key = slug + ' ' + ids[ref]hit = hpCache[key]if (hit != null) {hpTouch(key)hpStats[0] = hpStats[0] + 1return hit}hpStats[1] = hpStats[1] + 1loadHomepage(slug, ids[ref], (result) => {out = resultreturn null})if (out.found) { hpKeep(key, out, out.chars) }return out}static isMarkdown = (path) => { return path.toLowerCase().endsWith('.md') }// the files that make the homepage: [] (none), or the paths in tree orderstatic homepagePaths = (tree) => {docs = []let readme = nullfor (path of tree) {if (safePath(path)) {if (path.startsWith('$docs/') && isMarkdown(path) && docs.length < maxFiles) { docs.push(path) }if (readme == null && !path.includes('/') && path.toLowerCase() == 'readme.md') { readme = path }}}if (docs.length > 0) { return docs }return readme != null ? [readme] : []}// the homepage of a branch (its ref) → done({ files: [{ path, isDocs, blocks }], found, truncated })static loadHomepage = (slug, ref, done) => {if (ref == null) {done({ files = [] found = false truncated = false })return null}git(slug, ['ls-tree', '-r', '--name-only', ref], maxTree, (t) => {paths = t.ok ? homepagePaths(t.lines) : []let files = []let truncated = falselet chars = 0next = (i) => {if (i >= paths.length) {done({ files = files found = files.length > 0 truncated = truncated chars = chars })return null}git(slug, ['cat-file', 'blob', ref + ':' + paths[i]], maxLines + 1, (r) => {if (r.ok) {if (r.lines.length > maxLines) { truncated = true }lines = r.lines.length > maxLines ? r.lines.slice(0, maxLines) : r.linestext = lines.join(NL)chars = chars + text.lengthfiles.push({ path = paths[i] isDocs = paths[i].startsWith('$docs/') hash = sha256(text) blocks = markdownBlocks(text) })}next(i + 1)return null})return null}next(0)return null})}
Branches
- f1-broken-json
- f2-api-bugs
- gp-gate-ports
- heap-revert
- hl25-records
- ident-chooser-selector
- mainmain branch
- p51-public-pr-reads
- records-1112
- s25-no-session-on-login-error
- v59
- w52-person-approval
- w52b-approval-followup
- w52c-header-phone
- w53-pulls-speed-pr
- w54-repo-rename-pr
- w55-rename-org
- w56-owner-members
- w62-pr-close
- w63-force-push
- w64-org-slash
Latest commits
- c50d17efKeep function-route not-found style in shared stylesheetmre
- e42d78d3Preserve ticket state CSS and document function-route style gapmre
- c6b7154eWIP gitoria#26: move styles into nested component blocksmre
- 77a2c31cgitoria#31: plain declarations instead of needless let in lib/ and tools/ (103 let removed, 137 kept because reassigned); no other changemre
- 4b98ab62gitoria: an ident access key (idt_) works as the git password; read key clones only, bad key 401, ident down 503 (ident#27); gate tests/ident-access-gitoria.mjs in deploy.shmre
- 1185f686gitoria#36: a failed return from tickets shows a note on the settings page and leaves one log line per outcome; no code is exchangedmre
- 5d2c385agitoria: the sign-in callback keeps the session it finds and signs it in (same cookie, nothing signed out; ident#28); security gate 71+5 failing → 76 passed; deploy.sh runs tests/private.mjs toomre
- 241ef3f9deploy.sh: rebuild the sshd image before up -d (GO34c)mre
- 821cdbb1gitoria#34b: styles, create-repo tool, docs, push/ssh gates sign in through the selector, shots scriptmre
- a81abdc9gitoria#34: org page via function route, member-reachable orgs, selects, reportmre
- 17f031f0gitoria#34: private repos, organizations, members, move tool, gatemre
- 71b69343gitoria#23: the sidebar's Repositories link was a 404 after any in-page navigation (static menu re-evaluated publicUrl in the browser); the menu is an instance property now; gate taps it after a tab, folder, filemre
- 90b4f728gitoria#25: restore the homepage cache and the parsed-blocks cache (they store data under immutable keys, no HTML); the page key, the framework readme key and the textHash key stay removedmre
- a487248cgitoria#25: remove every cache that hid the page build (page key, Markdown render key, homepage cache, parsed-blocks cache) and the gate that only tested themmre
- 12f2f4fegitoria: re-vendor Hybriel set-day27b, a startup fault in on Error exits 1mre
- c21c084egitoria: write the standby service out in full so the boot gate can read the main servicemre
- e307d69agitoria: say what is per process during the hand-overmre
- f0cbe6a6gitoria: standby service on port 45014 and a deploy that hands over through it without a gapmre
- 727cd083gitoria#25: README: vendor notes for Hybriel c1b1010f and the page cachemre
- 61b72dedgitoria#25: re-vendor Hybriel master c1b1010f (W149 page cache), key the repo Readme page by slug and main commit, extend the readme-cache gatemre