Merge branch 'main' into ihrpr/resume-long-running-requests

This commit is contained in:
ihrpr 2025-04-15 22:10:58 +01:00
commit f9f2ba4717
No known key found for this signature in database
2 changed files with 76 additions and 23 deletions

View file

@ -21,4 +21,10 @@ export interface AuthInfo {
* When the token expires (in seconds since epoch).
*/
expiresAt?: number;
/**
* Additional data associated with the token.
* This field should be used for any additional data that needs to be attached to the auth info.
*/
extra?: Record<string, unknown>;
}

View file

@ -4,6 +4,7 @@ import { Transport } from "../shared/transport.js";
import { JSONRPCMessage, JSONRPCMessageSchema } from "../types.js";
import getRawBody from "raw-body";
import contentType from "content-type";
import { AuthInfo } from "./auth/types.js";
import { URL } from 'url';
const MAXIMUM_MESSAGE_SIZE = "4mb";
@ -19,7 +20,7 @@ export class SSEServerTransport implements Transport {
onclose?: () => void;
onerror?: (error: Error) => void;
onmessage?: (message: JSONRPCMessage) => void;
onmessage?: (message: JSONRPCMessage, extra?: { authInfo?: AuthInfo }) => void;
/**
* Creates a new SSE server transport, which will direct the client to POST messages to the relative or absolute URL identified by `_endpoint`.
@ -76,7 +77,7 @@ export class SSEServerTransport implements Transport {
* This should be called when a POST request is made to send a message to the server.
*/
async handlePostMessage(
req: IncomingMessage,
req: IncomingMessage & { auth?: AuthInfo },
res: ServerResponse,
parsedBody?: unknown,
): Promise<void> {
@ -85,6 +86,7 @@ export class SSEServerTransport implements Transport {
res.writeHead(500).end(message);
throw new Error(message);
}
const authInfo: AuthInfo | undefined = req.auth;
let body: string | unknown;
try {
@ -104,7 +106,7 @@ export class SSEServerTransport implements Transport {
}
try {
await this.handleMessage(typeof body === 'string' ? JSON.parse(body) : body);
await this.handleMessage(typeof body === 'string' ? JSON.parse(body) : body, { authInfo });
} catch {
res.writeHead(400).end(`Invalid message: ${body}`);
return;
@ -116,7 +118,7 @@ export class SSEServerTransport implements Transport {
/**
* 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.
*/
async handleMessage(message: unknown): Promise<void> {
async handleMessage(message: unknown, extra?: { authInfo?: AuthInfo }): Promise<void> {
let parsedMessage: JSONRPCMessage;
try {
parsedMessage = JSONRPCMessageSchema.parse(message);
@ -125,7 +127,7 @@ export class SSEServerTransport implements Transport {
throw error;
}
this.onmessage?.(parsedMessage);
this.onmessage?.(parsedMessage, extra);
}
async close(): Promise<void> {