gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit7c2277cc7c2277ccgitoria#38: worker reports W40–W46, logmre7c2277cc/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

  • 7c2277ccgitoria#38: worker reports W40–W46, logmre
  • 1c3fa842gitoria#45 #47: worker briefsmre
  • 0a46bf7dgitoria#47: apps API contract v1.3 (status read route, statuses + required on the pull)mre
  • 3373f7c3gitoria#38: push API contract v1.2.1, reviews, worker briefs and logmre
  • 34143d2cgitoria#46: closing brace of appComments lost in the mergemre
  • 54927c9cgitoria#46: PR statuses and required checks before merge (merge w46-status)mre
  • 187e1871gitoria#46: badges without decorative bordersmre
  • 6b022798gitoria#46: tests/status.mjs in GATESmre
  • c6162f00gitoria#46: status badges on /pulls, Required before merge in Settingsmre
  • e5a0b64egitoria#46: PUT statuses/<sha>, required contexts block the merge on the servermre
  • 26a140e3gitoria#44: reviews, inline threads, replies, conversation for apps (merge w44-reviews)mre
  • 888dbf87gitoria#44: unknown API paths answer the contract's JSON 404 and sit above the page routesmre
  • ea587d90gitoria#44: reviews with inline threads, replies, resolve and the PR conversation for apps (lib/reviews.hl, tests/reviews.mjs)mre
  • 49533a50gitoria#43: signed events to connected apps (merge w43-events)mre
  • 1a1e87d5gitoria#43: the sender never writes the apps row; Settings shows the newest deliverymre
  • cd442066gitoria#43: app-auth unit test reads the apps table from its new filemre
  • 5dccbe38gitoria#43: in-flight rows are left alone by the other process; gate in deploy.sh; READMEmre
  • 8c8805f3merge main (gitoria#42), delivery log and redelivery routes, sweep fixmre
  • 5a996ea8gitoria#42: short-lived app tokens, read-only git fetch with them (merge w42-app-token)mre
  • afdb0cbfgitoria#43: rename the moved helper (name clash in the browser build)mre