File tree Expand file tree Collapse file tree 4 files changed +16
-4
lines changed
Expand file tree Collapse file tree 4 files changed +16
-4
lines changed Original file line number Diff line number Diff line change 22
33This example shows a set up where a TypeScript function is build and deployed to CloudFront Functions via Terraform!
44
5- The function does one thing: if the path starts with ` /foo* ` it will redirect it to ` /bar* ` .
5+ The function does two things:
6+
7+ - Adds a md5 hash of the path as a query parameter
8+ - If the path starts with ` /foo* ` it will redirect it to ` /bar* `
69
710This can be tested by visiting the URL below and seeing the path change from ` /foo/nebula.webp ` to ` /bar/nebula.webp ` !
811
Original file line number Diff line number Diff line change 1+ import crypto from "crypto"
2+
13import type { CloudFrontFunctionsEvent } from "aws-lambda"
24
35type Response = CloudFrontFunctionsEvent [ "request" ] | CloudFrontFunctionsEvent [ "response" ]
@@ -17,14 +19,21 @@ var redirects: Redirect[] = [
1719function handler ( event : CloudFrontFunctionsEvent ) : Response {
1820 var request = event . request
1921
22+ var hasher = crypto . createHash ( "sha1" )
23+ hasher . update ( request . uri )
24+
2025 var match = redirects . find ( ( redirect ) => redirect . from . test ( request . uri ) )
2126 if ( match ) {
2227 return {
2328 statusCode : 302 ,
2429 statusDescription : "Found" ,
2530 cookies : { } ,
2631 headers : {
27- location : { value : request . uri . replace ( match . from , match . to ) } ,
32+ location : {
33+ value : `${ request . uri . replace ( match . from , match . to ) } ?hash=${ hasher . digest (
34+ "hex" ,
35+ ) } `,
36+ } ,
2837 } ,
2938 }
3039 }
Original file line number Diff line number Diff line change 2121
2222 // Extra types
2323 "lib" : [" es2020" , " es2021" ],
24- "types" : []
24+ "types" : [" node " ]
2525 },
2626
2727 "include" : [" src/**/*.ts" ]
Original file line number Diff line number Diff line change 3030 "scripts" : {
3131 "dev" : " tsup --watch" ,
3232 "build" : " tsup" ,
33- "lint" : " eslint src" ,
33+ "lint" : " eslint src example/src " ,
3434 "test" : " vitest run" ,
3535 "test:dev" : " vitest" ,
3636 "typecheck" : " tsc --noEmit --project tsconfig.json" ,
You can’t perform that action at this time.
0 commit comments