From af2aa4597ffe6b3719a17761d454e1840e48549e Mon Sep 17 00:00:00 2001 From: ankit25bcs10610 Date: Fri, 3 Jul 2026 01:58:57 +0530 Subject: [PATCH] fix(streaming): handle boundary-not-found in multipart transformers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `buffer.indexOf(boundary)` returns `-1` when the boundary is not in the current buffer, but `-1` is not nullish, so `boundaryIndex ?? buffer.length` never falls back — `safeLength` becomes `-1`. When a file chunk arrives before its closing boundary (the normal case for large uploads spanning multiple reads), `buffer.slice(0, -1)` drops the last byte from the emitted content and the non-file branch `buffer.slice(-1)` discards almost the entire buffer, corrupting chunked uploads in getFormdataToFormdataStreamTransformer and formDataToOctetStreamTransformer. Fall back to buffer.length only when the boundary is absent (`boundaryIndex === -1`). Co-Authored-By: Claude Opus 4.8 (1M context) --- src/handlers/streamHandlerUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/handlers/streamHandlerUtils.ts b/src/handlers/streamHandlerUtils.ts index 9cd9339d9..f28b4cd87 100644 --- a/src/handlers/streamHandlerUtils.ts +++ b/src/handlers/streamHandlerUtils.ts @@ -173,7 +173,7 @@ export const getFormdataToFormdataStreamTransformer = ( const boundaryIndex = buffer.indexOf(boundary); - const safeLength = boundaryIndex ?? buffer.length; + const safeLength = boundaryIndex === -1 ? buffer.length : boundaryIndex; const content = buffer.slice(0, safeLength); if (isFileContent) { @@ -268,7 +268,7 @@ export const formDataToOctetStreamTransformer = ( const boundaryIndex = buffer.indexOf(boundary); - const safeLength = boundaryIndex ?? buffer.length; + const safeLength = boundaryIndex === -1 ? buffer.length : boundaryIndex; // if (safeLength <= 0) break; const content = buffer.slice(0, safeLength);