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 fileTake it
feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
lines 1–11why, one step back1// @denoify-ignore2import 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 back12 key: string13 value: stringfeat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
lines 14–16why, one step back14}1516interface CloudFrontHeaders {feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
line 17why, one step back17 [name: string]: CloudFrontHeader[]feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
lines 18–20why, one step back18}1920interface CloudFrontCustomOrigin {feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 21–28why, one step back21 customHeaders: CloudFrontHeaders22 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 back29}30feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 31–36why, one step back31export interface CloudFrontRequest {32 clientIp: string33 headers: CloudFrontHeaders34 method: string35 querystring: string36 uri: stringfeat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
line 37why, one step back37 body?: {feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 38–42why, one step back38 inputTruncated: boolean39 action: string40 encoding: string41 data: string42 }feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
line 43why, one step back43 origin?: {feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 44–45why, one step back44 custom: CloudFrontCustomOrigin45 }feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
lines 46–47why, one step back46}47feat(adaptor): Lambda@Edge supports response callbacks (#1251)watanybefore adoptionba346a4
lines 48–53why, one step back48export interface CloudFrontResponse {49 headers: CloudFrontHeaders50 status: string51 statusDescription?: string52}53fix(lambda-edge) Adding Export Type CloudFrontResponse and CloudFrontConfig (#1263)watanybefore adoptioncbd5616
line 54why, one step back54export interface CloudFrontConfig {feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 55–58why, one step back55 distributionDomainName: string56 distributionId: string57 eventType: string58 requestId: stringfeat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
lines 59–62why, one step back59}6061interface CloudFrontEvent {62 cf: {feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 63–64why, one step back63 config: CloudFrontConfig64 request: CloudFrontRequestfeat(adaptor): Lambda@Edge supports response callbacks (#1251)watanybefore adoptionba346a4
line 65why, one step back65 response?: CloudFrontResponsefeat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
line 66why, one step back66 }feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
lines 67–68why, one step back67}68feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 69–70why, one step back69export interface CloudFrontEdgeEvent {70 Records: CloudFrontEvent[]feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
lines 71–72why, one step back71}72feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 73–78why, one step back73type CloudFrontContext = {}7475export interface Callback {76 (err: Error | null, result?: CloudFrontRequest | CloudFrontResult): void77}78fix(lambda-edge): update schema and refactor (#1230)watanybefore adoptionf37de25
line 79why, one step back79// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-generating-http-responses-in-requests.html#lambda-generating-http-responses-programming-modelfeat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
line 80why, one step back80interface CloudFrontResult {feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 81–82why, one step back81 status: string82 statusDescription?: stringfix(lambda-edge): update schema and refactor (#1230)watanybefore adoptionf37de25
line 83why, one step back83 headers?: {feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 84–90why, one step back84 [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 back91}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 back97const 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 back104105// 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 back107 return async (feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 108–110why, one step back108 event: CloudFrontEdgeEvent,109 context?: CloudFrontContext,110 callback?: Callbackfeat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
line 111why, one step back111 ): Promise<CloudFrontResult> => {feat(adaptor): Lambda@Edge supports response callbacks (#1251)watanybefore adoptionba346a4
line 112why, one step back112 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 back113 event,114 context,feat(adaptor): Lambda@Edge supports response callbacks (#1251)watanybefore adoptionba346a4
lines 115–117why, one step back115 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 back118 config: event.Records[0].cf.config,feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
line 119why, one step back119 request: event.Records[0].cf.request,feat(adaptor): Lambda@Edge supports response callbacks (#1251)watanybefore adoptionba346a4
line 120why, one step back120 response: event.Records[0].cf.response,feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
line 121why, one step back121 })feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
lines 122–126why, one step back122 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 back127 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 back129feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 130–131why, one step back130 return {131 status: res.status.toString(),feat(adaptor): Lambda@Edge supports response callbacks (#1251)watanybefore adoptionba346a4
line 132why, one step back132 headers: convertHeaders(res.headers),feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 133–134why, one step back133 body,134 }feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
lines 135–136why, one step back135}136feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
line 137why, one step back137const createRequest = (event: CloudFrontEdgeEvent) => {feat(adaptor): Lambda@Edge supports response callbacks (#1251)watanybefore adoptionba346a4
line 138why, one step back138 const queryString = event.Records[0].cf.request.querystringfeat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
lines 139–142why, one step back139 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 back143 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 back144 v.forEach((header) => headers.set(k, header.value))feat(adaptor): Lambda@Edge supports response callbacks (#1251)watanybefore adoptionba346a4
line 145why, one step back145 })feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
lines 146–147why, one step back146147 const requestBody = event.Records[0].cf.request.bodychore: add `format` script (#1334)watanybefore adoption954c06e
lines 148–149why, one step back148 const body =149 requestBody?.encoding === 'base64' && requestBody?.datafix: Safe modification of Buffer (#1238)watanybefore adoption301e142
line 150why, one step back150 ? Buffer.from(requestBody.data, 'base64')feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
line 151why, one step back151 : requestBody?.datafeat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
line 152why, one step back152feat(adaptor): Lambda@Edge supports response callbacks (#1251)watanybefore adoptionba346a4
lines 153–157why, one step back153 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 back158}159160export const isContentTypeBinary = (contentType: string) => {161 return !/^(text\/(plain|html|css|javascript|csv).*|application\/(.*json|.*xml).*|image\/svg\+xml)$/.test(162 contentType163 )164}