Skip to content
Myr

handler.ts

This file

Path

src/adapter/lambda-edge/handler.ts

164 lines · 4.1 KiB · TypeScript

Last changed by

chore: add `format` script (#1334)watanybefore adoption954c06e

History of this file

Take it

feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699

lines 1–11why, one step back
1// @denoify-ignore
2import crypto from 'crypto'3import type { Hono } from '../../hono'45import { encodeBase64 } from '../../utils/encode'67// eslint-disable-next-line @typescript-eslint/ban-ts-comment8// @ts-ignore9globalThis.crypto ??= crypto1011interface CloudFrontHeader {

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

lines 12–13why, one step back
12 key: string
13 value: string

feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699

lines 14–16why, one step back
14}
1516interface CloudFrontHeaders {

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

line 17why, one step back
17 [name: string]: CloudFrontHeader[]

feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699

lines 18–20why, one step back
18}
1920interface CloudFrontCustomOrigin {

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

lines 21–28why, one step back
21 customHeaders: CloudFrontHeaders
22 domainName: string23 keepaliveTimeout: number24 path: string25 port: number26 protocol: string27 readTimeout: number28 sslProtocols: string[]

feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699

lines 29–30why, one step back
29}
30

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

lines 31–36why, one step back
31export interface CloudFrontRequest {
32 clientIp: string33 headers: CloudFrontHeaders34 method: string35 querystring: string36 uri: string

feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699

line 37why, one step back
37 body?: {

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

lines 38–42why, one step back
38 inputTruncated: boolean
39 action: string40 encoding: string41 data: string42 }

feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699

line 43why, one step back
43 origin?: {

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

lines 44–45why, one step back
44 custom: CloudFrontCustomOrigin
45 }

feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699

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

feat(adaptor): Lambda@Edge supports response callbacks (#1251)watanybefore adoptionba346a4

lines 48–53why, one step back
48export interface CloudFrontResponse {
49 headers: CloudFrontHeaders50 status: string51 statusDescription?: string52}53

fix(lambda-edge) Adding Export Type CloudFrontResponse and CloudFrontConfig (#1263)watanybefore adoptioncbd5616

line 54why, one step back
54export interface CloudFrontConfig {

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

lines 55–58why, one step back
55 distributionDomainName: string
56 distributionId: string57 eventType: string58 requestId: string

feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699

lines 59–62why, one step back
59}
6061interface CloudFrontEvent {62 cf: {

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

lines 63–64why, one step back
63 config: CloudFrontConfig
64 request: CloudFrontRequest

feat(adaptor): Lambda@Edge supports response callbacks (#1251)watanybefore adoptionba346a4

line 65why, one step back
65 response?: CloudFrontResponse

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

line 66why, one step back
66 }

feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699

lines 67–68why, one step back
67}
68

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

lines 69–70why, one step back
69export interface CloudFrontEdgeEvent {
70 Records: CloudFrontEvent[]

feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699

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

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

lines 73–78why, one step back
73type CloudFrontContext = {}
7475export interface Callback {76 (err: Error | null, result?: CloudFrontRequest | CloudFrontResult): void77}78

fix(lambda-edge): update schema and refactor (#1230)watanybefore adoptionf37de25

line 79why, one step back
79// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-generating-http-responses-in-requests.html#lambda-generating-http-responses-programming-model

feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699

line 80why, one step back
80interface CloudFrontResult {

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

lines 81–82why, one step back
81 status: string
82 statusDescription?: string

fix(lambda-edge): update schema and refactor (#1230)watanybefore adoptionf37de25

line 83why, one step back
83 headers?: {

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

lines 84–90why, one step back
84 [header: string]: {
85 key: string86 value: string87 }[]88 }89 body?: string90 bodyEncoding?: 'text' | 'base64'

feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699

lines 91–96why, one step back
91}
9293/**94 * Accepts events from 'Lambda@Edge' event95 * https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html96 */

feat(adaptor): Lambda@Edge supports response callbacks (#1251)watanybefore adoptionba346a4

lines 97–103why, one step back
97const convertHeaders = (headers: Headers): CloudFrontHeaders => {
98 const cfHeaders: CloudFrontHeaders = {}99 headers.forEach((value, key) => {100 cfHeaders[key.toLowerCase()] = [{ key: key.toLowerCase(), value }]101 })102 return cfHeaders103}

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

lines 104–106why, one step back
104
105// eslint-disable-next-line @typescript-eslint/no-explicit-any106export const handle = (app: Hono<any>) => {

feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699

line 107why, one step back
107 return async (

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

lines 108–110why, one step back
108 event: CloudFrontEdgeEvent,
109 context?: CloudFrontContext,110 callback?: Callback

feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699

line 111why, one step back
111 ): Promise<CloudFrontResult> => {

feat(adaptor): Lambda@Edge supports response callbacks (#1251)watanybefore adoptionba346a4

line 112why, one step back
112 const res = await app.fetch(createRequest(event), {

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

lines 113–114why, one step back
113 event,
114 context,

feat(adaptor): Lambda@Edge supports response callbacks (#1251)watanybefore adoptionba346a4

lines 115–117why, one step back
115 callback: (err: Error | null, result?: CloudFrontResult | CloudFrontRequest) => {
116 callback?.(err, result)117 },

fix(lambda-edge) Adding Export Type CloudFrontResponse and CloudFrontConfig (#1263)watanybefore adoptioncbd5616

line 118why, one step back
118 config: event.Records[0].cf.config,

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

line 119why, one step back
119 request: event.Records[0].cf.request,

feat(adaptor): Lambda@Edge supports response callbacks (#1251)watanybefore adoptionba346a4

line 120why, one step back
120 response: event.Records[0].cf.response,

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

line 121why, one step back
121 })

feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699

lines 122–126why, one step back
122 return createResult(res)
123 }124}125126const createResult = async (res: Response): Promise<CloudFrontResult> => {

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

lines 127–128why, one step back
127 const isBase64Encoded = isContentTypeBinary(res.headers.get('content-type') || '')
128 const body = isBase64Encoded ? encodeBase64(await res.arrayBuffer()) : await res.text()

feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699

line 129why, one step back
129

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

lines 130–131why, one step back
130 return {
131 status: res.status.toString(),

feat(adaptor): Lambda@Edge supports response callbacks (#1251)watanybefore adoptionba346a4

line 132why, one step back
132 headers: convertHeaders(res.headers),

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

lines 133–134why, one step back
133 body,
134 }

feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699

lines 135–136why, one step back
135}
136

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

line 137why, one step back
137const createRequest = (event: CloudFrontEdgeEvent) => {

feat(adaptor): Lambda@Edge supports response callbacks (#1251)watanybefore adoptionba346a4

line 138why, one step back
138 const queryString = event.Records[0].cf.request.querystring

feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699

lines 139–142why, one step back
139 const urlPath = `https://${event.Records[0].cf.config.distributionDomainName}${event.Records[0].cf.request.uri}`
140 const url = queryString ? `${urlPath}?${queryString}` : urlPath141142 const headers = new Headers()

feat(adaptor): Lambda@Edge supports response callbacks (#1251)watanybefore adoptionba346a4

line 143why, one step back
143 Object.entries(event.Records[0].cf.request.headers).forEach(([k, v]) => {

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

line 144why, one step back
144 v.forEach((header) => headers.set(k, header.value))

feat(adaptor): Lambda@Edge supports response callbacks (#1251)watanybefore adoptionba346a4

line 145why, one step back
145 })

feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699

lines 146–147why, one step back
146
147 const requestBody = event.Records[0].cf.request.body

chore: add `format` script (#1334)watanybefore adoption954c06e

lines 148–149why, one step back
148 const body =
149 requestBody?.encoding === 'base64' && requestBody?.data

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

line 150why, one step back
150 ? Buffer.from(requestBody.data, 'base64')

feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160

line 151why, one step back
151 : requestBody?.data

feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699

line 152why, one step back
152

feat(adaptor): Lambda@Edge supports response callbacks (#1251)watanybefore adoptionba346a4

lines 153–157why, one step back
153 return new Request(url, {
154 headers,155 method: event.Records[0].cf.request.method,156 body,157 })

feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699

lines 158–164why, one step back
158}
159160export const isContentTypeBinary = (contentType: string) => {161 return !/^(text\/(plain|html|css|javascript|csv).*|application\/(.*json|.*xml).*|image\/svg\+xml)$/.test(162 contentType163 )164}