added session id to request handler extra data

This commit is contained in:
Jason Victor 2025-02-23 12:39:15 -05:00
commit 44c9bffa85
3 changed files with 14 additions and 3 deletions

View file

@ -136,8 +136,6 @@ export class SSEServerTransport implements Transport {
/**
* Returns the session ID for this transport.
*
* This can be used to route incoming POST requests.
*/
get sessionId(): string {
return this._sessionId;

View file

@ -74,6 +74,11 @@ export type RequestHandlerExtra = {
* An abort signal used to communicate if the request was cancelled from the sender's side.
*/
signal: AbortSignal;
/**
* The session ID from the transport, if available.
*/
sessionId?: string;
};
/**
@ -239,9 +244,15 @@ export abstract class Protocol<
const abortController = new AbortController();
this._requestHandlerAbortControllers.set(request.id, abortController);
// Create extra object with both abort signal and sessionId from transport
const extra: RequestHandlerExtra = {
signal: abortController.signal,
sessionId: this._transport?.sessionId,
};
// Starting with Promise.resolve() puts any synchronous errors into the monad as well.
Promise.resolve()
.then(() => handler(request, { signal: abortController.signal }))
.then(() => handler(request, extra))
.then(
(result) => {
if (abortController.signal.aborted) {

View file

@ -41,4 +41,6 @@ export interface Transport {
* Callback for when a message (request or response) is received over the connection.
*/
onmessage?: (message: JSONRPCMessage) => void;
sessionId?: string;
}