gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit9da65b429da65b42tests/browser.mjs: 404 checks (host, no hydration wait)MG9da65b42/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

  • 9da65b42tests/browser.mjs: 404 checks (host, no hydration wait)MG
  • 00517fafgitoria: shell 404 page for unknown paths (catch-all route, status 404, JSON 404 under /api), 390 gate checksMG
  • d9e977f0reports/MG-mobile.md, LOGMG
  • 2f2725actests/browser.mjs: commit-row check (one row, centred date)MG
  • 4df1ea01tests/browser.mjs: MG phone checks (list first, no dead strip, one branch heading, tab grid, commit rows, PR title row, ticket columns, wrapping commands)MG
  • c52424e9gitoria mobile: repo list before the forms, 3x2 tab grid, no duplicate branch heading, ordered commit rows, aligned ticket rows, pull title on its own row, wrapping clone commands, no dead strip under formsMG
  • 3a95252dG3: cache parsed README blocks by sha256 of the text (bounded LRU), reportmre
  • 570138cddeploy: security gate (tests/security.mjs)mre
  • da6b20fcgitoria SG: security audit fixes (uploadMax 0, Secure cookie on https, session rotation at login, nosniff on function routes, no identity in page data) + tests/security.mjs gate + reportmre
  • f98c981fgitoria B1: report wordingmre
  • c6364cedgitoria B1: compact Markdown blocks (one kind flag per block/span, empty and false fields omitted); same rendered HTML, page values -67%mre
  • cd99d2feWIP gitoria#22: always render the repo level's slot (missing-repo boot hang), pages render nothing without a repo; hydration diagnosis in reports/G2-hydrate.mdmre
  • e5404b9breport: commit idsmre
  • 63cfa21ereports/G2-repotabs.mdmre
  • 9b0516fdgitoria#22: mark the tab underline functional (check-theme), chain E2E: serve/measure split, bring the lisbeth tab to frontmre
  • 19bc1df8gitoria#22: all repo pages under ONE layout level (components/repo.hl, hl:web D-CHAIN): a tab click replaces only the page in the slot; head and tab nav stay the same DOM nodesmre
  • 40658417tests/browser.mjs: wait for the page to be live (hydrated) before clicking Merge — a click on the server-rendered button before the client mounted was lost (flaky 'Clashing change' under load)mre
  • 48a6154bre-vendor Hybriel 5406d146 (hl:web batches 9+10: #72 emits while the socket is closed delivered after reconnect, browser boot linear, faster list appends; contains int 6–14 + m406)mre
  • 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