-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
45 lines (40 loc) · 1.63 KB
/
index.ts
File metadata and controls
45 lines (40 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { RequestSigner } from 'aws4'
import { CloudFrontRequestEvent, Callback, Context } from 'aws-lambda'
import { Buffer } from 'node:buffer'
export const handler = (
event: CloudFrontRequestEvent,
context: Context,
callback: Callback
) => {
const request = event.Records[0].cf.request
delete request.headers['x-forwarded-for'] // fails signature verification if included
delete request.headers['content-length'] // causing failures on post/put ...
// assumes <jib>.lambda-url.<region>.on.aws
const region = request.origin!.custom?.domainName.split('.')[2]
const signer = new RequestSigner({
method: request.method,
hostname: request.origin!.custom?.domainName,
region,
path:
request.uri +
(request.querystring.length > 0 ? '?' + request.querystring : ''),
service: 'lambda',
headers: Object.keys(request.headers).reduce((headers, key) => {
headers[key] = request.headers[key][0].value
return headers
}, {} as { [l: string]: string }),
body:['POST','PUT'].includes(request.method) && request.body?.data ?
Buffer.from(request.body?.data, request.body?.encoding === 'base64'?'base64':'ascii') // should always be base64 https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/edge-functions-restrictions.html
: ""
})
const { headers } = signer.sign()
for (let head in headers) {
request.headers[head.toLowerCase()] = [
{
key: head,
value: headers[head]!.toString(),
},
]
}
callback(null, request)
}