Skip to content
Myr

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 file

Take it

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

lines 1–3why, one step back
1// @denoify-ignore
2import 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 back
4import type { Env } from '../../types'

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

line 5why, one step back
5

fix: replace to use better base64-encoder (#1165)IGUCHI Kanahirobefore adoption39dcab0

lines 6–7why, one step back
6import { encodeBase64 } from '../../utils/encode'
7

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

lines 8–9why, one step back
8// eslint-disable-next-line @typescript-eslint/ban-ts-comment
9// @ts-ignore

fix(tsconfig): remove `skipLibCheck` (#1201)Yusuke Wadabefore adoptionee5929b

line 10why, one step back
10globalThis.crypto ??= crypto

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

line 11why, one step back
11

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

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

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

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

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

lines 16–28why, one step back
16 rawPath: string
17 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 back
29 path: string
30 body: string | null31 isBase64Encoded: boolean

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

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

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

lines 33–37why, one step back
33 requestContext: {
34 domainName: string35 }36}37

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

lines 38–45why, one step back
38// When calling Lambda through an Lambda Function URLs
39interface 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 back
46 domainName: string

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

line 47why, one step back
47 http: {

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

line 48why, one step back
48 method: string

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

lines 49–52why, one step back
49 }
50 }51}52

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

lines 53–59why, one step back
53interface APIGatewayProxyResult {
54 statusCode: number55 body: string56 headers: Record<string, string>57 isBase64Encoded: boolean58}59

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

lines 60–62why, one step back
60/**
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 back
63export 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 back
64 return async (

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

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

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

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

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

lines 67–69why, one step back
67 const req = createRequest(event)
68 const res = await app.fetch(req)69

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

lines 70–72why, one step back
70 return createResult(res)
71 }72}

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

line 73why, one step back
73

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

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

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

lines 75–77why, one step back
75 const contentType = res.headers.get('content-type')
76 const isBase64Encoded = contentType && isContentTypeBinary(contentType) ? true : false77

fix: replace to use better base64-encoder (#1165)IGUCHI Kanahirobefore adoption39dcab0

lines 78–84why, one step back
78 let body: string
79 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 back
85 const result: APIGatewayProxyResult = {

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

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

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

lines 87–88why, one step back
87 headers: {},
88 statusCode: res.status,

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

line 89why, one step back
89 isBase64Encoded,

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

line 90why, one step back
90 }

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

lines 91–96why, one step back
91
92 res.headers.forEach((value, key) => {93 result.headers[key] = value94 })9596 return result

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

lines 97–98why, one step back
97}
98

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

lines 99–101why, one step back
99const createRequest = (
100 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent101) => {

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

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

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

lines 103–105why, one step back
103 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 back
106 const url = queryString ? `${urlPath}?${queryString}` : urlPath
107

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

lines 108–112why, one step back
108 const headers = new Headers()
109 for (const [k, v] of Object.entries(event.headers)) {110 if (v) headers.set(k, v)111 }112

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

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

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

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

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

line 115why, one step back
115 headers,

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

line 116why, one step back
116 method,

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

lines 117–119why, one step back
117 }
118119 if (event.body) {

fix: Safe modification of Buffer (#1238)watanybefore adoption301e142

line 120why, one step back
120 requestInit.body = event.isBase64Encoded ? Buffer.from(event.body, 'base64') : event.body

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

line 121why, one step back
121 }

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

line 122why, one step back
122

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

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

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

lines 124–125why, one step back
124}
125

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

lines 126–128why, one step back
126const 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 back
129 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 back
134 }
135

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

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

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

lines 137–138why, one step back
137}
138

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

lines 139–144why, one step back
139const isProxyEvent = (
140 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent141): event is APIGatewayProxyEvent => {142 return Object.prototype.hasOwnProperty.call(event, 'path')143}144

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

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

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

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

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

lines 147–148why, one step back
147): 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 back
149}
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 back
155}