@@ -68,7 +68,8 @@ export class SSEServerTransport implements Transport {
6868 async handlePostMessage (
6969 req : IncomingMessage ,
7070 res : ServerResponse ,
71- rawBody ?: string
71+ rawBody ?: string | Buffer ,
72+ bodyEncoding ?: BufferEncoding
7273 ) : Promise < void > {
7374 if ( ! this . _sseResponse ) {
7475 const message = "SSE connection not established" ;
@@ -83,10 +84,30 @@ export class SSEServerTransport implements Transport {
8384 throw new Error ( `Unsupported content-type: ${ ct } ` ) ;
8485 }
8586
86- body = rawBody ?? await getRawBody ( req , {
87- limit : MAXIMUM_MESSAGE_SIZE ,
88- encoding : ct . parameters . charset ?? "utf-8" ,
89- } ) ;
87+ if ( rawBody ) {
88+ if ( typeof rawBody === 'string' ) {
89+ body = rawBody ;
90+ } else if ( Buffer . isBuffer ( rawBody ) ) {
91+ if ( bodyEncoding ) {
92+ body = rawBody . toString ( bodyEncoding ) ;
93+ } else if ( ct . parameters . charset ) {
94+ if ( Buffer . isEncoding ( ct . parameters . charset ) ) {
95+ body = rawBody . toString ( ct . parameters . charset as BufferEncoding ) ;
96+ } else {
97+ throw new Error ( 'Unsupported buffer encoding: ${ct.parameters.charset}' )
98+ }
99+ } else {
100+ body = rawBody . toString ( 'utf8' ) ;
101+ }
102+ } else {
103+ throw new Error ( "Unsupported rawBody type. rawBody must be one of 'Buffer' or 'string'" ) ;
104+ }
105+ } else {
106+ body = rawBody ?? await getRawBody ( req , {
107+ limit : MAXIMUM_MESSAGE_SIZE ,
108+ encoding : ct . parameters . charset ?? "utf-8" ,
109+ } ) ;
110+ }
90111 } catch ( error ) {
91112 res . writeHead ( 400 ) . end ( String ( error ) ) ;
92113 this . onerror ?.( error as Error ) ;
0 commit comments