gitoriaLog in with ident

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchmain4f47843egate: ticket links use the tickets short URL (/<slug>/<n>, tickets#25)mremain/lib/util.hl

5.7 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. static repoOrigin = (slug) => { return scheme + '://' + slug + '.' + hostPort }
  25. // the session cookie's Domain: every <slug>.<host> shares the login. A host without a dot (localhost) or
  26. // an IP address gets a host-only cookie. GITORIA_COOKIE_DOMAIN overrides ('-' = host-only).
  27. static domainFor = (h) => {
  28. given = env('GITORIA_COOKIE_DOMAIN')
  29. if (given != null && given != '') { return given == '-' ? '' : given }
  30. if (!h.includes('.')) { return '' }
  31. last = h.charCodeAt(h.length - 1)
  32. if (last >= 48 && last <= 57) { return '' }
  33. return '.' + h
  34. }
  35. // THE REPO OF A REQUEST HOST (gitoria#16): hl:web hands a page component that declares `host = null` the
  36. // request's Host without port (hybriel#74). '' = the main address (or any other host) → the repo list;
  37. // else the label before .<hostOnly> (a slug that fails slugError, e.g. x.y, is "No such repository").
  38. static slugOfHost = (h) => {
  39. if (h == null || hlTypeName(h) != 'String') { return '' }
  40. x = h.toLowerCase()
  41. if (x == hostOnly) { return '' }
  42. tail = '.' + hostOnly
  43. return x.endsWith(tail) ? x.slice(0, x.length - tail.length) : ''
  44. }
  45. // ---- lists ---------------------------------------------------------------------------------------
  46. // an empty list read from hl:mpackdb comes back without a `.length`: every found list goes through here
  47. static countOfList = (list) => {
  48. if (list == null) { return 0 }
  49. n = list.length
  50. return n == null ? 0 : n
  51. }
  52. static firstOf = (list) => { return countOfList(list) > 0 ? list[0] : null }
  53. // rows newest first by `createdMs` (an insertion sort: no sort(), hybriel #1); sorts its copy and returns it
  54. static newestFirst = (rows) => {
  55. let i = 1
  56. while (i < rows.length) {
  57. let cur = rows[i]
  58. let j = i - 1
  59. while (j >= 0 && rows[j].createdMs < cur.createdMs) {
  60. rows[j + 1] = rows[j]
  61. j = j - 1
  62. }
  63. rows[j + 1] = cur
  64. i = i + 1
  65. }
  66. return rows
  67. }
  68. // ---- text checks ---------------------------------------------------------------------------------
  69. // a short one-line text: trimmed, at most `max` characters, no control characters → null or the complaint
  70. static plainError = (s, max, what) => {
  71. if (s == null || hlTypeName(s) != 'String') { return what + ' must be text' }
  72. t = s.trim()
  73. if (t.length > max) { return what + ' is too long (at most ' + max + ' characters)' }
  74. let i = 0
  75. while (i < t.length) {
  76. let c = t.charCodeAt(i)
  77. if (c < 32 || c == 127) { return what + ' must be one line without control characters' }
  78. i = i + 1
  79. }
  80. return null
  81. }
  82. // only lowercase hex (ident's one-time codes are 48 hex)
  83. static isHex = (s, max) => {
  84. if (s == null || hlTypeName(s) != 'String' || s.length == 0 || s.length > max) { return false }
  85. let i = 0
  86. while (i < s.length) {
  87. let c = s.charCodeAt(i)
  88. if (!((c >= 48 && c <= 57) || (c >= 97 && c <= 102))) { return false }
  89. i = i + 1
  90. }
  91. return true
  92. }
  93. // ---- Vienna time (ticket #10): epoch ms → "YYYY-MM-DD HH:MM" in Europe/Vienna ------------------------
  94. // hl:time has no time zones (now() + a UTC timestamp() only; "timezones are a userland
  95. // concern" — its header). This is the smallest correct userland rule for Vienna: the EU
  96. // rule in force since 1996 — CEST (UTC+2) from the last Sunday of March 01:00 UTC to the
  97. // last Sunday of October 01:00 UTC, CET (UTC+1) otherwise. Storage stays epoch ms (UTC);
  98. // only the rendering shifts.
  99. static DAY = 86400000
  100. static HOUR = 3600000
  101. // integer division for a >= 0 (the language has `%` but no floor in the core)
  102. static idiv = (a, b) => { return (a - a % b) / b }
  103. // days since 1970-01-01 of a civil date (H. Hinnant's days_from_civil, years >= 1970)
  104. static daysFromCivil = (y, m, d) => {
  105. yy = m <= 2 ? y - 1 : y
  106. era = idiv(yy, 400)
  107. yoe = yy - era * 400
  108. mp = m > 2 ? m - 3 : m + 9
  109. doy = idiv(153 * mp + 2, 5) + d - 1
  110. doe = yoe * 365 + idiv(yoe, 4) - idiv(yoe, 100) + doy
  111. return era * 146097 + doe - 719468
  112. }
  113. // the last Sunday of a 31-day month (March, October), 01:00 UTC, as epoch ms
  114. static lastSundayAt1 = (y, m) => {
  115. days = daysFromCivil(y, m, 31)
  116. wd = (days + 4) % 7 // 0 = Sunday: 1970-01-01 was a Thursday
  117. return (days - wd) * DAY + HOUR
  118. }
  119. static viennaOffset = (ms) => {
  120. y = toNumber(timestamp(ms).slice(0, 4))
  121. return ms >= lastSundayAt1(y, 3) && ms < lastSundayAt1(y, 10) ? 2 * HOUR : HOUR
  122. }
  123. // "YYYY-MM-DD HH:MM", Vienna wall time
  124. static localStamp = (ms) => {
  125. t = timestamp(ms + viennaOffset(ms))
  126. return t.slice(0, 10) + ' ' + t.slice(11, 16)
  127. }

Branches

  • mainmain branch

Latest commits

  • 4f47843egate: ticket links use the tickets short URL (/<slug>/<n>, tickets#25)mre
  • 86605446mission 002 (code order) 4/4: README file map + import order + 'Same output' test + gate run with a tickets HEAD copy, STATUS (entry, lessons), LOG, report; tests/realdata-baseline.mjs + realdata-compare.py (a cleanup answers the same on live data: pages, modules, API, git over HTTPS and SSH, faces), tests/letcount.pymre
  • cc7bf7bamission 002 (code order) 3/4: let only where a variable is reassigned or re-bound in a loop body (289 lets → plain declarations; 213 left: 125 reassigned, 88 loop-bound; no member/import/param clash). gates 200/0, 46/0, 44/0; real-data reads + writes identical (browser modules: var → const only)mre
  • 090a20c6mission 002 (code order) 2/4: one lib/ file per topic — git.hl split into git (calls, branches, init, temp folder) / homepage / code / pulls / releases (+ git-helpers: paths, ids, |||PR/|||RL markers); repos-helpers, tickets-helpers, transport-helpers; util.hl = localtime + env, storage dir, addresses, lists, text checks, one newest-first sort (was 3 copies); the function routes out of project.hl into lib/api.hl (thin; plumbing in api-helpers.hl), sshgate.hl folded into api.hl + sshkeys.hl keyLine + repos.hl mayPush; 'Make main' and the merge answer out of the faces (code.hl makeMain, pulls.hl pullsView), one login helper (users.hl userOfLoginCode); project.hl is the map. Session-writing routes get &req + &server.sessions. gates 200/0, 46/0, 44/0; real-data identical except /login/failed now shows the parked reason for a browser that already had a session (the old copy-of-req lost it)mre
  • 110c2799mission 002 (code order) 1/4: .hl files out of the root — lib/ (api, git, localtime, markdown, repos, sshgate, sshkeys, tickets, tokens, transport, users), components/styles.hl; jsoncheck.hl removed (imported nowhere); import paths only. gates 200/0, 46/0, 44/0; real-data reads + writes identicalmre
  • fdfb4b1bgitoria: Hybriel master 06617221 (plugin allocators 3a781359 + 413f60e4, mpackdb 2cb7ae5e, http1 773de63e); gates 200/0, 46/0, 44/0mre
  • 5b46ac84antcolony#40: LOG.md — missions 069/072 are antcolony missions (report paths on Byrodin)mre
  • 5602ff41gitoria: Hybriel master 190aa11d (fc838894 GC correctness, #127 mountKids by reference, #126, #48) — tracker README flat; gates 200/0, 46/0, 44/0mre
  • e85eaf01gitoria: 069 round 2 — hybriel 1a096ad3 not adopted (Markdown SSR still grows); browser gate waits for the server-side logout before restartmre
  • 09ce4f3fgitoria: mission 069 re-vendor hybriel 8efba065 stopped (big SSR pages grow + slow down); lambda audit clean; old vendor keptmre
  • 3dc43108antcolony#40: mission references point to the moved missionsmre
  • 8d9450fdantcolony#40: history (LOG.md), worker briefs (missions/) and reports moved here from antcolony, numbered per project; old numbers in antcolony docs/mission-map.mdmre
  • 205d5fe4gitoria: Hybriel master ff51cf46; ssh keys/tokens no double rows (session sync); gates follow #20mre
  • 9b27cb26gitoria#21: installable app (manifest, service worker, offline start page), own iconmre
  • 68dcb603deploy.sh: back up live storage/.sessions/.env before every deploy (newest 5 kept)mre
  • e2deed6dgitoria#20: "Add code" only on the Code page of an empty repository, no collapsiblemre
  • 8bb97ffddeploy.sh: never send .git or .gitignore to Byrodinmre
  • fd981932State of 2026-09-27; bin/ no longer tracked (Hybriel commit is in README)mre
  • 4a2d7125initial commitmre