gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit0d18abc40d18abc4Merge branch 'f13-avatar' into mainIdent0d18abc4/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

  • 0d18abc4Merge branch 'f13-avatar' into mainIdent
  • 2a99730a|||PR ident#37 avatar in hlmre
  • fa4ed25bMerge branch 'f11-upsert' into mainIdent
  • 8f3d9bc9|||PR ident#42 keyed upsert and removemre
  • 675493b1ident#42: editing an identity upserts the one edited row (face editIdentity answers { identity, row }); a create or delete keeps the whole list because the rows' position-dependent parts (default, Delete, Identity n) changemre
  • 45e55303ident#42: an app write patches one row: upsert on create, edit and new secret, one-argument remove on delete; the faces answer the changed app (or the deleted id), not the whole listmre
  • 991641d4Merge branch 'f10-access' into mainIdent
  • e56d8273|||PR ident#34 part 4: access and inbox fixed classesmre
  • df7696dbident#34: tests: access ticks selector quoting fixmre
  • 7215903eident#34: access: the unstyled classes all, app, tick, apptick, plus are dropped; the gate finds the app ticks by input[name=app]mre
  • 69b0a0ceident#34: access: the pick buttons use Style.pickCard instead of class pickmre
  • dc702419Merge branch 'f9-choose' into mainIdent
  • 8caec771|||PR ident#34 restore .choose hookmre
  • 0dbff960Merge branch 'f8-shared' into mainIdent
  • d69665f6|||PR ident#34 part 3: shared look classes (wide, avatar, chip/used, conn, choose, who, inboxlink) moved to statics or droppedmre
  • 3a64dc48ident#34: chipLook: radius as the same var() string (a bare token name in a static does not compile); selector gate selectormre
  • eb039073ident#34: tests: signin and migration find the choice without the classmre
  • 4b94a594ident#34: tests: fix selector of the identity choicemre
  • bebc88e4ident#34: main.hl drops the unstyled classes inboxlink and whomre
  • b4e7ad04ident#34: the identity choice link/button has no class (rule on #chooselist a)mre