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 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–48why, one step back46}4748interface CloudFrontConfig {feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 49–52why, one step back49 distributionDomainName: string50 distributionId: string51 eventType: string52 requestId: stringfeat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
lines 53–56why, one step back53}5455interface CloudFrontEvent {56 cf: {feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 57–59why, one step back57 config: CloudFrontConfig58 request: CloudFrontRequest59 }feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
lines 60–61why, one step back60}61feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 62–63why, one step back62export interface CloudFrontEdgeEvent {63 Records: CloudFrontEvent[]feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
lines 64–65why, one step back64}65feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 66–71why, one step back66type CloudFrontContext = {}6768export interface Callback {69 (err: Error | null, result?: CloudFrontRequest | CloudFrontResult): void70}71fix(lambda-edge): update schema and refactor (#1230)watanybefore adoptionf37de25
line 72why, one step back72// 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 73why, one step back73interface CloudFrontResult {feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 74–75why, one step back74 status: string75 statusDescription?: stringfix(lambda-edge): update schema and refactor (#1230)watanybefore adoptionf37de25
line 76why, one step back76 headers?: {feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 77–83why, one step back77 [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 back84}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 back9091// 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 back93 return async (feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 94–96why, one step back94 event: CloudFrontEdgeEvent,95 context?: CloudFrontContext,96 callback?: Callbackfeat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
lines 97–98why, one step back97 ): 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 back99 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 back105 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 back110 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 back111feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
line 112why, one step back112 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 back113feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
line 114why, one step back114 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 back115feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 116–118why, one step back116 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 back119feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 120–124why, one step back120 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 back125}126feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
line 127why, one step back127const createRequest = (event: CloudFrontEdgeEvent) => {feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
lines 128–133why, one step back128 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 back134 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 back135 }feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
line 136why, one step back136 const method = event.Records[0].cf.request.methodfeat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
lines 137–142why, one step back137 const requestInit: RequestInit = {138 headers,139 method,140 }141142 const requestBody = event.Records[0].cf.request.bodyfeat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
lines 143–144why, one step back143 requestInit.body =144 requestBody?.encoding === 'base64' && requestBody?.datafix: Safe modification of Buffer (#1238)watanybefore adoption301e142
line 145why, one step back145 ? Buffer.from(requestBody.data, 'base64')feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
line 146why, one step back146 : requestBody?.datafeat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
lines 147–149why, one step back147 return new Request(url, requestInit)148}149feat(lambda-edge): pass the `context` and `callback` as env (#1229)Yusuke Wadabefore adoptiond48c160
line 150why, one step back150const extractQueryString = (event: CloudFrontEdgeEvent) => {feat(adapter): AWS Lambda Adapter supports requests via Lambda@edge (#1210)watanybefore adoptionedca699
lines 151–158why, one step back151 return event.Records[0].cf.request.querystring152}153154export const isContentTypeBinary = (contentType: string) => {155 return !/^(text\/(plain|html|css|javascript|csv).*|application\/(.*json|.*xml).*|image\/svg\+xml)$/.test(156 contentType157 )158}