Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(adapter/aws-lambda): API GW Proxy Result should contain either of headers or multiValueHeaders, not both, to avoid confusing header merge mechanism * fix(adapter/aws-lambda): Infer response type from event

authored by TATSUNO “Taz” Yasuhiro · committed by GitHub · parent 035c2d7 · Browse files · 2 files+63 added−42 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+63 added−42 removed

  • tests:+21 added−6 removed
  • other:+42 added−36 removed

2 files+63 added−42 removed

modified:runtime-tests/lambda/index.test.ts+21 added-6 removed
247 unmodified lines
248 const response = await handler(event)249 expect(response.statusCode).toBe(200)250 expect(response.body).toBe('Hello Lambda!')251 expect(response.headers['content-type']).toMatch(/^text\/plain/)252 expect(response.multiValueHeaders).toBeUndefined()253 expect(response.isBase64Encoded).toBe(false)254 })255256 it('Should handle a GET request and return a 200 response with binary', async () => {
11 unmodified lines
268 const response = await handler(event)269 expect(response.statusCode).toBe(200)270 expect(response.body).toBe('RmFrZSBJbWFnZQ==')271 expect(response.headers['content-type']).toMatch(/^image\/png/)272 expect(response.multiValueHeaders).toBeUndefined()273 expect(response.isBase64Encoded).toBe(true)274 })275276 it('Should handle a GET request and return a 200 response (LambdaFunctionUrlEvent)', async () => {
13 unmodified lines
290 const response = await handler(event)291 expect(response.statusCode).toBe(200)292 expect(response.body).toBe('Hello Lambda!')293 expect(response.headers['content-type']).toMatch(/^text\/plain/)294 expect(response.multiValueHeaders).toBeUndefined()295 expect(response.isBase64Encoded).toBe(false)296 })297298 it('Should handle a GET request and return a 200 response (ALBEvent)', async () => {
12 unmodified lines
311 const response = await handler(event)312 expect(response.statusCode).toBe(200)313 expect(response.body).toBe('Hello Lambda!')314 expect(response.headers['content-type']).toMatch(/^text\/plain/)315 expect(response.multiValueHeaders).toBeUndefined()316 expect(response.isBase64Encoded).toBe(false)317 })318319 it('Should handle a GET request and return a 404 response', async () => {
223 unmodified lines
543 expect.objectContaining({544 'content-type': 'application/json',545 })546 )547 expect(albResponse.multiValueHeaders).toBeUndefined()548 })549550 it('Should extract single-value headers and return 200 (APIGatewayProxyEvent)', async () => {551 const apigatewayProxyEvent = {
139 unmodified lines
691692 expect(albResponse.statusCode).toBe(200)693 expect(albResponse.body).toBe('Valid Cookies')694 expect(albResponse.headers['content-type']).toMatch(/^text\/plain/)695 expect(albResponse.multiValueHeaders).toBeUndefined()696 expect(albResponse.isBase64Encoded).toBe(false)697 })698699 it('Should handle a GET request and return a 200 response if cookies match (ALBProxyEvent) with multi value headers', async () => {
14 unmodified lines
714 const albResponse = await handler(albEventMultiValueHeaders)715716 expect(albResponse.statusCode).toBe(200)717 expect(albResponse.body).toBe('Valid Cookies')712 expect(albResponse.headers['content-type']).toMatch(/^text\/plain/)718 expect(albResponse.headers).toBeUndefined()719 expect(albResponse.multiValueHeaders['content-type']).toEqual([720 expect.stringMatching(/^text\/plain/),721 ])722 expect(albResponse.isBase64Encoded).toBe(false)723 })724725 it('Should handle a POST request and return a 200 response with cookies (ALBProxyEvent) with default headers', async () => {
41 unmodified lines
767 const albResponse = await handler(albEventDefaultHeaders)768769 expect(albResponse.statusCode).toBe(200)770 expect(albResponse.body).toBe('Cookies Set')762 expect(albResponse.headers['content-type']).toMatch(/^text\/plain/)763 expect(albResponse.multiValueHeaders).toBeDefined()764 expect(albResponse.multiValueHeaders && albResponse.multiValueHeaders['set-cookie']).toEqual(771 expect(albResponse.headers).toBeUndefined()772 expect(albResponse.multiValueHeaders['set-cookie']).toEqual(773 expect.arrayContaining([testCookie1.serialized, testCookie2.serialized])774 )775 expect(albResponse.isBase64Encoded).toBe(false)776 })
24 unmodified lines
801 key2: 'value2',802 })803 )804 expect(albResponse.headers['content-type']).toMatch(/^application\/json/)805 expect(albResponse.multiValueHeaders).toBeUndefined()806 expect(albResponse.isBase64Encoded).toBe(false)807 })808809 it('Should handle a GET request and return a 200 response with single value multiQueryStringParameters (ALBProxyEvent)', async () => {
21 unmodified lines
831 key1: 'value1',832 key2: 'value2',833 })834 )826 expect(albResponse.headers['content-type']).toMatch(/^application\/json/)835 expect(albResponse.headers).toBeUndefined()836 expect(albResponse.multiValueHeaders['content-type']).toEqual([837 expect.stringMatching(/^application\/json/),838 ])839 expect(albResponse.isBase64Encoded).toBe(false)840 })841842 it('Should handle a GET request and return a 200 response with multi value multiQueryStringParameters (ALBProxyEvent)', async () => {
21 unmodified lines
864 key1: ['value1'],865 key2: ['value2', 'otherValue2'],866 })867 )856 expect(albResponse.headers['content-type']).toMatch(/^application\/json/)868 expect(albResponse.headers).toBeUndefined()869 expect(albResponse.multiValueHeaders['content-type']).toEqual([870 expect.stringMatching(/^application\/json/),871 ])872 expect(albResponse.isBase64Encoded).toBe(false)873 })874})875
170 unmodified lines
modified:src/adapter/aws-lambda/handler.ts+42 added-36 removed
68 unmodified lines
69 }70 requestContext: ALBRequestContext71}7273export interface APIGatewayProxyResult {73type WithHeaders = {74 headers: Record<string, string>75 multiValueHeaders?: undefined76}77type WithMultiValueHeaders = {78 headers?: undefined79 multiValueHeaders: Record<string, string[]>80}8182export type APIGatewayProxyResult = {83 statusCode: number84 statusDescription?: string85 body: string77 headers: Record<string, string>86 cookies?: string[]79 multiValueHeaders?: {80 [headerKey: string]: string[]81 }87 isBase64Encoded: boolean83}88} & (WithHeaders | WithMultiValueHeaders)8990const getRequestContext = (91 event: LambdaEvent92): ApiGatewayRequestContext | ApiGatewayRequestContextV2 | ALBRequestContext => {
73 unmodified lines
166 * Accepts events from API Gateway/ELB(`APIGatewayProxyEvent`) and directly through Function Url(`APIGatewayProxyEventV2`)167 */168export const handle = <E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'>(169 app: Hono<E, S, BasePath>165): ((event: LambdaEvent, lambdaContext?: LambdaContext) => Promise<APIGatewayProxyResult>) => {170): (<L extends LambdaEvent>(171 event: L,172 lambdaContext?: LambdaContext173) => Promise<174 APIGatewayProxyResult &175 (L extends { multiValueHeaders: Record<string, string[]> }176 ? WithMultiValueHeaders177 : WithHeaders)178>) => {179 // @ts-expect-error FIXME: Fix return typing180 return async (event, lambdaContext?) => {181 const processor = getProcessor(event)182183 const req = processor.createRequest(event)
19 unmodified lines
203 protected abstract getHeaders(event: E): Headers204205 protected abstract getCookies(event: E, headers: Headers): void206193 protected abstract setCookiesToResult(194 event: E,195 result: APIGatewayProxyResult,196 cookies: string[]197 ): void207 protected abstract setCookiesToResult(result: APIGatewayProxyResult, cookies: string[]): void208209 createRequest(event: E): Request {210 const queryString = this.getQueryString(event)211 const domainName =
31 unmodified lines
243 const body = isBase64Encoded ? encodeBase64(await res.arrayBuffer()) : await res.text()244245 const result: APIGatewayProxyResult = {246 body: body,237 headers: {},238 multiValueHeaders: event.multiValueHeaders ? {} : undefined,247 statusCode: res.status,248 isBase64Encoded,249 ...(event.multiValueHeaders250 ? {251 multiValueHeaders: {},252 }253 : {254 headers: {},255 }),256 }257258 this.setCookies(event, res, result)244 res.headers.forEach((value, key) => {245 result.headers[key] = value246 if (event.multiValueHeaders && result.multiValueHeaders) {259 if (result.multiValueHeaders) {260 res.headers.forEach((value, key) => {261 result.multiValueHeaders[key] = [value]248 }249 })262 })263 } else {264 res.headers.forEach((value, key) => {265 result.headers[key] = value266 })267 }268269 return result270 }271
5 unmodified lines
277 .filter(([k]) => k === 'set-cookie')278 .map(([, v]) => v)279280 if (Array.isArray(cookies)) {263 this.setCookiesToResult(event, result, cookies)281 this.setCookiesToResult(result, cookies)282 res.headers.delete('set-cookie')283 }284 }285 }
17 unmodified lines
303 headers.set('Cookie', event.cookies.join('; '))304 }305 }306289 protected setCookiesToResult(290 _: APIGatewayProxyEventV2,291 result: APIGatewayProxyResult,292 cookies: string[]293 ): void {307 protected setCookiesToResult(result: APIGatewayProxyResult, cookies: string[]): void {308 result.cookies = cookies309 }310311 protected getHeaders(event: APIGatewayProxyEventV2): Headers {
66 unmodified lines
378 }379 return headers380 }381368 protected setCookiesToResult(369 _: APIGatewayProxyEvent,370 result: APIGatewayProxyResult,371 cookies: string[]372 ): void {382 protected setCookiesToResult(result: APIGatewayProxyResult, cookies: string[]): void {383 result.multiValueHeaders = {384 'set-cookie': cookies,385 }386 }
68 unmodified lines
455 headers.append('Cookie', cookie)456 }457 }458449 protected setCookiesToResult(450 event: ALBProxyEvent,451 result: APIGatewayProxyResult,452 cookies: string[]453 ): void {459 protected setCookiesToResult(result: APIGatewayProxyResult, cookies: string[]): void {460 // when multi value headers is enabled455 if (event.multiValueHeaders && result.multiValueHeaders) {461 if (result.multiValueHeaders) {462 result.multiValueHeaders['set-cookie'] = cookies463 } else {464 // otherwise serialize the set-cookie465 result.headers['set-cookie'] = cookies.join(', ')
39 unmodified lines