tracker
All repositories: gitoria
4.1 KB
// lib/util.hl — small helpers every topic shares (env, paths, dates, lists, text), each ONCE (mission 028: they were copied// into follows, users, shows, watches, tmdbsync, catalog, franchises, details, people, merge, search, shortids).import { env } from 'hl:proc'import { now, timestamp } from 'hl:time'import { randomBytes } from 'hl:crypto'import { Response } from 'hl:http1'// an environment value, trimmed — `fallback` when it is not set or emptystatic envOr = (&name, &fallback) => {v = env(name)return v != null && v.trim() != '' ? v.trim() : fallback}// every table and file of the app's data: TRACKER_STORAGE, else storage/mpackdb beside project.hl (relative paths resolve// against the ENTRY script)static storageDir = envOr('TRACKER_STORAGE', './storage/mpackdb')// the image files: a title's poster (tmdb.hl syncPoster) and a person's photo (people.hl syncProfile), served by images.hlstatic postersDir = storageDir + '/posters'static profilesDir = storageDir + '/profiles'// a new record id: 16 hex (self-assigned — hl:mpackdb's own generated @id can repeat within one millisecond, hybriel#113)static newId = () => { return randomBytes(8, 'hex') }static hexDigits = '0123456789abcdef'// today as 'YYYY-MM-DD' (UTC, like the rest of the app — hl:time has no time zones)static todayStr = () => { return timestamp(now()).slice(0, 10) }// the day `days` from today ('YYYY-MM-DD', UTC; negative = before)static dayStr = (days) => { return timestamp(now() + days * 86400000).slice(0, 10) }// a stored date ('YYYY-MM-DD…') or '' when it is nonestatic dateOr = (&v) => { return v != null && hlTypeName(v) == 'String' && v.length >= 4 ? v : '' }// a face's text argument: the string itself, '' for anything else (a peer may send a list or an object: `'' + x` then aborts the// request with a 500 that names the source line)static textArg = (&v) => { return v != null && hlTypeName(v) == 'String' ? v : '' }// a text value or null when it is none or only white space (the value itself, not trimmed)static textOr = (&v) => { return v != null && hlTypeName(v) == 'String' && v.trim() != '' ? v : null }// an id (a string or a number) that is therestatic hasId = (&v) => { return v != null && ('' + v).trim() != '' }static listOr = (&v) => { return v == null ? [] : v }static countOfList = (&list) => {if (list == null) { return 0 }n = list.lengthreturn n == null ? 0 : n}static firstOf = (&list) => { return countOfList(&list) > 0 ? list[0] : null }// ---- sorting, hand-written (no list sort(), hybriel#1): a bottom-up merge sort of { key, … }, newest (largest) key first,// stable — the public lists (catalog.hl), a person's filmography and /people (people.hl). Tracker#37 (mission 032): it starts// from the runs that are already in order (not from single entries) — same result, but a list that is nearly sorted (/people:// 105k people, 3 runs) costs one merge pass: 0.15 s instead of 1.6 s on the live copystatic mergeDesc = (&a, &b) => {out = []let i = 0let j = 0while (i < a.length && j < b.length) {if (b[j].key > a[i].key) { out.push(b[j]) j = j + 1 } else { out.push(a[i]) i = i + 1 }}while (i < a.length) { out.push(a[i]) i = i + 1 }while (j < b.length) { out.push(b[j]) j = j + 1 }return out}static sortDesc = (&list) => {if (list.length == 0) { return [] }let runs = []let run = []for (x of list) {if (run.length > 0 && x.key > run[run.length - 1].key) { runs.push(run) run = [] }run.push(x)}runs.push(run)while (runs.length > 1) {let next = []let k = 0while (k + 1 < runs.length) { next.push(mergeDesc(&runs[k], runs[k + 1])) k = k + 2 }if (k < runs.length) { next.push(runs[k]) }runs = next}return runs[0]}// a permanent redirect (the route adapters: the old /my/ addresses, /shows/<slug> of a series or movie, short ids)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' } }) }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
- 7f04de33log: HM49Bmre
- 00616cf3log: #49 batch merged, DP67 startedmre
- d7232e86Merge hm49-styles: tracker#49 first batch of component stylesmre
- 079f376dMove seven components' styles into Style blocksmre
- 17d66feelog: #58 mergedmre
- 69a6640bMerge hm58-map: tracker#58 project.hl only the mapmre
- a85f7900Move route logic out of the project mapmre
- 13dea5belog: #56 live, HM49 and HM58 startedmre
- a31c8f2fdocs: tracker#36 remaining listmre
- 975d06d1log: #36 live, DP66 startedmre
- d6d03fa3mission: T36Bmre
- e7512d00status: #36 rolled back, retry askedmre
- c98e2043log: T36A rollbackmre
- ae905f15Merge t36a: tracker#36 apply rechecks, reportmre
- 796bfaceT36A: report validated fixes, failed live SSH check and verified rollbackmre
- 55257852log: #53 live, #56 mergedmre
- 78b39f1bMerge hm56-rows: tracker#56 list builders insert oncemre
- 7aea52batmdb fix: protect owners without slugs and record passing gatesmre
- 9b3c2559tmdb fix: revalidate planned identities and reserve target idsmre
- 6da612fclog: T36A tool defectmre