|
| 1 | +import type { JSONRPCMessage, JSONRPCRequest, RequestId } from '../types/index.js'; |
| 2 | +import { isJSONRPCNotification, ProtocolErrorCode } from '../types/index.js'; |
| 3 | +import { errorResponse } from './dispatcher.js'; |
| 4 | +import type { ListenContext, StatelessHandlers } from './stateless.js'; |
| 5 | +import { isStatelessRequest } from './stateless.js'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Per-message router for pipe-shaped server transports (stdio, in-memory). |
| 9 | + * Call once per inbound message. Returns `true` if the message was claimed by |
| 10 | + * the stateless path (so the caller should NOT pass it to legacy `onmessage`). |
| 11 | + * |
| 12 | + * Stateless requests are dispatched via {@linkcode StatelessHandlers}; |
| 13 | + * `notifications/cancelled` aborts a tracked in-flight request. |
| 14 | + */ |
| 15 | +export function routeServerStateless( |
| 16 | + message: JSONRPCMessage, |
| 17 | + handlers: StatelessHandlers, |
| 18 | + inflight: Map<RequestId, AbortController>, |
| 19 | + write: (m: JSONRPCMessage) => void, |
| 20 | + ctx: ListenContext, |
| 21 | + onerror?: (e: Error) => void |
| 22 | +): boolean { |
| 23 | + if (isStatelessRequest(message)) { |
| 24 | + const ac = new AbortController(); |
| 25 | + inflight.set(message.id, ac); |
| 26 | + void handleOne(handlers, message, ac, write, ctx) |
| 27 | + .catch(error => onerror?.(error instanceof Error ? error : new Error(String(error)))) |
| 28 | + .finally(() => inflight.delete(message.id)); |
| 29 | + return true; |
| 30 | + } |
| 31 | + if (isJSONRPCNotification(message) && message.method === 'notifications/cancelled') { |
| 32 | + const requestId = (message.params as { requestId?: RequestId } | undefined)?.requestId; |
| 33 | + const ac = requestId === undefined ? undefined : inflight.get(requestId); |
| 34 | + if (ac) { |
| 35 | + ac.abort(); |
| 36 | + return true; |
| 37 | + } |
| 38 | + } |
| 39 | + return false; |
| 40 | +} |
| 41 | + |
| 42 | +async function handleOne( |
| 43 | + handlers: StatelessHandlers, |
| 44 | + req: JSONRPCRequest, |
| 45 | + ac: AbortController, |
| 46 | + write: (m: JSONRPCMessage) => void, |
| 47 | + ctx: ListenContext |
| 48 | +): Promise<void> { |
| 49 | + if (req.method === 'subscriptions/listen') { |
| 50 | + let listenStream; |
| 51 | + try { |
| 52 | + listenStream = handlers.listen(req, ctx); |
| 53 | + } catch (error) { |
| 54 | + write( |
| 55 | + errorResponse(req.id, ProtocolErrorCode.InvalidParams, error instanceof Error ? error.message : 'Invalid listen request') |
| 56 | + ); |
| 57 | + return; |
| 58 | + } |
| 59 | + const { stream, close } = listenStream; |
| 60 | + ac.signal.addEventListener('abort', close, { once: true }); |
| 61 | + try { |
| 62 | + for await (const m of stream) { |
| 63 | + if (ac.signal.aborted) break; |
| 64 | + write(m); |
| 65 | + } |
| 66 | + } finally { |
| 67 | + ac.signal.removeEventListener('abort', close); |
| 68 | + close(); |
| 69 | + } |
| 70 | + } else { |
| 71 | + const response = await handlers.dispatch(req, { signal: ac.signal, authInfo: ctx.authInfo, notify: write }); |
| 72 | + write(response); |
| 73 | + } |
| 74 | +} |
0 commit comments