@@ -58,6 +58,7 @@ export function parseMultipart({
5858 const data = responseText ;
5959 const boundaryStr = `--${ boundary } ${ CRLF } ` ;
6060 let pos = lastProcessedLength ;
61+ let lastProcessedPos = pos ;
6162 const chunks : StreamingChunk [ ] = [ ] ;
6263
6364 while ( pos < data . length ) {
@@ -87,22 +88,27 @@ export function parseMultipart({
8788 }
8889
8990 // Extract content
90- const content = data . slice ( contentStart , contentEnd ) ;
91+ let content = data . slice ( contentStart , contentEnd ) ;
9192
9293 // Try to parse JSON content
9394 try {
9495 const parsedChunk = JSON . parse ( content ) as StreamingChunk ;
9596 chunks . push ( parsedChunk ) ;
9697 } catch {
98+ // try parse last chunk (has error with --boundary-- in content length)
99+ content = data . slice ( contentStart , contentEnd - '--boundary--' . length ) ;
100+ const parsedChunk = JSON . parse ( content ) as StreamingChunk ;
101+ chunks . push ( parsedChunk ) ;
97102 // Invalid JSON, skip this chunk
98103 }
99104
100105 // Move position to end of content
101106 pos = contentEnd ;
107+ lastProcessedPos = pos ;
102108 }
103109
104110 return {
105111 chunks,
106- lastProcessedLength : pos ,
112+ lastProcessedLength : lastProcessedPos ,
107113 } ;
108114}
0 commit comments