gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitc6d464e5c6d464e5Log: Apollo GO backlog, AP56 and BS1 startedmrec6d464e5/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

  • c6d464e5Log: Apollo GO backlog, AP56 and BS1 startedmre
  • 1eb6f1a1Missions AP56, BS1mre
  • 4871f8adLog: AP52 live on day52, browser gate greenmre
  • d9bf2988Merge ap52: day52, tracker#58 server argument, #51 modal close rule, #56 keyed season rowsmre
  • c4a60ef2Log: RT52 retest passed, AP52 startedmre
  • 83760a55Browser gate: search results belong to the address form of the typed text (tracker#52 navigate replace)mre
  • 6e951d91AP52: show-page rows are keyed records; closing a season is one upsert and removes (tracker#56, hybriel#331)mre
  • 3bb4ccddAP52: modal close button refers to its nested rule with Style.buttonModalX (tracker#51, hybriel#333)mre
  • ba16933fAP52: login callback and title route are static handlers taking the server as third argument (tracker#58, hybriel#332)mre
  • 85153900AP52: re-vendor Hybriel set day52 (source 77cfca31, bin 0a8ed496)mre
  • 7c051eddLog: hybriel#338 answermre
  • d049b8b8Log: HM50 live, #52 donemre
  • 6d05fa37Log: HM50 Part A mergedmre
  • 1c7dab74HM50: the shell reads the framework's online state, netProbe removed (tracker#55)mre
  • e8b91697HM50: search address follows the text via navigate(path, replace = true) (tracker#52)mre
  • 6fe1896cHM50: re-vendor Hybriel set day50 (source 0ae38232, bin d3ef9d16)mre
  • 2f391aa9Log: HM50 startedmre
  • 4b59c03dHM50 mission: doc pathmre
  • ed526d67HM50 missionmre
  • 2f986ca3Log: RT50 passmre