gitoria
All repositories: gitoria
6.4 KB
// lib/util.hl — small helpers every topic shares: env, the storage folder, the addresses (main / repo), lists, text// checks, the newest-first sort, Vienna time. Statics only; no topic logic here.import { env } from 'hl:proc'import { timestamp } from 'hl:time'// a newline (the language has no \n escape in a string: a literal line break, as in tickets mdview.hl)static NL = ""// ---- env and storage -----------------------------------------------------------------------------// Config (environment, or `.env` beside project.hl — never read or printed by workers)static envOr = (name, fallback) => {v = env(name)return v != null && v.trim() != '' ? v.trim() : fallback}// THE TABLE DIRECTORY of every hl:mpackdb table (GITORIA_STORAGE, default ./storage/mpackdb). A relative path resolves// against the ENTRY SCRIPT's directory (project.hl's) — a tool in tools/ must get an absolute one.static storageDir = envOr('GITORIA_STORAGE', './storage/mpackdb')// ---- the addresses: main = <scheme>://<hostport>, a repo = <scheme>://<slug>.<hostport> ---------// GITORIA_PUBLIC_URL the main address, default https://gitoria.worldapi.org; repos live at <scheme>://<slug>.<host[:port] of this url>static publicUrl = envOr('GITORIA_PUBLIC_URL', 'https://gitoria.worldapi.org')static schemeEnd = publicUrl.indexOf('://')static scheme = publicUrl.slice(0, schemeEnd)static hostPort = publicUrl.slice(schemeEnd + 3).replaceAll('/', '')static hostOnly = hostPort.split(':')[0]// 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>static orgOrigin = (org) => { return scheme + '://' + org + '.' + hostPort }static repoOrigin = (slug) => {i = slug.indexOf('/')return i < 0 ? scheme + '://' + slug + '.' + hostPort : orgOrigin(slug.slice(0, i)) + '/' + slug.slice(i + 1)}// the git clone address over HTTPS: <slug>.<host>/<slug>.git, inside an organization <org>.<host>/<name>.gitstatic cloneOrigin = (slug) => { return slug.includes('/') ? repoOrigin(slug) + '.git' : repoOrigin(slug) + '/' + slug + '.git' }// the path prefix of a repo's pages: '' on its own address, '/<name>' inside an organizationstatic px = (slug) => {i = slug.indexOf('/')return i < 0 ? '' : '/' + slug.slice(i + 1)}// the session cookie's Domain: every <slug>.<host> shares the login. A host without a dot (localhost) or// an IP address gets a host-only cookie. GITORIA_COOKIE_DOMAIN overrides ('-' = host-only).static domainFor = (h) => {given = env('GITORIA_COOKIE_DOMAIN')if (given != null && given != '') { return given == '-' ? '' : given }if (!h.includes('.')) { return '' }last = h.charCodeAt(h.length - 1)if (last >= 48 && last <= 57) { return '' }return '.' + h}// THE REPO OF A REQUEST HOST (gitoria#16): hl:web hands a page component that declares `host = null` the// request's Host without port (hybriel#74). '' = the main address (or any other host) → the repo list;// else the label before .<hostOnly> (a slug that fails slugError, e.g. x.y, is "No such repository").static slugOfHost = (h) => {if (h == null || hlTypeName(h) != 'String') { return '' }x = h.toLowerCase()if (x == hostOnly) { return '' }tail = '.' + hostOnlyreturn x.endsWith(tail) ? x.slice(0, x.length - tail.length) : ''}// ---- lists ---------------------------------------------------------------------------------------// an empty list read from hl:mpackdb comes back without a `.length`: every found list goes through herestatic countOfList = (list) => {if (list == null) { return 0 }n = list.lengthreturn n == null ? 0 : n}static firstOf = (list) => { return countOfList(list) > 0 ? list[0] : null }// rows newest first by `createdMs` (an insertion sort: no sort(), hybriel #1); sorts its copy and returns itstatic newestFirst = (rows) => {let i = 1while (i < rows.length) {cur = rows[i]let j = i - 1while (j >= 0 && rows[j].createdMs < cur.createdMs) {rows[j + 1] = rows[j]j = j - 1}rows[j + 1] = curi = i + 1}return rows}// ---- text checks ---------------------------------------------------------------------------------// a short one-line text: trimmed, at most `max` characters, no control characters → null or the complaintstatic plainError = (s, max, what) => {if (s == null || hlTypeName(s) != 'String') { return what + ' must be text' }t = s.trim()if (t.length > max) { return what + ' is too long (at most ' + max + ' characters)' }let i = 0while (i < t.length) {c = t.charCodeAt(i)if (c < 32 || c == 127) { return what + ' must be one line without control characters' }i = i + 1}return null}// only lowercase hex (ident's one-time codes are 48 hex)static isHex = (s, max) => {if (s == null || hlTypeName(s) != 'String' || s.length == 0 || s.length > max) { return false }let i = 0while (i < s.length) {c = s.charCodeAt(i)if (!((c >= 48 && c <= 57) || (c >= 97 && c <= 102))) { return false }i = i + 1}return true}// ---- Vienna time (ticket #10): epoch ms → "YYYY-MM-DD HH:MM" in Europe/Vienna ------------------------// hl:time has no time zones (now() + a UTC timestamp() only; "timezones are a userland// concern" — its header). This is the smallest correct userland rule for Vienna: the EU// rule in force since 1996 — CEST (UTC+2) from the last Sunday of March 01:00 UTC to the// last Sunday of October 01:00 UTC, CET (UTC+1) otherwise. Storage stays epoch ms (UTC);// only the rendering shifts.static DAY = 86400000static HOUR = 3600000// integer division for a >= 0 (the language has `%` but no floor in the core)static idiv = (a, b) => { return (a - a % b) / b }// days since 1970-01-01 of a civil date (H. Hinnant's days_from_civil, years >= 1970)static daysFromCivil = (y, m, d) => {yy = m <= 2 ? y - 1 : yera = idiv(yy, 400)yoe = yy - era * 400mp = m > 2 ? m - 3 : m + 9doy = idiv(153 * mp + 2, 5) + d - 1doe = yoe * 365 + idiv(yoe, 4) - idiv(yoe, 100) + doyreturn era * 146097 + doe - 719468}// the last Sunday of a 31-day month (March, October), 01:00 UTC, as epoch msstatic lastSundayAt1 = (y, m) => {days = daysFromCivil(y, m, 31)wd = (days + 4) % 7 // 0 = Sunday: 1970-01-01 was a Thursdayreturn (days - wd) * DAY + HOUR}static viennaOffset = (ms) => {y = toNumber(timestamp(ms).slice(0, 4))return ms >= lastSundayAt1(y, 3) && ms < lastSundayAt1(y, 10) ? 2 * HOUR : HOUR}// "YYYY-MM-DD HH:MM", Vienna wall timestatic localStamp = (ms) => {t = timestamp(ms + viennaOffset(ms))return t.slice(0, 10) + ' ' + t.slice(11, 16)}
Branches
- f1-broken-json
- f2-api-bugs
- gp-gate-ports
- heap-revert
- hl25-records
- ident-chooser-selector
- mainmain branch
- p51-public-pr-reads
- records-1112
- s25-no-session-on-login-error
- v59
- w52-person-approval
- w52b-approval-followup
- w52c-header-phone
- w53-pulls-speed-pr
- w54-repo-rename-pr
- w55-rename-org
- w56-owner-members
- w62-pr-close
- w63-force-push
- w64-org-slash
Latest commits
- 0a38f236Report repository creation API blockermre
- b5f72187Record malformed JSON blocker for repository creationmre
- a2ae8302logmre
- 6947cd58logmre
- 2175e48clogmre
- 40eb3ca1gitoria#38: review package, logmre
- 06a5a50egitoria#38: deploy b8252ca, logmre
- b8252ca0gitoria#46: W46b report, logmre
- 50093aafgitoria#46: statuses keyed by app + context (merge w46b-keyed)mre
- dd0267edgitoria#46: fix a label in the keyed gatemre
- b1dcb580gitoria#46: statuses keyed by app + context, required = app + context, old rows still readmre
- 2287363egitoria#47: W47 report, logmre
- 9f502db1gitoria#46: W46b briefmre
- 9678a8f3gitoria#46: contract v1.4, statuses keyed by app + contextmre
- bdcbbaa9gitoria#47: rate limits, status read route, statuses + required on the pull (merge w47-limits)mre
- 772c401fgitoria#45: W45 report, logmre
- b6b793afgitoria#45: PR page with reviews, threads, conversation, Review again (merge w45-pr-page)mre
- fb9e7554gitoria#45: browser gate: a /pulls title links to the PR pagemre
- f7198131gitoria#45: tests/pr-page.mjs gatemre
- 483141d0gitoria#45: Settings shows the Apps heading only to the ownermre