// Added 2026-11-29 to a modified Lucille (AGPL-3.0); see NOTICE.md. // Trail fork: pace / distance left / ETA for timed shares that carry a route. // Mirrors the Android app's TrailFormat so the phone and the viewer always agree. namespace trail { export const METRES_PER_MILE = 1609.344; /** Below this speed (m/s), pace and ETA are hidden. */ export const MIN_PACE_SPEED = 0.3; /** "9:32 /mi", "Stopped" for 0, or null when unknown or too slow. */ export function pace(speed: number | null | undefined): string | null { if (speed == null) { return null; } if (speed == 0) { return "Stopped"; } if (speed < MIN_PACE_SPEED) { return null; } let secs = Math.round(METRES_PER_MILE / speed); if (secs >= 3600) { secs = 3599; } return `${Math.floor(secs / 60)}:${String(secs % 60).padStart(2, "0")} /mi`; } export function remaining(remM: number | null | undefined): string | null { if (remM == null) { return null; } return `${(remM / METRES_PER_MILE).toFixed(1)} mi left`; } /** Arrival clock time from the fix's own time (not the viewer's clock), or null. */ export function eta(fixTimeMs: number, remM: number | null | undefined, speed: number | null | undefined, locale?: string, timeZone?: string): string | null { if (remM == null || speed == null || speed < MIN_PACE_SPEED) { return null; } let at = new Date(fixTimeMs + Math.round(remM / speed * 1000)); return at.toLocaleTimeString(locale, { hour: "numeric", minute: "2-digit", timeZone: timeZone }); } // ---- short links: box id derived from the key; keys in base64url ---- const BOX_PREFIX = "trailshare-box-v1"; export const BOX_ID_LENGTH = 16; /** First 16 bytes of SHA-256("trailshare-box-v1" ‖ secretKey). Same as the app's ShareLink. */ export async function deriveBoxId(secretKey: Uint8Array): Promise { let prefix = new TextEncoder().encode(BOX_PREFIX); let input = new Uint8Array(prefix.length + secretKey.length); input.set(prefix, 0); input.set(secretKey, prefix.length); let digest = await crypto.subtle.digest("SHA-256", input); return new Uint8Array(digest).slice(0, BOX_ID_LENGTH); } /** * Decodes a key from a link: unpadded base64url (short links) or hex (old links). * Returns null unless it decodes to exactly {@code length} bytes. */ export function decodeKey(s: string, length: number): Uint8Array | null { if (s.length == 2 * length && /^[0-9a-fA-F]+$/.test(s)) { let out = new Uint8Array(length); for (let i = 0; i < length; i++) { out[i] = parseInt(s.substring(2 * i, 2 * i + 2), 16); } return out; } if (!/^[A-Za-z0-9_-]+$/.test(s)) { return null; } let b64 = s.replace(/-/g, "+").replace(/_/g, "/"); b64 += "=".repeat((4 - b64.length % 4) % 4); let bin: string; try { bin = atob(b64); } catch { return null; } if (bin.length != length) { return null; } let out = new Uint8Array(length); for (let i = 0; i < length; i++) { out[i] = bin.charCodeAt(i); } return out; } /** "2.0 mi left · 10:00 /mi · ETA 6:20 PM", or null when the share has no route. */ export function status(fixTimeMs: number, remM: number | null | undefined, speed: number | null | undefined, locale?: string, timeZone?: string): string | null { let rem = remaining(remM); if (rem == null) { return null; } let parts = [rem]; let p = pace(speed); if (p != null) { parts.push(p); } let e = eta(fixTimeMs, remM, speed, locale, timeZone); if (e != null) { parts.push(`ETA ${e}`); } return parts.join(" · "); } } // for the Node unit tests; ignored in the browser declare var module: any; if (typeof module !== "undefined") { module.exports = trail; }