From 20e95917619fbcbf5e68da3b0ce6f70e2064ce68 Mon Sep 17 00:00:00 2001 From: Marius Maaland Date: Wed, 7 Oct 2026 20:18:35 +0200 Subject: [PATCH] Give every report a JSON twin at /s/.json (#35) The domain's newest scan as data: the same fields the MCP get_scan tool returns (scanId, domain, status, report) plus url, createdAt, finishedAt and the report page's address. The report itself matches schema/report.v1.json; it's null while the scan runs. An unknown domain gets a 404 saying where to scan it. Readable from anywhere (CORS *), cached for minutes once finished and seconds while running. The report page links to it, visibly and with a rel=alternate link for agents. Closes #35 Co-Authored-By: Claude Opus 5.5 --- apps/web/next.config.ts | 5 ++ .../web/src/app/api/reports/[domain]/route.ts | 10 +++ apps/web/src/app/globals.css | 14 ++++ apps/web/src/app/s/[domain]/page.tsx | 8 +- .../app/s/[domain]/report-sections.test.tsx | 5 +- .../src/app/s/[domain]/report-sections.tsx | 7 ++ apps/web/src/lib/report-json.test.ts | 73 +++++++++++++++++++ apps/web/src/lib/report-json.ts | 72 ++++++++++++++++++ 8 files changed, 191 insertions(+), 3 deletions(-) create mode 100644 apps/web/src/app/api/reports/[domain]/route.ts create mode 100644 apps/web/src/lib/report-json.test.ts create mode 100644 apps/web/src/lib/report-json.ts diff --git a/apps/web/next.config.ts b/apps/web/next.config.ts index 4c77b74..0a6f06d 100644 --- a/apps/web/next.config.ts +++ b/apps/web/next.config.ts @@ -7,6 +7,11 @@ const rootEnv = new URL('../../.env.local', import.meta.url); if (existsSync(rootEnv)) process.loadEnvFile(rootEnv); const nextConfig: NextConfig = { + // Every report page has a JSON twin at the same address plus .json. Domains have dots, so it + // can't be its own [domain] segment: it's rewritten to a route handler instead. + async rewrites() { + return [{ source: '/s/:domain(.+)\\.json', destination: '/api/reports/:domain' }]; + }, // The workspace packages ship TypeScript source, so Next compiles them like app code. // Only loaded when self-hosted with Chromium installed; never bundled. serverExternalPackages: ['playwright-core'], diff --git a/apps/web/src/app/api/reports/[domain]/route.ts b/apps/web/src/app/api/reports/[domain]/route.ts new file mode 100644 index 0000000..3a8cb2f --- /dev/null +++ b/apps/web/src/app/api/reports/[domain]/route.ts @@ -0,0 +1,10 @@ +import { reportJson } from '../../../../lib/report-json.ts'; +import { getStore } from '../../../../lib/services.ts'; + +// /s/.json lands here (see next.config.ts): the report page's JSON twin. +export async function GET(request: Request, { params }: { params: Promise<{ domain: string }> }) { + const domain = decodeURIComponent((await params).domain).toLowerCase(); + const scan = await getStore().latestScan(domain); + const { status, body, headers } = reportJson(domain, scan, new URL(request.url).origin); + return Response.json(body, { status, headers }); +} diff --git a/apps/web/src/app/globals.css b/apps/web/src/app/globals.css index 4a59c19..94c5eb8 100644 --- a/apps/web/src/app/globals.css +++ b/apps/web/src/app/globals.css @@ -650,6 +650,14 @@ details[open] > .tech-row { display: contents; } +.json-link { + align-self: center; + margin-right: var(--space-2); + font-family: var(--font-mono); + font-size: var(--text-data); + color: var(--ink-3); +} + .warn-text { color: var(--warn-ink); } @@ -818,6 +826,12 @@ details[open] > .tech-row { grid-template-columns: 1fr 1fr; } + .json-link { + grid-column: 1 / -1; + align-self: start; + margin: 0 0 var(--space-1); + } + .actions .button { width: 100%; height: var(--control-md); diff --git a/apps/web/src/app/s/[domain]/page.tsx b/apps/web/src/app/s/[domain]/page.tsx index 82b9c14..3bd9918 100644 --- a/apps/web/src/app/s/[domain]/page.tsx +++ b/apps/web/src/app/s/[domain]/page.tsx @@ -9,8 +9,12 @@ interface Props { } export async function generateMetadata({ params }: Props): Promise { - const { domain } = await params; - return { title: `${decodeURIComponent(domain)} · stackprobe` }; + const name = decodeURIComponent((await params).domain).toLowerCase(); + return { + title: `${name} · stackprobe`, + // Agents can find the report as data from the page itself. + alternates: { types: { 'application/json': `/s/${encodeURIComponent(name)}.json` } }, + }; } export default async function ScanPage({ params, searchParams }: Props) { diff --git a/apps/web/src/app/s/[domain]/report-sections.test.tsx b/apps/web/src/app/s/[domain]/report-sections.test.tsx index 77d034b..7bc0173 100644 --- a/apps/web/src/app/s/[domain]/report-sections.test.tsx +++ b/apps/web/src/app/s/[domain]/report-sections.test.tsx @@ -127,10 +127,13 @@ describe('ReportView', () => { expect(html).not.toContain('framer header'); }); - test('says which checks ran and offers copy and scan again', () => { + test('says which checks ran and offers the JSON twin, copy and scan again', () => { const html = render(); expect(html).toContain('Checks that ran: HTTP and a real browser.'); expect(html).toContain('Fingerprints abc123'); + expect(html).toContain( + 'acme.test.json', + ); expect(html).toContain('Copy link'); expect(html).toContain('Scan again'); }); diff --git a/apps/web/src/app/s/[domain]/report-sections.tsx b/apps/web/src/app/s/[domain]/report-sections.tsx index b727b30..4fbf6f8 100644 --- a/apps/web/src/app/s/[domain]/report-sections.tsx +++ b/apps/web/src/app/s/[domain]/report-sections.tsx @@ -141,6 +141,13 @@ export function ReportView({

+ + {scan.domain}.json +
diff --git a/apps/web/src/lib/report-json.test.ts b/apps/web/src/lib/report-json.test.ts new file mode 100644 index 0000000..1b960e4 --- /dev/null +++ b/apps/web/src/lib/report-json.test.ts @@ -0,0 +1,73 @@ +import { Report, type ScanRecord, scan } from '@drippa/stackprobe-core'; +import { describe, expect, test } from 'vitest'; +import { DOMAINS_DIR, loadRecording, ReplayNet } from '../../../../packages/core/test/fixtures.ts'; +import { reportJson } from './report-json.ts'; + +const origin = 'https://stackprobe.tech'; + +// A real report: the recorded drippa.ai scan, replayed without the network. +async function fixtureScan(): Promise { + const recording = loadRecording('drippa.ai', DOMAINS_DIR); + const report = await scan(recording.url, { + net: new ReplayNet(recording), + now: () => new Date(recording.recordedAt), + }); + return { + id: 'scan-1', + domain: 'drippa.ai', + url: recording.url, + status: 'done', + createdAt: '2026-10-07T12:00:00.000Z', + finishedAt: '2026-10-07T12:00:28.000Z', + report, + }; +} + +describe('reportJson', () => { + test('a finished scan: its report, valid against schema/report.v1.json, and the scan around it', async () => { + const record = await fixtureScan(); + const { status, body, headers } = reportJson('drippa.ai', record, origin); + expect(status).toBe(200); + expect(body).toMatchObject({ + scanId: 'scan-1', + domain: 'drippa.ai', + status: 'done', + createdAt: '2026-10-07T12:00:00.000Z', + finishedAt: '2026-10-07T12:00:28.000Z', + page: 'https://stackprobe.tech/s/drippa.ai', + }); + // The committed JSON Schema is generated from this zod schema, and core's tests keep the two + // equal, so parsing with it is validating against the file. + const report = 'report' in body ? body.report : null; + expect(Report.parse(JSON.parse(JSON.stringify(report)))).toEqual(record.report); + expect(headers['cache-control']).toMatch(/s-maxage=300/); + expect(headers['access-control-allow-origin']).toBe('*'); + }); + + test('a running scan: its status, no report yet, cached only for seconds', () => { + const running: ScanRecord = { + id: 'scan-2', + domain: 'acme.test', + url: 'https://acme.test/', + status: 'running', + createdAt: '2026-10-07T12:00:00.000Z', + finishedAt: null, + report: null, + }; + const { status, body, headers } = reportJson('acme.test', running, origin); + expect(status).toBe(200); + expect(body).toMatchObject({ status: 'running', report: null, finishedAt: null }); + expect(headers['cache-control']).toBe('public, max-age=0, s-maxage=2'); + }); + + test('an unknown domain: 404, with where to scan it', () => { + const { status, body } = reportJson('new.test', null, origin); + expect(status).toBe(404); + expect(body).toEqual({ + error: 'not_scanned', + domain: 'new.test', + message: "new.test hasn't been scanned yet. Open the page to scan it.", + page: 'https://stackprobe.tech/s/new.test', + }); + }); +}); diff --git a/apps/web/src/lib/report-json.ts b/apps/web/src/lib/report-json.ts new file mode 100644 index 0000000..87a8b9e --- /dev/null +++ b/apps/web/src/lib/report-json.ts @@ -0,0 +1,72 @@ +import type { Report, ScanRecord, ScanStatus } from '@drippa/stackprobe-core'; + +// The JSON twin of a report page, /s/.json: the domain's newest scan, as data. The same +// fields the MCP get_scan tool returns, plus the scan's address and times. `report` alone +// matches schema/report.v1.json; it's null while the scan runs. +export interface ReportJson { + scanId: string; + domain: string; + url: string; + status: ScanStatus; + createdAt: string; + finishedAt: string | null; + // The report page for people. + page: string; + report: Report | null; +} + +export interface NotScannedJson { + error: 'not_scanned'; + domain: string; + message: string; + page: string; +} + +export interface JsonResponse { + status: number; + body: ReportJson | NotScannedJson; + headers: Record; +} + +// Finished scans never change, but a new scan can replace them as the newest; running ones +// change every few seconds. +const CACHE = { + finished: 'public, max-age=60, s-maxage=300, stale-while-revalidate=3600', + running: 'public, max-age=0, s-maxage=2', + missing: 'public, max-age=0, s-maxage=5', +}; + +export function reportJson(domain: string, scan: ScanRecord | null, origin: string): JsonResponse { + const page = `${origin}/s/${encodeURIComponent(domain)}`; + // Reading is free and public: any site or agent may fetch it. + const headers = { 'access-control-allow-origin': '*' }; + if (!scan) { + return { + status: 404, + headers: { ...headers, 'cache-control': CACHE.missing }, + body: { + error: 'not_scanned', + domain, + message: `${domain} hasn't been scanned yet. Open the page to scan it.`, + page, + }, + }; + } + return { + status: 200, + headers: { + ...headers, + 'cache-control': scan.status === 'running' ? CACHE.running : CACHE.finished, + }, + body: { + scanId: scan.id, + domain: scan.domain, + url: scan.url, + status: scan.status, + createdAt: scan.createdAt, + finishedAt: scan.finishedAt, + page, + report: scan.report, + }, + }; +}