handler.ts
This file
Path
src/adapter/aws-lambda/handler.ts
155 lines · 4.2 KiB · TypeScript
Last changed by
fix(lambda): added generic parameters to aws-lambda handler to fix #1265 (#1269)DianomiJHbefore adoption8ef9b5e
History of this fileTake it
Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
lines 1–3why, one step back1// @denoify-ignore2import crypto from 'crypto'3import type { Hono } from '../../hono'fix(lambda): added generic parameters to aws-lambda handler to fix #1265 (#1269)DianomiJHbefore adoption8ef9b5e
line 4why, one step back4import type { Env } from '../../types'Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
line 5why, one step back5fix: replace to use better base64-encoder (#1165)IGUCHI Kanahirobefore adoption39dcab0
lines 6–7why, one step back6import { encodeBase64 } from '../../utils/encode'7Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
lines 8–9why, one step back8// eslint-disable-next-line @typescript-eslint/ban-ts-comment9// @ts-ignorefix(tsconfig): remove `skipLibCheck` (#1201)Yusuke Wadabefore adoptionee5929b
line 10why, one step back10globalThis.crypto ??= cryptoMerge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
line 11why, one step back11feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 12–13why, one step back12// When calling Lambda directly through function urls13interface APIGatewayProxyEventV2 {Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
lines 14–15why, one step back14 httpMethod: string15 headers: Record<string, string | undefined>feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 16–28why, one step back16 rawPath: string17 rawQueryString: string18 body: string | null19 isBase64Encoded: boolean20 requestContext: {21 domainName: string22 }23}2425// When calling Lambda through an API Gateway or an ELB26interface APIGatewayProxyEvent {27 httpMethod: string28 headers: Record<string, string | undefined>Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
lines 29–31why, one step back29 path: string30 body: string | null31 isBase64Encoded: booleanfeat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 32why, one step back32 queryStringParameters?: Record<string, string | undefined>Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
lines 33–37why, one step back33 requestContext: {34 domainName: string35 }36}37feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
lines 38–45why, one step back38// When calling Lambda through an Lambda Function URLs39interface LambdaFunctionUrlEvent {40 headers: Record<string, string | undefined>41 rawPath: string42 rawQueryString: string43 body: string | null44 isBase64Encoded: boolean45 requestContext: {fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
line 46why, one step back46 domainName: stringfeat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
line 47why, one step back47 http: {fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
line 48why, one step back48 method: stringfeat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
lines 49–52why, one step back49 }50 }51}52Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
lines 53–59why, one step back53interface APIGatewayProxyResult {54 statusCode: number55 body: string56 headers: Record<string, string>57 isBase64Encoded: boolean58}59feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 60–62why, one step back60/**61 * Accepts events from API Gateway/ELB(`APIGatewayProxyEvent`) and directly through Function Url(`APIGatewayProxyEventV2`)62 */fix(lambda): added generic parameters to aws-lambda handler to fix #1265 (#1269)DianomiJHbefore adoption8ef9b5e
line 63why, one step back63export const handle = <E extends Env = Env, S = {}, BasePath extends string = '/'>(app: Hono<E, S, BasePath>) => {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> => {Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
lines 67–69why, one step back67 const req = createRequest(event)68 const res = await app.fetch(req)69feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 70–72why, one step back70 return createResult(res)71 }72}Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
line 73why, one step back73feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 74why, one step back74const createResult = async (res: Response): Promise<APIGatewayProxyResult> => {fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
lines 75–77why, one step back75 const contentType = res.headers.get('content-type')76 const isBase64Encoded = contentType && isContentTypeBinary(contentType) ? true : false77fix: replace to use better base64-encoder (#1165)IGUCHI Kanahirobefore adoption39dcab0
lines 78–84why, one step back78 let body: string79 if (isBase64Encoded) {80 const buffer = await res.arrayBuffer()81 body = encodeBase64(buffer)82 } else {83 body = await res.text()84 }feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 85why, one step back85 const result: APIGatewayProxyResult = {fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
line 86why, one step back86 body: body,feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 87–88why, one step back87 headers: {},88 statusCode: res.status,fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
line 89why, one step back89 isBase64Encoded,Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
line 90why, one step back90 }feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 91–96why, one step back9192 res.headers.forEach((value, key) => {93 result.headers[key] = value94 })9596 return resultMerge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
lines 97–98why, one step back97}98fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
lines 99–101why, one step back99const createRequest = (100 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent101) => {feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 102why, one step back102 const queryString = extractQueryString(event)fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
lines 103–105why, one step back103 const urlPath = `https://${event.requestContext.domainName}${104 isProxyEvent(event) ? event.path : event.rawPath105 }`feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 106–107why, one step back106 const url = queryString ? `${urlPath}?${queryString}` : urlPath107Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
lines 108–112why, one step back108 const headers = new Headers()109 for (const [k, v] of Object.entries(event.headers)) {110 if (v) headers.set(k, v)111 }112feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
line 113why, one step back113 const method = 'httpMethod' in event ? event.httpMethod : event.requestContext.http.methodMerge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
line 114why, one step back114 const requestInit: RequestInit = {feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 115why, one step back115 headers,fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
line 116why, one step back116 method,Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
lines 117–119why, one step back117 }118119 if (event.body) {fix: Safe modification of Buffer (#1238)watanybefore adoption301e142
line 120why, one step back120 requestInit.body = event.isBase64Encoded ? Buffer.from(event.body, 'base64') : event.bodyMerge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
line 121why, one step back121 }feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 122why, one step back122Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
line 123why, one step back123 return new Request(url, requestInit)feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 124–125why, one step back124}125fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
lines 126–128why, one step back126const extractQueryString = (127 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent128) => {feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
lines 129–133why, one step back129 if (isProxyEvent(event)) {130 return Object.entries(event.queryStringParameters || {})131 .filter(([, value]) => value)132 .map(([key, value]) => `${key}=${value}`)133 .join('&')feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 134–135why, one step back134 }135feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
line 136why, one step back136 return isProxyEventV2(event) ? event.rawQueryString : event.rawQueryStringfeat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 137–138why, one step back137}138feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
lines 139–144why, one step back139const isProxyEvent = (140 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent141): event is APIGatewayProxyEvent => {142 return Object.prototype.hasOwnProperty.call(event, 'path')143}144feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 145why, one step back145const isProxyEventV2 = (feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
line 146why, one step back146 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEventfeat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 147–148why, one step back147): event is APIGatewayProxyEventV2 => {148 return Object.prototype.hasOwnProperty.call(event, 'rawPath')fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
lines 149–154why, one step back149}150151export const isContentTypeBinary = (contentType: string) => {152 return !/^(text\/(plain|html|css|javascript|csv).*|application\/(.*json|.*xml).*|image\/svg\+xml)$/.test(153 contentType154 )Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
line 155why, one step back155}