Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(adapter/aws-lambda): APIGWProxyResult should contain either of headers or multiValueHeaders, not both #3883

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions runtime-tests/lambda/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ describe('AWS Lambda Adapter for Hono', () => {
expect(response.statusCode).toBe(200)
expect(response.body).toBe('Hello Lambda!')
expect(response.headers['content-type']).toMatch(/^text\/plain/)
expect(response.multiValueHeaders).toBeUndefined()
expect(response.isBase64Encoded).toBe(false)
})

Expand All @@ -268,6 +269,7 @@ describe('AWS Lambda Adapter for Hono', () => {
expect(response.statusCode).toBe(200)
expect(response.body).toBe('RmFrZSBJbWFnZQ==')
expect(response.headers['content-type']).toMatch(/^image\/png/)
expect(response.multiValueHeaders).toBeUndefined()
expect(response.isBase64Encoded).toBe(true)
})

Expand All @@ -289,6 +291,7 @@ describe('AWS Lambda Adapter for Hono', () => {
expect(response.statusCode).toBe(200)
expect(response.body).toBe('Hello Lambda!')
expect(response.headers['content-type']).toMatch(/^text\/plain/)
expect(response.multiValueHeaders).toBeUndefined()
expect(response.isBase64Encoded).toBe(false)
})

Expand All @@ -309,6 +312,7 @@ describe('AWS Lambda Adapter for Hono', () => {
expect(response.statusCode).toBe(200)
expect(response.body).toBe('Hello Lambda!')
expect(response.headers['content-type']).toMatch(/^text\/plain/)
expect(response.multiValueHeaders).toBeUndefined()
expect(response.isBase64Encoded).toBe(false)
})

Expand Down Expand Up @@ -540,6 +544,7 @@ describe('AWS Lambda Adapter for Hono', () => {
'content-type': 'application/json',
})
)
expect(albResponse.multiValueHeaders).toBeUndefined()
})

it('Should extract single-value headers and return 200 (APIGatewayProxyEvent)', async () => {
Expand Down Expand Up @@ -687,6 +692,7 @@ describe('AWS Lambda Adapter for Hono', () => {
expect(albResponse.statusCode).toBe(200)
expect(albResponse.body).toBe('Valid Cookies')
expect(albResponse.headers['content-type']).toMatch(/^text\/plain/)
expect(albResponse.multiValueHeaders).toBeUndefined()
expect(albResponse.isBase64Encoded).toBe(false)
})

Expand All @@ -709,7 +715,10 @@ describe('AWS Lambda Adapter for Hono', () => {

expect(albResponse.statusCode).toBe(200)
expect(albResponse.body).toBe('Valid Cookies')
expect(albResponse.headers['content-type']).toMatch(/^text\/plain/)
expect(albResponse.headers).toBeUndefined()
expect(albResponse.multiValueHeaders['content-type']).toEqual([
expect.stringMatching(/^text\/plain/),
])
expect(albResponse.isBase64Encoded).toBe(false)
})

Expand Down Expand Up @@ -759,9 +768,8 @@ describe('AWS Lambda Adapter for Hono', () => {

expect(albResponse.statusCode).toBe(200)
expect(albResponse.body).toBe('Cookies Set')
expect(albResponse.headers['content-type']).toMatch(/^text\/plain/)
expect(albResponse.multiValueHeaders).toBeDefined()
expect(albResponse.multiValueHeaders && albResponse.multiValueHeaders['set-cookie']).toEqual(
expect(albResponse.headers).toBeUndefined()
expect(albResponse.multiValueHeaders['set-cookie']).toEqual(
expect.arrayContaining([testCookie1.serialized, testCookie2.serialized])
)
expect(albResponse.isBase64Encoded).toBe(false)
Expand Down Expand Up @@ -794,6 +802,7 @@ describe('AWS Lambda Adapter for Hono', () => {
})
)
expect(albResponse.headers['content-type']).toMatch(/^application\/json/)
expect(albResponse.multiValueHeaders).toBeUndefined()
expect(albResponse.isBase64Encoded).toBe(false)
})

Expand Down Expand Up @@ -823,7 +832,10 @@ describe('AWS Lambda Adapter for Hono', () => {
key2: 'value2',
})
)
expect(albResponse.headers['content-type']).toMatch(/^application\/json/)
expect(albResponse.headers).toBeUndefined()
expect(albResponse.multiValueHeaders['content-type']).toEqual([
expect.stringMatching(/^application\/json/),
])
expect(albResponse.isBase64Encoded).toBe(false)
})

