gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit8ad275a48ad275a4Log/status: tracker 68, SR1Tracker8ad275a4/lib/util.hl

4.1 KB

  1. // lib/util.hl — small helpers every topic shares (env, paths, dates, lists, text), each ONCE (mission 028: they were copied
  2. // into follows, users, shows, watches, tmdbsync, catalog, franchises, details, people, merge, search, shortids).
  3. import { env } from 'hl:proc'
  4. import { now, timestamp } from 'hl:time'
  5. import { randomBytes } from 'hl:crypto'
  6. import { Response } from 'hl:http1'
  7. // an environment value, trimmed — `fallback` when it is not set or empty
  8. static envOr = (&name, &fallback) => {
  9. v = env(name)
  10. return v != null && v.trim() != '' ? v.trim() : fallback
  11. }
  12. // every table and file of the app's data: TRACKER_STORAGE, else storage/mpackdb beside project.hl (relative paths resolve
  13. // against the ENTRY script)
  14. static storageDir = envOr('TRACKER_STORAGE', './storage/mpackdb')
  15. // the image files: a title's poster (tmdb.hl syncPoster) and a person's photo (people.hl syncProfile), served by images.hl
  16. static postersDir = storageDir + '/posters'
  17. static profilesDir = storageDir + '/profiles'
  18. // a new record id: 16 hex (self-assigned — hl:mpackdb's own generated @id can repeat within one millisecond, hybriel#113)
  19. static newId = () => { return randomBytes(8, 'hex') }
  20. static hexDigits = '0123456789abcdef'
  21. // today as 'YYYY-MM-DD' (UTC, like the rest of the app — hl:time has no time zones)
  22. static todayStr = () => { return timestamp(now()).slice(0, 10) }
  23. // the day `days` from today ('YYYY-MM-DD', UTC; negative = before)
  24. static dayStr = (days) => { return timestamp(now() + days * 86400000).slice(0, 10) }
  25. // a stored date ('YYYY-MM-DD…') or '' when it is none
  26. static dateOr = (&v) => { return v != null && hlTypeName(v) == 'String' && v.length >= 4 ? v : '' }
  27. // a face's text argument: the string itself, '' for anything else (a peer may send a list or an object: `'' + x` then aborts the
  28. // request with a 500 that names the source line)
  29. static textArg = (&v) => { return v != null && hlTypeName(v) == 'String' ? v : '' }
  30. // a text value or null when it is none or only white space (the value itself, not trimmed)
  31. static textOr = (&v) => { return v != null && hlTypeName(v) == 'String' && v.trim() != '' ? v : null }
  32. // an id (a string or a number) that is there
  33. static hasId = (&v) => { return v != null && ('' + v).trim() != '' }
  34. static listOr = (&v) => { return v == null ? [] : v }
  35. static countOfList = (&list) => {
  36. if (list == null) { return 0 }
  37. n = list.length
  38. return n == null ? 0 : n
  39. }
  40. static firstOf = (&list) => { return countOfList(&list) > 0 ? list[0] : null }
  41. // ---- sorting, hand-written (no list sort(), hybriel#1): a bottom-up merge sort of { key, … }, newest (largest) key first,
  42. // stable — the public lists (catalog.hl), a person's filmography and /people (people.hl). Tracker#37 (mission 032): it starts
  43. // from the runs that are already in order (not from single entries) — same result, but a list that is nearly sorted (/people:
  44. // 105k people, 3 runs) costs one merge pass: 0.15 s instead of 1.6 s on the live copy
  45. static mergeDesc = (&a, &b) => {
  46. out = []
  47. let i = 0
  48. let j = 0
  49. while (i < a.length && j < b.length) {
  50. if (b[j].key > a[i].key) { out.push(b[j]) j = j + 1 } else { out.push(a[i]) i = i + 1 }
  51. }
  52. while (i < a.length) { out.push(a[i]) i = i + 1 }
  53. while (j < b.length) { out.push(b[j]) j = j + 1 }
  54. return out
  55. }
  56. static sortDesc = (&list) => {
  57. if (list.length == 0) { return [] }
  58. let runs = []
  59. let run = []
  60. for (x of list) {
  61. if (run.length > 0 && x.key > run[run.length - 1].key) { runs.push(run) run = [] }
  62. run.push(x)
  63. }
  64. runs.push(run)
  65. while (runs.length > 1) {
  66. next = []
  67. let k = 0
  68. while (k + 1 < runs.length) { next.push(mergeDesc(&runs[k], runs[k + 1])) k = k + 2 }
  69. if (k < runs.length) { next.push(runs[k]) }
  70. runs = next
  71. }
  72. return runs[0]
  73. }
  74. // a permanent redirect (the route adapters: the old /my/ addresses, /shows/<slug> of a series or movie, short ids)
  75. static movedResponse = (&path) => { return new Response('moved to ' + path, { status = 301 headers = { 'Location' = path 'Content-Type' = 'text/plain; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' } }) }
  76. static notFoundText = (&text) => { return new Response(text, { status = 404 headers = { 'Content-Type' = 'text/plain; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' } }) }

Branches

Latest commits

  • 8ad275a4Log/status: tracker 68, SR1Tracker
  • f5a06a0fLog: gitoria push setup, SR1Tracker
  • d6b109baMission SR1mre
  • 608b7558Log: FF1 merged via PR #1mre
  • 2939d94cMerge branch 'ff1-gate-failfast' into mainTracker
  • c934361a|||PR tests: gates fail fast when the app exits during startmre
  • d714ae42Log/status: AP56 closed out, FF1mre
  • a429d088Mission FF1mre
  • 03bbfba3Log: AP56c causemre
  • 24845fe3Log: AP56b result, AP56cmre
  • 6a6d71eaLog: gitoria membershipmre
  • 8b842dc8Log + CONDUCTOR: gitoria PR switch-onmre
  • 6bb7c20estatus: window 2026-11-09mre
  • 5db84974Log: Apollo window GO, AP56b started, BS1 copy removedmre
  • e84586a7Mission AP56bmre
  • 15917f03Record PR workflow rule (gitoria#50)mre
  • 942ac995status: paused workers statemre
  • 44b934e5log: respawn cleanupmre
  • 03a80097Log: Anton GC A/Bmre
  • c7cdef62Log: Apollo STOP, workers pausedmre