import { existsSync } from 'node:fs' import { join } from 'node:path' import type { DoctorLive, DoctorRule, LiveResponse, RuleFinding } from '../types.js' import { run } from '../util.js' import { noindex } from './security.js' import { parseToolOutput, titleSlug } from './tooling.js' /** GET-only fetcher with a cache (each path is requested once per run) and a doctor user agent. */ export function createLiveFetcher(url: string, options: { adminRoute?: string; staging?: boolean } = {}): DoctorLive { const base = url.replace(/\/+$/, '') const cache = new Map>() const fetchOnce = (path: string, init: { redirect?: 'follow' | 'manual' } = {}) => { const key = `${init.redirect ?? 'follow'} ${path}` if (!cache.has(key)) { cache.set( key, (async () => { try { const res = await fetch(base + path, { headers: { 'user-agent': 'ipal-doctor (+https://intecion.com; read-only)' }, redirect: init.redirect ?? 'follow', signal: AbortSignal.timeout(20_000), }) return { headers: res.headers, status: res.status, text: await res.text() } } catch (err) { const e = err as { cause?: { code?: string }; message?: string } return { error: e.cause?.code ?? e.message ?? 'unreachable', headers: new Headers(), status: 0, text: '' } } })(), ) } return cache.get(key)! } return { adminRoute: options.adminRoute ?? '/admin', fetch: fetchOnce, staging: options.staging ?? false, url: base } } export const liveRules: DoctorRule[] = [ { id: 'live-reachable', check: async (ctx) => { const home = await ctx.live!.fetch('/', { redirect: 'manual' }) return home.status ? [] : [{ message: `${ctx.live!.url} is unreachable (${home.error})`, severity: 'error' }] }, group: 'live', source: 'live', }, { id: 'live-health', check: async (ctx) => { const res = await ctx.live!.fetch('/api/health') return res.status === 200 ? [] : [{ fix: 'src/app/api/health/route.ts → healthRoute (Coolify health check)', message: `/api/health → ${res.status || res.error}`, severity: 'error' }] }, docs: 'docs/deployment.md', group: 'live', source: 'live', }, { id: 'live-noindex', check: async (ctx) => { const home = await ctx.live!.fetch('/') if (!home.status) {return []} const isNoindex = noindex(home.text, home.headers.get('x-robots-tag')) if (ctx.live!.staging) {return isNoindex ? [] : [{ fix: 'IPAL_NOINDEX=1 on the staging app', message: 'the staging site is indexable', severity: 'error' }]} return isNoindex ? [{ fix: 'unset IPAL_NOINDEX on production and redeploy', message: 'the production home page is noindex', severity: 'error' }] : [] }, docs: 'docs/seo.md', group: 'live', source: 'live', }, { id: 'live-seo', check: async (ctx) => { // ipal-seo-check of the doctor's own version: robots, sitemap, canonical, hreflang, noindex, JSON-LD, llms, MCP. const script = join(ctx.packageRoot, 'docs/agent/skills/ipal-seo-check/scripts/seo-check.mjs') if (!existsSync(script)) {return [{ message: 'ipal-seo-check not found', severity: 'warning' }]} const home = await ctx.live!.fetch('/', { redirect: 'manual' }) if (!home.status) {return []} const res = run(process.execPath, [script, ctx.live!.url, '--max', '10'], ctx.root, 300_000) const out: RuleFinding[] = [] // INFO lines are notes (e.g. "no verification meta" with DNS verification) — not findings. for (const line of parseToolOutput(res.stdout).filter((l) => l.level !== 'info')) { const items = line.items.length ? `: ${line.items.slice(0, 3).join('; ')}${line.items.length > 3 ? ` … +${line.items.length - 3}` : ''}` : '' out.push({ fix: line.rule, message: `${line.title}${items}`, rule: `live-seo:${titleSlug(line.title.replace(/[\d/]+/g, ' '))}`, severity: line.level }) } if (res.code === 2) {out.push({ message: `ipal-seo-check: ${res.stderr.trim()}`, severity: 'error' })} return out }, docs: 'docs/agent/skills/ipal-seo-check/SKILL.md', group: 'live', source: 'live', }, ]