gitoria
All repositories: gitoria
5.7 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]static repoOrigin = (slug) => { return scheme + '://' + slug + '.' + hostPort }// 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) {let 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) {let 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) {let 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
- 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
- 43ad2bcfgitoria header: the selector alone plus your name and Log out as before the theme missions; tests follow (WIP: host-only cookie experiment fails)mre
- ac2c4e3fident selector back in the header (creator 2026-10-24): selector top right signed in and out on phone and desktop, no second Log in button, new gate checks (WIP: gates not re-run after the last edits)mre
- b279f536theme: sync from layouts 4994037 (list rows stack, td word-keep, heading code, edited tap target)mre
- 2b01515ctests/ticketskit: a failed start kills the tickets server (own process group); stop() kills the groupmre
- 109cfc5ctests/ticketskit: keep the rotated session cookie of the tickets login (tickets ST2 session rotation)mre
- 7eb6be27sessions last 99 years (creator 2026-10-23: logins never expire; only sign out ends them)mre
- b3911918reports/MG-mobile.md: 404 fixed, rebased gatesMG
- 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