Repository Public
1import Fastify, { type FastifyInstance } from "fastify";2import { readFileSync } from "node:fs";3import { loadConfig, type Config } from "./config.ts";4import { openDatabase, schemaVersion, type DB } from "./db.ts";5import { clientKey, registerAuthRoutes, sessionFrom, type Deps } from "./auth.ts";6import { registerSyncRoutes } from "./sync.ts";7import { parseSearchRequest } from "./search/normalize.ts";8import { SearchService, TAVILY_USD_PER_CREDIT } from "./search/service.ts";9import { HttpError, OpsLog, RateLimiter, fetchJSON, now, safeEqual, utcDay } from "./util.ts";1011export interface App { server: FastifyInstance; config: Config; db: DB; search: SearchService; log: OpsLog }1213export function buildApp(overrides: Partial<Config> = {}): App {14 const config = loadConfig(overrides);15 const db = openDatabase(config.dbPath);16 const log = new OpsLog();17 const limiter = new RateLimiter();18 const deps: Deps = { config, db, log, limiter };19 const search = new SearchService(config, db, log);20 const startedAt = now();2122 // Request logging is off on purpose: bodies contain search queries and URLs would be logged with them.23 const server = Fastify({ logger: false, trustProxy: true, bodyLimit: 1024 * 1024 });2425 server.setErrorHandler((error: any, _request, reply) => {26 if (error instanceof HttpError) {27 if (error.extra?.retryAfter) reply.header("retry-after", String(error.extra.retryAfter));28 return reply.status(error.status).send({ error: { code: error.code, message: error.message, ...error.extra } });29 }30 if (error?.validation || error?.statusCode === 400 || error?.code === "FST_ERR_CTP_INVALID_JSON_BODY") {31 return reply.status(400).send({ error: { code: "bad_request", message: "Malformed request" } });32 }33 if (error?.statusCode === 413) return reply.status(413).send({ error: { code: "too_large", message: "Request too large" } });34 log.add("error", "server", `unhandled ${error?.name ?? "error"}`);35 return reply.status(500).send({ error: { code: "internal", message: "Internal error" } });36 });37 server.setNotFoundHandler((_request, reply) => reply.status(404).send({ error: { code: "not_found", message: "Not found" } }));38 server.addHook("onSend", async (_request, reply) => {39 reply.header("cache-control", "no-store");40 reply.header("x-content-type-options", "nosniff");41 reply.header("referrer-policy", "no-referrer");42 });4344 server.get("/api/health", async () => ({45 ok: true, service: "sp-browser-backend", version: config.version, schema: schemaVersion(db),46 providers: { tavily: !!config.tavilyKey, youtube: !!config.youtubeKey },47 }));4849 // Apple fetches this to tie the app's passkeys to this domain (webcredentials).50 server.get("/.well-known/apple-app-site-association", async (_request, reply) => {51 reply.header("content-type", "application/json");52 return { webcredentials: { apps: config.appleAppIDs } };53 });5455 // ---- SP Search ----56 server.post("/api/search", async (request) => {57 const session = sessionFrom(deps, request);58 if (!session && !config.anonSearch) throw new HttpError(401, "unauthenticated", "Sign in to search");59 const wait = session60 ? limiter.check(`search:u:${session.userId}`, config.userSearchPerHour, 3600_000)61 : limiter.check(`search:a:${clientKey(deps, request)}`, config.anonSearchPerHour, 3600_000);62 if (wait) throw new HttpError(429, "rate_limited", "Too many searches. Try again shortly.", { retryAfter: wait });63 return search.search(parseSearchRequest(request.body));64 });6566 server.get("/api/search/capabilities", async () => ({67 types: ["web", "images", "videos"],68 web: { provider: "tavily", pagination: "batches-of-20-then-other-domains", summary: true, relatedSearches: false, spellcheck: false, autocomplete: false },69 images: { provider: "tavily", dimensions: "measured-on-device" },70 videos: {71 provider: config.youtubeKey ? "youtube-data-api" : "tavily+oembed", platforms: ["youtube", "vimeo", "dailymotion"],72 duration: config.youtubeKey ? "all" : "vimeo-only", publishedDate: config.youtubeKey ? "all" : "vimeo-only",73 },74 filters: { language: true, region: true, timeRange: ["day", "week", "month", "year"], safeSearch: true },75 }));7677 registerAuthRoutes(server, deps);78 registerSyncRoutes(server, deps);7980 // ---- Administration (bearer ADMIN_TOKEN) ----81 let tavilyUsage: { at: number; value: unknown } = { at: 0, value: null };82 function requireAdmin(request: any): void {83 const header = String(request.headers.authorization ?? "");84 if (!config.adminToken || !header.startsWith("Bearer ") || !safeEqual(header.slice(7), config.adminToken)) throw new HttpError(401, "unauthenticated", "Admin token required");85 }8687 server.get("/api/admin/overview", async (request) => {88 requireAdmin(request);89 const since = utcDay(now() - 29 * 86400_000);90 const usage = db.prepare("SELECT day, provider, kind, requests, credits, cache_hits, errors, latency_ms_total FROM usage_daily WHERE day >= ? ORDER BY day DESC, provider, kind").all(since) as any[];91 const tavilyCredits30 = usage.filter((u) => u.provider === "tavily").reduce((sum, u) => sum + u.credits, 0);92 if (config.tavilyKey && now() - tavilyUsage.at > 15 * 60_000) {93 try {94 const { status, body } = await fetchJSON(`${config.tavilyBase}/usage`, { headers: { authorization: `Bearer ${config.tavilyKey}` }, timeoutMs: 8000 });95 tavilyUsage = { at: now(), value: status === 200 ? body : { error: `HTTP ${status}` } };96 } catch { tavilyUsage = { at: now(), value: { error: "unreachable" } }; }97 }98 const one = (sql: string, ...params: any[]) => (db.prepare(sql).get(...params) as any).n as number;99 return {100 service: { version: config.version, schema: schemaVersion(db), startedAt, uptimeSeconds: Math.round((now() - startedAt) / 1000), memoryMB: Math.round(process.memoryUsage().rss / 1048576) },101 search: {102 today: { day: utcDay(), tavilyCredits: search.creditsToday("tavily"), budget: config.dailyCreditBudget },103 last30Days: { tavilyCredits: tavilyCredits30, estimatedCostUSD: Math.round(tavilyCredits30 * TAVILY_USD_PER_CREDIT * 100) / 100, usdPerCredit: TAVILY_USD_PER_CREDIT },104 cache: search.cacheStats(), usage, providerAccount: tavilyUsage.value,105 },106 identity: {107 users: one("SELECT COUNT(*) AS n FROM users"), passkeys: one("SELECT COUNT(*) AS n FROM credentials"),108 devices: one("SELECT COUNT(*) AS n FROM devices WHERE revoked_at IS NULL"), activeSessions: one("SELECT COUNT(*) AS n FROM sessions WHERE expires_at > ?", now()),109 unusedInvites: one("SELECT COUNT(*) AS n FROM invites WHERE used_at IS NULL"),110 },111 sync: {112 accountsWithSync: one("SELECT COUNT(*) AS n FROM account_keys"), records: one("SELECT COUNT(*) AS n FROM sync_records WHERE deleted = 0"),113 tombstones: one("SELECT COUNT(*) AS n FROM sync_records WHERE deleted = 1"),114 ciphertextBytes: one("SELECT COALESCE(SUM(LENGTH(payload)),0) AS n FROM sync_records"),115 devicesWaitingForKey: one("SELECT COUNT(*) AS n FROM devices WHERE revoked_at IS NULL AND key_envelope IS NULL AND user_id IN (SELECT user_id FROM account_keys)"),116 lastSyncAt: (db.prepare("SELECT MAX(last_sync_at) AS n FROM devices").get() as any).n,117 byCollection: db.prepare("SELECT collection, COUNT(*) AS records FROM sync_records WHERE deleted = 0 GROUP BY collection").all(),118 },119 errors: { last24h: log.countSince("error", now() - 86400_000), warnings24h: log.countSince("warn", now() - 86400_000) },120 log: log.recent(120),121 };122 });123124 const adminPage = readFileSync(new URL("../public/admin.html", import.meta.url), "utf8");125 server.get("/admin", async (_request, reply) => {126 reply.header("content-type", "text/html; charset=utf-8");127 reply.header("content-security-policy", "default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; connect-src 'self'");128 return adminPage;129 });130131 // Housekeeping: expired sessions and challenges, tombstones older than a year.132 const sweep = setInterval(() => {133 try {134 db.prepare("DELETE FROM sessions WHERE expires_at < ?").run(now());135 db.prepare("DELETE FROM challenges WHERE expires_at < ?").run(now());136 db.prepare("DELETE FROM sync_records WHERE deleted = 1 AND updated_at < ?").run(now() - 365 * 86400_000);137 } catch { log.add("error", "server", "housekeeping failed"); }138 }, 3600_000);139 sweep.unref();140 server.addHook("onClose", async () => { clearInterval(sweep); db.close(); });141142 return { server, config, db, search, log };143}144145if (import.meta.url === `file://${process.argv[1]}`) {146 const app = buildApp();147 if (!process.env.SERVER_SECRET) console.warn("SERVER_SECRET is not set: rate-limit keys change at every restart.");148 if (!app.config.tavilyKey) console.warn("TAVILY_API_KEY is not set: searches will answer 503.");149 app.server.listen({ port: app.config.port, host: app.config.host }).then(() => {150 console.log(`sp-browser-backend ${app.config.version} listening on ${app.config.host}:${app.config.port} (rp ${app.config.rpID})`);151 });152 for (const signal of ["SIGINT", "SIGTERM"] as const) process.on(signal, () => app.server.close().then(() => process.exit(0)));153}154