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
- f29e2502log: RV39 gate fixmre
- ad529ac1log: m572 verdictmre
- 1b37d404RV39 on hold: hybriel#308mre
- f9127c68Revert "Merge branch 'rv39'"mre
- cd39f3d3log: hybriel#308mre
- 51341d6elog: RV39 deploy startedmre
- c9e29e06Merge branch 'rv39'mre
- 0f8d1fdclog: RV39 report, browser 387/2mre
- 50249b0bRV39: reportmre
- 10ed9581compose: HL_GC_STATS=1mre
- 955da19bRV39: re-vendor Hybriel set day39 (source 51e921d7, bin 6b481aa7)mre
- 64b8d1dclog: RV39 startedmre
- 1f2df72amission RV39mre
- 6c1936f9log+status: GC planmre
- 2846a840log: m569 result, GC hypothesismre
- b24cd2d5log: Anton on hl:fetch, mission 569mre
- 7d42cac1log+status: SL1 measured, fetch stalls remainmre
- 8e5f2ceflog: SL1 deployedmre
- 29f34212log: SL1 deploy startedmre
- 529dd741Merge sl1-slices: slice details repair and person fill (SL1)mre