gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitc50d17efc50d17efKeep function-route not-found style in shared stylesheetmrec50d17ef/lib/util.hl

6.4 KB

  1. // lib/util.hl — small helpers every topic shares: env, the storage folder, the addresses (main / repo), lists, text
  2. // checks, the newest-first sort, Vienna time. Statics only; no topic logic here.
  3. import { env } from 'hl:proc'
  4. import { timestamp } from 'hl:time'
  5. // a newline (the language has no \n escape in a string: a literal line break, as in tickets mdview.hl)
  6. static NL = "
  7. "
  8. // ---- env and storage -----------------------------------------------------------------------------
  9. // Config (environment, or `.env` beside project.hl — never read or printed by workers)
  10. static envOr = (name, fallback) => {
  11. v = env(name)
  12. return v != null && v.trim() != '' ? v.trim() : fallback
  13. }
  14. // THE TABLE DIRECTORY of every hl:mpackdb table (GITORIA_STORAGE, default ./storage/mpackdb). A relative path resolves
  15. // against the ENTRY SCRIPT's directory (project.hl's) — a tool in tools/ must get an absolute one.
  16. static storageDir = envOr('GITORIA_STORAGE', './storage/mpackdb')
  17. // ---- the addresses: main = <scheme>://<hostport>, a repo = <scheme>://<slug>.<hostport> ---------
  18. // GITORIA_PUBLIC_URL the main address, default https://gitoria.worldapi.org; repos live at <scheme>://<slug>.<host[:port] of this url>
  19. static publicUrl = envOr('GITORIA_PUBLIC_URL', 'https://gitoria.worldapi.org')
  20. static schemeEnd = publicUrl.indexOf('://')
  21. static scheme = publicUrl.slice(0, schemeEnd)
  22. static hostPort = publicUrl.slice(schemeEnd + 3).replaceAll('/', '')
  23. static hostOnly = hostPort.split(':')[0]
  24. // a repo's full name `slug` is `name` (top level) or `org/name` (GO34): the first lives at <name>.<host>, the second at <org>.<host>/<name>
  25. static orgOrigin = (org) => { return scheme + '://' + org + '.' + hostPort }
  26. static repoOrigin = (slug) => {
  27. i = slug.indexOf('/')
  28. return i < 0 ? scheme + '://' + slug + '.' + hostPort : orgOrigin(slug.slice(0, i)) + '/' + slug.slice(i + 1)
  29. }
  30. // the git clone address over HTTPS: <slug>.<host>/<slug>.git, inside an organization <org>.<host>/<name>.git
  31. static cloneOrigin = (slug) => { return slug.includes('/') ? repoOrigin(slug) + '.git' : repoOrigin(slug) + '/' + slug + '.git' }
  32. // the path prefix of a repo's pages: '' on its own address, '/<name>' inside an organization
  33. static px = (slug) => {
  34. i = slug.indexOf('/')
  35. return i < 0 ? '' : '/' + slug.slice(i + 1)
  36. }
  37. // the session cookie's Domain: every <slug>.<host> shares the login. A host without a dot (localhost) or
  38. // an IP address gets a host-only cookie. GITORIA_COOKIE_DOMAIN overrides ('-' = host-only).
  39. static domainFor = (h) => {
  40. given = env('GITORIA_COOKIE_DOMAIN')
  41. if (given != null && given != '') { return given == '-' ? '' : given }
  42. if (!h.includes('.')) { return '' }
  43. last = h.charCodeAt(h.length - 1)
  44. if (last >= 48 && last <= 57) { return '' }
  45. return '.' + h
  46. }
  47. // THE REPO OF A REQUEST HOST (gitoria#16): hl:web hands a page component that declares `host = null` the
  48. // request's Host without port (hybriel#74). '' = the main address (or any other host) → the repo list;
  49. // else the label before .<hostOnly> (a slug that fails slugError, e.g. x.y, is "No such repository").
  50. static slugOfHost = (h) => {
  51. if (h == null || hlTypeName(h) != 'String') { return '' }
  52. x = h.toLowerCase()
  53. if (x == hostOnly) { return '' }
  54. tail = '.' + hostOnly
  55. return x.endsWith(tail) ? x.slice(0, x.length - tail.length) : ''
  56. }
  57. // ---- lists ---------------------------------------------------------------------------------------
  58. // an empty list read from hl:mpackdb comes back without a `.length`: every found list goes through here
  59. static countOfList = (list) => {
  60. if (list == null) { return 0 }
  61. n = list.length
  62. return n == null ? 0 : n
  63. }
  64. static firstOf = (list) => { return countOfList(list) > 0 ? list[0] : null }
  65. // rows newest first by `createdMs` (an insertion sort: no sort(), hybriel #1); sorts its copy and returns it
  66. static newestFirst = (rows) => {
  67. let i = 1
  68. while (i < rows.length) {
  69. cur = rows[i]
  70. let j = i - 1
  71. while (j >= 0 && rows[j].createdMs < cur.createdMs) {
  72. rows[j + 1] = rows[j]
  73. j = j - 1
  74. }
  75. rows[j + 1] = cur
  76. i = i + 1
  77. }
  78. return rows
  79. }
  80. // ---- text checks ---------------------------------------------------------------------------------
  81. // a short one-line text: trimmed, at most `max` characters, no control characters → null or the complaint
  82. static plainError = (s, max, what) => {
  83. if (s == null || hlTypeName(s) != 'String') { return what + ' must be text' }
  84. t = s.trim()
  85. if (t.length > max) { return what + ' is too long (at most ' + max + ' characters)' }
  86. let i = 0
  87. while (i < t.length) {
  88. c = t.charCodeAt(i)
  89. if (c < 32 || c == 127) { return what + ' must be one line without control characters' }
  90. i = i + 1
  91. }
  92. return null
  93. }
  94. // only lowercase hex (ident's one-time codes are 48 hex)
  95. static isHex = (s, max) => {
  96. if (s == null || hlTypeName(s) != 'String' || s.length == 0 || s.length > max) { return false }
  97. let i = 0
  98. while (i < s.length) {
  99. c = s.charCodeAt(i)
  100. if (!((c >= 48 && c <= 57) || (c >= 97 && c <= 102))) { return false }
  101. i = i + 1
  102. }
  103. return true
  104. }
  105. // ---- Vienna time (ticket #10): epoch ms → "YYYY-MM-DD HH:MM" in Europe/Vienna ------------------------
  106. // hl:time has no time zones (now() + a UTC timestamp() only; "timezones are a userland
  107. // concern" — its header). This is the smallest correct userland rule for Vienna: the EU
  108. // rule in force since 1996 — CEST (UTC+2) from the last Sunday of March 01:00 UTC to the
  109. // last Sunday of October 01:00 UTC, CET (UTC+1) otherwise. Storage stays epoch ms (UTC);
  110. // only the rendering shifts.
  111. static DAY = 86400000
  112. static HOUR = 3600000
  113. // integer division for a >= 0 (the language has `%` but no floor in the core)
  114. static idiv = (a, b) => { return (a - a % b) / b }
  115. // days since 1970-01-01 of a civil date (H. Hinnant's days_from_civil, years >= 1970)
  116. static daysFromCivil = (y, m, d) => {
  117. yy = m <= 2 ? y - 1 : y
  118. era = idiv(yy, 400)
  119. yoe = yy - era * 400
  120. mp = m > 2 ? m - 3 : m + 9
  121. doy = idiv(153 * mp + 2, 5) + d - 1
  122. doe = yoe * 365 + idiv(yoe, 4) - idiv(yoe, 100) + doy
  123. return era * 146097 + doe - 719468
  124. }
  125. // the last Sunday of a 31-day month (March, October), 01:00 UTC, as epoch ms
  126. static lastSundayAt1 = (y, m) => {
  127. days = daysFromCivil(y, m, 31)
  128. wd = (days + 4) % 7 // 0 = Sunday: 1970-01-01 was a Thursday
  129. return (days - wd) * DAY + HOUR
  130. }
  131. static viennaOffset = (ms) => {
  132. y = toNumber(timestamp(ms).slice(0, 4))
  133. return ms >= lastSundayAt1(y, 3) && ms < lastSundayAt1(y, 10) ? 2 * HOUR : HOUR
  134. }
  135. // "YYYY-MM-DD HH:MM", Vienna wall time
  136. static localStamp = (ms) => {
  137. t = timestamp(ms + viennaOffset(ms))
  138. return t.slice(0, 10) + ' ' + t.slice(11, 16)
  139. }

Branches

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