gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit90b4f72890b4f728gitoria#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 removedmre90b4f728/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

  • 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
  • 5b0b4b9ereadme-cache gate: use the security gate's ports when no own ports are givenmre
  • bc8f20d9gitoria#25: re-vendor Hybriel c09538bb with the W141 render cache, cache the homepage per commit, add the readme-cache gatemre
  • 9feebbd3Re-vendor Hybriel to ded47c13 (W145: Firefox start script fix)mre
  • 28ed41e5gitoria#24: one sign-in comes from hl:web batch 20, the /session/share workaround is gonemre
  • 3b2669e4gs: drop the unbuildable legacy check, fix pwa fetchmre
  • 2a2fdd77gs: C2 check before cookie experimentsmre
  • 76e0587aWIP gs 2mre
  • a80da7bdWIP gs: security gate for lazy sessions, g24 browser checksmre
  • 23fb05d8re-vendor Hybriel aaa39e69 (hl:web batch 20: login emit and Sessions.mint, tab follows a login, signed-in session cookie wins among duplicates and is re-issued with the Domain, W85 faster render, lazy sessions)mre
  • a0727ca3ident#31: the selector's sign-in returns to the page the user started on (return attribute = the login callback with next), gate checkmre
  • a06975f9gitoria: a gap between your name and Log out in the phone header (they touched); gate checks itmre
  • 32ba9d59gitoria: leave the host-only cookie experiment out of the browser gate (gitoria#24 is a separate mission)mre