Skip to content

Commit c4381b8

Browse files
committed
Pass extra object instead of authInfo directly
1 parent bc38bb2 commit c4381b8

File tree

4 files changed

+21
-24
lines changed

4 files changed

+21
-24
lines changed

Diff for: src/inMemory.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ describe("InMemoryTransport", () => {
5252

5353
let receivedMessage: JSONRPCMessage | undefined;
5454
let receivedAuthInfo: AuthInfo | undefined;
55-
serverTransport.onmessage = (msg, auth) => {
55+
serverTransport.onmessage = (msg, extra) => {
5656
receivedMessage = msg;
57-
receivedAuthInfo = auth;
57+
receivedAuthInfo = extra?.authInfo;
5858
};
5959

60-
await clientTransport.sendWithAuth(message, authInfo);
60+
await clientTransport.send(message, { authInfo });
6161
expect(receivedMessage).toEqual(message);
6262
expect(receivedAuthInfo).toEqual(authInfo);
6363
});

Diff for: src/inMemory.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { AuthInfo } from "./server/auth/types.js";
44

55
interface QueuedMessage {
66
message: JSONRPCMessage;
7-
authInfo?: AuthInfo;
7+
extra?: { authInfo?: AuthInfo };
88
}
99

1010
/**
@@ -16,7 +16,7 @@ export class InMemoryTransport implements Transport {
1616

1717
onclose?: () => void;
1818
onerror?: (error: Error) => void;
19-
onmessage?: (message: JSONRPCMessage, authInfo?: AuthInfo) => void;
19+
onmessage?: (message: JSONRPCMessage, extra?: { authInfo?: AuthInfo }) => void;
2020
sessionId?: string;
2121

2222
/**
@@ -34,7 +34,7 @@ export class InMemoryTransport implements Transport {
3434
// Process any messages that were queued before start was called
3535
while (this._messageQueue.length > 0) {
3636
const queuedMessage = this._messageQueue.shift()!;
37-
this.onmessage?.(queuedMessage.message, queuedMessage.authInfo);
37+
this.onmessage?.(queuedMessage.message, queuedMessage.extra);
3838
}
3939
}
4040

@@ -45,23 +45,19 @@ export class InMemoryTransport implements Transport {
4545
this.onclose?.();
4646
}
4747

48-
async send(message: JSONRPCMessage): Promise<void> {
49-
return this.sendWithAuth(message);
50-
}
51-
5248
/**
5349
* Sends a message with optional auth info.
5450
* This is useful for testing authentication scenarios.
5551
*/
56-
async send(message: JSONRPCMessage, options?: { authInfo?: AuthInfo }): Promise<void>
52+
async send(message: JSONRPCMessage, options?: { authInfo?: AuthInfo }): Promise<void> {
5753
if (!this._otherTransport) {
5854
throw new Error("Not connected");
5955
}
6056

6157
if (this._otherTransport.onmessage) {
62-
this._otherTransport.onmessage(message, authInfo);
58+
this._otherTransport.onmessage(message, { authInfo: options?.authInfo });
6359
} else {
64-
this._otherTransport._messageQueue.push({ message, authInfo });
60+
this._otherTransport._messageQueue.push({ message, extra: { authInfo: options?.authInfo } });
6561
}
6662
}
6763
}

Diff for: src/server/sse.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class SSEServerTransport implements Transport {
1919

2020
onclose?: () => void;
2121
onerror?: (error: Error) => void;
22-
onmessage?: (message: JSONRPCMessage, authInfo?: AuthInfo) => void;
22+
onmessage?: (message: JSONRPCMessage, extra?: { authInfo?: AuthInfo }) => void;
2323

2424
/**
2525
* Creates a new SSE server transport, which will direct the client to POST messages to the relative or absolute URL identified by `_endpoint`.
@@ -96,7 +96,7 @@ export class SSEServerTransport implements Transport {
9696
}
9797

9898
try {
99-
await this.handleMessage(typeof body === 'string' ? JSON.parse(body) : body, authInfo);
99+
await this.handleMessage(typeof body === 'string' ? JSON.parse(body) : body, { authInfo });
100100
} catch {
101101
res.writeHead(400).end(`Invalid message: ${body}`);
102102
return;
@@ -108,7 +108,7 @@ export class SSEServerTransport implements Transport {
108108
/**
109109
* 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.
110110
*/
111-
async handleMessage(message: unknown, authInfo?: AuthInfo): Promise<void> {
111+
async handleMessage(message: unknown, extra?: { authInfo?: AuthInfo }): Promise<void> {
112112
let parsedMessage: JSONRPCMessage;
113113
try {
114114
parsedMessage = JSONRPCMessageSchema.parse(message);
@@ -117,7 +117,7 @@ export class SSEServerTransport implements Transport {
117117
throw error;
118118
}
119119

120-
this.onmessage?.(parsedMessage, authInfo);
120+
this.onmessage?.(parsedMessage, extra);
121121
}
122122

123123
async close(): Promise<void> {

Diff for: src/shared/protocol.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,11 @@ export abstract class Protocol<
246246
this._onerror(error);
247247
};
248248

249-
this._transport.onmessage = (message, authInfo) => {
249+
this._transport.onmessage = (message, extra) => {
250250
if (!("method" in message)) {
251251
this._onresponse(message);
252252
} else if ("id" in message) {
253-
this._onrequest(message, authInfo);
253+
this._onrequest(message, extra);
254254
} else {
255255
this._onnotification(message);
256256
}
@@ -296,7 +296,7 @@ export abstract class Protocol<
296296
);
297297
}
298298

299-
private _onrequest(request: JSONRPCRequest, authInfo?: AuthInfo): void {
299+
private _onrequest(request: JSONRPCRequest, extra?: { authInfo?: AuthInfo }): void {
300300
const handler =
301301
this._requestHandlers.get(request.method) ?? this.fallbackRequestHandler;
302302

@@ -321,16 +321,17 @@ export abstract class Protocol<
321321
const abortController = new AbortController();
322322
this._requestHandlerAbortControllers.set(request.id, abortController);
323323

324-
// Create extra object with both abort signal and sessionId from transport
325-
const extra: RequestHandlerExtra = {
324+
// Create fullExtra object with both abort signal and sessionId from transport
325+
// in addition to any authInfo provided
326+
const fullExtra: RequestHandlerExtra = {
326327
signal: abortController.signal,
327328
sessionId: this._transport?.sessionId,
328-
authInfo,
329+
authInfo: extra?.authInfo,
329330
};
330331

331332
// Starting with Promise.resolve() puts any synchronous errors into the monad as well.
332333
Promise.resolve()
333-
.then(() => handler(request, extra))
334+
.then(() => handler(request, fullExtra))
334335
.then(
335336
(result) => {
336337
if (abortController.signal.aborted) {

0 commit comments

Comments
 (0)