gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commite0f0ddaae0f0ddaaident#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)mree0f0ddaa/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

  • 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
  • f2d9f79fident#33: the apps list and the secret box move into apps.hl's Stylemre
  • 8370b748ident#33: the app request box moves into home.hl's Stylemre
  • 40bf3b97ident#33: the 404 page's home link rule moves into notfound.hlmre
  • 40627a89ident#33: the time zone section's spacing moves into timezone.hlmre
  • d389db08ident#33: the identity page's rules and the Notif. chip move into identity.hl's Stylemre
  • ddda0b5bident#33: the identity row's parts move into identitylist.hl's Stylemre
  • 47e516afident#33 ident#34: the identity form's avatar rules move into identityform.hl; the preview and Remove button are hidden with the hidden attribute instead of built class strings (avatarPreviewClass, avatarRemoveClass gone)mre
  • 5cb5cf0eident#33: the code field's look moves into codestep.hl's Stylemre
  • 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