Skip to content

fix(large-response-middleware): use Buffer.byteLength for accurate UTF-8 size measurement#7

Merged
paulofph merged 1 commit into
mainfrom
fix/large-response-buffer-byte-length
Mar 27, 2026
Merged

fix(large-response-middleware): use Buffer.byteLength for accurate UTF-8 size measurement#7
paulofph merged 1 commit into
mainfrom
fix/large-response-buffer-byte-length

Conversation

@paulofph
Copy link
Copy Markdown
Member

Summary

  • Replace payload.length * 1.0 with Buffer.byteLength(payload, 'utf8') for accurate byte counting
  • Add node to tsconfig.json types so Buffer resolves without a TS error

Problem

String.prototype.length counts JavaScript UTF-16 code units, not bytes. For responses containing multi-byte UTF-8 characters the middleware was significantly underestimating the actual payload size, causing it to miss the error threshold and pass an oversized response to the Lambda runtime — which then hard-rejected it with:

LAMBDA_RUNTIME Failed to post handler success response. Http response code: 413.
{"errorMessage":"Exceeded maximum allowed payload size (6291556 bytes)."}

This meant the client received a 413 even when it had correctly sent Accept: application/large-response.vnd+json.

Test plan

  • All 8 existing tests pass (npm test)
  • tsc --noEmit clean

🤖 Generated with Claude Sonnet 4.6

…F-8 size measurement

payload.length counts JavaScript string characters, which undercounts
multi-byte UTF-8 content. This caused the middleware to underestimate
response sizes and miss the error threshold, allowing oversized payloads
through to the Lambda runtime which then rejected them with a 413.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Member

@alexmarqs alexmarqs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm! thanks 🤝

@paulofph paulofph merged commit 16ce1e0 into main Mar 27, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants