tickets
All repositories: gitoria
6.1 KB
// lib/util.hl — small helpers every topic shares (env, storage path, URLs, lists, text checks, Vienna time).// Statics only; no topic logic here.import { env } from 'hl:proc'import { timestamp } from 'hl:time'// ---- env and storage -----------------------------------------------------------------------static envOr = (name, fallback) => {v = env(name)return v != null && v.trim() != '' ? v.trim() : fallback}// THE TABLE DIRECTORY of every hl:mpackdb table (storage/mpackdb/; TICKETS_STORAGE moves it — the gate runs on its// own). Relative paths resolve against the ENTRY SCRIPT's directory (project.hl's) — tools must pass an absolute one.static storageDir = env('TICKETS_STORAGE') != null ? env('TICKETS_STORAGE') : './storage/mpackdb'// ---- the URLs a person uses ------------------------------------------------------------------// ticket #25: a project's page is /<slug>, a ticket's /<slug>/<number> (the old /projects/… forms answer 301);// the API keeps its /api/projects/… pathsstatic ticketHref = (project, number) => { return '/' + project + '/' + number }static ticketApiHref = (project, number) => { return '/api/projects/' + project + '/tickets/' + number }static projectHrefOf = (project) => { return '/' + project }static settingsHrefOf = (project) => { return '/projects/' + project + '/settings' }// ---- lists and records -----------------------------------------------------------------------// an empty list written to hl:mpackdb comes back as an empty hybrid (`.length` null):// every read of a found list goes through herestatic countOf = (list) => {if (list == null) { return 0 }n = list.lengthreturn n == null ? 0 : n}static first = (list) => { return countOf(list) > 0 ? list[0] : null }// every record is written as a whole (hl:mpackdb's update replaces it)static merged = (rec, changes) => {out = {}for (k of rec.keys()) { out[k] = rec[k] }for (k of changes.keys()) { out[k] = changes[k] }return out}// insertion sorts (no list sort() in the language, hybriel #6) — stable; they sort their copy and return itstatic oldestFirst = (rows, field) => {let i = 1while (i < rows.length) {cur = rows[i]let j = i - 1while (j >= 0 && rows[j][field] > cur[field]) {rows[j + 1] = rows[j]j = j - 1}rows[j + 1] = curi = i + 1}return rows}static newestFirst = (rows, field) => {let i = 1while (i < rows.length) {cur = rows[i]let j = i - 1while (j >= 0 && rows[j][field] < cur[field]) {rows[j + 1] = rows[j]j = j - 1}rows[j + 1] = curi = i + 1}return rows}// ---- text ---------------------------------------------------------------------------------------static clean = (s) => { return s == null ? '' : ('' + s).trim() }// a SUBJECT is one line (mission ST): a line break or other control character in it becomes a space — it stands in a heading of the// Markdown view, in list rows, in a <title>, in a mail subject; a break there let a writer start their own heading / lines in what// programs and agents read. (The summary and comments are the multi-line texts.)static oneLine = (s) => {t = clean(s)let out = ''let i = 0while (i < t.length) {c = t.charCodeAt(i)out = out + (c < 32 || c == 127 ? ' ' : t[i])i = i + 1}return out.trim()}// a query value, percent-encoded (encodeURIComponent is a global, hybriel#14)static encode = (s) => { return encodeURIComponent('' + s) }// a string of ASCII digits only (a ticket number in a URL); '' is notstatic isDigits = (s) => {if (s == null) { return false }t = '' + sif (t.length == 0 || t.length > 12) { return false }let i = 0while (i < t.length) {c = t.charCodeAt(i)if (c < 48 || c > 57) { return false }i = i + 1}return true}// 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}// a short one-line text: trimmed, at most `max` characters, no control charactersstatic 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}// ---- time: epoch ms → "YYYY-MM-DD HH:MM" in Europe/Vienna (ticket #10) ---------------------------// 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. Checked against node's Intl (tests/localtime.hl + the gate).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 time — what a person readsstatic localStamp = (ms) => {t = timestamp(ms + viennaOffset(ms))return t.slice(0, 10) + ' ' + t.slice(11, 16)}
Branches
Latest commits
- e843ff7cOne-time purge of miserit-anthar#94 (#94, identity rule): idempotent step in lib/migrate.hl, fixture gate tests/purge94.mjsmre
- 506a4efdmission 028: #94, identity rulemre
- 78c7f48eRefine mpackdb holdmre
- 3d7d89c8Record mpackdb hold until day38mre
- f1bf7195#51 done; my subscription idmre
- 9870b945status: #51 proof greenmre
- e92173c6log: #51 live push proofmre
- 72d87041log: Hermes push livemre
- 1d69bf7dTickets conductor: park until Hermes pushmre
- 80d8cfd1Tickets conductor: #51 livemre
- 61641dd9Complete subscriptions deployment evidencemre
- c980856cRecord successful subscriptions deployment and live checksmre
- 4b4710adMerge branch 's51-subscriptions'mre
- a731ef7eTickets conductor: S51 reviewed, deploy stepmre
- b487b05aRecord verified direct push protocol handoffmre
- 74e34cc3Verify encrypted push secrets and direct push retry semanticsmre
- a1ac5cd0Push subscription notices directly to signed HyperConductor chat endpointsmre
- d662ad27Remove superseded waker service for direct chat push protocolmre
- c0ac927aTickets conductor: S51 redo for push protocolmre
- ff2e8701Finalize subscription build verification and handoff reportmre