Skip to content

Commit 4d2098d

Browse files
committed
add crypto usage to example
1 parent fec4b35 commit 4d2098d

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

example/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
This 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

710
This can be tested by visiting the URL below and seeing the path change from `/foo/nebula.webp` to `/bar/nebula.webp`!
811

example/src/handler.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import crypto from "crypto"
2+
13
import type { CloudFrontFunctionsEvent } from "aws-lambda"
24

35
type Response = CloudFrontFunctionsEvent["request"] | CloudFrontFunctionsEvent["response"]
@@ -17,14 +19,21 @@ var redirects: Redirect[] = [
1719
function 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
}

example/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
// Extra types
2323
"lib": ["es2020", "es2021"],
24-
"types": []
24+
"types": ["node"]
2525
},
2626

2727
"include": ["src/**/*.ts"]

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
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",

0 commit comments

Comments
 (0)