Skip to content

Commit 1d3e433

Browse files
committed
chore: drop raw-body dependency
1 parent abb2f0a commit 1d3e433

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

package-lock.json

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
"express": "^5.0.1",
7272
"express-rate-limit": "^7.5.0",
7373
"pkce-challenge": "^5.0.0",
74-
"raw-body": "^3.0.0",
7574
"zod": "^3.23.8",
7675
"zod-to-json-schema": "^3.24.1"
7776
},
@@ -100,4 +99,4 @@
10099
"resolutions": {
101100
"strip-ansi": "6.0.1"
102101
}
103-
}
102+
}

src/server/sse.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ import { randomUUID } from "node:crypto";
22
import { IncomingMessage, ServerResponse } from "node:http";
33
import { Transport } from "../shared/transport.js";
44
import { JSONRPCMessage, JSONRPCMessageSchema, MessageExtraInfo, RequestInfo } from "../types.js";
5-
import getRawBody from "raw-body";
65
import contentType from "content-type";
76
import { AuthInfo } from "./auth/types.js";
87
import { URL } from 'url';
98

10-
const MAXIMUM_MESSAGE_SIZE = "4mb";
9+
const MAXIMUM_MESSAGE_SIZE = 4 * 1024 * 1024; // 4MB
1110

1211
/**
1312
* Configuration options for SSEServerTransport.
@@ -161,7 +160,7 @@ export class SSEServerTransport implements Transport {
161160

162161
body = parsedBody ?? await getRawBody(req, {
163162
limit: MAXIMUM_MESSAGE_SIZE,
164-
encoding: ct.parameters.charset ?? "utf-8",
163+
encoding: (ct.parameters.charset as BufferEncoding) ?? "utf-8",
165164
});
166165
} catch (error) {
167166
res.writeHead(400).end(String(error));
@@ -219,3 +218,23 @@ export class SSEServerTransport implements Transport {
219218
return this._sessionId;
220219
}
221220
}
221+
222+
export function getRawBody(req: IncomingMessage, { limit, encoding }: { limit: number, encoding: BufferEncoding }) {
223+
return new Promise<string>((resolve, reject) => {
224+
let received = 0;
225+
226+
const chunks: Buffer[] = [];
227+
req.on("data", (chunk: Buffer) => {
228+
received += chunk.length;
229+
if (received > limit)
230+
return reject(new Error(`Message size exceeds limit of ${limit} bytes`));
231+
chunks.push(chunk);
232+
});
233+
req.on('end', () => {
234+
resolve(Buffer.concat(chunks).toString(encoding));
235+
});
236+
req.on('error', (error) => {
237+
reject(error);
238+
});
239+
});
240+
}

src/server/streamableHttp.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { IncomingMessage, ServerResponse } from "node:http";
22
import { Transport } from "../shared/transport.js";
33
import { MessageExtraInfo, RequestInfo, isInitializeRequest, isJSONRPCError, isJSONRPCRequest, isJSONRPCResponse, JSONRPCMessage, JSONRPCMessageSchema, RequestId, SUPPORTED_PROTOCOL_VERSIONS, DEFAULT_NEGOTIATED_PROTOCOL_VERSION } from "../types.js";
4-
import getRawBody from "raw-body";
54
import contentType from "content-type";
65
import { randomUUID } from "node:crypto";
76
import { AuthInfo } from "./auth/types.js";
7+
import { getRawBody } from "./sse.js";
88

9-
const MAXIMUM_MESSAGE_SIZE = "4mb";
9+
const MAXIMUM_MESSAGE_SIZE = 4 * 1024 * 1024; // 4MB
1010

1111
export type StreamId = string;
1212
export type EventId = string;
@@ -412,9 +412,9 @@ export class StreamableHTTPServerTransport implements Transport {
412412
const parsedCt = contentType.parse(ct);
413413
const body = await getRawBody(req, {
414414
limit: MAXIMUM_MESSAGE_SIZE,
415-
encoding: parsedCt.parameters.charset ?? "utf-8",
415+
encoding: (parsedCt.parameters.charset as BufferEncoding) ?? "utf-8",
416416
});
417-
rawMessage = JSON.parse(body.toString());
417+
rawMessage = JSON.parse(body);
418418
}
419419

420420
let messages: JSONRPCMessage[];

0 commit comments

Comments
 (0)