gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit6280c92f6280c92fre-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre6280c92f/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

  • 6280c92fre-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre
  • 147744bftests/migration: the fixture app origin follows MIGRATION_GATE_TESTAPP_PORT (oldstore-009 OLD_TESTAPP)mre
  • 48cd36bctests/migration: ports from env (MIGRATION_GATE_PORT, MIGRATION_GATE_TESTAPP_PORT); 8395/8396 were taken by a foreign servermre
  • 3d9b2fd8docker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
  • 19b7e3c3deploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • c8a596f4re-vendor Hybriel 66c3db71 (web batch 4); e2e: main menu marked after in-place sign-in is a normal check; report 014mre
  • 8f3dc1ectests: delivery allow-list for the stopped server's network errors uses the gate port, not 8700mre
  • 9cc8eddftests: notify and iplimit gate Chrome ports from env (NOTIFY_GATE_CHROME, IPLIMIT_GATE_CHROME_A/B)mre
  • d6da2d02tests: delivery and migration gate ports from env (DELIVERY_GATE_PORT, DELIVERY_GATE_CHROME, MIGRATION_GATE_CHROME) — hard-coded 8700-8719 collided with a worker during deploymre
  • 7276a008ident mission 013: re-vendor Hybriel master f9614007 (#200, #202 mpackdb copy fix); gates and e2e rerunmre
  • e13e30c3ident mission 013: report, mission, repro, LOGmre
  • eff734f3ident mission 013: in-place sign-out also closes the code step (no leftover code form); checksmre
  • b7a61546ident mission 013: e2e — in-place main-menu mark kept as a known gapmre
  • 59b43d81ident mission 013: sign out everywhere keeps this browser's own (signed-out) session so the page goes on in placemre
  • 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