Skip to content
Myr

handler.ts

This file

Path

src/adapter/lambda-edge/handler.ts

158 lines · 3.9 KiB · TypeScript

Last changed by

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

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–48why, one step back
46}
4748interface CloudFrontConfig {

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

lines 49–52why, one step back
49 distributionDomainName: string
50 distributionId: string51 eventType: string52 requestId: string

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

lines 53–56why, one step back
53}
5455interface CloudFrontEvent {56 cf: {

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

lines 57–59why, one step back
57 config: CloudFrontConfig
58 request: CloudFrontRequest59 }

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

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

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

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

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

lines 64–65why, one step back
64}
65

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

lines 66–71why, one step back
66type CloudFrontContext = {}
6768export interface Callback {69 (err: Error | null, result?: CloudFrontRequest | CloudFrontResult): void70}71

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

line 72why, one step back
72// 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 73why, one step back
73interface CloudFrontResult {

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

lines 74–75why, one step back
74 status: string
75 statusDescription?: string

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

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

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

lines 77–83why, one step back
77 [header: string]: {
78 key: string79 value: string80 }[]81 }82 body?: string83 bodyEncoding?: 'text' | 'base64'

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

lines 84–89why, one step back
84}
8586/**87 * Accepts events from 'Lambda@Edge' event88 * https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html89 */

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

lines 90–92why, one step back
90
91// eslint-disable-next-line @typescript-eslint/no-explicit-any92export const handle = (app: Hono<any>) => {

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

line 93why, one step back
93 return async (

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

lines 94–96why, one step back
94 event: CloudFrontEdgeEvent,
95 context?: CloudFrontContext,96 callback?: Callback

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

lines 97–98why, one step back
97 ): Promise<CloudFrontResult> => {
98 const req = createRequest(event)

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

lines 99–104why, one step back
99 const res = await app.fetch(req, {
100 event,101 context,102 callback,103 request: event.Records[0].cf.request,104 })

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

lines 105–109why, one step back
105 return createResult(res)
106 }107}108109const createResult = async (res: Response): Promise<CloudFrontResult> => {

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

line 110why, one step back
110 const isBase64Encoded = isContentTypeBinary(res.headers.get('content-type') || '')

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

line 111why, one step back
111

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

line 112why, one step back
112 const body = isBase64Encoded ? encodeBase64(await res.arrayBuffer()) : await res.text()

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

line 113why, one step back
113

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

line 114why, one step back
114 const headers: { [header: string]: { key: string; value: string }[] } = {}

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

line 115why, one step back
115

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

lines 116–118why, one step back
116 res.headers.forEach((value, key) => {
117 headers[key.toLowerCase()] = [{ key: key.toLowerCase(), value }]118 })

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

line 119why, one step back
119

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

lines 120–124why, one step back
120 return {
121 status: res.status.toString(),122 headers,123 body,124 }

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

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

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

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

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

lines 128–133why, one step back
128 const queryString = extractQueryString(event)
129 const urlPath = `https://${event.Records[0].cf.config.distributionDomainName}${event.Records[0].cf.request.uri}`130 const url = queryString ? `${urlPath}?${queryString}` : urlPath131132 const headers = new Headers()133 for (const [k, v] of Object.entries(event.Records[0].cf.request.headers)) {

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

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

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

line 135why, one step back
135 }

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

line 136why, one step back
136 const method = event.Records[0].cf.request.method

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

lines 137–142why, one step back
137 const requestInit: RequestInit = {
138 headers,139 method,140 }141142 const requestBody = event.Records[0].cf.request.body

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

lines 143–144why, one step back
143 requestInit.body =
144 requestBody?.encoding === 'base64' && requestBody?.data

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

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

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

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

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

lines 147–149why, one step back
147 return new Request(url, requestInit)
148}149

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

line 150why, one step back
150const extractQueryString = (event: CloudFrontEdgeEvent) => {

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

lines 151–158why, one step back
151 return event.Records[0].cf.request.querystring
152}153154export const isContentTypeBinary = (contentType: string) => {155 return !/^(text\/(plain|html|css|javascript|csv).*|application\/(.*json|.*xml).*|image\/svg\+xml)$/.test(156 contentType157 )158}