File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -216,4 +216,33 @@ describe("streamProxy", () => {
216216 expect ( result . stopReason ) . toBe ( "error" ) ;
217217 expect ( result . errorMessage ) . toContain ( "exceeded max buffer size" ) ;
218218 } ) ;
219+
220+ it ( "handles a large transport chunk containing many valid newline-delimited proxy SSE lines" , async ( ) => {
221+ // Regression: one TCP read can deliver >64 KiB of already-delimited lines;
222+ // the cap must apply only to the unterminated tail, not the full chunk.
223+ const encoder = new TextEncoder ( ) ;
224+ const line = `data: ${ JSON . stringify ( { type : "done" , reason : "stop" , usage } ) } \n` ;
225+ const manyLines = line . repeat ( 2000 ) ;
226+ vi . stubGlobal (
227+ "fetch" ,
228+ vi . fn ( async ( ) => ( {
229+ ok : true ,
230+ status : 200 ,
231+ body : new ReadableStream ( {
232+ start ( controller ) {
233+ controller . enqueue ( encoder . encode ( manyLines ) ) ;
234+ controller . close ( ) ;
235+ } ,
236+ } ) ,
237+ } ) ) ,
238+ ) ;
239+
240+ const stream = streamProxy ( model , context , {
241+ authToken : "token" ,
242+ proxyUrl : "https://proxy.example" ,
243+ } ) ;
244+ const result = await stream . result ( ) ;
245+
246+ expect ( result . stopReason ) . toBe ( "stop" ) ;
247+ } ) ;
219248} ) ;
Original file line number Diff line number Diff line change @@ -229,13 +229,13 @@ export function streamProxy(
229229 }
230230
231231 buffer += decoder . decode ( value , { stream : true } ) ;
232+ const lines = buffer . split ( "\n" ) ;
233+ buffer = lines . pop ( ) || "" ;
232234 if ( buffer . length > PROXY_SSE_BUFFER_MAX_BYTES ) {
233235 throw new Error (
234236 `Proxy SSE response exceeded max buffer size (${ PROXY_SSE_BUFFER_MAX_BYTES } bytes) without line boundary` ,
235237 ) ;
236238 }
237- const lines = buffer . split ( "\n" ) ;
238- buffer = lines . pop ( ) || "" ;
239239
240240 for ( const line of lines ) {
241241 processSseLine ( line ) ;
You can’t perform that action at this time.
0 commit comments