handler.ts
This file
Path
src/adapter/aws-lambda/handler.ts
161 lines · 4.3 KiB · TypeScript
Last changed by
fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
History of this fileTake it
Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
lines 1–8why, one step back1// @denoify-ignore2import crypto from 'crypto'3import type { Hono } from '../../hono'45// eslint-disable-next-line @typescript-eslint/ban-ts-comment6// @ts-ignore7globalThis.crypto = crypto8feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 9–10why, one step back9// When calling Lambda directly through function urls10interface APIGatewayProxyEventV2 {Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
lines 11–12why, one step back11 httpMethod: string12 headers: Record<string, string | undefined>feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 13–25why, one step back13 rawPath: string14 rawQueryString: string15 body: string | null16 isBase64Encoded: boolean17 requestContext: {18 domainName: string19 }20}2122// When calling Lambda through an API Gateway or an ELB23interface APIGatewayProxyEvent {24 httpMethod: string25 headers: Record<string, string | undefined>Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
lines 26–28why, one step back26 path: string27 body: string | null28 isBase64Encoded: booleanfeat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 29why, one step back29 queryStringParameters?: Record<string, string | undefined>Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
lines 30–34why, one step back30 requestContext: {31 domainName: string32 }33}34feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
lines 35–42why, 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: boolean42 requestContext: {fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
line 43why, one step back43 domainName: stringfeat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
line 44why, one step back44 http: {fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
line 45why, one step back45 method: stringfeat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
lines 46–49why, one step back46 }47 }48}49Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
lines 50–56why, one step back50interface APIGatewayProxyResult {51 statusCode: number52 body: string53 headers: Record<string, string>54 isBase64Encoded: boolean55}56feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 57–59why, one step back57/**58 * Accepts events from API Gateway/ELB(`APIGatewayProxyEvent`) and directly through Function Url(`APIGatewayProxyEventV2`)59 */Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
line 60why, one step back60export const handle = (app: Hono) => {feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 61why, one step back61 return async (feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
line 62why, one step back62 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEventfeat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 63why, one step back63 ): Promise<APIGatewayProxyResult> => {Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
lines 64–66why, one step back64 const req = createRequest(event)65 const res = await app.fetch(req)66feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 67–69why, one step back67 return createResult(res)68 }69}Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
line 70why, one step back70feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 71why, one step back71const createResult = async (res: Response): Promise<APIGatewayProxyResult> => {fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
lines 72–76why, one step back72 const contentType = res.headers.get('content-type')73 const isBase64Encoded = contentType && isContentTypeBinary(contentType) ? true : false7475 const body = isBase64Encoded ? await fromReadableToString(res) : await res.text()76feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 77why, one step back77 const result: APIGatewayProxyResult = {fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
line 78why, one step back78 body: body,feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 79–80why, one step back79 headers: {},80 statusCode: res.status,fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
line 81why, one step back81 isBase64Encoded,Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
line 82why, one step back82 }feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 83–88why, one step back8384 res.headers.forEach((value, key) => {85 result.headers[key] = value86 })8788 return resultMerge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
lines 89–90why, one step back89}90fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
lines 91–93why, one step back91const createRequest = (92 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent93) => {feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 94why, one step back94 const queryString = extractQueryString(event)fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
lines 95–97why, one step back95 const urlPath = `https://${event.requestContext.domainName}${96 isProxyEvent(event) ? event.path : event.rawPath97 }`feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 98–99why, one step back98 const url = queryString ? `${urlPath}?${queryString}` : urlPath99Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
lines 100–104why, one step back100 const headers = new Headers()101 for (const [k, v] of Object.entries(event.headers)) {102 if (v) headers.set(k, v)103 }104feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
line 105why, one step back105 const method = 'httpMethod' in event ? event.httpMethod : event.requestContext.http.methodMerge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
line 106why, one step back106 const requestInit: RequestInit = {feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 107why, one step back107 headers,fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
line 108why, one step back108 method,Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
lines 109–113why, one step back109 }110111 if (event.body) {112 requestInit.body = event.isBase64Encoded ? atob(event.body) : event.body113 }feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 114why, one step back114Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
line 115why, one step back115 return new Request(url, requestInit)feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 116–117why, one step back116}117fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
lines 118–120why, one step back118const extractQueryString = (119 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent120) => {feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
lines 121–125why, one step back121 if (isProxyEvent(event)) {122 return Object.entries(event.queryStringParameters || {})123 .filter(([, value]) => value)124 .map(([key, value]) => `${key}=${value}`)125 .join('&')feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 126–127why, one step back126 }127feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
line 128why, one step back128 return isProxyEventV2(event) ? event.rawQueryString : event.rawQueryStringfeat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 129–130why, one step back129}130feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
lines 131–136why, one step back131const isProxyEvent = (132 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent133): event is APIGatewayProxyEvent => {134 return Object.prototype.hasOwnProperty.call(event, 'path')135}136feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
line 137why, one step back137const isProxyEventV2 = (feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51
line 138why, one step back138 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEventfeat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167
lines 139–154why, one step back139): event is APIGatewayProxyEventV2 => {140 return Object.prototype.hasOwnProperty.call(event, 'rawPath')141}142143const fromReadableToString = async (res: Response) => {144 const stream = res.body || new ReadableStream()145 const decoder = new TextDecoder()146 let string = ''147148 // eslint-disable-next-line @typescript-eslint/ban-ts-comment149 // @ts-ignore: asking for asyncIterator150 for await (const chunk of stream) {151 string += decoder.decode(chunk)152 }153154 return btoa(string)fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408
lines 155–160why, one step back155}156157export const isContentTypeBinary = (contentType: string) => {158 return !/^(text\/(plain|html|css|javascript|csv).*|application\/(.*json|.*xml).*|image\/svg\+xml)$/.test(159 contentType160 )Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd
line 161why, one step back161}