Skip to content

Commit

Permalink
fix(adapter/aws-lambda): use content-encoding to determine isBase64En…
Browse files Browse the repository at this point in the history
…coded (#1295)

* aws-lambda adapter: use content-encoding to determine isBase64Encoded

* when contentType is binary, skip checking contentEncoding

* prettier
  • Loading branch information
if1live authored Aug 5, 2023
1 parent 35246d0 commit a7dd281
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/adapter/aws-lambda/handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isContentTypeBinary } from './handler'
import { isContentTypeBinary, isContentEncodingBinary } from './handler'

describe('isContentTypeBinary', () => {
it('Should determine whether it is binary', () => {
Expand All @@ -13,3 +13,15 @@ describe('isContentTypeBinary', () => {
expect(isContentTypeBinary('application/json; charset=UTF-8')).toBe(false)
})
})

describe('isContentEncodingBinary', () => {
it('Should determine whether it is compressed', () => {
expect(isContentEncodingBinary('gzip')).toBe(true)
expect(isContentEncodingBinary('compress')).toBe(true)
expect(isContentEncodingBinary('deflate')).toBe(true)
expect(isContentEncodingBinary('br')).toBe(true)
expect(isContentEncodingBinary('deflate, gzip')).toBe(true)
expect(isContentEncodingBinary('')).toBe(false)
expect(isContentEncodingBinary('unknown')).toBe(false)
})
})
18 changes: 16 additions & 2 deletions src/adapter/aws-lambda/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ interface APIGatewayProxyResult {
/**
* Accepts events from API Gateway/ELB(`APIGatewayProxyEvent`) and directly through Function Url(`APIGatewayProxyEventV2`)
*/
export const handle = <E extends Env = Env, S = {}, BasePath extends string = '/'>(app: Hono<E, S, BasePath>) => {
export const handle = <E extends Env = Env, S = {}, BasePath extends string = '/'>(
app: Hono<E, S, BasePath>
) => {
return async (
event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent
): Promise<APIGatewayProxyResult> => {
Expand All @@ -73,7 +75,12 @@ export const handle = <E extends Env = Env, S = {}, BasePath extends string = '/

const createResult = async (res: Response): Promise<APIGatewayProxyResult> => {
const contentType = res.headers.get('content-type')
const isBase64Encoded = contentType && isContentTypeBinary(contentType) ? true : false
let isBase64Encoded = contentType && isContentTypeBinary(contentType) ? true : false

if (!isBase64Encoded) {
const contentEncoding = res.headers.get('content-encoding')
isBase64Encoded = isContentEncodingBinary(contentEncoding)
}

let body: string
if (isBase64Encoded) {
Expand Down Expand Up @@ -153,3 +160,10 @@ export const isContentTypeBinary = (contentType: string) => {
contentType
)
}

export const isContentEncodingBinary = (contentEncoding: string | null) => {
if (contentEncoding === null) {
return false
}
return /^(gzip|deflate|compress|br)/.test(contentEncoding)
}

0 comments on commit a7dd281

Please sign in to comment.