diff --git a/README.md b/README.md index 637da686..120b5034 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,6 @@ Construct | Description [feature-env-handlers](packages/feature-env-handlers) | Lambda@Edge handlers to support feature environments [geoip-redirect](packages/geoip-redirect) | [graphql-server](packages/graphql-server) | -[lambda-at-edge-handlers](packages/lambda-at-edge-handlers) | [prerender-fargate](packages/prerender-fargate) | [prerender-proxy](packages/prerender-proxy) | [rabbitmq](packages/rabbitmq) | diff --git a/packages/lambda-at-edge-handlers/index.ts b/packages/lambda-at-edge-handlers/index.ts deleted file mode 100644 index 3bade8fe..00000000 --- a/packages/lambda-at-edge-handlers/index.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { handler as PrerenderHandler } from "./lib/prerender"; -import { handler as PrerenderCacheControlHandler } from "./lib/cache-control"; -import { handler as PrerenderErrorResponseHandler } from "./lib/error-response"; -import { handler as PrerenderCheckHandler } from "./lib/prerender-check"; -import { handler as GeoIpRedirectHandler } from "./lib/redirect"; - -export { - PrerenderHandler, - PrerenderCacheControlHandler, - PrerenderErrorResponseHandler, - PrerenderCheckHandler, - GeoIpRedirectHandler, -}; diff --git a/packages/lambda-at-edge-handlers/lib/cache-control.ts b/packages/lambda-at-edge-handlers/lib/cache-control.ts deleted file mode 100644 index 66236483..00000000 --- a/packages/lambda-at-edge-handlers/lib/cache-control.ts +++ /dev/null @@ -1,23 +0,0 @@ -import "source-map-support/register"; -import { CloudFrontResponseEvent, CloudFrontResponse } from "aws-lambda"; -/* - Prerender cache control function - Consider associate this function as *origin response* function -*/ -export const handler = async ( - event: CloudFrontResponseEvent -): Promise => { - const cacheKey = process.env.PRERENDER_CACHE_KEY || "x-prerender-requestid"; - const cacheMaxAge = process.env.PRERENDER_CACHE_MAX_AGE || "0"; - const response = event.Records[0].cf.response; - - if (response.headers[`${cacheKey}`]) { - response.headers["Cache-Control"] = [ - { - key: "Cache-Control", - value: `max-age=${cacheMaxAge}`, - }, - ]; - } - return response; -}; diff --git a/packages/lambda-at-edge-handlers/lib/error-response.ts b/packages/lambda-at-edge-handlers/lib/error-response.ts deleted file mode 100644 index 4327a5d4..00000000 --- a/packages/lambda-at-edge-handlers/lib/error-response.ts +++ /dev/null @@ -1,62 +0,0 @@ -import "source-map-support/register"; -import { - CloudFrontRequest, - CloudFrontResponseEvent, - CloudFrontResponse, -} from "aws-lambda"; -import axios from "axios"; -import * as https from "https"; - -const FRONTEND_HOST = process.env.FRONTEND_HOST; -const PATH_PREFIX = process.env.PATH_PREFIX; - -// Create axios client outside of lambda function for re-use between calls -const instance = axios.create({ - timeout: 1000, - // Don't follow redirects - maxRedirects: 0, - // Only valid response codes are 200 - validateStatus: function (status) { - return status == 200; - }, - // keep connection alive so we don't constantly do SSL negotiation - httpsAgent: new https.Agent({ keepAlive: true }), -}); - -export const handler = ( - event: CloudFrontResponseEvent -): Promise => { - const response = event.Records[0].cf.response; - const request = event.Records[0].cf.request; - - if ( - response.status != "200" && - !request.headers["x-request-prerender"] && - request.uri != `${PATH_PREFIX}/index.html` - ) { - // Fetch default page and return body - return instance - .get(`https://${FRONTEND_HOST}${PATH_PREFIX}/index.html`) - .then(_res => { - // Commenting this as there is body is not defined in the CloudFrontResponse type - // response.body = res.data; - - response.headers["content-type"] = [ - { - key: "Content-Type", - value: "text/html", - }, - ]; - - // Remove content-length if set as this may be the value from the origin. - delete response.headers["content-length"]; - - return response; - }) - .catch(_err => { - return response; - }); - } - - return Promise.resolve(response); -}; diff --git a/packages/lambda-at-edge-handlers/lib/prerender-check.ts b/packages/lambda-at-edge-handlers/lib/prerender-check.ts deleted file mode 100644 index ef4d991d..00000000 --- a/packages/lambda-at-edge-handlers/lib/prerender-check.ts +++ /dev/null @@ -1,38 +0,0 @@ -import "source-map-support/register"; -import { CloudFrontRequest, CloudFrontRequestEvent } from "aws-lambda"; - -const IS_BOT = - /googlebot|Google-InspectionTool|Schema-Markup-Validator|SchemaBot|chrome-lighthouse|lighthouse|adsbot-google|Feedfetcher-Google|bingbot|yandex|baiduspider|Facebot|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator/i; -const IS_FILE = - /\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff|svg|eot)$/i; - -export const handler = async ( - event: CloudFrontRequestEvent -): Promise => { - const request = event.Records[0].cf.request; - - // If the request is from a bot, is not a file and is not from prerender - // then set the x-request-prerender header so the origin-request lambda function - // alters the origin to prerender.io - if ( - !IS_FILE.test(request.uri) && - IS_BOT.test(request.headers["user-agent"][0].value) && - // Check if the request is from Prerender service - !request.headers["x-prerender"] - ) { - // Consumed by OriginRequest Lambda@Edge to determine if this request needs to be send to Prerender service rather than other origins. - request.headers["x-request-prerender"] = [ - { - key: "x-request-prerender", - value: "true", - }, - ]; - - // Consumed by OriginRequest Lambda@Edge, only when x-request-prerender header is set. Prerender service will send request to this host. - request.headers["x-prerender-host"] = [ - { key: "X-Prerender-Host", value: request.headers.host[0].value }, - ]; - } - - return request; -}; diff --git a/packages/lambda-at-edge-handlers/lib/prerender.ts b/packages/lambda-at-edge-handlers/lib/prerender.ts deleted file mode 100644 index 14166866..00000000 --- a/packages/lambda-at-edge-handlers/lib/prerender.ts +++ /dev/null @@ -1,55 +0,0 @@ -import "source-map-support/register"; -import { - CloudFrontRequest, - CloudFrontRequestEvent, - CloudFrontResponse, -} from "aws-lambda"; - -const PRERENDER_TOKEN = process.env.PRERENDER_TOKEN - ? process.env.PRERENDER_TOKEN - : "undefined"; -const PATH_PREFIX = process.env.PATH_PREFIX; -const PRERENDER_URL = process.env.PRERENDER_URL - ? process.env.PRERENDER_URL - : "service.prerender.io"; - -export const handler = async ( - event: CloudFrontRequestEvent -): Promise => { - const request = event.Records[0].cf.request; - - // viewer-request function will determine whether we prerender or not - // if we should we add prerender as our custom origin - if (request.headers["x-request-prerender"]) { - // Cloudfront will alter the request for / to /index.html - // since it is defined as the default root object - // we do not want to do this when prerendering the homepage - if (request.uri === `${PATH_PREFIX}/index.html`) { - request.uri = `${PATH_PREFIX}/`; - } - - request.origin = { - custom: { - domainName: PRERENDER_URL, - port: 443, - protocol: "https", - readTimeout: 20, - keepaliveTimeout: 5, - sslProtocols: ["TLSv1", "TLSv1.1", "TLSv1.2"], - path: "/https%3A%2F%2F" + request.headers["x-prerender-host"][0].value, - customHeaders: { - "x-prerender-token": [ - { - key: "x-prerender-token", - value: PRERENDER_TOKEN, - }, - ], - }, - }, - }; - } else { - request.uri = `${PATH_PREFIX}/index.html`; - } - - return request; -}; diff --git a/packages/lambda-at-edge-handlers/lib/redirect.ts b/packages/lambda-at-edge-handlers/lib/redirect.ts deleted file mode 100644 index 400e22f9..00000000 --- a/packages/lambda-at-edge-handlers/lib/redirect.ts +++ /dev/null @@ -1,43 +0,0 @@ -import "source-map-support/register"; -import { - CloudFrontRequestEvent, - CloudFrontResponse, - CloudFrontRequest, -} from "aws-lambda"; - -const REDIRECT_HOST = process.env.REDIRECT_HOST || ""; -const SUPPORTED_REGIONS = new RegExp(process.env.SUPPORTED_REGIONS || ""); -const DEFAULT_REGION = process.env.DEFAULT_REGION || ""; - -export const handler = async ( - event: CloudFrontRequestEvent -): Promise => { - const request = event.Records[0].cf.request; - - let redirectURL = `https://${REDIRECT_HOST}/`; - if (request.headers["cloudfront-viewer-country"]) { - const countryCode = request.headers["cloudfront-viewer-country"][0].value; - if (SUPPORTED_REGIONS.test(countryCode)) { - redirectURL = `${redirectURL}${countryCode.toLowerCase()}${request.uri}`; - } else { - redirectURL = `${redirectURL}${DEFAULT_REGION.toLowerCase()}${ - request.uri - }`; - } - - return { - status: "302", - statusDescription: "Found", - headers: { - location: [ - { - key: "Location", - value: redirectURL, - }, - ], - }, - }; - } - - return request; -}; diff --git a/packages/lambda-at-edge-handlers/package-lock.json b/packages/lambda-at-edge-handlers/package-lock.json deleted file mode 100644 index 7430fdfc..00000000 --- a/packages/lambda-at-edge-handlers/package-lock.json +++ /dev/null @@ -1,85 +0,0 @@ -{ - "name": "@aligent/cdk-lambda-at-edge-handlers", - "version": "0.1.1", - "lockfileVersion": 2, - "requires": true, - "packages": { - "": { - "name": "@aligent/cdk-lambda-at-edge-handlers", - "version": "0.1.1", - "license": "GPL-3.0-only", - "dependencies": { - "@types/aws-lambda": "^8.10.77", - "esbuild": "0.12.15", - "source-map-support": "^0.5.16" - } - }, - "node_modules/@types/aws-lambda": { - "version": "8.10.109", - "resolved": "https://registry.npmjs.org/@types/aws-lambda/-/aws-lambda-8.10.109.tgz", - "integrity": "sha512-/ME92FneNyXQzrAfcnQQlW1XkCZGPDlpi2ao1MJwecN+6SbeonKeggU8eybv1DfKli90FAVT1MlIZVXfwVuCyg==" - }, - "node_modules/buffer-from": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", - "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==" - }, - "node_modules/esbuild": { - "version": "0.12.15", - "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.12.15.tgz", - "integrity": "sha512-72V4JNd2+48eOVCXx49xoSWHgC3/cCy96e7mbXKY+WOWghN00cCmlGnwVLRhRHorvv0dgCyuMYBZlM2xDM5OQw==", - "hasInstallScript": true, - "bin": { - "esbuild": "bin/esbuild" - } - }, - "node_modules/source-map": { - "version": "0.6.1", - "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", - "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/source-map-support": { - "version": "0.5.21", - "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.21.tgz", - "integrity": "sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==", - "dependencies": { - "buffer-from": "^1.0.0", - "source-map": "^0.6.0" - } - } - }, - "dependencies": { - "@types/aws-lambda": { - "version": "8.10.109", - "resolved": "https://registry.npmjs.org/@types/aws-lambda/-/aws-lambda-8.10.109.tgz", - "integrity": "sha512-/ME92FneNyXQzrAfcnQQlW1XkCZGPDlpi2ao1MJwecN+6SbeonKeggU8eybv1DfKli90FAVT1MlIZVXfwVuCyg==" - }, - "buffer-from": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", - "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==" - }, - "esbuild": { - "version": "0.12.15", - "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.12.15.tgz", - "integrity": "sha512-72V4JNd2+48eOVCXx49xoSWHgC3/cCy96e7mbXKY+WOWghN00cCmlGnwVLRhRHorvv0dgCyuMYBZlM2xDM5OQw==" - }, - "source-map": { - "version": "0.6.1", - "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", - "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==" - }, - "source-map-support": { - "version": "0.5.21", - "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.21.tgz", - "integrity": "sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==", - "requires": { - "buffer-from": "^1.0.0", - "source-map": "^0.6.0" - } - } - } -} diff --git a/packages/lambda-at-edge-handlers/package.json b/packages/lambda-at-edge-handlers/package.json deleted file mode 100644 index 3a6d3933..00000000 --- a/packages/lambda-at-edge-handlers/package.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "name": "@aligent/cdk-lambda-at-edge-handlers", - "version": "0.1.1", - "description": "A Cloudfront Lambda@Edge handlers powered by Middy", - "main": "index.js", - "scripts": { - "build": "tsc", - "watch": "tsc -w", - "test": "echo \"No test specified\" && exit 0" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/aligent/aws-cdk-constructs.git" - }, - "license": "GPL-3.0-only", - "bugs": { - "url": "https://github.com/aligent/cdk-constructs/issues" - }, - "homepage": "https://github.com/aligent/cdk-constructs#readme", - "devDependencies": { - "@types/aws-lambda": "^8.10.122" - }, - "dependencies": { - "source-map-support": "^0.5.21", - "axios": "^1.5.1" - } -} diff --git a/packages/lambda-at-edge-handlers/tsconfig.json b/packages/lambda-at-edge-handlers/tsconfig.json deleted file mode 100644 index 4082f16a..00000000 --- a/packages/lambda-at-edge-handlers/tsconfig.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "extends": "../../tsconfig.json" -}