Skip to content
Myr

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

2 files+69 added−2 removed

modified:src/adapter/lambda-edge/handler.test.ts+65 added-1 removed
1import { describe } from 'vitest'2import { setCookie } from '../../helper/cookie'3import { Hono } from '../../hono'4import { bodyLimit } from '../../middleware/body-limit'5import { encodeBase64 } from '../../utils/encode'5import { decodeBase64, encodeBase64 } from '../../utils/encode'6import type { Callback, CloudFrontEdgeEvent, CloudFrontRequest } from './handler'7import { createBody, handle, isContentTypeBinary } from './handler'89describe('isContentTypeBinary', () => {
289 unmodified lines
299300 const res = await handler(cloudFrontEdgeEvent)301 expect(res).toMatchObject({ status: '200', body: 'normal' })302 })303304 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()309310 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)320321 const body = encodeBase64(gzipped)322 const res = await handler(cloudFrontEdgeEvent)323324 expect(res).toMatchObject({ bodyEncoding: 'base64', body })325326 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 })331332 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)342343 const res = await handler(cloudFrontEdgeEvent)344345 expect(res).toMatchObject({346 bodyEncoding: 'base64',347 body: encodeBase64(new TextEncoder().encode('zstd bytes').buffer),348 })349 })350351 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)361362 const res = await handler(cloudFrontEdgeEvent)363364 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 lines
146 }147}148149const 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()155156 return {157 status: res.status.toString(),
61 unmodified lines