From dad9eafe3e6aa356dcf2579386807fad85f99770 Mon Sep 17 00:00:00 2001 From: akazwz <50396286+akazwz@users.noreply.github.com> Date: Sun, 9 Mar 2025 10:01:07 +0800 Subject: [PATCH] Improve SSE endpoint sessionId parameter handling Add dynamic query parameter separator for sessionId to handle endpoints with existing query parameters more robustly --- src/server/sse.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/server/sse.ts b/src/server/sse.ts index 84c1cbb9..8aa01928 100644 --- a/src/server/sse.ts +++ b/src/server/sse.ts @@ -49,8 +49,17 @@ export class SSEServerTransport implements Transport { }); // Send the endpoint event + /** + * Determine the appropriate separator for adding the sessionId parameter. + * Uses '&' if the endpoint already contains a query parameter (has a '?'), + * otherwise uses '?' to start the query string. + * + * Note: This approach works for standard endpoints but doesn't handle + * the edge case where '?' might be part of the path itself. + */ + const separator = this._endpoint.includes('?') ? '&' : '?'; this.res.write( - `event: endpoint\ndata: ${encodeURI(this._endpoint)}?sessionId=${this._sessionId}\n\n`, + `event: endpoint\ndata: ${encodeURI(this._endpoint)}${separator}sessionId=${this._sessionId}\n\n`, ); this._sseResponse = this.res;