gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit4ca2f9214ca2f921deploy.sh: tar --hard-dereference so backups hold no hard linksmre4ca2f921/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

  • 4ca2f921deploy.sh: tar --hard-dereference so backups hold no hard linksmre
  • 23971fb7ident#49: media queries in the language's @media formmre
  • 73f4f97dident: re-vendor to hybriel day38b (mpackdb store-open fix)mre
  • aa056c03ident: a startup error prints its message before exit 1 (hybriel#247)mre
  • ee37a861ident#G48: gitoria's registration gets the wildcard origin of its repo addresses at load (gate tests/gitoria-origin.mjs)mre
  • dc6d486fident#27: the docs gate expects the new Access entry in the main menumre
  • d277633cident#27: README: identity access and the introspect endpointmre
  • e999b0d3ident#27: the Access tab — one key per identity for all its apps: give access in three steps, the overview matrix, a cell's sheet (since, last use, level, rotate, revoke), the app's check POST /api/introspect, agent identities hidden from the login picker, an inbox icon in the header's icon style (gate: tests/access.mjs)mre
  • 436206fcident#47: deploy.sh runs the sign-in gate on the deploy's own port slot, like the sessionwrites and docs gates (the default 8700 can be taken by another process)mre
  • a4887ea3ident#47: the selector's sign-in frame no longer has a Continue button; the identity row itself is the link (gate tests/signin.mjs fails on the old code)mre
  • d8ec703dident#45: /docs shows docs/IDENT.md (hl:markdown, vendored from ac1ed9da), one Docs entry in the main menu, README for developers only, gates tests/docs.mjs and tests/sessionwrites.mjs in deploy.shmre
  • ec6aad35Merge branch 'a28g-amp-callsites' into d45-docsmre
  • 6bfbd3f2ident#45: docs/IDENT.md, what ident is and how it works; CONCEPT.md marks one id per app as droppedmre
  • c0edca26ident#28 A28G: write & at every call that hands a session to an &-parameter; gate tests/sessionwrites.mjs pins that sign-in, pending address, embed sign-in and selector land in the session filesmre
  • 80965c08ident#28: README says the deploy goes through the standbymre
  • 2f41670cident#28: ident-standby service on port 45012 (no background round in the standby) and deploy.sh hands over through standby-swap.shmre
  • a544b261ident#28: README names the new vendored Hybrielmre
  • 716860b0ident#28: gates follow Hybriel ac1ed9da (lazy sessions, meta attribute order, JSON key order, default X-Frame-Options switched off for the embed page)mre
  • 889e00fbident#28: Hybriel master ac1ed9da (sessions reload a newer file, a replayed reconnect carries the server session); a startup fault exits 1 (on Error)mre
  • f5a55f8aident#11: apps can ask for nickname, firstname and lastname (fields list on /api/exchange, new POST /api/profile); empty fields come back null; gate checks, READMEmre