// Proxy for licensed translations (NLT, CSB, NASB) served by api.bible. // The API key stays on the server (API_BIBLE_KEY, loaded from .env if present); // the browser only ever talks to /api/bible/... import { existsSync, readFileSync } from 'fs'; import { fileURLToPath } from 'url'; import { dirname, join } from 'path'; const __dirname = dirname(fileURLToPath(import.meta.url)); // Minimal .env loader so the key doesn't have to be exported in every launcher // (setup.sh, systemd, docker). Real environment variables win. const envPath = join(__dirname, '..', '.env'); if (existsSync(envPath)) { for (const line of readFileSync(envPath, 'utf8').split('\n')) { const m = line.match(/^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*?)\s*$/); if (m && process.env[m[1]] === undefined) process.env[m[1]] = m[2].replace(/^(['"])(.*)\1$/, '$2'); } } const API_BASE = 'https://rest.api.bible/v1'; // Canonical book order (index + 1 = the book number the ESV API uses in ranges). const BOOK_ORDER = ['GEN','EXO','LEV','NUM','DEU','JOS','JDG','RUT','1SA','2SA','1KI','2KI','1CH','2CH','EZR','NEH','EST','JOB','PSA','PRO','ECC','SNG','ISA','JER','LAM','EZK','DAN','HOS','JOL','AMO','OBA','JON','MIC','NAM','HAB','ZEP','HAG','ZEC','MAL','MAT','MRK','LUK','JHN','ACT','ROM','1CO','2CO','GAL','EPH','PHP','COL','1TH','2TH','1TI','2TI','TIT','PHM','HEB','JAS','1PE','2PE','1JN','2JN','3JN','JUD','REV']; export const LICENSED_TRANSLATIONS = { ESV: { provider: 'esv', name: 'English Standard Version' }, NLT: { provider: 'api.bible', bibleId: 'd6e14a625393b4da-01', name: 'New Living Translation' }, CSB: { provider: 'api.bible', bibleId: 'a556c5305ee15c3f-01', name: 'Christian Standard Bible' }, NASB: { provider: 'api.bible', bibleId: 'b8ee27bcd1cae43a-01', name: 'New American Standard Bible (1995)' }, }; const CACHE_TTL = 24 * 60 * 60 * 1000; const CACHE_MAX = 1000; // Crossway limits how much ESV text an app may store, so ESV chapters are only // held briefly and in small numbers (enough to absorb repeat loads). const ESV_CACHE_TTL = 60 * 60 * 1000; const ESV_CACHE_MAX = 20; const cache = new Map(); // "NLT/PSA/23" -> { at, payload } // api.bible returns plain text with inline "[12]" verse markers, poetry line // breaks, and stray "#" / nbsp characters. Split it back into { number, text }. export function parseChapterText(content) { const clean = String(content ?? '') .replace(/ /g, ' ') .replace(/#/g, ''); const parts = clean.split(/\[(\d+)\]/); const verses = []; for (let i = 1; i < parts.length; i += 2) { const text = parts[i + 1].replace(/\s+/g, ' ').replace(/\s+,$/, '').trim(); if (text) verses.push({ number: Number(parts[i]), text }); } return verses; } export function registerBibleRoutes(app, requireAuth) { app.get('/api/bible/translations', requireAuth, (_req, res) => { res.json(Object.entries(LICENSED_TRANSLATIONS).map(([id, t]) => ({ id, name: t.name, available: Boolean(t.provider === 'esv' ? process.env.ESV_API_KEY : process.env.API_BIBLE_KEY), }))); }); // ESV chapter audio (read by David Cochran Heath). The ESV API answers with a // redirect to a public mp3; hand that URL to the browser's