gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commite24b59a6e24b59a6ident#40.3: the time zone is one component (components/timezone.hl); its face changeTimeZone moves with itmree24b59a6/lib/util.hl

4.8 KB

  1. // lib/util.hl — SMALL SHARED HELPERS of ident (mission 009, code order): the storage directory and env numbers,
  2. // lists and records, ids, text checks, the email shape, oldest-first order. Each one exists once; the topic files
  3. // import what they use. Statics only.
  4. import { env } from 'hl:proc'
  5. import { isZone } from 'hl:time'
  6. // keepEmpty preserves the null-only reads used by storage and listener configuration.
  7. static envString = (name, fallback, keepEmpty = false) => {
  8. v = env(name)
  9. return v == null || (!keepEmpty && v == '') ? fallback : v
  10. }
  11. // IDENT_STORAGE names another table DIRECTORY (the gates run on their own). Relative
  12. // paths resolve against the ENTRY SCRIPT's directory — tools must pass an absolute one.
  13. static dir = envString('IDENT_STORAGE', './storage/mpackdb', true)
  14. static envNumber = (name, fallback) => {
  15. v = envString(name, null)
  16. if (v == null || v == '') { return fallback }
  17. n = toNumber(v)
  18. return n == null ? fallback : n
  19. }
  20. // an empty list written to hl:mpackdb comes back as an empty hybrid (`.length` null)
  21. static countOf = (list) => {
  22. if (list == null) { return 0 }
  23. n = list.length
  24. return n == null ? 0 : n
  25. }
  26. static first = (list) => { return countOf(list) > 0 ? list[0] : null }
  27. static str = (v) => { return v == null ? '' : v }
  28. static isList = (v) => { return v != null && hlTypeName(v) == 'Hybrid' && v.length != null }
  29. static isObject = (v) => { return v != null && hlTypeName(v) == 'Hybrid' && v.length == null }
  30. // A PUBLIC ID is an mpackdb UUID: a non-empty string (a number is never one — an old
  31. // numeric id from before mission 009 finds nothing)
  32. static isId = (v) => { return v != null && hlTypeName(v) == 'String' && v.length > 0 && v.length <= 64 }
  33. // every record is written as a whole (hl:mpackdb's update replaces it)
  34. static merged = (rec, changes) => {
  35. out = {}
  36. for (k of rec.keys()) { out[k] = rec[k] }
  37. for (k of changes.keys()) { out[k] = changes[k] }
  38. return out
  39. }
  40. // OLDEST FIRST: by the stored `created`, a tie by the id (never by key order)
  41. static olderThan = (a, b) => {
  42. ca = a.created == null ? 0 : a.created
  43. cb = b.created == null ? 0 : b.created
  44. if (ca != cb) { return ca < cb }
  45. return a.id < b.id // strings order by code point (hybriel#2)
  46. }
  47. // a found list, oldest first (insertion sort by hand: no list sort(), hybriel #6)
  48. static oldestFirst = (list) => {
  49. out = []
  50. if (countOf(list) == 0) { return out }
  51. for (x of list) { out.push(x) }
  52. let i = 1
  53. while (i < out.length) {
  54. let cur = out[i]
  55. let j = i - 1
  56. while (j >= 0 && olderThan(cur, out[j])) {
  57. out[j + 1] = out[j]
  58. j = j - 1
  59. }
  60. out[j + 1] = cur
  61. i = i + 1
  62. }
  63. return out
  64. }
  65. // ---- text checks ------------------------------------------------------------------------
  66. // no control characters (a field is one line of text)
  67. static hasControl = (s) => {
  68. let i = 0
  69. while (i < s.length) {
  70. let c = s.charCodeAt(i)
  71. if (c < 32 || c == 127) { return true }
  72. i = i + 1
  73. }
  74. return false
  75. }
  76. // the characters a URL may hold (a return URL, a notification's icon / link)
  77. static urlChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~:/?&=%+,;@!$()*[]'
  78. // AN EMAIL ADDRESS: lowercased and trimmed, then a plain whitelist shape
  79. static normEmail = (email) => { return email == null ? '' : ('' + email).trim().toLowerCase() }
  80. static validEmail = (email) => {
  81. if (email == null || email.length < 3 || email.length > 200) { return false }
  82. at = email.indexOf('@')
  83. if (at < 1 || at != email.lastIndexOf('@') || at == email.length - 1) { return false }
  84. if (!email.slice(at + 1, email.length).includes('.')) { return false }
  85. // a whitelist: the language has no escapes to name a CR or a tab with
  86. ok = 'abcdefghijklmnopqrstuvwxyz0123456789.-_+@'
  87. let i = 0
  88. while (i < email.length) {
  89. if (!ok.includes(email[i])) { return false }
  90. i = i + 1
  91. }
  92. return true
  93. }
  94. // A TIME ZONE is an IANA name as the browser reports it (`Europe/Vienna`, `UTC`,
  95. // `America/Argentina/Buenos_Aires`, `Etc/GMT+5`). The server checks the shape, then asks hl:time's zone database
  96. // (hybriel#5, `isZone`: the system's tz files); the page checks the name against the browser's own list
  97. // (Intl.supportedValuesOf) before it sends it.
  98. static zoneChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/_+-'
  99. static checkTimeZone = (tz) => {
  100. if (tz == null || hlTypeName(tz) != 'String') { return { error = 'the time zone must be a string' } }
  101. t = tz.trim()
  102. if (t == '') { return { error = 'the time zone is empty' } }
  103. if (t.length > 64) { return { error = 'the time zone name is too long' } }
  104. let i = 0
  105. while (i < t.length) {
  106. if (!zoneChars.includes(t[i])) { return { error = 'that is not a time zone name (like Europe/Vienna)' } }
  107. i = i + 1
  108. }
  109. if (t[0] == '/' || t[t.length - 1] == '/' || t.includes('//')) { return { error = 'that is not a time zone name (like Europe/Vienna)' } }
  110. if (!isZone(t)) { return { error = 'unknown time zone: ' + t } }
  111. return { timeZone = t }
  112. }

Branches

Latest commits

  • 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
  • aa056c03ident: a startup error prints its message before exit 1 (hybriel#247)mre
  • ee37a861ident#G48: gitoria's registration gets the wildcard origin of its repo addresses at load (gate tests/gitoria-origin.mjs)mre
  • dc6d486fident#27: the docs gate expects the new Access entry in the main menumre
  • d277633cident#27: README: identity access and the introspect endpointmre
  • e999b0d3ident#27: the Access tab — one key per identity for all its apps: give access in three steps, the overview matrix, a cell's sheet (since, last use, level, rotate, revoke), the app's check POST /api/introspect, agent identities hidden from the login picker, an inbox icon in the header's icon style (gate: tests/access.mjs)mre