Skip to content

Commit ce34649

Browse files
committed
fix: correct response truncation to return first N bytes
1 parent 0883419 commit ce34649

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

src/index.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -354,29 +354,22 @@ See the config resource for all configuration options.
354354
}
355355
};
356356

357-
// Check response size
358-
const stringified = JSON.stringify(responseObj, null, 2);
359-
const totalBytes = Buffer.from(stringified).length;
360-
361-
if (totalBytes > RESPONSE_SIZE_LIMIT) {
362-
// Convert body to string if it isn't already
363-
const bodyStr = typeof response.data === 'string'
364-
? response.data
365-
: JSON.stringify(response.data);
366-
367-
// Calculate how much we need to truncate
368-
const currentSize = Buffer.from(bodyStr).length;
369-
const targetSize = Math.max(0, currentSize - (totalBytes - RESPONSE_SIZE_LIMIT));
370-
371-
// Create truncated response
372-
responseObj.response.body = bodyStr.slice(0, targetSize);
357+
// Check response body size independently
358+
const bodyStr = typeof response.data === 'string'
359+
? response.data
360+
: JSON.stringify(response.data);
361+
const bodySize = Buffer.from(bodyStr).length;
362+
363+
if (bodySize > RESPONSE_SIZE_LIMIT) {
364+
// Simply truncate to the size limit
365+
responseObj.response.body = bodyStr.slice(0, RESPONSE_SIZE_LIMIT);
373366
responseObj.validation.messages.push(
374-
`Response truncated: ${targetSize} of ${currentSize} bytes returned due to size limit (${RESPONSE_SIZE_LIMIT} bytes)`
367+
`Response truncated: ${RESPONSE_SIZE_LIMIT} of ${bodySize} bytes returned due to size limit (${RESPONSE_SIZE_LIMIT} bytes)`
375368
);
376369
responseObj.validation.truncated = {
377-
originalSize: currentSize,
378-
returnedSize: targetSize,
379-
truncationPoint: targetSize,
370+
originalSize: bodySize,
371+
returnedSize: RESPONSE_SIZE_LIMIT,
372+
truncationPoint: RESPONSE_SIZE_LIMIT,
380373
sizeLimit: RESPONSE_SIZE_LIMIT
381374
};
382375
}

0 commit comments

Comments
 (0)