gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitddc7d135ddc7d135Merge branch 'f7-hooks' into mainIdentddc7d135/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

  • ddc7d135Merge branch 'f7-hooks' into mainIdent
  • eee0b513|||PR ident#34 part: test-hook classes replaced by name/element/attribute selectors (tests adjusted), the shared lead/message/notice looks are statics in styles.hlmre
  • ccd6d0c4ident#34: browser gate: rowButton finds the row's button by namemre
  • 24dd414eident#34: the shared lead, message and notice looks are statics in components/styles.hl, imported by the components (lib/looks.hl gone)mre
  • 03ed26c7ident#34: access.hl: the remaining lead classes droppedmre
  • 1d066c42ident#34: access.hl drops its unstyled lead class (the shared lead rule would otherwise mute it)mre
  • 1e1dd7b4ident#34: the access page keeps its lead class string (the page has no ident-card, so its lead line was never muted; a shared rule would change the look)mre
  • 89ac6553ident#34: the lead, message and notice lines are named with Style.pLead / pMessage / pNotice (shared bodies in lib/looks.hl) instead of class stringsmre
  • e84be3deident#34: browser gate lists the Edit/Delete buttons by namemre
  • fb7111faident#34: access page: unstyled test-hook classes (idrow, never, who, whoname, appname, neverrevoke) dropped; the tests use element/name selectorsmre
  • 9f2ad803ident#34: notification switches: push/email by name=..., locked by aria-disabled, the switch look is the role=switch button (no switch/push/email/locked classes)mre
  • e0f0ddaaident#34: inbox: the Mark read/unread button is styled as the button of its note-foot, the icon is found as the row's img (no toggle/icon classes)mre
  • 16b49756ident#34: unused row classes (choice, usedrow, kind, shortid) droppedmre
  • f9d6fb38ident#34: apps and identity rows: the Edit/New secret/Delete buttons carry name=..., the API key is found by its element; unused row classes (app, identity, shortid) droppedmre
  • b323265aMerge branch 'f5-styles' into mainIdent
  • 9ced53c3|||PR ident#33 ident#35 every component's styles live in its own Style block; styles.hl keeps the page-wide rest; the account bar is phone first (min-width: 60rem block); the avatar preview is hidden with the hidden attribute (ident#34 in part)mre
  • f28333e4ident#33 ident#35: styles.hl keeps only the accent, the page column and the shared patterns; the account bar is written phone first (base rule + one min-width: 60rem block) instead of a max-width overridemre
  • 5b4541fcident#33: the selector sign-in frame's rules move into embed.hl's Stylemre
  • 00235f21ident#33: the per-app notification switches move into appsettings.hl's Stylemre
  • 2249945cident#33: the inbox and connection list rules move into inbox.hl's Stylemre