@@ -4,6 +4,7 @@ import { Transport } from "../shared/transport.js";
44import { JSONRPCMessage , JSONRPCMessageSchema } from "../types.js" ;
55import getRawBody from "raw-body" ;
66import contentType from "content-type" ;
7+ import { AuthInfo } from "./auth/types.js" ;
78
89const MAXIMUM_MESSAGE_SIZE = "4mb" ;
910
@@ -18,7 +19,7 @@ export class SSEServerTransport implements Transport {
1819
1920 onclose ?: ( ) => void ;
2021 onerror ?: ( error : Error ) => void ;
21- onmessage ?: ( message : JSONRPCMessage ) => void ;
22+ onmessage ?: ( message : JSONRPCMessage , authInfo ?: AuthInfo ) => void ;
2223
2324 /**
2425 * Creates a new SSE server transport, which will direct the client to POST messages to the relative or absolute URL identified by `_endpoint`.
@@ -66,7 +67,7 @@ export class SSEServerTransport implements Transport {
6667 * This should be called when a POST request is made to send a message to the server.
6768 */
6869 async handlePostMessage (
69- req : IncomingMessage ,
70+ req : IncomingMessage & { auth ?: AuthInfo } ,
7071 res : ServerResponse ,
7172 parsedBody ?: unknown ,
7273 ) : Promise < void > {
@@ -75,6 +76,7 @@ export class SSEServerTransport implements Transport {
7576 res . writeHead ( 500 ) . end ( message ) ;
7677 throw new Error ( message ) ;
7778 }
79+ const authInfo : AuthInfo | undefined = req . auth ;
7880
7981 let body : string | unknown ;
8082 try {
@@ -94,7 +96,7 @@ export class SSEServerTransport implements Transport {
9496 }
9597
9698 try {
97- await this . handleMessage ( typeof body === 'string' ? JSON . parse ( body ) : body ) ;
99+ await this . handleMessage ( typeof body === 'string' ? JSON . parse ( body ) : body , authInfo ) ;
98100 } catch {
99101 res . writeHead ( 400 ) . end ( `Invalid message: ${ body } ` ) ;
100102 return ;
@@ -106,7 +108,7 @@ export class SSEServerTransport implements Transport {
106108 /**
107109 * Handle a client message, regardless of how it arrived. This can be used to inform the server of messages that arrive via a means different than HTTP POST.
108110 */
109- async handleMessage ( message : unknown ) : Promise < void > {
111+ async handleMessage ( message : unknown , authInfo ?: AuthInfo ) : Promise < void > {
110112 let parsedMessage : JSONRPCMessage ;
111113 try {
112114 parsedMessage = JSONRPCMessageSchema . parse ( message ) ;
@@ -115,7 +117,7 @@ export class SSEServerTransport implements Transport {
115117 throw error ;
116118 }
117119
118- this . onmessage ?.( parsedMessage ) ;
120+ this . onmessage ?.( parsedMessage , authInfo ) ;
119121 }
120122
121123 async close ( ) : Promise < void > {
0 commit comments