Skip to content

Commit fdfc491

Browse files
committed
method to find boundary and its bounds
1 parent e005989 commit fdfc491

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/Multipart.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,36 @@ export class Multipart implements Part {
279279
return -1;
280280
}
281281

282+
/**
283+
* Find boundary delimiter start and end index
284+
* @param data Multipart body bytes
285+
* @param boundary The multipart boundary bytes
286+
* @param [start] The index to start the search at (i.e. the number of bytes to skip/ignore at the beginning of the byte array). Defaults to 0.
287+
* @internal
288+
*/
289+
private static findBoundaryBounds(data: Uint8Array, boundary: Uint8Array, start = 0): [number, number] | null {
290+
if (start >= data.length) return null;
291+
const boundaryStartIndex = Multipart.findSequenceIndex(data, Multipart.combineArrays([Multipart.CRLF, Multipart.DOUBLE_DASH, boundary]), start);
292+
if (boundaryStartIndex === -1) return null;
293+
// ignore any linear whitespace
294+
let currentEndOfBoundaryIndex = boundaryStartIndex + boundary.length + 4;
295+
while (currentEndOfBoundaryIndex < data.length) {
296+
const byte = data[currentEndOfBoundaryIndex];
297+
if (byte === Multipart.CR) break;
298+
if (byte === Multipart.LF) return null;
299+
if (byte === Multipart.SP || byte === 0x09) {
300+
currentEndOfBoundaryIndex++;
301+
continue;
302+
}
303+
// encountered non-linear whitespace after boundary and before any CR or LF
304+
// meaning the boundary could not be terminated, therefore continue search for boundary
305+
return Multipart.findBoundaryBounds(data, boundary, boundaryStartIndex + 2);
306+
}
307+
if (data[currentEndOfBoundaryIndex + 1] !== Multipart.LF) return null;
308+
309+
return [boundaryStartIndex, currentEndOfBoundaryIndex + 2];
310+
}
311+
282312
/**
283313
* Parse header params in the format `key=value;foo = "bar"; baz`
284314
*/

0 commit comments

Comments
 (0)