handler.ts
This file
Path
src/adapter/aws-lambda/handler.ts
475 lines · 14.0 KiB · TypeScript
Last changed by
refactor(aws-lambda): remove unused setHeadersToResult (#2828)TATSUNO “Taz” Yasuhirobefore adoption0735e0c
History of this fileTake it
The walk stopped after 200 commits: lines older than 2c44ad7 point at it.
Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
line 1why, one step back1import crypto from 'node:crypto'Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
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'Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
line 5why, one step back5import type {Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
line 6why, one step back6 ALBRequestContext,Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
lines 7–9why, one step back7 ApiGatewayRequestContext,8 ApiGatewayRequestContextV2,9} from './custom-context'Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
line 10why, one step back10import type { Handler, LambdaContext } from './types'Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
lines 11–22why, one step back1112// eslint-disable-next-line @typescript-eslint/ban-ts-comment13// @ts-ignore14globalThis.crypto ??= crypto1516export type LambdaEvent = APIGatewayProxyEvent | APIGatewayProxyEventV2 | ALBProxyEvent1718// When calling HTTP API or Lambda directly through function urls19export interface APIGatewayProxyEventV2 {20 version: string21 routeKey: string22 headers: Record<string, string | undefined>fix(adapter): handle multi value headers in AWS Lambda (#2494)TATSUNO “Taz” Yasuhirobefore adoptionacb56b8
line 23why, one step back23 multiValueHeaders?: undefinedMerge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
lines 24–64why, one step back24 cookies?: string[]25 rawPath: string26 rawQueryString: string27 body: string | null28 isBase64Encoded: boolean29 requestContext: ApiGatewayRequestContextV230 queryStringParameters?: {31 [name: string]: string | undefined32 }33 pathParameters?: {34 [name: string]: string | undefined35 }36 stageVariables?: {37 [name: string]: string | undefined38 }39}4041// When calling Lambda through an API Gateway42export interface APIGatewayProxyEvent {43 version: string44 httpMethod: string45 headers: Record<string, string | undefined>46 multiValueHeaders?: {47 [headerKey: string]: string[]48 }49 path: string50 body: string | null51 isBase64Encoded: boolean52 queryStringParameters?: Record<string, string | undefined>53 requestContext: ApiGatewayRequestContext54 resource: string55 multiValueQueryStringParameters?: {56 [parameterKey: string]: string[]57 }58 pathParameters?: Record<string, string>59 stageVariables?: Record<string, string>60}6162// When calling Lambda through an Application Load Balancer63export interface ALBProxyEvent {64 httpMethod: stringfix(adapter): handle multi value headers in AWS Lambda (#2494)TATSUNO “Taz” Yasuhirobefore adoptionacb56b8
lines 65–66why, one step back65 headers?: Record<string, string | undefined>66 multiValueHeaders?: Record<string, string[] | undefined>Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
lines 67–70why, one step back67 path: string68 body: string | null69 isBase64Encoded: boolean70 queryStringParameters?: Record<string, string | undefined>feat(aws-lambda): add support for alb multiValueQueryStringParameters (#2751)Ilias YAHIAbefore adoptiona437161
lines 71–73why, one step back71 multiValueQueryStringParameters?: {72 [parameterKey: string]: string[]73 }Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
lines 74–112why, one step back74 requestContext: ALBRequestContext75}7677export interface APIGatewayProxyResult {78 statusCode: number79 statusDescription?: string80 body: string81 headers: Record<string, string>82 cookies?: string[]83 multiValueHeaders?: {84 [headerKey: string]: string[]85 }86 isBase64Encoded: boolean87}8889const getRequestContext = (90 event: LambdaEvent91): ApiGatewayRequestContext | ApiGatewayRequestContextV2 | ALBRequestContext => {92 return event.requestContext93}9495const streamToNodeStream = async (96 reader: ReadableStreamDefaultReader<Uint8Array>,97 writer: NodeJS.WritableStream98) => {99 let readResult = await reader.read()100 while (!readResult.done) {101 writer.write(readResult.value)102 readResult = await reader.read()103 }104 writer.end()105}106107export const streamHandle = <108 E extends Env = Env,109 S extends Schema = {},110 BasePath extends string = '/'111>(112 app: Hono<E, S, BasePath>Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
lines 113–114why, one step back113): Handler => {114 // @ts-expect-error awslambda is not a standard APIMerge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
lines 115–116why, one step back115 return awslambda.streamifyResponse(116 async (event: LambdaEvent, responseStream: NodeJS.WritableStream, context: LambdaContext) => {refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 117why, one step back117 const processor = getProcessor(event)Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
line 118why, one step back118 try {refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 119why, one step back119 const req = processor.createRequest(event)Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
lines 120–133why, one step back120 const requestContext = getRequestContext(event)121122 const res = await app.fetch(req, {123 event,124 requestContext,125 context,126 })127128 // Check content type129 const httpResponseMetadata = {130 statusCode: res.status,131 headers: Object.fromEntries(res.headers.entries()),132 }133fix(aws-lambda): handle response without body (#2401) (#2413)Markus Wolfbefore adoptionf590d0f
line 134why, one step back134 // Update response streamMerge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
line 135why, one step back135 // @ts-expect-error awslambda is not a standard APIfix(aws-lambda): handle response without body (#2401) (#2413)Markus Wolfbefore adoptionf590d0f
lines 136–137why, one step back136 responseStream = awslambda.HttpResponseStream.from(responseStream, httpResponseMetadata)137Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
line 138why, one step back138 if (res.body) {fix(aws-lambda): handle response without body (#2401) (#2413)Markus Wolfbefore adoptionf590d0f
lines 139–141why, one step back139 await streamToNodeStream(res.body.getReader(), responseStream)140 } else {141 responseStream.write('')Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
lines 142–157why, one step back142 }143 } catch (error) {144 console.error('Error processing request:', error)145 responseStream.write('Internal Server Error')146 } finally {147 responseStream.end()148 }149 }150 )151}152153/**154 * Accepts events from API Gateway/ELB(`APIGatewayProxyEvent`) and directly through Function Url(`APIGatewayProxyEventV2`)155 */156export const handle = <E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'>(157 app: Hono<E, S, BasePath>Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
lines 158–159why, one step back158): ((event: LambdaEvent, lambdaContext?: LambdaContext) => Promise<APIGatewayProxyResult>) => {159 return async (event, lambdaContext?) => {refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 160–162why, one step back160 const processor = getProcessor(event)161162 const req = processor.createRequest(event)Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
lines 163–170why, one step back163 const requestContext = getRequestContext(event)164165 const res = await app.fetch(req, {166 event,167 requestContext,168 lambdaContext,169 })170refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 171why, one step back171 return processor.createResult(event, res)Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
lines 172–174why, one step back172 }173}174refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 175–176why, one step back175abstract class EventProcessor<E extends LambdaEvent> {176 protected abstract getPath(event: E): stringMerge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
line 177why, one step back177refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 178why, one step back178 protected abstract getMethod(event: E): stringMerge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
line 179why, one step back179refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 180why, one step back180 protected abstract getQueryString(event: E): stringMerge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
line 181why, one step back181feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
lines 182–183why, one step back182 protected abstract getHeaders(event: E): Headers183refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 184–185why, one step back184 protected abstract getCookies(event: E, headers: Headers): void185feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
lines 186–190why, one step back186 protected abstract setCookiesToResult(187 event: E,188 result: APIGatewayProxyResult,189 cookies: string[]190 ): voidrefactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 191–201why, one step back191192 createRequest(event: E): Request {193 const queryString = this.getQueryString(event)194 const domainName =195 event.requestContext && 'domainName' in event.requestContext196 ? event.requestContext.domainName197 : event.headers?.['host'] ?? event.multiValueHeaders?.['host']?.[0]198 const path = this.getPath(event)199 const urlPath = `https://${domainName}${path}`200 const url = queryString ? `${urlPath}?${queryString}` : urlPath201feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
line 202why, one step back202 const headers = this.getHeaders(event)refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 203–210why, one step back203204 const method = this.getMethod(event)205 const requestInit: RequestInit = {206 headers,207 method,208 }209210 if (event.body) {Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
line 211why, one step back211 requestInit.body = event.isBase64Encoded ? decodeBase64(event.body) : event.bodyrefactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 212–214why, one step back212 }213214 return new Request(url, requestInit)Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
lines 215–216why, one step back215 }216refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 217–219why, one step back217 async createResult(event: E, res: Response): Promise<APIGatewayProxyResult> {218 const contentType = res.headers.get('content-type')219 let isBase64Encoded = contentType && isContentTypeBinary(contentType) ? true : falseMerge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
line 220why, one step back220refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 221–224why, one step back221 if (!isBase64Encoded) {222 const contentEncoding = res.headers.get('content-encoding')223 isBase64Encoded = isContentEncodingBinary(contentEncoding)224 }Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
line 225why, one step back225refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 226why, one step back226 const body = isBase64Encoded ? encodeBase64(await res.arrayBuffer()) : await res.text()Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
line 227why, one step back227refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 228–230why, one step back228 const result: APIGatewayProxyResult = {229 body: body,230 headers: {},feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
line 231why, one step back231 multiValueHeaders: event.multiValueHeaders ? {} : undefined,refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 232–233why, one step back232 statusCode: res.status,233 isBase64Encoded,fix(adapter): handle multi value headers in AWS Lambda (#2494)TATSUNO “Taz” Yasuhirobefore adoptionacb56b8
line 234why, one step back234 }refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 235–238why, one step back235236 this.setCookies(event, res, result)237 res.headers.forEach((value, key) => {238 result.headers[key] = valuefeat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
lines 239–241why, one step back239 if (event.multiValueHeaders && result.multiValueHeaders) {240 result.multiValueHeaders[key] = [value]241 }refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 242–244why, one step back242 })243244 return resultfix(adapter): handle multi value headers in AWS Lambda (#2494)TATSUNO “Taz” Yasuhirobefore adoptionacb56b8
line 245why, one step back245 }refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 246why, one step back246feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
line 247why, one step back247 setCookies(event: E, res: Response, result: APIGatewayProxyResult) {refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 248–250why, one step back248 if (res.headers.has('set-cookie')) {249 const cookies = res.headers.get('set-cookie')?.split(', ')250 if (Array.isArray(cookies)) {feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
line 251why, one step back251 this.setCookiesToResult(event, result, cookies)refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 252why, one step back252 res.headers.delete('set-cookie')fix(adapter): handle multi value headers in AWS Lambda (#2494)TATSUNO “Taz” Yasuhirobefore adoptionacb56b8
line 253why, one step back253 }Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
lines 254–255why, one step back254 }255 }refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 256why, one step back256}Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
line 257why, one step back257Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
line 258why, one step back258class EventV2Processor extends EventProcessor<APIGatewayProxyEventV2> {refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 259–260why, one step back259 protected getPath(event: APIGatewayProxyEventV2): string {260 return event.rawPathMerge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
lines 261–262why, one step back261 }262refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 263–264why, one step back263 protected getMethod(event: APIGatewayProxyEventV2): string {264 return event.requestContext.http.methodMerge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
lines 265–266why, one step back265 }266refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 267–269why, one step back267 protected getQueryString(event: APIGatewayProxyEventV2): string {268 return event.rawQueryString269 }Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
line 270why, one step back270refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 271–275why, one step back271 protected getCookies(event: APIGatewayProxyEventV2, headers: Headers): void {272 if (Array.isArray(event.cookies)) {273 headers.set('Cookie', event.cookies.join('; '))274 }275 }Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
line 276why, one step back276feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
lines 277–281why, one step back277 protected setCookiesToResult(278 _: APIGatewayProxyEventV2,279 result: APIGatewayProxyResult,280 cookies: string[]281 ): void {refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 282why, one step back282 result.cookies = cookiesMerge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
line 283why, one step back283 }feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
lines 284–296why, one step back284285 protected getHeaders(event: APIGatewayProxyEventV2): Headers {286 const headers = new Headers()287 this.getCookies(event, headers)288 if (event.headers) {289 for (const [k, v] of Object.entries(event.headers)) {290 if (v) {291 headers.set(k, v)292 }293 }294 }295 return headers296 }Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
line 297why, one step back297}Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
line 298why, one step back298Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
lines 299–301why, one step back299const v2Processor: EventV2Processor = new EventV2Processor()300301class EventV1Processor extends EventProcessor<Exclude<LambdaEvent, APIGatewayProxyEventV2>> {refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 302–325why, one step back302 protected getPath(event: Exclude<LambdaEvent, APIGatewayProxyEventV2>): string {303 return event.path304 }305306 protected getMethod(event: Exclude<LambdaEvent, APIGatewayProxyEventV2>): string {307 return event.httpMethod308 }309310 protected getQueryString(event: Exclude<LambdaEvent, APIGatewayProxyEventV2>): string {311 return Object.entries(event.queryStringParameters || {})312 .filter(([, value]) => value)313 .map(([key, value]) => `${key}=${value}`)314 .join('&')315 }316317 protected getCookies(318 // eslint-disable-next-line @typescript-eslint/no-unused-vars319 event: Exclude<LambdaEvent, APIGatewayProxyEventV2>,320 // eslint-disable-next-line @typescript-eslint/no-unused-vars321 headers: Headers322 ): void {323 // nop324 }325feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
lines 326–352why, one step back326 protected getHeaders(event: APIGatewayProxyEvent): Headers {327 const headers = new Headers()328 this.getCookies(event, headers)329 if (event.headers) {330 for (const [k, v] of Object.entries(event.headers)) {331 if (v) {332 headers.set(k, v)333 }334 }335 }336 if (event.multiValueHeaders) {337 for (const [k, values] of Object.entries(event.multiValueHeaders)) {338 if (values) {339 // avoid duplicating already set headers340 const foundK = headers.get(k)341 values.forEach((v) => (!foundK || !foundK.includes(v)) && headers.append(k, v))342 }343 }344 }345 return headers346 }347348 protected setCookiesToResult(349 _: APIGatewayProxyEvent,350 result: APIGatewayProxyResult,351 cookies: string[]352 ): void {refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 353–354why, one step back353 result.multiValueHeaders = {354 'set-cookie': cookies,Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
line 355why, one step back355 }refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 356why, one step back356 }Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
line 357why, one step back357}refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
line 358why, one step back358Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
lines 359–361why, one step back359const v1Processor: EventV1Processor = new EventV1Processor()360361class ALBProcessor extends EventProcessor<ALBProxyEvent> {feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
lines 362–381why, one step back362 protected getHeaders(event: ALBProxyEvent): Headers {363 const headers = new Headers()364 // if multiValueHeaders is present the ALB will use it instead of the headers field365 // https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#multi-value-headers366 if (event.multiValueHeaders) {367 for (const [key, values] of Object.entries(event.multiValueHeaders)) {368 if (values && Array.isArray(values)) {369 // https://www.rfc-editor.org/rfc/rfc9110.html#name-common-rules-for-defining-f370 headers.set(key, values.join('; '))371 }372 }373 } else {374 for (const [key, value] of Object.entries(event.headers ?? {})) {375 if (value) {376 headers.set(key, value)377 }378 }379 }380 return headers381 }refactor(aws-lambda): remove unused setHeadersToResult (#2828)TATSUNO “Taz” Yasuhirobefore adoption0735e0c
line 382why, one step back382feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
lines 383–391why, one step back383 protected getPath(event: ALBProxyEvent): string {384 return event.path385 }386387 protected getMethod(event: ALBProxyEvent): string {388 return event.httpMethod389 }390391 protected getQueryString(event: ALBProxyEvent): string {feat(aws-lambda): add support for alb multiValueQueryStringParameters (#2751)Ilias YAHIAbefore adoptiona437161
line 392why, one step back392 // 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 393–405why, one step back393 /* 394 In other cases like when using the serverless framework, the event object does contain both queryStringParameters and multiValueQueryStringParameters:395 Below is an example event object for this URL: /payment/b8c55e69?select=amount&select=currency396 {397 ...398 queryStringParameters: { select: 'currency' },399 multiValueQueryStringParameters: { select: [ 'amount', 'currency' ] },400 }401 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.402 A simple fix would be to invert the if statement and check the multiValueQueryStringParameters first.403 */404 if (event.multiValueQueryStringParameters) {405 return Object.entries(event.multiValueQueryStringParameters || {})feat(aws-lambda): add support for alb multiValueQueryStringParameters (#2751)Ilias YAHIAbefore adoptiona437161
line 406why, one step back406 .filter(([, value]) => value)fix(aws-lambda): Update handler.ts getQueryString (#2782)Tshepo Mgagabefore adoptiondfbc6c4
line 407why, one step back407 .map(([key, value]) => `${key}=${value.join(`&${key}=`)}`)feat(aws-lambda): add support for alb multiValueQueryStringParameters (#2751)Ilias YAHIAbefore adoptiona437161
lines 408–409why, one step back408 .join('&')409 } else {fix(aws-lambda): Update handler.ts getQueryString (#2782)Tshepo Mgagabefore adoptiondfbc6c4
line 410why, one step back410 return Object.entries(event.queryStringParameters || {})feat(aws-lambda): add support for alb multiValueQueryStringParameters (#2751)Ilias YAHIAbefore adoptiona437161
line 411why, one step back411 .filter(([, value]) => value)fix(aws-lambda): Update handler.ts getQueryString (#2782)Tshepo Mgagabefore adoptiondfbc6c4
line 412why, one step back412 .map(([key, value]) => `${key}=${value}`)feat(aws-lambda): add support for alb multiValueQueryStringParameters (#2751)Ilias YAHIAbefore adoptiona437161
lines 413–414why, one step back413 .join('&')414 }feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
lines 415–441why, one step back415 }416417 protected getCookies(event: ALBProxyEvent, headers: Headers): void {418 let cookie419 if (event.multiValueHeaders) {420 cookie = event.multiValueHeaders['cookie']?.join('; ')421 } else {422 cookie = event.headers ? event.headers['cookie'] : undefined423 }424 if (cookie) {425 headers.append('Cookie', cookie)426 }427 }428429 protected setCookiesToResult(430 event: ALBProxyEvent,431 result: APIGatewayProxyResult,432 cookies: string[]433 ): void {434 // when multi value headers is enabled435 if (event.multiValueHeaders && result.multiValueHeaders) {436 result.multiValueHeaders['set-cookie'] = cookies437 } else {438 // otherwise serialize the set-cookie439 result.headers['set-cookie'] = cookies.join(', ')440 }441 }Merge pull request #2769 from honojs/nextYusuke Wadabefore adoptionacd3634
lines 442–444why, one step back442}443444const albProcessor: ALBProcessor = new ALBProcessor()feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
line 445why, one step back445fix(aws-lambda): avoid duplicating already set headers (#2585)Aluisio Amorimbefore adoptionc95e135
line 446why, one step back446export const getProcessor = (event: LambdaEvent): EventProcessor<LambdaEvent> => {feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
lines 447–449why, one step back447 if (isProxyEventALB(event)) {448 return albProcessor449 }refactor(aws-lambda): replace conditional logics with polymorphism (#2521)TATSUNO “Taz” Yasuhirobefore adoptionc83d80f
lines 450–451why, one step back450 if (isProxyEventV2(event)) {451 return v2ProcessorMerge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
line 452why, one step back452 }feat(aws-lambda): add alb event processor (#2657)Ilias YAHIAbefore adoptione1257f1
lines 453–456why, one step back453 return v1Processor454}455456const isProxyEventALB = (event: LambdaEvent): event is ALBProxyEvent => {refactor(aws-lambda): Object.hasOwn is recommended (#2831)TATSUNO “Taz” Yasuhirobefore adoption029cf71
line 457why, one step back457 return Object.hasOwn(event.requestContext, 'elb')Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
lines 458–460why, one step back458}459460const isProxyEventV2 = (event: LambdaEvent): event is APIGatewayProxyEventV2 => {refactor(aws-lambda): Object.hasOwn is recommended (#2831)TATSUNO “Taz” Yasuhirobefore adoption029cf71
line 461why, one step back461 return Object.hasOwn(event, 'rawPath')Merge pull request #2167 from honojs/v4Yusuke Wadabefore adoption2c44ad7
lines 462–475why, one step back462}463464export const isContentTypeBinary = (contentType: string) => {465 return !/^(text\/(plain|html|css|javascript|csv).*|application\/(.*json|.*xml).*|image\/svg\+xml.*)$/.test(466 contentType467 )468}469470export const isContentEncodingBinary = (contentEncoding: string | null) => {471 if (contentEncoding === null) {472 return false473 }474 return /^(gzip|deflate|compress|br)/.test(contentEncoding)475}