/** * MEGA Radio track list (titles, lengths, open dates). Releases are data-driven: a track plays once its MP3 * is fully stored in this package's storage (uploaded by the owner with `npm run upload-radio`) and its open * date (America/New_York) has passed. Until then it shows as `?????`. Runtime-free. */ export type Track = { n: number title: string length?: string /** YYYY-MM-DD (America/New_York) the track unlocks */ opens?: string /** where the MP3 sits on Noah's machine (scripts/upload-radio.ts only); its cover is ` (cover).png` beside it */ localFile?: string } export type Catalog = { album: string; page: string; tracks: Track[] } /** A track as shown to listeners: never file locations. */ export type PublicTrack = { n: number; title: string; length: string | null; opens: string | null; released: boolean } export type View = { album: string; page: string; tracks: PublicTrack[] } /** The track list (the released tracks and the announced open dates). */ export const radio: Catalog = { album: 'WAKE - Control Plane', page: 'https://mega-radio--noah.kody.run/', tracks: [ { n: 1, title: 'Wake', length: '0:47', localFile: '~/mega-dev/radio/01 Wake.mp3' }, { n: 2, title: 'Someone Who Can Read Slowly', length: '3:20', localFile: '~/mega-dev/radio/02 Someone Who Can Read Slowly.mp3' }, { n: 3, title: 'The Message I Never Sent', length: '3:23', localFile: '~/mega-dev/radio/03 The Message I Never Sent.mp3' }, { n: 4, title: 'The Sticker on the Screen', length: '3:28', localFile: '~/mega-dev/radio/04 The Sticker on the Screen.mp3' }, { n: 5, title: 'Entry Twenty-Three', length: '2:59', localFile: '~/mega-dev/radio/05 Entry Twenty-Three.mp3' }, { n: 6, title: '?????', opens: '2026-11-18' }, { n: 7, title: '?????', opens: '2026-11-23' }, ], } /** Two-digit track number used in file keys and URLs. */ export const nn = (n: number) => String(n).padStart(2, '0') /** Storage key of a track's MP3. */ export const mp3Key = (n: number) => `radio/${nn(n)}.mp3` /** * Today's date in New York as YYYY-MM-DD. * * @param now - the current instant * @returns the calendar date */ export function nyDate(now = new Date()): string { const p = Object.fromEntries(new Intl.DateTimeFormat('en-US', { timeZone: 'America/New_York', year: 'numeric', month: '2-digit', day: '2-digit' }).formatToParts(now).map((x) => [x.type, x.value])) return `${p.year}-${p.month}-${p.day}` } /** True once a track's open date (New York) has come, or when it has none. */ export const isOpen = (t: Track, now = new Date()) => !t.opens || t.opens <= nyDate(now) /** * Keep only the catalog fields (drops localFile and anything unknown). * * @param c - a catalog * @returns a clean catalog, tracks sorted by number * @throws when the shape is wrong */ export function cleanCatalog(c: unknown): Catalog { const o = c as Partial | null if (!o || typeof o.album !== 'string' || typeof o.page !== 'string' || !Array.isArray(o.tracks)) throw new Error('radio catalog must be { album, page, tracks[] }') const tracks = o.tracks.map((t) => { const n = Number(t?.n) if (!Number.isInteger(n) || n < 1 || n > 99 || typeof t.title !== 'string') throw new Error(`bad radio track ${JSON.stringify(t)}`) const out: Track = { n, title: t.title } if (typeof t.length === 'string' && t.length) out.length = t.length if (typeof t.opens === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(t.opens)) out.opens = t.opens return out }) return { album: o.album, page: o.page, tracks: tracks.sort((a, b) => a.n - b.n) } } /** * The listener view: which tracks play right now. * * @param c - the catalog * @param stored - keys of fully stored files (e.g. `radio/03.mp3`) * @param now - the current instant * @returns album, page and tracks with `released` */ export function view(c: Catalog, stored: Iterable, now = new Date()): View { const have = new Set(stored) return { album: c.album, page: c.page, tracks: c.tracks.map((t) => ({ n: t.n, title: t.title, length: t.length ?? null, opens: t.opens ?? null, released: have.has(mp3Key(t.n)) && isOpen(t, now) })), } } /** Hide titles and lengths of tracks that are not released yet (what listeners see). */ export const maskLocked = (v: View): View => ({ ...v, tracks: v.tracks.map((t) => (t.released ? t : { ...t, title: '?????', length: null })) }) /** The seed view (tracks with a localFile count as released); used before storage is read and in tests. */ export const seedView = (): View => view(radio, radio.tracks.filter((t) => t.localFile).map((t) => mp3Key(t.n))) /** * Files to upload for each seed track with a local file (scripts/upload-radio.ts). * * @param home - the uploader's home directory (expands `~/`) * @returns storage key and local path for each MP3 and cover */ export function radioFiles(home: string): { key: string; path: string }[] { return radio.tracks .filter((t) => t.localFile) .flatMap((t) => { const mp3 = t.localFile!.replace(/^~\//, `${home}/`) return [ { key: `radio/${nn(t.n)}.mp3`, path: mp3 }, { key: `radio/${nn(t.n)}.png`, path: mp3.replace(/\.mp3$/, ' (cover).png') }, ] }) }