ident
All repositories: gitoria
4.8 KB
// lib/util.hl — SMALL SHARED HELPERS of ident (mission 009, code order): the storage directory and env numbers,// lists and records, ids, text checks, the email shape, oldest-first order. Each one exists once; the topic files// import what they use. Statics only.import { env } from 'hl:proc'import { isZone } from 'hl:time'// keepEmpty preserves the null-only reads used by storage and listener configuration.static envString = (name, fallback, keepEmpty = false) => {v = env(name)return v == null || (!keepEmpty && v == '') ? fallback : v}// IDENT_STORAGE names another table DIRECTORY (the gates run on their own). Relative// paths resolve against the ENTRY SCRIPT's directory — tools must pass an absolute one.static dir = envString('IDENT_STORAGE', './storage/mpackdb', true)static envNumber = (name, fallback) => {v = envString(name, null)if (v == null || v == '') { return fallback }n = toNumber(v)return n == null ? fallback : n}// an empty list written to hl:mpackdb comes back as an empty hybrid (`.length` null)static countOf = (list) => {if (list == null) { return 0 }n = list.lengthreturn n == null ? 0 : n}static first = (list) => { return countOf(list) > 0 ? list[0] : null }static str = (v) => { return v == null ? '' : v }static isList = (v) => { return v != null && hlTypeName(v) == 'Hybrid' && v.length != null }static isObject = (v) => { return v != null && hlTypeName(v) == 'Hybrid' && v.length == null }// A PUBLIC ID is an mpackdb UUID: a non-empty string (a number is never one — an old// numeric id from before mission 009 finds nothing)static isId = (v) => { return v != null && hlTypeName(v) == 'String' && v.length > 0 && v.length <= 64 }// 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}// OLDEST FIRST: by the stored `created`, a tie by the id (never by key order)static olderThan = (a, b) => {ca = a.created == null ? 0 : a.createdcb = b.created == null ? 0 : b.createdif (ca != cb) { return ca < cb }return a.id < b.id // strings order by code point (hybriel#2)}// a found list, oldest first (insertion sort by hand: no list sort(), hybriel #6)static oldestFirst = (list) => {out = []if (countOf(list) == 0) { return out }for (x of list) { out.push(x) }let i = 1while (i < out.length) {let cur = out[i]let j = i - 1while (j >= 0 && olderThan(cur, out[j])) {out[j + 1] = out[j]j = j - 1}out[j + 1] = curi = i + 1}return out}// ---- text checks ------------------------------------------------------------------------// no control characters (a field is one line of text)static hasControl = (s) => {let i = 0while (i < s.length) {let c = s.charCodeAt(i)if (c < 32 || c == 127) { return true }i = i + 1}return false}// the characters a URL may hold (a return URL, a notification's icon / link)static urlChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~:/?&=%+,;@!$()*[]'// AN EMAIL ADDRESS: lowercased and trimmed, then a plain whitelist shapestatic normEmail = (email) => { return email == null ? '' : ('' + email).trim().toLowerCase() }static validEmail = (email) => {if (email == null || email.length < 3 || email.length > 200) { return false }at = email.indexOf('@')if (at < 1 || at != email.lastIndexOf('@') || at == email.length - 1) { return false }if (!email.slice(at + 1, email.length).includes('.')) { return false }// a whitelist: the language has no escapes to name a CR or a tab withok = 'abcdefghijklmnopqrstuvwxyz0123456789.-_+@'let i = 0while (i < email.length) {if (!ok.includes(email[i])) { return false }i = i + 1}return true}// A TIME ZONE is an IANA name as the browser reports it (`Europe/Vienna`, `UTC`,// `America/Argentina/Buenos_Aires`, `Etc/GMT+5`). The server checks the shape, then asks hl:time's zone database// (hybriel#5, `isZone`: the system's tz files); the page checks the name against the browser's own list// (Intl.supportedValuesOf) before it sends it.static zoneChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/_+-'static checkTimeZone = (tz) => {if (tz == null || hlTypeName(tz) != 'String') { return { error = 'the time zone must be a string' } }t = tz.trim()if (t == '') { return { error = 'the time zone is empty' } }if (t.length > 64) { return { error = 'the time zone name is too long' } }let i = 0while (i < t.length) {if (!zoneChars.includes(t[i])) { return { error = 'that is not a time zone name (like Europe/Vienna)' } }i = i + 1}if (t[0] == '/' || t[t.length - 1] == '/' || t.includes('//')) { return { error = 'that is not a time zone name (like Europe/Vienna)' } }if (!isZone(t)) { return { error = 'unknown time zone: ' + t } }return { timeZone = t }}
Branches
Latest commits
- f421cd60ident#33: the shell's own rules (user menu address, offline note, network probe) move into main.hl's Stylemre
- c512fb96Merge branch 'f4-homesplit' into mainIdent
- 07d1717c|||PR ident#40 home.hl is split into components, one per area: email step, identity form, identity list, app identity choice, time zone; each moves its faces with it, the email step is shared with embed.hl (#40.3, rest of #40.2)mre
- 59e8fcc1ident#40.3: the app's identity choice is one component (components/chooseidentity.hl); its face chooseIdentity moves with itmre
- e23efdfcident#40.3: the identity list is one component (components/identitylist.hl); its delete and face dropIdentity move with itmre
- e24b59a6ident#40.3: the time zone is one component (components/timezone.hl); its face changeTimeZone moves with itmre
- 8cbc5717ident#40.3: the email step is one component (components/emailstep.hl) placed by home.hl and embed.hlmre
- 6fad3bd2Merge branch 'f3-codestep' into mainIdent
- 51e387f2|||PR ident#40 the code step is one component (components/codestep.hl) placed by home.hl and embed.hl; its verify face, setCode, browserZone and verify call exist once (#40.2)mre
- c28bddeeMerge branch 'f2-shell' into mainIdent
- c3a40ff6|||PR ident#40 one shell for the app and /start (account part off on /start, #40.1); startshell.hl removed; unused signOut faces gone (#40.4)mre
- 060f3870ident#40.4: use the shell sign-out faces in regression gatesmre
- 9b94aa6aMerge branch 'f1b-cleanup' into mainIdent
- edcfce3f|||PR ident#41 ident#43 ident#44 login error page as a component (same status codes and text, pixel-identical), declarations and route wiring cleaned upmre
- 3ac85995ident#43 ident#44: plain declarations for never-reassigned lets, one envString helper, named route functions and audience rule out of project.hlmre
- c7eef14aMerge branch 'nv1-navigate' into mainIdent
- 644f9775|||PR ident#36 navigate() instead of window.location in the shell, start shell and homemre
- 4ca2f921deploy.sh: tar --hard-dereference so backups hold no hard linksmre
- 23971fb7ident#49: media queries in the language's @media formmre
- 73f4f97dident: re-vendor to hybriel day38b (mpackdb store-open fix)mre