@@ -4,6 +4,7 @@ import { Transport } from "../shared/transport.js";
4
4
import { JSONRPCMessage , JSONRPCMessageSchema } from "../types.js" ;
5
5
import getRawBody from "raw-body" ;
6
6
import contentType from "content-type" ;
7
+ import { AuthInfo } from "./auth/types.js" ;
7
8
8
9
const MAXIMUM_MESSAGE_SIZE = "4mb" ;
9
10
@@ -18,7 +19,7 @@ export class SSEServerTransport implements Transport {
18
19
19
20
onclose ?: ( ) => void ;
20
21
onerror ?: ( error : Error ) => void ;
21
- onmessage ?: ( message : JSONRPCMessage ) => void ;
22
+ onmessage ?: ( message : JSONRPCMessage , authInfo ?: AuthInfo ) => void ;
22
23
23
24
/**
24
25
* 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 {
66
67
* This should be called when a POST request is made to send a message to the server.
67
68
*/
68
69
async handlePostMessage (
69
- req : IncomingMessage ,
70
+ req : IncomingMessage & { auth ?: AuthInfo } ,
70
71
res : ServerResponse ,
71
72
parsedBody ?: unknown ,
72
73
) : Promise < void > {
@@ -75,6 +76,7 @@ export class SSEServerTransport implements Transport {
75
76
res . writeHead ( 500 ) . end ( message ) ;
76
77
throw new Error ( message ) ;
77
78
}
79
+ const authInfo : AuthInfo | undefined = req . auth ;
78
80
79
81
let body : string | unknown ;
80
82
try {
@@ -94,7 +96,7 @@ export class SSEServerTransport implements Transport {
94
96
}
95
97
96
98
try {
97
- await this . handleMessage ( typeof body === 'string' ? JSON . parse ( body ) : body ) ;
99
+ await this . handleMessage ( typeof body === 'string' ? JSON . parse ( body ) : body , authInfo ) ;
98
100
} catch {
99
101
res . writeHead ( 400 ) . end ( `Invalid message: ${ body } ` ) ;
100
102
return ;
@@ -106,7 +108,7 @@ export class SSEServerTransport implements Transport {
106
108
/**
107
109
* 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.
108
110
*/
109
- async handleMessage ( message : unknown ) : Promise < void > {
111
+ async handleMessage ( message : unknown , authInfo ?: AuthInfo ) : Promise < void > {
110
112
let parsedMessage : JSONRPCMessage ;
111
113
try {
112
114
parsedMessage = JSONRPCMessageSchema . parse ( message ) ;
@@ -115,7 +117,7 @@ export class SSEServerTransport implements Transport {
115
117
throw error ;
116
118
}
117
119
118
- this . onmessage ?.( parsedMessage ) ;
120
+ this . onmessage ?.( parsedMessage , authInfo ) ;
119
121
}
120
122
121
123
async close ( ) : Promise < void > {
0 commit comments