1
1
import { Transport } from "./shared/transport.js" ;
2
2
import { JSONRPCMessage } from "./types.js" ;
3
+ import { AuthInfo } from "./server/auth/types.js" ;
4
+
5
+ interface QueuedMessage {
6
+ message : JSONRPCMessage ;
7
+ authInfo ?: AuthInfo ;
8
+ }
3
9
4
10
/**
5
11
* In-memory transport for creating clients and servers that talk to each other within the same process.
6
12
*/
7
13
export class InMemoryTransport implements Transport {
8
14
private _otherTransport ?: InMemoryTransport ;
9
- private _messageQueue : JSONRPCMessage [ ] = [ ] ;
15
+ private _messageQueue : QueuedMessage [ ] = [ ] ;
10
16
11
17
onclose ?: ( ) => void ;
12
18
onerror ?: ( error : Error ) => void ;
13
- onmessage ?: ( message : JSONRPCMessage ) => void ;
19
+ onmessage ?: ( message : JSONRPCMessage , authInfo ?: AuthInfo ) => void ;
14
20
sessionId ?: string ;
15
21
16
22
/**
@@ -27,10 +33,8 @@ export class InMemoryTransport implements Transport {
27
33
async start ( ) : Promise < void > {
28
34
// Process any messages that were queued before start was called
29
35
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 ) ;
34
38
}
35
39
}
36
40
@@ -42,14 +46,22 @@ export class InMemoryTransport implements Transport {
42
46
}
43
47
44
48
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 > {
45
57
if ( ! this . _otherTransport ) {
46
58
throw new Error ( "Not connected" ) ;
47
59
}
48
60
49
61
if ( this . _otherTransport . onmessage ) {
50
- this . _otherTransport . onmessage ( message ) ;
62
+ this . _otherTransport . onmessage ( message , authInfo ) ;
51
63
} else {
52
- this . _otherTransport . _messageQueue . push ( message ) ;
64
+ this . _otherTransport . _messageQueue . push ( { message, authInfo } ) ;
53
65
}
54
66
}
55
67
}
0 commit comments