handler.ts
This file
Path
src/adapter/aws-lambda/handler.ts
169 lines · 4.9 KiB · TypeScript
Last changed by
fix(types): remove type-errors for routes (#1371)Yusuke Wadabefore adoption6034dd7
History of this fileTake it
The walk stopped after 200 commits: lines older than 309c38a point at it.
denoifyYusuke Wadabefore adoption309c38a
lines 1–3why, one step back1// @denoify-ignore2import crypto from 'crypto'3import type { Hono } from '../../hono'fix(types): remove type-errors for routes (#1371)Yusuke Wadabefore adoption6034dd7
line 4why, one step back4import type { Env, Schema } from '../../types'denoifyYusuke Wadabefore adoption309c38a
line 5why, one step back5fix: replace to use better base64-encoder (#1165)IGUCHI Kanahirobefore adoption39dcab0
line 6why, one step back6import { encodeBase64 } from '../../utils/encode'fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
line 7why, one step back7import type { ApiGatewayRequestContext, LambdaFunctionUrlRequestContext } from './custom-context'fix: replace to use better base64-encoder (#1165)IGUCHI Kanahirobefore adoption39dcab0
line 8why, one step back8denoifyYusuke Wadabefore adoption309c38a
lines 9–10why, one step back9// eslint-disable-next-line @typescript-eslint/ban-ts-comment10// @ts-ignorefix(tsconfig): remove `skipLibCheck` (#1201)Yusuke Wadabefore adoptionee5929b
line 11why, one step back11globalThis.crypto ??= cryptodenoifyYusuke Wadabefore adoption309c38a
line 12why, one step back12feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 13–14why, one step back13// When calling Lambda directly through function urls14interface APIGatewayProxyEventV2 {denoifyYusuke Wadabefore adoption309c38a
lines 15–16why, one step back15 httpMethod: string16 headers: Record<string, string | undefined>feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 17–20why, one step back17 rawPath: string18 rawQueryString: string19 body: string | null20 isBase64Encoded: booleanfix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
line 21why, one step back21 requestContext: ApiGatewayRequestContextfeat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 22–27why, one step back22}2324// When calling Lambda through an API Gateway or an ELB25interface APIGatewayProxyEvent {26 httpMethod: string27 headers: Record<string, string | undefined>denoifyYusuke Wadabefore adoption309c38a
lines 28–30why, one step back28 path: string29 body: string | null30 isBase64Encoded: booleanfeat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 31why, one step back31 queryStringParameters?: Record<string, string | undefined>fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
line 32why, one step back32 requestContext: ApiGatewayRequestContextdenoifyYusuke Wadabefore adoption309c38a
lines 33–34why, one step back33}34feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
lines 35–41why, one step back35// When calling Lambda through an Lambda Function URLs36interface LambdaFunctionUrlEvent {37 headers: Record<string, string | undefined>38 rawPath: string39 rawQueryString: string40 body: string | null41 isBase64Encoded: booleanfix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
line 42why, one step back42 requestContext: LambdaFunctionUrlRequestContextfeat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
lines 43–44why, one step back43}44denoifyYusuke Wadabefore adoption309c38a
lines 45–51why, one step back45interface APIGatewayProxyResult {46 statusCode: number47 body: string48 headers: Record<string, string>49 isBase64Encoded: boolean50}51fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
lines 52–57why, one step back52const getRequestContext = (53 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent54): ApiGatewayRequestContext | LambdaFunctionUrlRequestContext => {55 return event.requestContext56}57feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 58–60why, one step back58/**59 * Accepts events from API Gateway/ELB(`APIGatewayProxyEvent`) and directly through Function Url(`APIGatewayProxyEventV2`)60 */fix(types): remove type-errors for routes (#1371)Yusuke Wadabefore adoption6034dd7
line 61why, one step back61export const handle = <E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'>(fix(adapter/aws-lambda): use content-encoding to determine isBase64Encoded (#1295)byunghoo.yubefore adoptiona7dd281
lines 62–63why, one step back62 app: Hono<E, S, BasePath>63) => {feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 64why, one step back64 return async (feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
line 65why, one step back65 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEventfeat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 66why, one step back66 ): Promise<APIGatewayProxyResult> => {denoifyYusuke Wadabefore adoption309c38a
line 67why, one step back67 const req = createRequest(event)fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
lines 68–70why, one step back68 const requestContext = getRequestContext(event)6970 const res = await app.fetch(req, { requestContext })denoifyYusuke Wadabefore adoption309c38a
line 71why, one step back71feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 72–74why, one step back72 return createResult(res)73 }74}denoifyYusuke Wadabefore adoption309c38a
line 75why, one step back75feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 76why, one step back76const createResult = async (res: Response): Promise<APIGatewayProxyResult> => {fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
line 77why, one step back77 const contentType = res.headers.get('content-type')fix(adapter/aws-lambda): use content-encoding to determine isBase64Encoded (#1295)byunghoo.yubefore adoptiona7dd281
line 78why, one step back78 let isBase64Encoded = contentType && isContentTypeBinary(contentType) ? true : falsefix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
line 79why, one step back79fix(adapter/aws-lambda): use content-encoding to determine isBase64Encoded (#1295)byunghoo.yubefore adoptiona7dd281
lines 80–84why, one step back80 if (!isBase64Encoded) {81 const contentEncoding = res.headers.get('content-encoding')82 isBase64Encoded = isContentEncodingBinary(contentEncoding)83 }84fix: replace to use better base64-encoder (#1165)IGUCHI Kanahirobefore adoption39dcab0
lines 85–91why, one step back85 let body: string86 if (isBase64Encoded) {87 const buffer = await res.arrayBuffer()88 body = encodeBase64(buffer)89 } else {90 body = await res.text()91 }feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 92why, one step back92 const result: APIGatewayProxyResult = {fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
line 93why, one step back93 body: body,feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 94–95why, one step back94 headers: {},95 statusCode: res.status,fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
line 96why, one step back96 isBase64Encoded,denoifyYusuke Wadabefore adoption309c38a
line 97why, one step back97 }feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 98–103why, one step back9899 res.headers.forEach((value, key) => {100 result.headers[key] = value101 })102103 return resultdenoifyYusuke Wadabefore adoption309c38a
lines 104–105why, one step back104}105fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
lines 106–108why, one step back106const createRequest = (107 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent108) => {feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 109why, one step back109 const queryString = extractQueryString(event)fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
lines 110–112why, one step back110 const urlPath = `https://${event.requestContext.domainName}${111 isProxyEvent(event) ? event.path : event.rawPath112 }`feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 113–114why, one step back113 const url = queryString ? `${urlPath}?${queryString}` : urlPath114denoifyYusuke Wadabefore adoption309c38a
lines 115–119why, one step back115 const headers = new Headers()116 for (const [k, v] of Object.entries(event.headers)) {117 if (v) headers.set(k, v)118 }119feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
line 120why, one step back120 const method = 'httpMethod' in event ? event.httpMethod : event.requestContext.http.methoddenoifyYusuke Wadabefore adoption309c38a
line 121why, one step back121 const requestInit: RequestInit = {feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 122why, one step back122 headers,fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
line 123why, one step back123 method,denoifyYusuke Wadabefore adoption309c38a
lines 124–126why, one step back124 }125126 if (event.body) {fix: Safe modification of Buffer (#1238)watanybefore adoption301e142
line 127why, one step back127 requestInit.body = event.isBase64Encoded ? Buffer.from(event.body, 'base64') : event.bodydenoifyYusuke Wadabefore adoption309c38a
line 128why, one step back128 }feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 129why, one step back129denoifyYusuke Wadabefore adoption309c38a
line 130why, one step back130 return new Request(url, requestInit)feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 131–132why, one step back131}132fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
lines 133–135why, one step back133const extractQueryString = (134 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent135) => {feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
lines 136–140why, one step back136 if (isProxyEvent(event)) {137 return Object.entries(event.queryStringParameters || {})138 .filter(([, value]) => value)139 .map(([key, value]) => `${key}=${value}`)140 .join('&')feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 141–142why, one step back141 }142feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
line 143why, one step back143 return isProxyEventV2(event) ? event.rawQueryString : event.rawQueryStringfeat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 144–145why, one step back144}145feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
lines 146–151why, one step back146const isProxyEvent = (147 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent148): event is APIGatewayProxyEvent => {149 return Object.prototype.hasOwnProperty.call(event, 'path')150}151feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 152why, one step back152const isProxyEventV2 = (feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
line 153why, one step back153 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEventfeat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 154–155why, one step back154): event is APIGatewayProxyEventV2 => {155 return Object.prototype.hasOwnProperty.call(event, 'rawPath')fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
lines 156–161why, one step back156}157158export const isContentTypeBinary = (contentType: string) => {159 return !/^(text\/(plain|html|css|javascript|csv).*|application\/(.*json|.*xml).*|image\/svg\+xml)$/.test(160 contentType161 )fix(adapter/aws-lambda): use content-encoding to determine isBase64Encoded (#1295)byunghoo.yubefore adoptiona7dd281
lines 162–168why, one step back162}163164export const isContentEncodingBinary = (contentEncoding: string | null) => {165 if (contentEncoding === null) {166 return false167 }168 return /^(gzip|deflate|compress|br)/.test(contentEncoding)denoifyYusuke Wadabefore adoption309c38a
line 169why, one step back169}