fix(lambda-edge): base64 encode content-encoded response bodies (#5099)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent ba89e97 · Browse files · 2 files·+69 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+69 added−2 removed
- tests:+65 added−1 removed
- other:+4 added−1 removed
Provenance
- Commit
6217f4b- Tree
e583cd3- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
ba89e97
2 files changed+69 added−2 removed
modified:src/adapter/lambda-edge/handler.test.ts+65 added-1 removed
1 import { describe } from 'vitest'2 import { setCookie } from '../../helper/cookie'3 import { Hono } from '../../hono'4 import { bodyLimit } from '../../middleware/body-limit'5-import { encodeBase64 } from '../../utils/encode'5+import { decodeBase64, encodeBase64 } from '../../utils/encode'6 import type { Callback, CloudFrontEdgeEvent, CloudFrontRequest } from './handler'7 import { createBody, handle, isContentTypeBinary } from './handler'8 9 describe('isContentTypeBinary', () => {289 unmodified lines299 300 const res = await handler(cloudFrontEdgeEvent)301 expect(res).toMatchObject({ status: '200', body: 'normal' })302 })303+304+ it('Should base64 encode a compressed response with a textual content-type', async () => {305+ const payload = new TextEncoder().encode('a'.repeat(100))306+ const gzipped = await new Response(307+ new Blob([payload]).stream().pipeThrough(new CompressionStream('gzip'))308+ ).arrayBuffer()309+310+ const app = new Hono()311+ app.get('/test-path', () => {312+ return new Response(gzipped, {313+ headers: {314+ 'content-type': 'text/html; charset=UTF-8',315+ 'content-encoding': 'gzip',316+ },317+ })318+ })319+ const handler = handle(app)320+321+ const body = encodeBase64(gzipped)322+ const res = await handler(cloudFrontEdgeEvent)323+324+ expect(res).toMatchObject({ bodyEncoding: 'base64', body })325+326+ const decompressed = await new Response(327+ new Blob([decodeBase64(body)]).stream().pipeThrough(new DecompressionStream('gzip'))328+ ).text()329+ expect(decompressed).toBe('a'.repeat(100))330+ })331+332+ it('Should base64 encode a response with an unknown content-encoding', async () => {333+ const app = new Hono()334+ app.get('/test-path', (c) => {335+ return c.text('zstd bytes', {336+ headers: {337+ 'content-encoding': 'zstd',338+ },339+ })340+ })341+ const handler = handle(app)342+343+ const res = await handler(cloudFrontEdgeEvent)344+345+ expect(res).toMatchObject({346+ bodyEncoding: 'base64',347+ body: encodeBase64(new TextEncoder().encode('zstd bytes').buffer),348+ })349+ })350+351+ it('Should not base64 encode a response with the identity content-encoding', async () => {352+ const app = new Hono()353+ app.get('/test-path', (c) => {354+ return c.text('plain', {355+ headers: {356+ 'content-encoding': 'identity',357+ },358+ })359+ })360+ const handler = handle(app)361+362+ const res = await handler(cloudFrontEdgeEvent)363+364+ expect(res).toMatchObject({ body: 'plain' })365+ expect(res).not.toHaveProperty('bodyEncoding')366+ })367 })modified:src/adapter/lambda-edge/handler.ts+4 added-1 removed
145 unmodified lines146 }147 }148 149 const createResult = async (res: Response): Promise<CloudFrontResult> => {150- const isBase64Encoded = isContentTypeBinary(res.headers.get('content-type') || '')150+ const contentEncoding = res.headers.get('content-encoding')151+ const isBase64Encoded =152+ isContentTypeBinary(res.headers.get('content-type') || '') ||153+ (!!contentEncoding && !/^identity$/i.test(contentEncoding))154 const body = isBase64Encoded ? encodeBase64(await res.arrayBuffer()) : await res.text()155 156 return {157 status: res.status.toString(),61 unmodified lines