Skip to content
Myr

handler.ts

This file

Path

src/adapter/aws-lambda/handler.ts

274 lines · 7.4 KiB · TypeScript

Last changed by

fix: add missing tail to svg regex (#1788)Elias Brangebefore adoption521db64

History of this file

Take it

The walk stopped after 200 commits: lines older than 48a8729 point at it.

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

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'

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

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

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

lines 7–11why, one step back
7import type {
8 ApiGatewayRequestContext,9 ApiGatewayRequestContextV2,10 ALBRequestContext,11} from './custom-context'

feat(adaptor): Accessable Lambda "Context" (#1523)watanybefore adoption43749a0

line 12why, one step back
12import type { LambdaContext } from './types'

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

lines 13–17why, one step back
13
14// eslint-disable-next-line @typescript-eslint/ban-ts-comment15// @ts-ignore16globalThis.crypto ??= crypto17

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

lines 18–20why, one step back
18export type LambdaEvent = APIGatewayProxyEvent | APIGatewayProxyEventV2 | ALBProxyEvent
1920// When calling HTTP API or Lambda directly through function urls

Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a

line 21why, one step back
21export interface APIGatewayProxyEventV2 {

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

lines 22–23why, one step back
22 version: string
23 routeKey: string

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

line 24why, one step back
24 headers: Record<string, string | undefined>

fix(lambda): Aws lambda cookies fix (#1613)Ochirjav Jargalsaikhanbefore adoption5f02375

line 25why, one step back
25 cookies?: string[]

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

lines 26–29why, one step back
26 rawPath: string
27 rawQueryString: string28 body: string | null29 isBase64Encoded: boolean

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

lines 30–39why, one step back
30 requestContext: ApiGatewayRequestContextV2
31 queryStringParameters?: {32 [name: string]: string | undefined33 }34 pathParameters?: {35 [name: string]: string | undefined36 }37 stageVariables?: {38 [name: string]: string | undefined39 }

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

lines 40–41why, one step back
40}
41

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

line 42why, one step back
42// When calling Lambda through an API Gateway

Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a

line 43why, one step back
43export interface APIGatewayProxyEvent {

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

line 44why, one step back
44 version: string

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

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

fix(lambda): Aws lambda cookies fix (#1613)Ochirjav Jargalsaikhanbefore adoption5f02375

lines 47–49why, one step back
47 multiValueHeaders?: {
48 [headerKey: string]: string[]49 }

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

lines 50–53why, one step back
50 path: string
51 body: string | null52 isBase64Encoded: boolean53 queryStringParameters?: Record<string, string | undefined>

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

line 54why, one step back
54 requestContext: ApiGatewayRequestContext

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

lines 55–60why, one step back
55 resource: string
56 multiValueQueryStringParameters?: {57 [parameterKey: string]: string[]58 }59 pathParameters?: Record<string, string>60 stageVariables?: Record<string, string>

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

lines 61–62why, one step back
61}
62

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

lines 63–65why, one step back
63// When calling Lambda through an Application Load Balancer
64export interface ALBProxyEvent {65 httpMethod: string

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

line 66why, one step back
66 headers: Record<string, string | undefined>

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

line 67why, one step back
67 path: string

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

lines 68–69why, one step back
68 body: string | null
69 isBase64Encoded: boolean

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

lines 70–71why, one step back
70 queryStringParameters?: Record<string, string | undefined>
71 requestContext: ALBRequestContext

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

lines 72–73why, one step back
72}
73

fix(aws-lambda): export `APIGatewayProxyResult` (#1711)juhobefore adoption0a2b54a

line 74why, one step back
74export interface APIGatewayProxyResult {

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

line 75why, one step back
75 statusCode: number

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

line 76why, one step back
76 statusDescription?: string

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

lines 77–78why, one step back
77 body: string
78 headers: Record<string, string>

fix(lambda): Aws lambda cookies fix (#1613)Ochirjav Jargalsaikhanbefore adoption5f02375

lines 79–82why, one step back
79 cookies?: string[]
80 multiValueHeaders?: {81 [headerKey: string]: string[]82 }

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

lines 83–85why, one step back
83 isBase64Encoded: boolean
84}85

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

line 86why, one step back
86const getRequestContext = (

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

lines 87–88why, one step back
87 event: LambdaEvent
88): ApiGatewayRequestContext | ApiGatewayRequestContextV2 | ALBRequestContext => {

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

line 89why, one step back
89 return event.requestContext

Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a

lines 90–111why, one step back
90}
9192const streamToNodeStream = async (93 reader: ReadableStreamDefaultReader<Uint8Array>,94 writer: NodeJS.WritableStream95) => {96 let readResult = await reader.read()97 while (!readResult.done) {98 writer.write(readResult.value)99 readResult = await reader.read()100 }101 writer.end()102}103104export const streamHandle = <105 E extends Env = Env,106 S extends Schema = {},107 BasePath extends string = '/'108>(109 app: Hono<E, S, BasePath>110) => {111 return awslambda.streamifyResponse(

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

line 112why, one step back
112 async (event: LambdaEvent, responseStream: NodeJS.WritableStream, context: LambdaContext) => {

Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a

lines 113–117why, one step back
113 try {
114 const req = createRequest(event)115 const requestContext = getRequestContext(event)116117 const res = await app.fetch(req, {

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

line 118why, one step back
118 event,

Merge pull request #1690 from honojs/nextYusuke Wadabefore adoption5e3e85a

lines 119–142why, one step back
119 requestContext,
120 context,121 })122123 // Check content type124 const httpResponseMetadata = {125 statusCode: res.status,126 headers: Object.fromEntries(res.headers.entries()),127 }128129 if (res.body) {130 await streamToNodeStream(131 res.body.getReader(),132 awslambda.HttpResponseStream.from(responseStream, httpResponseMetadata)133 )134 }135 } catch (error) {136 console.error('Error processing request:', error)137 responseStream.write('Internal Server Error')138 } finally {139 responseStream.end()140 }141 }142 )

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

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

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

lines 145–147why, one step back
145/**
146 * Accepts events from API Gateway/ELB(`APIGatewayProxyEvent`) and directly through Function Url(`APIGatewayProxyEventV2`)147 */

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

line 148why, one step back
148export const handle = <E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'>(

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

lines 149–151why, one step back
149 app: Hono<E, S, BasePath>
150) => {151 return async (

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

line 152why, one step back
152 event: LambdaEvent,

feat(adaptor): Accessable Lambda "Context" (#1523)watanybefore adoption43749a0

line 153why, one step back
153 lambdaContext?: LambdaContext

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

lines 154–155why, one step back
154 ): Promise<APIGatewayProxyResult> => {
155 const req = createRequest(event)

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

lines 156–157why, one step back
156 const requestContext = getRequestContext(event)
157

feat(adaptor): Accessable Lambda "Context" (#1523)watanybefore adoption43749a0

line 158why, one step back
158 const res = await app.fetch(req, {

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

line 159why, one step back
159 event,

feat(adaptor): Accessable Lambda "Context" (#1523)watanybefore adoption43749a0

lines 160–162why, one step back
160 requestContext,
161 lambdaContext,162 })

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

line 163why, one step back
163

fix(lambda): Aws lambda cookies fix (#1613)Ochirjav Jargalsaikhanbefore adoption5f02375

line 164why, one step back
164 return createResult(event, res)

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

lines 165–167why, one step back
165 }
166}167

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

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

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

lines 169–190why, one step back
169 const contentType = res.headers.get('content-type')
170 let isBase64Encoded = contentType && isContentTypeBinary(contentType) ? true : false171172 if (!isBase64Encoded) {173 const contentEncoding = res.headers.get('content-encoding')174 isBase64Encoded = isContentEncodingBinary(contentEncoding)175 }176177 let body: string178 if (isBase64Encoded) {179 const buffer = await res.arrayBuffer()180 body = encodeBase64(buffer)181 } else {182 body = await res.text()183 }184 const result: APIGatewayProxyResult = {185 body: body,186 headers: {},187 statusCode: res.status,188 isBase64Encoded,189 }190

fix(lambda): Aws lambda cookies fix (#1613)Ochirjav Jargalsaikhanbefore adoption5f02375

line 191why, one step back
191 setCookies(event, res, result)

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

lines 192–198why, one step back
192 res.headers.forEach((value, key) => {
193 result.headers[key] = value194 })195196 return result197}198

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

line 199why, one step back
199const createRequest = (event: LambdaEvent) => {

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

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

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

lines 201–206why, one step back
201 const domainName =
202 event.requestContext && 'domainName' in event.requestContext203 ? event.requestContext.domainName204 : event.headers['host']205 const path = isProxyEventV2(event) ? event.rawPath : event.path206 const urlPath = `https://${domainName}${path}`

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

lines 207–209why, one step back
207 const url = queryString ? `${urlPath}?${queryString}` : urlPath
208209 const headers = new Headers()

fix(lambda): Aws lambda cookies fix (#1613)Ochirjav Jargalsaikhanbefore adoption5f02375

line 210why, one step back
210 getCookies(event, headers)

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

lines 211–214why, one step back
211 for (const [k, v] of Object.entries(event.headers)) {
212 if (v) headers.set(k, v)213 }214

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

line 215why, one step back
215 const method = isProxyEventV2(event) ? event.requestContext.http.method : event.httpMethod

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

lines 216–227why, one step back
216 const requestInit: RequestInit = {
217 headers,218 method,219 }220221 if (event.body) {222 requestInit.body = event.isBase64Encoded ? Buffer.from(event.body, 'base64') : event.body223 }224225 return new Request(url, requestInit)226}227

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

lines 228–234why, one step back
228const extractQueryString = (event: LambdaEvent) => {
229 return isProxyEventV2(event)230 ? event.rawQueryString231 : Object.entries(event.queryStringParameters || {})232 .filter(([, value]) => value)233 .map(([key, value]) => `${key}=${value}`)234 .join('&')

fix(lambda): Aws lambda cookies fix (#1613)Ochirjav Jargalsaikhanbefore adoption5f02375

lines 235–236why, one step back
235}
236

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

line 237why, one step back
237const getCookies = (event: LambdaEvent, headers: Headers) => {

fix(lambda): Aws lambda cookies fix (#1613)Ochirjav Jargalsaikhanbefore adoption5f02375

lines 238–242why, one step back
238 if (isProxyEventV2(event) && Array.isArray(event.cookies)) {
239 headers.set('Cookie', event.cookies.join('; '))240 }241}242

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

line 243why, one step back
243const setCookies = (event: LambdaEvent, res: Response, result: APIGatewayProxyResult) => {

fix(lambda): Aws lambda cookies fix (#1613)Ochirjav Jargalsaikhanbefore adoption5f02375

lines 244–256why, one step back
244 if (res.headers.has('set-cookie')) {
245 const cookies = res.headers.get('set-cookie')?.split(', ')246 if (Array.isArray(cookies)) {247 if (isProxyEventV2(event)) {248 result.cookies = cookies249 } else {250 result.multiValueHeaders = {251 'set-cookie': cookies,252 }253 }254 res.headers.delete('set-cookie')255 }256 }

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

lines 257–258why, one step back
257}
258

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

line 259why, one step back
259const isProxyEventV2 = (event: LambdaEvent): event is APIGatewayProxyEventV2 => {

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

lines 260–263why, one step back
260 return Object.prototype.hasOwnProperty.call(event, 'rawPath')
261}262263export const isContentTypeBinary = (contentType: string) => {

fix: add missing tail to svg regex (#1788)Elias Brangebefore adoption521db64

line 264why, one step back
264 return !/^(text\/(plain|html|css|javascript|csv).*|application\/(.*json|.*xml).*|image\/svg\+xml.*)$/.test(

perf(hono-base): use `constructor.name` instead of `instanceof` (#1311)Yusuke Wadabefore adoption48a8729

lines 265–274why, one step back
265 contentType
266 )267}268269export const isContentEncodingBinary = (contentEncoding: string | null) => {270 if (contentEncoding === null) {271 return false272 }273 return /^(gzip|deflate|compress|br)/.test(contentEncoding)274}