From a68ae1b9f5c871311237351a0929286a7c9c808d Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 14:56:48 +0800 Subject: [PATCH] Import packages/plugin/src/plugin-route-dispatcher.ts --- .../plugin/src/plugin-route-dispatcher.ts | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 packages/plugin/src/plugin-route-dispatcher.ts diff --git a/packages/plugin/src/plugin-route-dispatcher.ts b/packages/plugin/src/plugin-route-dispatcher.ts new file mode 100644 index 0000000..d1bc347 --- /dev/null +++ b/packages/plugin/src/plugin-route-dispatcher.ts @@ -0,0 +1,64 @@ +import type { PluginRouteDispatcher } from './plugin-route-bridge.js' + +export interface PluginRouteConnectionLike { + createSharedFetchHandler?(channel: '/api'): { fetch(request: Request, peer?: unknown): Promise } + authenticatedUrl?(baseUrl: string): string +} + +/** Resolve lazily: Connection may activate after the Remote plugin. */ +export function createPluginRouteDispatcher( + readConnection: () => PluginRouteConnectionLike | undefined, + readPort: () => number | undefined, + peer?: unknown, +): PluginRouteDispatcher { + let cookie: string | undefined + return async (route, body, signal) => { + signal.throwIfAborted() + const connection = readConnection() + const handler = connection?.createSharedFetchHandler?.('/api') + let response: Response + if (handler !== undefined) { + // The authenticated Remote channel has already admitted this peer. Invoke + // the official local Fetch carrier, including on Desktop without HTTP. + response = await withSignal(handler.fetch(new Request(`http://127.0.0.1/${route}`, { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify(body ?? {}), + signal, + }), peer), signal) + } else { + const port = readPort() + if (connection?.authenticatedUrl === undefined || typeof port !== 'number') { + return { status: 503, json: { ok: false, error: { code: 'gateway/unavailable', message: 'The local plugin route carrier is unavailable.' } } } + } + const origin = `http://127.0.0.1:${port}` + if (cookie === undefined) { + const launch = await fetch(connection.authenticatedUrl(`${origin}/`), { redirect: 'manual', signal }) + const setCookie = typeof launch.headers.getSetCookie === 'function' ? launch.headers.getSetCookie() : [] + cookie = setCookie.map(item => item.split(';', 1)[0]).join('; ') || undefined + } + response = await fetch(`${origin}/${route}`, { + method: 'POST', + headers: { 'content-type': 'application/json', ...(cookie === undefined ? {} : { cookie }) }, + body: JSON.stringify(body ?? {}), + signal, + }) + if (response.status === 401) cookie = undefined + } + const text = await response.text() + let json: unknown = null + if (text !== '') { + try { json = JSON.parse(text) } catch { json = text } + } + return { status: response.status, json } + } +} + +function withSignal(pending: Promise, signal: AbortSignal): Promise { + return new Promise((resolve, reject) => { + const onAbort = () => reject(signal.reason) + signal.addEventListener('abort', onAbort, { once: true }) + if (signal.aborted) onAbort() + void pending.then(resolve, reject).finally(() => signal.removeEventListener('abort', onAbort)) + }) +}