gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit5e11184c5e11184cW60: record partial validation, framework gaps and cleanupmre5e11184c/lib/util.hl

7.1 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, trim = true) => {
  11. v = env(name)
  12. text = v != null && trim ? v.trim() : v
  13. return text != null && text != '' ? text : fallback
  14. }
  15. // THE TABLE DIRECTORY of every hl:mpackdb table (GITORIA_STORAGE, default ./storage/mpackdb). A relative path resolves
  16. // against the ENTRY SCRIPT's directory (project.hl's) — a tool in tools/ must get an absolute one.
  17. static storageDir = envOr('GITORIA_STORAGE', './storage/mpackdb')
  18. // ---- the addresses: main = <scheme>://<hostport>, a repo = <scheme>://<slug>.<hostport> ---------
  19. // GITORIA_PUBLIC_URL the main address, default https://gitoria.worldapi.org; repos live at <scheme>://<slug>.<host[:port] of this url>
  20. static publicUrl = envOr('GITORIA_PUBLIC_URL', 'https://gitoria.worldapi.org')
  21. static schemeEnd = publicUrl.indexOf('://')
  22. static scheme = publicUrl.slice(0, schemeEnd)
  23. static hostPort = publicUrl.slice(schemeEnd + 3).replaceAll('/', '')
  24. static hostOnly = hostPort.split(':')[0]
  25. // 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>
  26. static orgOrigin = (org) => { return scheme + '://' + org + '.' + hostPort }
  27. static repoOrigin = (slug) => {
  28. i = slug.indexOf('/')
  29. return i < 0 ? scheme + '://' + slug + '.' + hostPort : orgOrigin(slug.slice(0, i)) + '/' + slug.slice(i + 1)
  30. }
  31. // the git clone address over HTTPS: <slug>.<host>/<slug>.git, inside an organization <org>.<host>/<name>.git
  32. static cloneOrigin = (slug) => { return slug.includes('/') ? repoOrigin(slug) + '.git' : repoOrigin(slug) + '/' + slug + '.git' }
  33. // the path prefix of a repo's pages: '' on its own address, '/<name>' inside an organization
  34. static px = (slug) => {
  35. i = slug.indexOf('/')
  36. return i < 0 ? '' : '/' + slug.slice(i + 1)
  37. }
  38. // the session cookie's Domain: every <slug>.<host> shares the login. A host without a dot (localhost) or
  39. // an IP address gets a host-only cookie. GITORIA_COOKIE_DOMAIN overrides ('-' = host-only).
  40. static domainFor = (h) => {
  41. given = env('GITORIA_COOKIE_DOMAIN')
  42. if (given != null && given != '') { return given == '-' ? '' : given }
  43. if (!h.includes('.')) { return '' }
  44. last = h.charCodeAt(h.length - 1)
  45. if (last >= 48 && last <= 57) { return '' }
  46. return '.' + h
  47. }
  48. // THE REPO OF A REQUEST HOST (gitoria#16): hl:web hands a page component that declares `host = null` the
  49. // request's Host without port (hybriel#74). '' = the main address (or any other host) → the repo list;
  50. // else the label before .<hostOnly> (a slug that fails slugError, e.g. x.y, is "No such repository").
  51. static slugOfHost = (h) => {
  52. if (h == null || hlTypeName(h) != 'String') { return '' }
  53. x = h.toLowerCase()
  54. if (x == hostOnly) { return '' }
  55. tail = '.' + hostOnly
  56. return x.endsWith(tail) ? x.slice(0, x.length - tail.length) : ''
  57. }
  58. // ---- lists ---------------------------------------------------------------------------------------
  59. // an empty list read from hl:mpackdb comes back without a `.length`: every found list goes through here
  60. static countOfList = (list) => {
  61. if (list == null) { return 0 }
  62. n = list.length
  63. return n == null ? 0 : n
  64. }
  65. static firstOf = (list) => { return countOfList(list) > 0 ? list[0] : null }
  66. // rows newest first by `createdMs` (an insertion sort: no sort(), hybriel #1); sorts its copy and returns it
  67. static newestFirst = (rows) => {
  68. let i = 1
  69. while (i < rows.length) {
  70. cur = rows[i]
  71. let j = i - 1
  72. while (j >= 0 && rows[j].createdMs < cur.createdMs) {
  73. rows[j + 1] = rows[j]
  74. j = j - 1
  75. }
  76. rows[j + 1] = cur
  77. i = i + 1
  78. }
  79. return rows
  80. }
  81. // ---- text checks ---------------------------------------------------------------------------------
  82. // a short one-line text: trimmed, at most `max` characters, no control characters → null or the complaint
  83. static plainError = (s, max, what) => {
  84. if (s == null || hlTypeName(s) != 'String') { return what + ' must be text' }
  85. t = s.trim()
  86. if (t.length > max) { return what + ' is too long (at most ' + max + ' characters)' }
  87. let i = 0
  88. while (i < t.length) {
  89. c = t.charCodeAt(i)
  90. if (c < 32 || c == 127) { return what + ' must be one line without control characters' }
  91. i = i + 1
  92. }
  93. return null
  94. }
  95. // only lowercase hex (ident's one-time codes are 48 hex)
  96. static isHex = (s, max, min = 1) => {
  97. if (s == null || hlTypeName(s) != 'String' || s.length < min || s.length > max) { return false }
  98. let i = 0
  99. while (i < s.length) {
  100. c = s.charCodeAt(i)
  101. if (!((c >= 48 && c <= 57) || (c >= 97 && c <= 102))) { return false }
  102. i = i + 1
  103. }
  104. return true
  105. }
  106. // ASCII decimal digits and base64 alphabet; callers supply their existing length limits.
  107. static isDigit = (c) => { return c >= 48 && c <= 57 }
  108. static isDigits = (s, max) => {
  109. if (s == '' || s.length > max) { return false }
  110. let i = 0
  111. while (i < s.length) {
  112. if (!isDigit(s.charCodeAt(i))) { return false }
  113. i = i + 1
  114. }
  115. return true
  116. }
  117. static isBase64 = (s, max, min = 1) => {
  118. if (s.length < min || s.length > max) { return false }
  119. let i = 0
  120. while (i < s.length) {
  121. c = s.charCodeAt(i)
  122. if (!(isDigit(c) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122) || c == 43 || c == 47 || c == 61)) { return false }
  123. i = i + 1
  124. }
  125. return true
  126. }
  127. // ---- Vienna time (ticket #10): epoch ms → "YYYY-MM-DD HH:MM" in Europe/Vienna ------------------------
  128. // hl:time has no time zones (now() + a UTC timestamp() only; "timezones are a userland
  129. // concern" — its header). This is the smallest correct userland rule for Vienna: the EU
  130. // rule in force since 1996 — CEST (UTC+2) from the last Sunday of March 01:00 UTC to the
  131. // last Sunday of October 01:00 UTC, CET (UTC+1) otherwise. Storage stays epoch ms (UTC);
  132. // only the rendering shifts.
  133. static DAY = 86400000
  134. static HOUR = 3600000
  135. // integer division for a >= 0 (the language has `%` but no floor in the core)
  136. static idiv = (a, b) => { return (a - a % b) / b }
  137. // days since 1970-01-01 of a civil date (H. Hinnant's days_from_civil, years >= 1970)
  138. static daysFromCivil = (y, m, d) => {
  139. yy = m <= 2 ? y - 1 : y
  140. era = idiv(yy, 400)
  141. yoe = yy - era * 400
  142. mp = m > 2 ? m - 3 : m + 9
  143. doy = idiv(153 * mp + 2, 5) + d - 1
  144. doe = yoe * 365 + idiv(yoe, 4) - idiv(yoe, 100) + doy
  145. return era * 146097 + doe - 719468
  146. }
  147. // the last Sunday of a 31-day month (March, October), 01:00 UTC, as epoch ms
  148. static lastSundayAt1 = (y, m) => {
  149. days = daysFromCivil(y, m, 31)
  150. wd = (days + 4) % 7 // 0 = Sunday: 1970-01-01 was a Thursday
  151. return (days - wd) * DAY + HOUR
  152. }
  153. static viennaOffset = (ms) => {
  154. y = toNumber(timestamp(ms).slice(0, 4))
  155. return ms >= lastSundayAt1(y, 3) && ms < lastSundayAt1(y, 10) ? 2 * HOUR : HOUR
  156. }
  157. // "YYYY-MM-DD HH:MM", Vienna wall time
  158. static localStamp = (ms) => {
  159. t = timestamp(ms + viennaOffset(ms))
  160. return t.slice(0, 10) + ' ' + t.slice(11, 16)
  161. }

