Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 309c38a point at it.

denoifyYusuke Wadabefore adoption309c38a

lines 1–3why, one step back
1// @denoify-ignore
2import crypto from 'crypto'3import type { Hono } from '../../hono'

fix(types): remove type-errors for routes (#1371)Yusuke Wadabefore adoption6034dd7

line 4why, one step back
4import type { Env, Schema } from '../../types'

denoifyYusuke Wadabefore adoption309c38a

line 5why, one step back
5

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

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

fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27

line 7why, one step back
7import type { ApiGatewayRequestContext, LambdaFunctionUrlRequestContext } from './custom-context'

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

line 8why, one step back
8

denoifyYusuke Wadabefore adoption309c38a

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

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

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

denoifyYusuke Wadabefore adoption309c38a

line 12why, one step back
12

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

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

denoifyYusuke Wadabefore adoption309c38a

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

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

lines 17–20why, one step back
17 rawPath: string
18 rawQueryString: string19 body: string | null20 isBase64Encoded: boolean

fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27

line 21why, one step back
21 requestContext: ApiGatewayRequestContext

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

lines 22–27why, one step back
22}
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 back
28 path: string
29 body: string | null30 isBase64Encoded: boolean

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

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

fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27

line 32why, one step back
32 requestContext: ApiGatewayRequestContext

denoifyYusuke Wadabefore adoption309c38a

lines 33–34why, one step back
33}
34

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

lines 35–41why, 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: boolean

fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27

line 42why, one step back
42 requestContext: LambdaFunctionUrlRequestContext

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

lines 43–44why, one step back
43}
44

denoifyYusuke Wadabefore adoption309c38a

lines 45–51why, one step back
45interface APIGatewayProxyResult {
46 statusCode: number47 body: string48 headers: Record<string, string>49 isBase64Encoded: boolean50}51

fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27

lines 52–57why, one step back
52const getRequestContext = (
53 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent54): ApiGatewayRequestContext | LambdaFunctionUrlRequestContext => {55 return event.requestContext56}57

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

lines 58–60why, one step back
58/**
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 back
61export 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 back
62 app: Hono<E, S, BasePath>
63) => {

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> => {

denoifyYusuke Wadabefore adoption309c38a

line 67why, one step back
67 const req = createRequest(event)

fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27

lines 68–70why, one step back
68 const requestContext = getRequestContext(event)
6970 const res = await app.fetch(req, { requestContext })

denoifyYusuke Wadabefore adoption309c38a

line 71why, one step back
71

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

lines 72–74why, one step back
72 return createResult(res)
73 }74}

denoifyYusuke Wadabefore adoption309c38a

line 75why, one step back
75

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

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

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

line 77why, one step back
77 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 back
78 let isBase64Encoded = contentType && isContentTypeBinary(contentType) ? true : false

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

line 79why, one step back
79

fix(adapter/aws-lambda): use content-encoding to determine isBase64Encoded (#1295)byunghoo.yubefore adoptiona7dd281

lines 80–84why, one step back
80 if (!isBase64Encoded) {
81 const contentEncoding = res.headers.get('content-encoding')82 isBase64Encoded = isContentEncodingBinary(contentEncoding)83 }84

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

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

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

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

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

lines 94–95why, one step back
94 headers: {},
95 statusCode: res.status,

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

line 96why, one step back
96 isBase64Encoded,

denoifyYusuke Wadabefore adoption309c38a

line 97why, one step back
97 }

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

lines 98–103why, one step back
98
99 res.headers.forEach((value, key) => {100 result.headers[key] = value101 })102103 return result

denoifyYusuke Wadabefore adoption309c38a

lines 104–105why, one step back
104}
105

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

lines 106–108why, one step back
106const createRequest = (
107 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent108) => {

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

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

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

lines 110–112why, one step back
110 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 back
113 const url = queryString ? `${urlPath}?${queryString}` : urlPath
114

denoifyYusuke Wadabefore adoption309c38a

lines 115–119why, one step back
115 const headers = new Headers()
116 for (const [k, v] of Object.entries(event.headers)) {117 if (v) headers.set(k, v)118 }119

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

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

denoifyYusuke Wadabefore adoption309c38a

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

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

line 122why, one step back
122 headers,

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

line 123why, one step back
123 method,

denoifyYusuke Wadabefore adoption309c38a

lines 124–126why, one step back
124 }
125126 if (event.body) {

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

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

denoifyYusuke Wadabefore adoption309c38a

line 128why, one step back
128 }

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

line 129why, one step back
129

denoifyYusuke Wadabefore adoption309c38a

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

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

lines 131–132why, one step back
131}
132

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

lines 133–135why, one step back
133const 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 back
136 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 back
141 }
142

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

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

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

lines 144–145why, one step back
144}
145

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

lines 146–151why, one step back
146const isProxyEvent = (
147 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent148): event is APIGatewayProxyEvent => {149 return Object.prototype.hasOwnProperty.call(event, 'path')150}151

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

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

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

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

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

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