11/* eslint-disable camelcase */
22import type { StreamingChunk } from '../../types/store/streaming' ;
33
4- // interface Headers {
5- // contentType?: string;
6- // contentLength?: number;
7- // }
8-
9- interface MultipartState {
10- lastProcessedLength : number ;
11- }
12-
134interface MultipartResult {
145 chunks : StreamingChunk [ ] ;
15- state : MultipartState ;
6+ lastProcessedLength : number ;
167}
178
18- // function parseHeaders(headerLines: string[]): Headers {
19- // const headers: Headers = {};
20- // for (const line of headerLines) {
21- // const [key, value] = line.split(': ');
22- // switch (key.toLowerCase()) {
23- // case 'content-type':
24- // headers.contentType = value;
25- // break;
26- // case 'content-length':
27- // headers.contentLength = Number(value);
28- // break;
29- // }
30- // }
31- // return headers;
32- // }
33-
349const CRLF = '\r\n' ;
3510
3611export function parseMultipart ( {
3712 responseText,
38- state = { lastProcessedLength : 0 } ,
13+ lastProcessedLength,
3914 boundary = 'boundary' ,
4015} : {
4116 responseText : string ;
42- state ?: MultipartState ;
17+ lastProcessedLength : number ;
4318 boundary ?: string ;
4419} ) : MultipartResult {
45- // Combine buffer with new data
46- const newData = responseText . slice ( state . lastProcessedLength ) ;
20+ const newData = responseText . slice ( lastProcessedLength ) ;
4721
4822 if ( ! newData ) {
49- return { chunks : [ ] , state } ;
23+ return { chunks : [ ] , lastProcessedLength } ;
5024 }
5125
5226 // Split on boundary with double dashes and CRLF
5327 const boundaryStr = `--${ boundary } ${ CRLF } ` ;
5428 const parts = newData . split ( boundaryStr ) ;
5529
56- let currentPosition = state . lastProcessedLength ;
30+ let currentPosition = lastProcessedLength ;
5731 const chunks : StreamingChunk [ ] = [ ] ;
5832
5933 for ( let i = 0 ; i < parts . length ; i ++ ) {
6034 const part = parts [ i ] ;
6135 const isLastPart = i === parts . length - 1 ;
6236
63- // Split part into lines by CRLF
6437 const lines = part . split ( CRLF ) ;
6538
66- // Find the empty line that separates headers from content
6739 const emptyLineIndex = lines . findIndex ( ( line ) => line === '' ) ;
6840 if ( emptyLineIndex === - 1 || ! lines [ emptyLineIndex + 1 ] ) {
6941 if ( isLastPart ) {
@@ -72,38 +44,20 @@ export function parseMultipart({
7244 continue ;
7345 }
7446
75- // const headers = parseHeaders(lines.slice(0, emptyLineIndex));
76-
7747 const jsonContent = lines [ emptyLineIndex + 1 ] ;
7848
79- // if (headers.contentLength) {
80- // jsonContent = jsonContent.slice(0, headers.contentLength);
81- // }
82-
83- // if (headers.contentLength && jsonContent.length < headers.contentLength) {
84- // newBuffer = jsonContent;
85- // break;
86- // }
87-
8849 let parsedChunk : StreamingChunk | null = null ;
8950 try {
9051 parsedChunk = JSON . parse ( jsonContent ) as StreamingChunk ;
9152 } catch { }
9253
93- // If it's the last part and not a complete chunk, store in buffer
9454 if ( ! parsedChunk ) {
9555 break ;
9656 }
97- // Track position by adding boundary length and full part length
57+
9858 chunks . push ( parsedChunk ) ;
9959 currentPosition += boundaryStr . length + part . length ;
10060 }
10161
102- // Update state with precise position tracking
103- const newState : MultipartState = {
104- // Only update lastProcessedLength if we actually processed some chunks
105- lastProcessedLength : currentPosition ,
106- } ;
107-
108- return { chunks, state : newState } ;
62+ return { chunks, lastProcessedLength : currentPosition } ;
10963}
0 commit comments