Branches

Latest commits

  • 5e11184cW60: record partial validation, framework gaps and cleanupmre
  • 20fe13fagitoria#33: centralize login and tickets connection statemre
  • 859754c0gitoria#29: retain the offline note and record the framework gapmre
  • d140bc04gitoria#30: reproduce the upsert ordering constraintmre
  • 6960e8f8gitoria#28: keep form values in bound component membersmre
  • 0db27200gitoria#32: share character checks and environment lookupmre
  • 452f6a1amission W60: logic clean-upsmre
  • 1448c899log: cleanup, #49 GPTBot, tracker mentions on #36mre
  • fcf95936log: v1.5.1 provenmre
  • 22105420log: W51b livemre
  • 7e8aabeareport: W51b pull source verificationmre
  • 6c18de76fix: preserve pull sources across containing branchesmre
  • fc36fa2elog: W51b pull source bugmre
  • 56bc1220log: v1.5.1 livemre
  • 845fc0aareport: W51mre
  • 83127391fix: return v1.5.1 app and merge error detailsmre
  • b07d0e97log: W51 v1.5.1 startedmre
  • 0b417d06docs: apps-api v1.5 user tokens (#48, W49)mre
  • d046940alog: vinzenz contract note sentmre
  • db443456log: vinzenz note retry 2mre