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
Latest commits
- cd99d2feWIP gitoria#22: always render the repo level's slot (missing-repo boot hang), pages render nothing without a repo; hydration diagnosis in reports/G2-hydrate.mdmre
- e5404b9breport: commit idsmre
- 63cfa21ereports/G2-repotabs.mdmre
- 9b0516fdgitoria#22: mark the tab underline functional (check-theme), chain E2E: serve/measure split, bring the lisbeth tab to frontmre
- 19bc1df8gitoria#22: all repo pages under ONE layout level (components/repo.hl, hl:web D-CHAIN): a tab click replaces only the page in the slot; head and tab nav stay the same DOM nodesmre
- 40658417tests/browser.mjs: wait for the page to be live (hydrated) before clicking Merge — a click on the server-rendered button before the client mounted was lost (flaky 'Clashing change' under load)mre
- 48a6154bre-vendor Hybriel 5406d146 (hl:web batches 9+10: #72 emits while the socket is closed delivered after reconnect, browser boot linear, faster list appends; contains int 6–14 + m406)mre
- e972e8d7tests: short-id-switch (GITORIA_SWITCH_IDENT_PORT/_PORT) and ssh tunnel (GITORIA_GATE_TUNNEL_PORT) ports from env, defaults unchangedmre
- f06f5349re-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre
- c7d3cc87docker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
- f0e7ff8cdeploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
- fe5f707cgitoria 010: report, mission, comment on why current = true staysmre
- 9705ca86gitoria 010: gate: menu marker in the server HTMLmre
- 8cff663agitoria 010: main-menu.hl from layouts 0bcc629 (route marker)mre
- 359c79f7gitoria 010: re-vendor Hybriel master f9614007 (plugins whole, bin/ git-ignored)mre
- 6bd7fb3cgitoria 009: theme re-copied from layouts 76f99f6 (menu marks the current page), current = true on the menu item, e2e marker checks, reportmre
- 44de915cidentkit: refuse a taken ident port, cold-start deadline, abort on dead ident, clear no-code error (as notes)mre
- 2bf5343egitoria 007: theme re-copied from layouts dbe3de7 (44 px taps), redundant tap overrides dropped, crumbs class, real-browser walk test, reportmre
- 18843648deploy.sh: a gate without a 'passed,' line (check-theme) no longer ends the script silently (grep under pipefail) — print check-theme's summary, as layouts doesmre
- e6dff0cbgitoria#23: report 005, STATUS, LOGmre