Skip to content
Myr

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 file

Take it

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

lines 1–8why, one step back
1// @denoify-ignore
2import crypto from 'crypto'3import type { Hono } from '../../hono'45// eslint-disable-next-line @typescript-eslint/ban-ts-comment6// @ts-ignore7globalThis.crypto = crypto8

feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167

lines 9–10why, one step back
9// When calling Lambda directly through function urls
10interface APIGatewayProxyEventV2 {

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

lines 11–12why, one step back
11 httpMethod: string
12 headers: Record<string, string | undefined>

feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167

lines 13–25why, one step back
13 rawPath: string
14 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 back
26 path: string
27 body: string | null28 isBase64Encoded: boolean

feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167

line 29why, one step back
29 queryStringParameters?: Record<string, string | undefined>

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

lines 30–34why, one step back
30 requestContext: {
31 domainName: string32 }33}34

feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51

lines 35–42why, one step back
35// When calling Lambda through an Lambda Function URLs
36interface 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 back
43 domainName: string

feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51

line 44why, one step back
44 http: {

fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408

line 45why, one step back
45 method: string

feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51

lines 46–49why, one step back
46 }
47 }48}49

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

lines 50–56why, one step back
50interface APIGatewayProxyResult {
51 statusCode: number52 body: string53 headers: Record<string, string>54 isBase64Encoded: boolean55}56

feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167

lines 57–59why, one step back
57/**
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 back
60export const handle = (app: Hono) => {

feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167

line 61why, one step back
61 return async (

feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51

line 62why, one step back
62 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent

feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167

line 63why, one step back
63 ): Promise<APIGatewayProxyResult> => {

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

lines 64–66why, one step back
64 const req = createRequest(event)
65 const res = await app.fetch(req)66

feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167

lines 67–69why, one step back
67 return createResult(res)
68 }69}

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

line 70why, one step back
70

feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167

line 71why, one step back
71const createResult = async (res: Response): Promise<APIGatewayProxyResult> => {

fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408

lines 72–76why, one step back
72 const contentType = res.headers.get('content-type')
73 const isBase64Encoded = contentType && isContentTypeBinary(contentType) ? true : false7475 const body = isBase64Encoded ? await fromReadableToString(res) : await res.text()76

feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167

line 77why, one step back
77 const result: APIGatewayProxyResult = {

fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408

line 78why, one step back
78 body: body,

feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167

lines 79–80why, one step back
79 headers: {},
80 statusCode: res.status,

fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408

line 81why, one step back
81 isBase64Encoded,

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

line 82why, one step back
82 }

feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167

lines 83–88why, one step back
83
84 res.headers.forEach((value, key) => {85 result.headers[key] = value86 })8788 return result

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

lines 89–90why, one step back
89}
90

fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408

lines 91–93why, one step back
91const createRequest = (
92 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent93) => {

feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167

line 94why, one step back
94 const queryString = extractQueryString(event)

fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408

lines 95–97why, one step back
95 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 back
98 const url = queryString ? `${urlPath}?${queryString}` : urlPath
99

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

lines 100–104why, one step back
100 const headers = new Headers()
101 for (const [k, v] of Object.entries(event.headers)) {102 if (v) headers.set(k, v)103 }104

feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51

line 105why, one step back
105 const method = 'httpMethod' in event ? event.httpMethod : event.requestContext.http.method

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

line 106why, one step back
106 const requestInit: RequestInit = {

feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167

line 107why, one step back
107 headers,

fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408

line 108why, one step back
108 method,

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

lines 109–113why, one step back
109 }
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 back
114

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

line 115why, one step back
115 return new Request(url, requestInit)

feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167

lines 116–117why, one step back
116}
117

fix(lambda): don't encode text (#1091)Yusuke Wadabefore adoption821a408

lines 118–120why, one step back
118const 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 back
121 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 back
126 }
127

feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51

line 128why, one step back
128 return isProxyEventV2(event) ? event.rawQueryString : event.rawQueryString

feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167

lines 129–130why, one step back
129}
130

feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51

lines 131–136why, one step back
131const isProxyEvent = (
132 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent133): event is APIGatewayProxyEvent => {134 return Object.prototype.hasOwnProperty.call(event, 'path')135}136

feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167

line 137why, one step back
137const isProxyEventV2 = (

feat(adapter): AWS Lambda Adapter supports requests via Lambda Function URLs. (#1076)watanybefore adoptione7c0a51

line 138why, one step back
138 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent

feat(adapter): Added aws-handler support for APIGatewayProxyEventV2 (#1009)Stefan Schonertbefore adoptiona2e6167

lines 139–154why, one step back
139): 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 back
155}
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 back
161}