11import { Transport } from "./shared/transport.js" ;
22import { JSONRPCMessage } from "./types.js" ;
3+ import { AuthInfo } from "./server/auth/types.js" ;
4+
5+ interface QueuedMessage {
6+ message : JSONRPCMessage ;
7+ authInfo ?: AuthInfo ;
8+ }
39
410/**
511 * In-memory transport for creating clients and servers that talk to each other within the same process.
612 */
713export class InMemoryTransport implements Transport {
814 private _otherTransport ?: InMemoryTransport ;
9- private _messageQueue : JSONRPCMessage [ ] = [ ] ;
15+ private _messageQueue : QueuedMessage [ ] = [ ] ;
1016
1117 onclose ?: ( ) => void ;
1218 onerror ?: ( error : Error ) => void ;
13- onmessage ?: ( message : JSONRPCMessage ) => void ;
19+ onmessage ?: ( message : JSONRPCMessage , authInfo ?: AuthInfo ) => void ;
1420 sessionId ?: string ;
1521
1622 /**
@@ -27,10 +33,8 @@ export class InMemoryTransport implements Transport {
2733 async start ( ) : Promise < void > {
2834 // Process any messages that were queued before start was called
2935 while ( this . _messageQueue . length > 0 ) {
30- const message = this . _messageQueue . shift ( ) ;
31- if ( message ) {
32- this . onmessage ?.( message ) ;
33- }
36+ const queuedMessage = this . _messageQueue . shift ( ) ! ;
37+ this . onmessage ?.( queuedMessage . message , queuedMessage . authInfo ) ;
3438 }
3539 }
3640
@@ -42,14 +46,22 @@ export class InMemoryTransport implements Transport {
4246 }
4347
4448 async send ( message : JSONRPCMessage ) : Promise < void > {
49+ return this . sendWithAuth ( message ) ;
50+ }
51+
52+ /**
53+ * Sends a message with optional auth info.
54+ * This is useful for testing authentication scenarios.
55+ */
56+ async sendWithAuth ( message : JSONRPCMessage , authInfo ?: AuthInfo ) : Promise < void > {
4557 if ( ! this . _otherTransport ) {
4658 throw new Error ( "Not connected" ) ;
4759 }
4860
4961 if ( this . _otherTransport . onmessage ) {
50- this . _otherTransport . onmessage ( message ) ;
62+ this . _otherTransport . onmessage ( message , authInfo ) ;
5163 } else {
52- this . _otherTransport . _messageQueue . push ( message ) ;
64+ this . _otherTransport . _messageQueue . push ( { message, authInfo } ) ;
5365 }
5466 }
5567}
0 commit comments