From 6535f93f2de061f03a8d09c6684e195f233ffb1a Mon Sep 17 00:00:00 2001 From: brn Date: Mon, 2 Dec 2024 23:40:12 +0900 Subject: [PATCH 1/2] feat: Add a parsedBody optional parameter Please enter the commit message for your changes. Lines starting --- src/server/sse.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/server/sse.ts b/src/server/sse.ts index 1342fcc..fe03182 100644 --- a/src/server/sse.ts +++ b/src/server/sse.ts @@ -68,6 +68,7 @@ export class SSEServerTransport implements Transport { async handlePostMessage( req: IncomingMessage, res: ServerResponse, + parsedBody?: unknown, ): Promise { if (!this._sseResponse) { const message = "SSE connection not established"; @@ -75,14 +76,14 @@ export class SSEServerTransport implements Transport { throw new Error(message); } - let body: string; + let body: string | unknown; try { const ct = contentType.parse(req.headers["content-type"] ?? ""); if (ct.type !== "application/json") { throw new Error(`Unsupported content-type: ${ct}`); } - body = await getRawBody(req, { + body = parsedBody ?? await getRawBody(req, { limit: MAXIMUM_MESSAGE_SIZE, encoding: ct.parameters.charset ?? "utf-8", }); @@ -93,7 +94,7 @@ export class SSEServerTransport implements Transport { } try { - await this.handleMessage(JSON.parse(body)); + await this.handleMessage(typeof body === 'string'? JSON.parse(body): body); } catch { res.writeHead(400).end(`Invalid message: ${body}`); return; From 1f835e044fddd50b04e5efd676d8dcfc66af8300 Mon Sep 17 00:00:00 2001 From: Justin Spahr-Summers Date: Mon, 9 Dec 2024 13:43:17 +0000 Subject: [PATCH 2/2] Whitespace tweaks --- src/server/sse.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server/sse.ts b/src/server/sse.ts index fe03182..84c1cbb 100644 --- a/src/server/sse.ts +++ b/src/server/sse.ts @@ -94,7 +94,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); } catch { res.writeHead(400).end(`Invalid message: ${body}`); return;