From 8ea512df699f0a161fe0027c9b28695ab6f96246 Mon Sep 17 00:00:00 2001 From: arlo Date: Thu, 3 Dec 2026 14:39:54 +0800 Subject: [PATCH] fix(hub): advertise SSE path relative to connection meta --- .../hub/src/node/__tests__/initiate.test.ts | 22 +++++++++++++++++++ packages/hub/src/node/initiate.ts | 19 +++++++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/packages/hub/src/node/__tests__/initiate.test.ts b/packages/hub/src/node/__tests__/initiate.test.ts index 4cb844ee..cc630198 100644 --- a/packages/hub/src/node/__tests__/initiate.test.ts +++ b/packages/hub/src/node/__tests__/initiate.test.ts @@ -184,6 +184,28 @@ describe('initHub', () => { } }) + it('keeps a stripped proxy prefix when resolving the SSE path', async () => { + const hub = initHub({ + base: DEVFRAMES_HUB_BASE, + auth: false, + devframes: [ + makeFrame('alpha', makeDist('alpha')), + ], + }) + try { + for (const frame of ['', 'alpha/']) { + const metaPath = `${DEVFRAMES_HUB_BASE}${frame}__connection.json` + const response = await hub.handler(new Request(`http://localhost${metaPath}`)) + const meta = await response.json() + expect(new URL(meta.sse.path, `http://localhost/foo${metaPath}`).pathname) + .toBe(`/foo${DEVFRAMES_HUB_BASE}__sse`) + } + } + finally { + await hub.close() + } + }) + it('ui slot: viewer owns the root, embedded.js serves the entry, discovery still wins', async () => { const viewerDist = makeDist('hub viewer') const embeddedDir = mkdtempSync(join(tmpdir(), 'hub-embedded-')) diff --git a/packages/hub/src/node/initiate.ts b/packages/hub/src/node/initiate.ts index 46180849..b87656b1 100644 --- a/packages/hub/src/node/initiate.ts +++ b/packages/hub/src/node/initiate.ts @@ -17,7 +17,7 @@ import { createInteractiveAuth } from 'devframe/recipes/interactive-auth' import { mountStaticHandler } from 'devframe/utils/serve-static' import { joinURL, withoutLeadingSlash } from 'devframe/utils/url' import { H3 } from 'h3' -import { resolve } from 'pathe' +import { relative, resolve } from 'pathe' import { DEVFRAMES_HUB_BASE, DOCK_RENDERERS_STATE_KEY, normalizeHubBase } from '../constants' import { mountDevframes, renderClientImportsModule, resolveDevframesInput, resolveRendererRegistrations } from './assemble' import { createHubContext } from './context' @@ -342,6 +342,19 @@ export interface HubInstance { close: () => Promise } +/** Resolve SSE from the metadata directory so proxy prefixes stay intact. */ +function withRelativeSsePath(meta: ConnectionMeta | undefined, base: string): ConnectionMeta | undefined { + if (typeof meta?.sse !== 'object' || !meta.sse.path) + return meta + return { + ...meta, + sse: { + ...meta.sse, + path: relative(base, meta.sse.path), + }, + } +} + /** * Resolve the shared hub context: reuse a caller-supplied one, or build an * h3-backed context that serves the hub's connection meta under every frame. @@ -378,7 +391,7 @@ async function resolveHubContext( * matter how deep the frame base is. */ mountConnectionMeta: (frameBase: string) => { - app.use(joinURL(frameBase, DEVFRAME_CONNECTION_META_FILENAME), () => api.connectionMeta()) + app.use(joinURL(frameBase, DEVFRAME_CONNECTION_META_FILENAME), () => withRelativeSsePath(api.connectionMeta(), frameBase)) }, } return createHubContext({ @@ -562,7 +575,7 @@ export function initHub(options: InitHubOptions): HubInstance { // Hub-level discovery endpoints, registered before the viewer's static // mount so its SPA-fallback can't swallow them. - app.use(joinURL(base, DEVFRAME_CONNECTION_META_FILENAME), () => meta) + app.use(joinURL(base, DEVFRAME_CONNECTION_META_FILENAME), () => withRelativeSsePath(meta, base)) const indexDocument = (): Record => ({ name: options.name,