Skip to content

fix(lambda-edge): base64 encode compressed response bodies#5003

Open
LeSingh1 wants to merge 1 commit into
honojs:mainfrom
LeSingh1:fix/lambda-edge-binary-compressed-body
Open

fix(lambda-edge): base64 encode compressed response bodies#5003
LeSingh1 wants to merge 1 commit into
honojs:mainfrom
LeSingh1:fix/lambda-edge-binary-compressed-body

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Fixes #4254

When the lambda-edge adapter builds a CloudFront result, it decides whether to base64 encode the body using only the content-type. A compressed response can keep a textual content-type such as text/html while the actual body is gzip bytes. The compress middleware does exactly this. In that case the adapter calls res.text() on binary data, which corrupts the body, and CloudFront returns an empty or broken response.

This checks the content-encoding header as a fallback when the content-type is not detected as binary. If the response is gzip, deflate, compress, or br encoded, the body is base64 encoded with bodyEncoding set to base64. The aws-lambda adapter already does the same thing in its createResult, so this brings lambda-edge in line with it.

I added a small isContentEncodingBinary helper that mirrors the one in the aws-lambda adapter, with unit tests, plus a handle test that runs a gzip-encoded text/html response through the adapter and verifies the body comes back base64 encoded and decompresses to the original payload.

Note: a previous attempt at this issue (#4874) was closed for including unrelated changes. This PR only touches the lambda-edge handler and its test.

The lambda-edge adapter decided whether to base64 encode the response
body solely from the content-type. A compressed response, such as one
produced by the compress middleware, can carry a textual content-type
like text/html while the body is gzip bytes. In that case res.text()
was called on binary data and the body was corrupted, so CloudFront
returned an empty or broken response.

Check the content-encoding header as a fallback when the content-type
is not binary, matching the behavior already used by the aws-lambda
adapter.

Fixes honojs#4254
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.

hono/lambda-edge adapter should treat compressed response body as binary

1 participant