Expand Down Expand Up @@ -853,7 +865,10 @@ describe('AWS Lambda Adapter for Hono', () => {
key2: ['value2', 'otherValue2'],
})
)
expect(albResponse.headers['content-type']).toMatch(/^application\/json/)
expect(albResponse.headers).toBeUndefined()
expect(albResponse.multiValueHeaders['content-type']).toEqual([
expect.stringMatching(/^application\/json/),
])
expect(albResponse.isBase64Encoded).toBe(false)
})
})
Expand Down
78 changes: 42 additions & 36 deletions src/adapter/aws-lambda/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,22 @@
requestContext: ALBRequestContext
}

export interface APIGatewayProxyResult {
type WithHeaders = {
headers: Record<string, string>
multiValueHeaders?: undefined
}
type WithMultiValueHeaders = {
headers?: undefined
multiValueHeaders: Record<string, string[]>
}

export type APIGatewayProxyResult = {
statusCode: number
statusDescription?: string
body: string
headers: Record<string, string>
cookies?: string[]
multiValueHeaders?: {
[headerKey: string]: string[]
}
isBase64Encoded: boolean
}
} & (WithHeaders | WithMultiValueHeaders)

const getRequestContext = (
event: LambdaEvent
Expand Down Expand Up @@ -167,7 +172,16 @@
*/
export const handle = <E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'>(
app: Hono<E, S, BasePath>
): ((event: LambdaEvent, lambdaContext?: LambdaContext) => Promise<APIGatewayProxyResult>) => {
): (<L extends LambdaEvent>(
event: L,
lambdaContext?: LambdaContext
) => Promise<
APIGatewayProxyResult &
(L extends { multiValueHeaders: Record<string, string[]> }
? WithMultiValueHeaders
: WithHeaders)
>) => {
// @ts-expect-error FIXME: Fix return typing
return async (event, lambdaContext?) => {
const processor = getProcessor(event)

Expand Down Expand Up @@ -195,11 +209,7 @@

protected abstract getCookies(event: E, headers: Headers): void

protected abstract setCookiesToResult(
event: E,
result: APIGatewayProxyResult,
cookies: string[]
): void
protected abstract setCookiesToResult(result: APIGatewayProxyResult, cookies: string[]): void

createRequest(event: E): Request {
const queryString = this.getQueryString(event)
Expand Down Expand Up @@ -239,24 +249,32 @@

const result: APIGatewayProxyResult = {
body: body,
headers: {},
multiValueHeaders: event.multiValueHeaders ? {} : undefined,
statusCode: res.status,
isBase64Encoded,
...(event.multiValueHeaders
? {
multiValueHeaders: {},
}
: {
headers: {},
}),
}

this.setCookies(event, res, result)
res.headers.forEach((value, key) => {
result.headers[key] = value
if (event.multiValueHeaders && result.multiValueHeaders) {
if (result.multiValueHeaders) {
res.headers.forEach((value, key) => {
result.multiValueHeaders[key] = [value]
}
})
})
} else {
res.headers.forEach((value, key) => {
result.headers[key] = value
})
}

return result
}

setCookies(event: E, res: Response, result: APIGatewayProxyResult) {

Check failure on line 277 in src/adapter/aws-lambda/handler.ts

View workflow job for this annotation

GitHub Actions / Main

'event' is declared but its value is never read.

Check failure on line 277 in src/adapter/aws-lambda/handler.ts

View workflow job for this annotation

GitHub Actions / Node.js v18.18.2

'event' is declared but its value is never read.

Check failure on line 277 in src/adapter/aws-lambda/handler.ts

View workflow job for this annotation

GitHub Actions / Node.js v20.x

'event' is declared but its value is never read.

Check failure on line 277 in src/adapter/aws-lambda/handler.ts

View workflow job for this annotation

GitHub Actions / workerd

'event' is declared but its value is never read.

Check failure on line 277 in src/adapter/aws-lambda/handler.ts

View workflow job for this annotation

GitHub Actions / Node.js v22.x

'event' is declared but its value is never read.
if (res.headers.has('set-cookie')) {
const cookies = res.headers.getSetCookie
? res.headers.getSetCookie()
Expand All @@ -265,7 +283,7 @@
.map(([, v]) => v)

if (Array.isArray(cookies)) {
this.setCookiesToResult(event, result, cookies)
this.setCookiesToResult(result, cookies)
res.headers.delete('set-cookie')
}
}
Expand All @@ -291,11 +309,7 @@
}
}

protected setCookiesToResult(
_: APIGatewayProxyEventV2,
result: APIGatewayProxyResult,
cookies: string[]
): void {
protected setCookiesToResult(result: APIGatewayProxyResult, cookies: string[]): void {
result.cookies = cookies
}

Expand Down Expand Up @@ -341,9 +355,9 @@

protected getCookies(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
event: Exclude<LambdaEvent, APIGatewayProxyEventV2>,

Check failure on line 358 in src/adapter/aws-lambda/handler.ts

View workflow job for this annotation

GitHub Actions / Main

'event' is declared but its value is never read.

Check failure on line 358 in src/adapter/aws-lambda/handler.ts

View workflow job for this annotation

GitHub Actions / Node.js v18.18.2

'event' is declared but its value is never read.

Check failure on line 358 in src/adapter/aws-lambda/handler.ts

View workflow job for this annotation

GitHub Actions / Node.js v20.x

'event' is declared but its value is never read.

Check failure on line 358 in src/adapter/aws-lambda/handler.ts

View workflow job for this annotation

GitHub Actions / workerd

'event' is declared but its value is never read.

Check failure on line 358 in src/adapter/aws-lambda/handler.ts

View workflow job for this annotation

GitHub Actions / Node.js v22.x

'event' is declared but its value is never read.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
headers: Headers

Check failure on line 360 in src/adapter/aws-lambda/handler.ts

View workflow job for this annotation

GitHub Actions / Main

'headers' is declared but its value is never read.

Check failure on line 360 in src/adapter/aws-lambda/handler.ts

View workflow job for this annotation

GitHub Actions / Node.js v18.18.2

'headers' is declared but its value is never read.

Check failure on line 360 in src/adapter/aws-lambda/handler.ts

View workflow job for this annotation

GitHub Actions / Node.js v20.x

'headers' is declared but its value is never read.

Check failure on line 360 in src/adapter/aws-lambda/handler.ts

View workflow job for this annotation

GitHub Actions / workerd

'headers' is declared but its value is never read.

Check failure on line 360 in src/adapter/aws-lambda/handler.ts

View workflow job for this annotation

GitHub Actions / Node.js v22.x

'headers' is declared but its value is never read.
): void {
// nop
}
Expand All @@ -370,11 +384,7 @@
return headers
}

protected setCookiesToResult(
_: APIGatewayProxyEvent,
result: APIGatewayProxyResult,
cookies: string[]
): void {
protected setCookiesToResult(result: APIGatewayProxyResult, cookies: string[]): void {
result.multiValueHeaders = {
'set-cookie': cookies,
}
Expand Down Expand Up @@ -451,13 +461,9 @@
}
}

protected setCookiesToResult(
event: ALBProxyEvent,
result: APIGatewayProxyResult,
cookies: string[]
): void {
protected setCookiesToResult(result: APIGatewayProxyResult, cookies: string[]): void {
// when multi value headers is enabled
if (event.multiValueHeaders && result.multiValueHeaders) {
if (result.multiValueHeaders) {
result.multiValueHeaders['set-cookie'] = cookies
} else {
// otherwise serialize the set-cookie
Expand Down
Loading