gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit716e14ff716e14ffident mission 013: browser gate — Bob signs out everywhere (no second code request)mre716e14ff/lib/util.hl

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

Branches

Latest commits

  • 716e14ffident mission 013: browser gate — Bob signs out everywhere (no second code request)mre
  • 5885f1c9ident mission 013 step 2: stopgap reload removed (in-place sign-in/out reach the shell via emit, notices back, route-marked main menu), gates updatedmre
  • 734ac0a9ident mission 013 step 1: re-vendor Hybriel ce6f2e0a (plugins whole; bin/ is git-ignored)mre
  • b37875ccident mission 012 part 3: real-Chrome fixes (brand 44 px, identity form width), e2e walk, report, logmre
  • f989c2ebident mission 012: stopgap reload after in-place sign-in/sign-out (until hybriel#193), gates wait for cold start, welcome end reloadsmre
  • 443291d4ident mission 012: /start on its own shell (no account), browser gate 152/0, reportmre
  • 021dd0fcident mission 012: browser gate waits cold start (IDENT_COLD_MS), header selectors, e2e checks; shell events from a page do not reach it (probe), current not marked, account-bar link 44 pxmre
  • 97f77b07ident mission 012 (WIP): onto the layouts theme dbe3de7, shell on AppHeader/MainMenu/UserMenu/Sidebar + ContentFirst, styles cut, check-theme gatemre
  • 8e4f435bpaths: projects moved into /media/STORAGE/projects/WorldAPI/mre
  • fa288f63ident mission 011: report wording (Intl zone names)mre
  • 961fae01ident mission 011: report, LOG, STATUSmre
  • 65348039ident#7: delivery gate — console check split at the gate's own server restart (49 checks)mre
  • dfa7b783ident#7: README (Delivery, env, gate, files, data), deploy.sh runs tests/delivery.mjsmre
  • ab6331f4ident#7: tests/delivery.mjs — fake clock gate: Vienna/New York/Tokyo/UTC 17:00, urgent, mail off, restart, DST days, zone changemre
  • 7869f57bident#7: delivery — the daily mail at 17:00 in the user's zone (digests table, once per local day, test clock IDENT_TEST_CLOCK), urgent mailed at once, push hookmre
  • 371ef1b5ident#7: lib/mail.hl sendNotificationMail (sink: <sink>.mail, one JSON line per mail)mre
  • 39749e0bident#7: time zones through hl:time — server checks isZone, an account without a known zone reads as UTC, allAccountsmre
  • 7a54fe72mission brief 011mre
  • a6a874cdident mission 010: vendor hybriel 0f396652 (plugins + compose cache volume/tmpfs + deploy URL wait + report)mre
  • 4e7cf78emission brief 010mre