From 73cd874caef66382083a388d1a26c020e46bf6be Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Sun, 28 Jun 2026 02:31:52 -0500 Subject: [PATCH] feat: opt-in GET /__graph__ dev endpoint rendering the live graph Signed-off-by: Diego Resendez --- src/app.ts | 17 +++++++++++++++-- test/app.test.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/app.ts b/src/app.ts index f9a78f7..1bbf352 100644 --- a/src/app.ts +++ b/src/app.ts @@ -2,7 +2,7 @@ import http from 'http'; import { Bus } from './bus'; import { Container } from './container'; import { topoSort, subgraphFor, GraphNode } from './graph'; -import { toMermaid, toDOT, type GraphView } from './graph-viz'; +import { toMermaid, toDOT, graphHtml, type GraphView } from './graph-viz'; import { runPipeline, PipelineStep } from './pipeline'; import { createHttpServer, parseQuery, RouteDef, WsRouteDef, RequestLimits } from './http'; import { isAsyncIterable } from './channel'; @@ -51,7 +51,7 @@ export interface MeshConfig { timeoutMs?: number; } -export function createApp(opts: { modules: Ctor[]; plugins?: Plugin[]; mesh?: MeshConfig; limits?: RequestLimits }): App { +export function createApp(opts: { modules: Ctor[]; plugins?: Plugin[]; mesh?: MeshConfig; limits?: RequestLimits; devGraph?: boolean }): App { const bus = new Bus(); const container = new Container(); let server: http.Server | undefined; @@ -310,6 +310,19 @@ export function createApp(opts: { modules: Ctor[]; plugins?: Plugin[]; mesh?: Me ...remoteRoutes, ]; + if (opts.devGraph) { + httpRoutes.push({ + method: 'GET', pattern: '/__graph__', transport: 'buffer', + handler: async (req) => { + const mermaid = toMermaid(graph()); + const accept = String(req.headers['accept'] ?? ''); + return accept.includes('text/plain') + ? { status: 200, headers: { 'content-type': 'text/plain' }, body: mermaid } + : { status: 200, headers: { 'content-type': 'text/html' }, body: graphHtml(mermaid) }; + }, + }); + } + const wsRoutes: WsRouteDef[] = routePlans .filter((plan) => plan.transport === 'ws' || plan.transport === 'negotiate') .map((plan) => ({ diff --git a/test/app.test.ts b/test/app.test.ts index 162c732..3ab5680 100644 --- a/test/app.test.ts +++ b/test/app.test.ts @@ -238,6 +238,32 @@ describe('app.graph / explain', () => { }); }); +describe('devGraph endpoint', () => { + const portOf = (s: any) => (s.address() as any).port; + it('serves /__graph__ as HTML when devGraph:true', async () => { + @Route('/u') class Ctl { @Get('/me') me() { return { ok: 1 }; } } + @Module({ mountpoint: '/api', controllers: [Ctl] }) class M {} + const app = createApp({ modules: [M], devGraph: true }); + const s = await app.listen(0); + const html = await fetch(`http://127.0.0.1:${portOf(s)}/__graph__`).then((r) => r.text()); + expect(html).toContain('class="mermaid"'); + expect(html).toContain('flowchart'); + const mmd = await fetch(`http://127.0.0.1:${portOf(s)}/__graph__`, { headers: { accept: 'text/plain' } }); + expect(mmd.headers.get('content-type')).toContain('text/plain'); + expect(await mmd.text()).toContain('flowchart'); + s.close(); + }); + it('does NOT mount /__graph__ without devGraph', async () => { + @Route('/u') class Ctl { @Get('/me') me() { return { ok: 1 }; } } + @Module({ mountpoint: '/api', controllers: [Ctl] }) class M {} + const app = createApp({ modules: [M] }); + const s = await app.listen(0); + const res = await fetch(`http://127.0.0.1:${portOf(s)}/__graph__`); + expect(res.status).toBe(404); + s.close(); + }); +}); + describe('graceful shutdown', () => { it('app.close() stops accepting and resolves, even with an SSE stream open', async () => { @Route('/')