Skip to content

Commit fb1365d

Browse files
committed
fix: better parseMultipart
1 parent f036177 commit fb1365d

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

src/services/parsers/parseMultipart.ts

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable camelcase */
21
import type {StreamingChunk} from '../../types/store/streaming';
32

43
interface MultipartResult {
@@ -9,25 +8,20 @@ interface MultipartResult {
98
const CRLF = '\r\n';
109
const HEADER_VALUE_DELIMITER = ': ';
1110

12-
interface ParsedHeaders {
13-
contentType?: string;
14-
contentLength?: number;
15-
}
16-
17-
function parseHeaders(data: string, startPos: number): [ParsedHeaders, number] {
18-
const headers: ParsedHeaders = {};
11+
function getContentHeadersData(data: string, startPos: number): [number, number] {
12+
let contentLength = 0;
1913
let pos = startPos;
2014

2115
while (pos < data.length) {
2216
// Check for end of headers
2317
if (data.startsWith(CRLF, pos)) {
24-
return [headers, pos + CRLF.length];
18+
return [contentLength, pos + CRLF.length];
2519
}
2620

2721
const nextCRLF = data.indexOf(CRLF, pos);
2822
if (nextCRLF === -1) {
2923
// Headers are incomplete
30-
return [headers, startPos];
24+
return [contentLength, startPos];
3125
}
3226

3327
const line = data.slice(pos, nextCRLF);
@@ -37,12 +31,10 @@ function parseHeaders(data: string, startPos: number): [ParsedHeaders, number] {
3731
const header = line.slice(0, colonIndex).toLowerCase();
3832
const value = line.slice(colonIndex + HEADER_VALUE_DELIMITER.length, nextCRLF);
3933

40-
if (header.toLowerCase() === 'content-type') {
41-
headers.contentType = value;
42-
} else if (header.toLowerCase() === 'content-length') {
34+
if (header.toLowerCase() === 'content-length') {
4335
const length = parseInt(value, 10);
4436
if (!isNaN(length)) {
45-
headers.contentLength = length;
37+
contentLength = length;
4638
}
4739
}
4840
}
@@ -51,7 +43,7 @@ function parseHeaders(data: string, startPos: number): [ParsedHeaders, number] {
5143
}
5244

5345
// Headers are incomplete
54-
return [headers, startPos];
46+
return [contentLength, startPos];
5547
}
5648

5749
export function parseMultipart({
@@ -79,15 +71,15 @@ export function parseMultipart({
7971
pos = boundaryPos + boundaryStr.length;
8072

8173
// Parse headers
82-
const [headers, contentStart] = parseHeaders(data, pos);
83-
if (contentStart === pos || !headers.contentLength) {
74+
const [contentLength, contentStart] = getContentHeadersData(data, pos);
75+
if (contentStart === pos || !contentLength) {
8476
// Headers were incomplete or invalid
8577
pos = lastProcessedLength;
8678
break;
8779
}
8880

8981
// Check if we have enough data for the content
90-
const contentEnd = contentStart + headers.contentLength;
82+
const contentEnd = contentStart + contentLength;
9183
if (contentEnd > data.length) {
9284
// Content is incomplete
9385
pos = lastProcessedLength;
@@ -99,10 +91,8 @@ export function parseMultipart({
9991

10092
// Try to parse JSON content
10193
try {
102-
if (headers.contentType === 'application/json') {
103-
const parsedChunk = JSON.parse(content) as StreamingChunk;
104-
chunks.push(parsedChunk);
105-
}
94+
const parsedChunk = JSON.parse(content) as StreamingChunk;
95+
chunks.push(parsedChunk);
10696
} catch {
10797
// Invalid JSON, skip this chunk
10898
}

0 commit comments

Comments
 (0)