tracker
All repositories: gitoria
3.9 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 (project.hl movedTo: 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' } }) }
Branches
Latest commits
- f52862a2log: HM62b startedmre
- 88fde2c4mission HM62bmre
- a722e526log: #62 blocked by hybriel#317mre
- 074a7418log: #62 assigned to Tracker; pending rulemre
- 06f6ae8fHM62 report: mpackdb index gapmre
- 86fe5430log: HM62 startedmre
- 755307bbmission HM62mre
- 4b2f2995HM61 report: tracker#61 packed people ordermre
- 48faa84flog: #60 deploy startedmre
- 90734c6cMerge branch 'hm60-shortids'mre
- e0db0d1bHM60 report: tracker#60 packed short-ID ownersmre
- 3c79b576Pack short-ID ownership into byte buffersmre
- e822524bstatus wordingmre
- 1c7f0480log: HM60/HM61 startedmre
- 2841d215missions HM60 HM61mre
- 289c6ce4HM1 report: heap object breakdownmre
- 2e7d6257log: HM1 startedmre
- 2f790465mission HM1: heap object breakdownmre
- a18957falog: evening re-measure after CBmre
- aac9a98dlog: evening re-measure startmre