handler.ts
This file
Path
src/adapter/aws-lambda/handler.ts
487 lines · 14.2 KiB · TypeScript
Last changed by
fix(aws-lambda): handle multiple cookies in streaming responses (#2926)Markus Wolfbefore adoptiona2a04a9
History of this fileTake it
The walk stopped after 200 commits: lines older than 31d2ea4 point at it.
Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
line 1why, one step back1import crypto from 'node:crypto'fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
lines 2–3why, one step back2import type { Hono } from '../../hono'3import type { Env, Schema } from '../../types'Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
line 4why, one step back4import { decodeBase64, encodeBase64 } from '../../utils/encode'fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
line 5why, one step back5import type {Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
line 6why, one step back6 ALBRequestContext,fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
lines 7–8why, one step back7 ApiGatewayRequestContext,8 ApiGatewayRequestContextV2,refactor(aws-lambda): merge custom-context into types (#2889)TATSUNO “Taz” Yasuhirobefore adoptionec3648d
lines 9–11why, one step back9 Handler,10 LambdaContext,11} from './types'fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
lines 12–23why, one step back1213// eslint-disable-next-line @typescript-eslint/ban-ts-comment14// @ts-ignore15globalThis.crypto ??= crypto1617export type LambdaEvent = APIGatewayProxyEvent | APIGatewayProxyEventV2 | ALBProxyEvent1819// When calling HTTP API or Lambda directly through function urls20export interface APIGatewayProxyEventV2 {21 version: string22 routeKey: string23 headers: Record<string, string | undefined>fix(adapter): handle multi value headers in AWS Lambda (#2494)TATSUNO “Taz” Yasuhirobefore adoptionacb56b8
line 24why, one step back24 multiValueHeaders?: undefinedfix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
lines 25–65why, one step back25 cookies?: string[]26 rawPath: string27 rawQueryString: string28 body: string | null29 isBase64Encoded: boolean30 requestContext: ApiGatewayRequestContextV231 queryStringParameters?: {32 [name: string]: string | undefined33 }34 pathParameters?: {35 [name: string]: string | undefined36 }37 stageVariables?: {38 [name: string]: string | undefined39 }40}4142// When calling Lambda through an API Gateway43export interface APIGatewayProxyEvent {44 version: string45 httpMethod: string46 headers: Record<string, string | undefined>47 multiValueHeaders?: {48 [headerKey: string]: string[]49 }50 path: string51 body: string | null52 isBase64Encoded: boolean53 queryStringParameters?: Record<string, string | undefined>54 requestContext: ApiGatewayRequestContext55 resource: string56 multiValueQueryStringParameters?: {57 [parameterKey: string]: string[]58 }59 pathParameters?: Record<string, string>60 stageVariables?: Record<string, string>61}6263// When calling Lambda through an Application Load Balancer64export interface ALBProxyEvent {65 httpMethod: stringfix(adapter): handle multi value headers in AWS Lambda (#2494)TATSUNO “Taz” Yasuhirobefore adoptionacb56b8
lines 66–67why, one step back66 headers?: Record<string, string | undefined>67 multiValueHeaders?: Record<string, string[] | undefined>fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
lines 68–71why, one step back68 path: string69 body: string | null70 isBase64Encoded: boolean71 queryStringParameters?: Record<string, string | undefined>feat(aws-lambda): add support for alb multiValueQueryStringParameters (#2751)Ilias YAHIAbefore adoptiona437161
lines 72–74why, one step back72 multiValueQueryStringParameters?: {73 [parameterKey: string]: string[]74 }fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
lines 75–98why, one step back75 requestContext: ALBRequestContext76}7778export interface APIGatewayProxyResult {79 statusCode: number80 statusDescription?: string81 body: string82 headers: Record<string, string>83 cookies?: string[]84 multiValueHeaders?: {85 [headerKey: string]: string[]86 }87 isBase64Encoded: boolean88}8990const getRequestContext = (91 event: LambdaEvent92): ApiGatewayRequestContext | ApiGatewayRequestContextV2 | ALBRequestContext => {93 return event.requestContext94}9596const streamToNodeStream = async (97 reader: ReadableStreamDefaultReader<Uint8Array>,98 writer: NodeJS.WritableStreamfix: add return types of void function (#2870)Ame_xbefore adoptiona852327
line 99why, one step back99): Promise<void> => {fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
lines 100–113why, one step back100 let readResult = await reader.read()101 while (!readResult.done) {102 writer.write(readResult.value)103 readResult = await reader.read()104 }105 writer.end()106}107108export const streamHandle = <109 E extends Env = Env,110 S extends Schema = {},111 BasePath extends string = '/'112>(113 app: Hono<E, S, BasePath>Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
lines 114–115why, one step back114): Handler => {115 // @ts-expect-error awslambda is not a standard APIfix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
lines 116–117why, one step back116 return awslambda.streamifyResponse(117 async (event: LambdaEvent, responseStream: NodeJS.WritableStream, context: LambdaContext) => {refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 118why, one step back118 const processor = getProcessor(event)fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
line 119why, one step back119 try {refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 120why, one step back120 const req = processor.createRequest(event)fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
lines 121–128why, one step back121 const requestContext = getRequestContext(event)122123 const res = await app.fetch(req, {124 event,125 requestContext,126 context,127 })128fix(aws-lambda): handle multiple cookies in streaming responses (#2926)Markus Wolfbefore adoptiona2a04a9
lines 129–138why, one step back129 const headers: Record<string, string> = {}130 const cookies: string[] = []131 res.headers.forEach((value, name) => {132 if (name === 'set-cookie') {133 cookies.push(value)134 } else {135 headers[name] = value136 }137 })138fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
lines 139–141why, one step back139 // Check content type140 const httpResponseMetadata = {141 statusCode: res.status,fix(aws-lambda): handle multiple cookies in streaming responses (#2926)Markus Wolfbefore adoptiona2a04a9
lines 142–143why, one step back142 headers,143 cookies,fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
lines 144–145why, one step back144 }145fix(aws-lambda): handle response without body (#2401) (#2413)Markus Wolfbefore adoptionf590d0f
line 146why, one step back146 // Update response streamMerge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
line 147why, one step back147 // @ts-expect-error awslambda is not a standard APIfix(aws-lambda): handle response without body (#2401) (#2413)Markus Wolfbefore adoptionf590d0f
lines 148–149why, one step back148 responseStream = awslambda.HttpResponseStream.from(responseStream, httpResponseMetadata)149fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
line 150why, one step back150 if (res.body) {fix(aws-lambda): handle response without body (#2401) (#2413)Markus Wolfbefore adoptionf590d0f
lines 151–153why, one step back151 await streamToNodeStream(res.body.getReader(), responseStream)152 } else {153 responseStream.write('')fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
lines 154–169why, one step back154 }155 } catch (error) {156 console.error('Error processing request:', error)157 responseStream.write('Internal Server Error')158 } finally {159 responseStream.end()160 }161 }162 )163}164165/**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>Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
lines 170–171why, one step back170): ((event: LambdaEvent, lambdaContext?: LambdaContext) => Promise<APIGatewayProxyResult>) => {171 return async (event, lambdaContext?) => {refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 172–174why, one step back172 const processor = getProcessor(event)173174 const req = processor.createRequest(event)fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
lines 175–182why, one step back175 const requestContext = getRequestContext(event)176177 const res = await app.fetch(req, {178 event,179 requestContext,180 lambdaContext,181 })182refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 183why, one step back183 return processor.createResult(event, res)fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
lines 184–186why, one step back184 }185}186refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 187–188why, one step back187abstract class EventProcessor<E extends LambdaEvent> {188 protected abstract getPath(event: E): stringfix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
line 189why, one step back189refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 190why, one step back190 protected abstract getMethod(event: E): stringfix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
line 191why, one step back191refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 192why, one step back192 protected abstract getQueryString(event: E): stringfix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
line 193why, one step back193feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
lines 194–195why, one step back194 protected abstract getHeaders(event: E): Headers195refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 196–197why, one step back196 protected abstract getCookies(event: E, headers: Headers): void197feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
lines 198–202why, one step back198 protected abstract setCookiesToResult(199 event: E,200 result: APIGatewayProxyResult,201 cookies: string[]202 ): voidrefactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 203–213why, one step back203204 createRequest(event: E): Request {205 const queryString = this.getQueryString(event)206 const domainName =207 event.requestContext && 'domainName' in event.requestContext208 ? event.requestContext.domainName209 : event.headers?.['host'] ?? event.multiValueHeaders?.['host']?.[0]210 const path = this.getPath(event)211 const urlPath = `https://${domainName}${path}`212 const url = queryString ? `${urlPath}?${queryString}` : urlPath213feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
line 214why, one step back214 const headers = this.getHeaders(event)refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 215–222why, one step back215216 const method = this.getMethod(event)217 const requestInit: RequestInit = {218 headers,219 method,220 }221222 if (event.body) {Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
line 223why, one step back223 requestInit.body = event.isBase64Encoded ? decodeBase64(event.body) : event.bodyrefactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 224–226why, one step back224 }225226 return new Request(url, requestInit)fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
lines 227–228why, one step back227 }228refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 229–231why, one step back229 async createResult(event: E, res: Response): Promise<APIGatewayProxyResult> {230 const contentType = res.headers.get('content-type')231 let isBase64Encoded = contentType && isContentTypeBinary(contentType) ? true : falsefix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
line 232why, one step back232refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 233–236why, one step back233 if (!isBase64Encoded) {234 const contentEncoding = res.headers.get('content-encoding')235 isBase64Encoded = isContentEncodingBinary(contentEncoding)236 }fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
line 237why, one step back237refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 238why, one step back238 const body = isBase64Encoded ? encodeBase64(await res.arrayBuffer()) : await res.text()fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
line 239why, one step back239refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 240–242why, one step back240 const result: APIGatewayProxyResult = {241 body: body,242 headers: {},feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
line 243why, one step back243 multiValueHeaders: event.multiValueHeaders ? {} : undefined,refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 244–245why, one step back244 statusCode: res.status,245 isBase64Encoded,fix(adapter): handle multi value headers in AWS Lambda (#2494)TATSUNO “Taz” Yasuhirobefore adoptionacb56b8
line 246why, one step back246 }refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 247–250why, one step back247248 this.setCookies(event, res, result)249 res.headers.forEach((value, key) => {250 result.headers[key] = valuefeat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
lines 251–253why, one step back251 if (event.multiValueHeaders && result.multiValueHeaders) {252 result.multiValueHeaders[key] = [value]253 }refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 254–256why, one step back254 })255256 return resultfix(adapter): handle multi value headers in AWS Lambda (#2494)TATSUNO “Taz” Yasuhirobefore adoptionacb56b8
line 257why, one step back257 }refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 258why, one step back258feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
line 259why, one step back259 setCookies(event: E, res: Response, result: APIGatewayProxyResult) {refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 260–262why, one step back260 if (res.headers.has('set-cookie')) {261 const cookies = res.headers.get('set-cookie')?.split(', ')262 if (Array.isArray(cookies)) {feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
line 263why, one step back263 this.setCookiesToResult(event, result, cookies)refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 264why, one step back264 res.headers.delete('set-cookie')fix(adapter): handle multi value headers in AWS Lambda (#2494)TATSUNO “Taz” Yasuhirobefore adoptionacb56b8
line 265why, one step back265 }fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
lines 266–267why, one step back266 }267 }refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 268why, one step back268}fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
line 269why, one step back269Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
line 270why, one step back270class EventV2Processor extends EventProcessor<APIGatewayProxyEventV2> {refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 271–272why, one step back271 protected getPath(event: APIGatewayProxyEventV2): string {272 return event.rawPathfix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
lines 273–274why, one step back273 }274refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 275–276why, one step back275 protected getMethod(event: APIGatewayProxyEventV2): string {276 return event.requestContext.http.methodfix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
lines 277–278why, one step back277 }278refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 279–281why, one step back279 protected getQueryString(event: APIGatewayProxyEventV2): string {280 return event.rawQueryString281 }fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
line 282why, one step back282refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 283–287why, one step back283 protected getCookies(event: APIGatewayProxyEventV2, headers: Headers): void {284 if (Array.isArray(event.cookies)) {285 headers.set('Cookie', event.cookies.join('; '))286 }287 }fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
line 288why, one step back288feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
lines 289–293why, one step back289 protected setCookiesToResult(290 _: APIGatewayProxyEventV2,291 result: APIGatewayProxyResult,292 cookies: string[]293 ): void {refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 294why, one step back294 result.cookies = cookiesfix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
line 295why, one step back295 }feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
lines 296–308why, one step back296297 protected getHeaders(event: APIGatewayProxyEventV2): Headers {298 const headers = new Headers()299 this.getCookies(event, headers)300 if (event.headers) {301 for (const [k, v] of Object.entries(event.headers)) {302 if (v) {303 headers.set(k, v)304 }305 }306 }307 return headers308 }Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
line 309why, one step back309}fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
line 310why, one step back310Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
lines 311–313why, one step back311const v2Processor: EventV2Processor = new EventV2Processor()312313class EventV1Processor extends EventProcessor<Exclude<LambdaEvent, APIGatewayProxyEventV2>> {refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 314–337why, one step back314 protected getPath(event: Exclude<LambdaEvent, APIGatewayProxyEventV2>): string {315 return event.path316 }317318 protected getMethod(event: Exclude<LambdaEvent, APIGatewayProxyEventV2>): string {319 return event.httpMethod320 }321322 protected getQueryString(event: Exclude<LambdaEvent, APIGatewayProxyEventV2>): string {323 return Object.entries(event.queryStringParameters || {})324 .filter(([, value]) => value)325 .map(([key, value]) => `${key}=${value}`)326 .join('&')327 }328329 protected getCookies(330 // eslint-disable-next-line @typescript-eslint/no-unused-vars331 event: Exclude<LambdaEvent, APIGatewayProxyEventV2>,332 // eslint-disable-next-line @typescript-eslint/no-unused-vars333 headers: Headers334 ): void {335 // nop336 }337feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
lines 338–364why, one step back338 protected getHeaders(event: APIGatewayProxyEvent): Headers {339 const headers = new Headers()340 this.getCookies(event, headers)341 if (event.headers) {342 for (const [k, v] of Object.entries(event.headers)) {343 if (v) {344 headers.set(k, v)345 }346 }347 }348 if (event.multiValueHeaders) {349 for (const [k, values] of Object.entries(event.multiValueHeaders)) {350 if (values) {351 // avoid duplicating already set headers352 const foundK = headers.get(k)353 values.forEach((v) => (!foundK || !foundK.includes(v)) && headers.append(k, v))354 }355 }356 }357 return headers358 }359360 protected setCookiesToResult(361 _: APIGatewayProxyEvent,362 result: APIGatewayProxyResult,363 cookies: string[]364 ): void {refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 365–366why, one step back365 result.multiValueHeaders = {366 'set-cookie': cookies,fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
line 367why, one step back367 }refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 368why, one step back368 }Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
line 369why, one step back369}refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 370why, one step back370Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
lines 371–373why, one step back371const v1Processor: EventV1Processor = new EventV1Processor()372373class ALBProcessor extends EventProcessor<ALBProxyEvent> {feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
lines 374–393why, one step back374 protected getHeaders(event: ALBProxyEvent): Headers {375 const headers = new Headers()376 // if multiValueHeaders is present the ALB will use it instead of the headers field377 // https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#multi-value-headers378 if (event.multiValueHeaders) {379 for (const [key, values] of Object.entries(event.multiValueHeaders)) {380 if (values && Array.isArray(values)) {381 // https://www.rfc-editor.org/rfc/rfc9110.html#name-common-rules-for-defining-f382 headers.set(key, values.join('; '))383 }384 }385 } else {386 for (const [key, value] of Object.entries(event.headers ?? {})) {387 if (value) {388 headers.set(key, value)389 }390 }391 }392 return headers393 }refactor(aws-lambda): remove unused setHeadersToResult (#2828)TATSUNO “Taz” Yasuhirobefore adoption0735e0c
line 394why, one step back394feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
lines 395–403why, one step back395 protected getPath(event: ALBProxyEvent): string {396 return event.path397 }398399 protected getMethod(event: ALBProxyEvent): string {400 return event.httpMethod401 }402403 protected getQueryString(event: ALBProxyEvent): string {feat(aws-lambda): add support for alb multiValueQueryStringParameters (#2751)Ilias YAHIAbefore adoptiona437161
line 404why, one step back404 // In the case of ALB Integration either queryStringParameters or multiValueQueryStringParameters can be present not bothfix(aws-lambda): Update handler.ts getQueryString (#2782)Tshepo Mgagabefore adoptiondfbc6c4
lines 405–417why, one step back405 /* 406 In other cases like when using the serverless framework, the event object does contain both queryStringParameters and multiValueQueryStringParameters:407 Below is an example event object for this URL: /payment/b8c55e69?select=amount&select=currency408 {409 ...410 queryStringParameters: { select: 'currency' },411 multiValueQueryStringParameters: { select: [ 'amount', 'currency' ] },412 }413 The expected results is for select to be an array with two items. However the pre-fix code is only returning one item ('currency') in the array.414 A simple fix would be to invert the if statement and check the multiValueQueryStringParameters first.415 */416 if (event.multiValueQueryStringParameters) {417 return Object.entries(event.multiValueQueryStringParameters || {})feat(aws-lambda): add support for alb multiValueQueryStringParameters (#2751)Ilias YAHIAbefore adoptiona437161
line 418why, one step back418 .filter(([, value]) => value)fix(aws-lambda): Update handler.ts getQueryString (#2782)Tshepo Mgagabefore adoptiondfbc6c4
line 419why, one step back419 .map(([key, value]) => `${key}=${value.join(`&${key}=`)}`)feat(aws-lambda): add support for alb multiValueQueryStringParameters (#2751)Ilias YAHIAbefore adoptiona437161
lines 420–421why, one step back420 .join('&')421 } else {fix(aws-lambda): Update handler.ts getQueryString (#2782)Tshepo Mgagabefore adoptiondfbc6c4
line 422why, one step back422 return Object.entries(event.queryStringParameters || {})feat(aws-lambda): add support for alb multiValueQueryStringParameters (#2751)Ilias YAHIAbefore adoptiona437161
line 423why, one step back423 .filter(([, value]) => value)fix(aws-lambda): Update handler.ts getQueryString (#2782)Tshepo Mgagabefore adoptiondfbc6c4
line 424why, one step back424 .map(([key, value]) => `${key}=${value}`)feat(aws-lambda): add support for alb multiValueQueryStringParameters (#2751)Ilias YAHIAbefore adoptiona437161
lines 425–426why, one step back425 .join('&')426 }feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
lines 427–453why, one step back427 }428429 protected getCookies(event: ALBProxyEvent, headers: Headers): void {430 let cookie431 if (event.multiValueHeaders) {432 cookie = event.multiValueHeaders['cookie']?.join('; ')433 } else {434 cookie = event.headers ? event.headers['cookie'] : undefined435 }436 if (cookie) {437 headers.append('Cookie', cookie)438 }439 }440441 protected setCookiesToResult(442 event: ALBProxyEvent,443 result: APIGatewayProxyResult,444 cookies: string[]445 ): void {446 // when multi value headers is enabled447 if (event.multiValueHeaders && result.multiValueHeaders) {448 result.multiValueHeaders['set-cookie'] = cookies449 } else {450 // otherwise serialize the set-cookie451 result.headers['set-cookie'] = cookies.join(', ')452 }453 }Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
lines 454–456why, one step back454}455456const albProcessor: ALBProcessor = new ALBProcessor()feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
line 457why, one step back457fix(aws-lambda): avoid duplicating already set headers (#2585)Aluisio Amorimbefore adoptionc95e135
line 458why, one step back458export const getProcessor = (event: LambdaEvent): EventProcessor<LambdaEvent> => {feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
lines 459–461why, one step back459 if (isProxyEventALB(event)) {460 return albProcessor461 }refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 462–463why, one step back462 if (isProxyEventV2(event)) {463 return v2Processorfix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
line 464why, one step back464 }feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
lines 465–468why, one step back465 return v1Processor466}467468const isProxyEventALB = (event: LambdaEvent): event is ALBProxyEvent => {refactor(aws-lambda): Object.hasOwn is recommended (#2831)TATSUNO “Taz” Yasuhirobefore adoption029cf71
line 469why, one step back469 return Object.hasOwn(event.requestContext, 'elb')fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
lines 470–472why, one step back470}471472const isProxyEventV2 = (event: LambdaEvent): event is APIGatewayProxyEventV2 => {refactor(aws-lambda): Object.hasOwn is recommended (#2831)TATSUNO “Taz” Yasuhirobefore adoption029cf71
line 473why, one step back473 return Object.hasOwn(event, 'rawPath')fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
lines 474–487why, one step back474}475476export const isContentTypeBinary = (contentType: string) => {477 return !/^(text\/(plain|html|css|javascript|csv).*|application\/(.*json|.*xml).*|image\/svg\+xml.*)$/.test(478 contentType479 )480}481482export const isContentEncodingBinary = (contentEncoding: string | null) => {483 if (contentEncoding === null) {484 return false485 }486 return /^(gzip|deflate|compress|br)/.test(contentEncoding)487}