From df13a62d9306eac8ac23cd626aaf4b9826000d0c Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Fri, 10 Jul 2026 00:10:54 -0500 Subject: [PATCH] fix(app): serve /__graph__ and /__openapi__ over app.fetch (dev tools on all runtimes) Signed-off-by: Diego Resendez --- CHANGELOG.md | 5 +++++ src/app/index.ts | 17 +++++++++++++---- test/app.test.ts | 24 ++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45124f7..82e9b68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,11 @@ green-tea uses calendar versioning: `YY.MM.PATCH`. - **Cloudflare Workers / edge adapter** (`@green-tea/core/edge`): `edgeHandler(app)` runs HTTP + SSE + WebSocket on workerd, reusing the neutral `app.upgrade` / `WsSocket` capability. Requires the `nodejs_compat` compatibility flag. Green Tea now runs on Node, Deno, Bun, and the edge — with identical WebSocket, rooms, and channel behaviour on all four. - **`app.upgrade(request, socket)`**: neutral WebSocket entry point for non-Node runtimes, built on a shared `WsSocket` capability. WebSocket logic is now runtime-agnostic (`src/http/ws-core.ts`). +### Fixed +- The opt-in dev routes `/__graph__` (graph viewer) and `/__openapi__` are now + served over `app.fetch` too, so they work on every runtime (Deno/Bun/edge), + not only the Node `app.listen()` path. + ### Changed - App-scope providers now boot exactly once (memoized): a second `app.listen()` call no longer re-runs provider factories or their side effects. diff --git a/src/app/index.ts b/src/app/index.ts index 0d1c0b7..4a00cc0 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -119,6 +119,15 @@ export function createApp(opts: { info, ); + // Dev-only introspection routes (opt-in). Shared by BOTH listen() and app.fetch so the + // graph viewer + OpenAPI doc are served on every runtime (Node/Deno/Bun/edge), not Node alone. + const devRoutes = (): RouteDef[] => { + const routes: RouteDef[] = []; + if (opts.devGraph) routes.push(devGraphRoute(graph)); + if (opts.devOpenapi) routes.push(openApiRoute(openapi)); + return routes; + }; + const providedSeed = (plan: RoutePlan) => seedProviders(container, plan); const planSteps = (plan: RoutePlan) => compilePlanSteps(runners, plan); @@ -147,6 +156,7 @@ export function createApp(opts: { { providedSeed, planSteps, bus, onError: opts.onError }, fetchOpts, bootAppProviders, + devRoutes, ); const upgradeFn = buildAppUpgrade( () => booted, @@ -179,9 +189,7 @@ export function createApp(opts: { deps, }); - const httpRoutes = buildHttpRoutes(routePlans, remoteRoutes, deps); - if (opts.devGraph) httpRoutes.push(devGraphRoute(graph)); - if (opts.devOpenapi) httpRoutes.push(openApiRoute(openapi)); + const httpRoutes = [...buildHttpRoutes(routePlans, remoteRoutes, deps), ...devRoutes()]; const wsRoutes = buildWsRoutes(routePlans, deps); server = createHttpServer(httpRoutes, wsRoutes, bus, meshControl, { @@ -638,13 +646,14 @@ function buildAppFetch( deps: PipelineDeps, fetchOpts: HttpOptions, ensureBooted: () => Promise, + devRoutes: () => RouteDef[], ): (request: Request) => Promise { let handler: ((request: Request) => Promise) | undefined; return async (request: Request): Promise => { if (!handler) { if (!getBooted()) throw new Error('fetch() unavailable before listen() for mesh apps'); - handler = buildFetch(buildHttpRoutes(routePlans, [], deps), fetchOpts); + handler = buildFetch([...buildHttpRoutes(routePlans, [], deps), ...devRoutes()], fetchOpts); } await ensureBooted(); // idempotent: no-op if listen() already booted the providers diff --git a/test/app.test.ts b/test/app.test.ts index 4b52a50..fc944ad 100644 --- a/test/app.test.ts +++ b/test/app.test.ts @@ -398,6 +398,30 @@ describe('devGraph endpoint', () => { expect(res.status).toBe(404); s.close(); }); + + it('serves /__graph__ and /__openapi__ over app.fetch too (runtime-agnostic dev tools)', 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, devOpenapi: true }); + + const graph = await app.fetch(new Request('http://x/__graph__')); + expect(graph.status).toBe(200); + expect(await graph.text()).toContain('cytoscape'); + + const openapi = await app.fetch(new Request('http://x/__openapi__')); + expect(openapi.status).toBe(200); + expect(((await openapi.json()) as { openapi: string }).openapi).toBe('3.1.0'); + + // still absent when the flags are off, on the fetch path too + const off = createApp({ modules: [M] }); + expect((await off.fetch(new Request('http://x/__graph__'))).status).toBe(404); + }); }); describe('graceful shutdown', () => {