gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commite972e8d7e972e8d7tests: short-id-switch (GITORIA_SWITCH_IDENT_PORT/_PORT) and ssh tunnel (GITORIA_GATE_TUNNEL_PORT) ports from env, defaults unchangedmree972e8d7/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

Latest commits

  • e972e8d7tests: short-id-switch (GITORIA_SWITCH_IDENT_PORT/_PORT) and ssh tunnel (GITORIA_GATE_TUNNEL_PORT) ports from env, defaults unchangedmre
  • f06f5349re-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre
  • c7d3cc87docker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
  • f0e7ff8cdeploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • fe5f707cgitoria 010: report, mission, comment on why current = true staysmre
  • 9705ca86gitoria 010: gate: menu marker in the server HTMLmre
  • 8cff663agitoria 010: main-menu.hl from layouts 0bcc629 (route marker)mre
  • 359c79f7gitoria 010: re-vendor Hybriel master f9614007 (plugins whole, bin/ git-ignored)mre
  • 6bd7fb3cgitoria 009: theme re-copied from layouts 76f99f6 (menu marks the current page), current = true on the menu item, e2e marker checks, reportmre
  • 44de915cidentkit: refuse a taken ident port, cold-start deadline, abort on dead ident, clear no-code error (as notes)mre
  • 2bf5343egitoria 007: theme re-copied from layouts dbe3de7 (44 px taps), redundant tap overrides dropped, crumbs class, real-browser walk test, reportmre
  • 18843648deploy.sh: a gate without a 'passed,' line (check-theme) no longer ends the script silently (grep under pipefail) — print check-theme's summary, as layouts doesmre
  • e6dff0cbgitoria#23: report 005, STATUS, LOGmre
  • 7f992d62gitoria#23: phone first on the shared theme — brand, branch and pull links 44 px, readme code 14 px; gate block "phone first" (15 pages at 390 + the three dropdowns, fails 20 on old), dropdown-aware login/logout clicks, check-theme in GATESmre
  • a2a840a5gitoria#23 WIP (quota stop): theme from layouts, styles inherit it, shell on the theme's header/menus/sidebar, phone measurement script; gates not runmre
  • 12b665b0mission brief 005mre
  • b680c483Revert "gitoria#22: RepoTabs wrapping component for the repo tabs (active tab, no content for a missing repo)"mre
  • 4fda244fgitoria#22: RepoTabs wrapping component for the repo tabs (active tab, no content for a missing repo)mre
  • 0fd5e280mission brief 004mre
  • e1d425e7paths: projects moved into /media/STORAGE/projects/WorldAPI/mre