Skip to content

Commit 22370be

Browse files
Merge pull request #81 from brn/main
Add a parsedBody optional parameter
2 parents 989550d + 1f835e0 commit 22370be

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/server/sse.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,22 @@ export class SSEServerTransport implements Transport {
6868
async handlePostMessage(
6969
req: IncomingMessage,
7070
res: ServerResponse,
71+
parsedBody?: unknown,
7172
): Promise<void> {
7273
if (!this._sseResponse) {
7374
const message = "SSE connection not established";
7475
res.writeHead(500).end(message);
7576
throw new Error(message);
7677
}
7778

78-
let body: string;
79+
let body: string | unknown;
7980
try {
8081
const ct = contentType.parse(req.headers["content-type"] ?? "");
8182
if (ct.type !== "application/json") {
8283
throw new Error(`Unsupported content-type: ${ct}`);
8384
}
8485

85-
body = await getRawBody(req, {
86+
body = parsedBody ?? await getRawBody(req, {
8687
limit: MAXIMUM_MESSAGE_SIZE,
8788
encoding: ct.parameters.charset ?? "utf-8",
8889
});
@@ -93,7 +94,7 @@ export class SSEServerTransport implements Transport {
9394
}
9495

9596
try {
96-
await this.handleMessage(JSON.parse(body));
97+
await this.handleMessage(typeof body === 'string' ? JSON.parse(body) : body);
9798
} catch {
9899
res.writeHead(400).end(`Invalid message: ${body}`);
99100
return;

0 commit comments

Comments
 (0)