diff --git a/package-lock.json b/package-lock.json index e7f40d92..f8379a32 100644 --- a/package-lock.json +++ b/package-lock.json @@ -42,6 +42,10 @@ "resolved": "packages/geoip-redirect", "link": true }, + "node_modules/@aligent/cdk-prerender-proxy": { + "resolved": "packages/prerender-proxy", + "link": true + }, "node_modules/@aligent/cdk-rabbitmq": { "resolved": "packages/rabbitmq", "link": true @@ -2159,6 +2163,11 @@ "node": ">=8" } }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, "node_modules/aws-cdk": { "version": "2.97.0", "resolved": "https://registry.npmjs.org/aws-cdk/-/aws-cdk-2.97.0.tgz", @@ -2514,6 +2523,16 @@ "node": ">= 6" } }, + "node_modules/axios": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.5.1.tgz", + "integrity": "sha512-Q28iYCWzNHjAm+yEAot5QaAMxhMghWLFVf7rRdwhUI+c2jix2DUXjAHXVi+s1ibs3mjPO/cCgbA++3BjD0vP/A==", + "dependencies": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/babel-jest": { "version": "29.7.0", "resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-29.7.0.tgz", @@ -2848,6 +2867,17 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", "dev": true }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -2955,6 +2985,14 @@ "node": ">=0.10.0" } }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/detect-newline": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/detect-newline/-/detect-newline-3.1.0.tgz", @@ -3489,6 +3527,38 @@ "integrity": "sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ==", "dev": true }, + "node_modules/follow-redirects": { + "version": "1.15.3", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.3.tgz", + "integrity": "sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -4671,6 +4741,25 @@ "node": ">=8.6" } }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/mimic-fn": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", @@ -5061,6 +5150,11 @@ "node": ">= 6" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/punycode": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.0.tgz", @@ -5887,6 +5981,28 @@ "typescript": "~5.2.2" } }, + "packages/prerender-proxy": { + "name": "@aligent/cdk-prerender-proxy", + "version": "2.0.0", + "license": "GPL-3.0-only", + "dependencies": { + "@types/aws-lambda": "^8.10.122", + "aws-cdk-lib": "2.97.0", + "axios": "^1.5.1", + "constructs": "^10.0.0", + "esbuild": "^0.17.0", + "source-map-support": "^0.5.21" + }, + "devDependencies": { + "@types/jest": "^29.5.5", + "@types/node": "20.6.3", + "aws-cdk": "2.97.0", + "jest": "^29.7.0", + "ts-jest": "^29.1.1", + "ts-node": "^10.9.1", + "typescript": "~5.2.2" + } + }, "packages/rabbitmq": { "name": "@aligent/cdk-rabbitmq", "version": "2.0.0", diff --git a/packages/prerender-proxy/README.md b/packages/prerender-proxy/README.md new file mode 100644 index 00000000..7f8503e2 --- /dev/null +++ b/packages/prerender-proxy/README.md @@ -0,0 +1,26 @@ +# Prerender Proxy + +This library provides two function constructs and a construct that creates two Lambda@Edge functions to use prerender.io as a Cloudfront Origin for site indexers (Google, Bing, etc). + +The `prerender-check` is a `viewer-request` function that will check if a requester is from a indexer and if it is adds a header so that the second function `prerender` (`origin-request`) will alter the origin to prerender. + +The `prerender` will function also make a HEAD request to a nominated backend to detect 301 and 302 redirects and if so forward them on to the frontend. This ensures that your SEO rankings are not penalized by having multiple pages at the same URL. + +These functions are intended to be added to an existing Cloudfront + +## Cache Control + +The intention of `cache-control` function is to avoid/control CloudFront Caches for `prerender` bot. +This function to be associated with CloudFront's `origin response` + +## Props + +`redirectBackendOrigin`: The backend origin to make the HEAD request to +`redirectFrontendHost`: This hostname is used to replace the backend host for any redirects that contain the backend host +`prerenderToken`: Your prerender.io authentication token +`prerenderUrl`: The URL of your Prerender service (optional: defaults to prerender.io) +`pathPrefix`: A prefix path (optional) +`cacheControlProps.cacheKey`: An optional parameter, which is to set `PRERENDER_CACHE_KEY` to be used in *[prerender CloudFront cache control function]* +`cacheControlProps.maxAge`: An optional parameter, which is to set `PRERENDER_CACHE_MAX_AGE` to be used in *[prerender CloudFront cache control function]* + +[prerender CloudFront cache control function]:lib/handlers/cache-control.ts \ No newline at end of file diff --git a/packages/prerender-proxy/index.ts b/packages/prerender-proxy/index.ts new file mode 100644 index 00000000..fc41368b --- /dev/null +++ b/packages/prerender-proxy/index.ts @@ -0,0 +1,29 @@ +import { + PrerenderLambda, + PrerenderLambdaProps, +} from "./lib/prerender-lambda-construct"; +import { + PrerenderFunction, + PrerenderFunctionOptions, +} from "./lib/prerender-construct"; +import { PrerenderCheckFunction } from "./lib/prerender-check-construct"; +import { + ErrorResponseFunction, + ErrorResponseFunctionOptions, +} from "./lib/error-response-construct"; +import { + CloudFrontCacheControl, + CloudFrontCacheControlOptions, +} from "./lib/prerender-cf-cache-control-construct"; + +export { + PrerenderLambda, + PrerenderFunction, + PrerenderCheckFunction, + ErrorResponseFunction, + CloudFrontCacheControl, + CloudFrontCacheControlOptions, + ErrorResponseFunctionOptions, + PrerenderFunctionOptions, + PrerenderLambdaProps, +}; diff --git a/packages/prerender-proxy/lib/error-response-construct.ts b/packages/prerender-proxy/lib/error-response-construct.ts new file mode 100644 index 00000000..c84bb68c --- /dev/null +++ b/packages/prerender-proxy/lib/error-response-construct.ts @@ -0,0 +1,54 @@ +import { AssetHashType, DockerImage } from "aws-cdk-lib"; +import { EdgeFunction } from "aws-cdk-lib/aws-cloudfront/lib/experimental"; +import { Code, IVersion, Runtime, Version } from "aws-cdk-lib/aws-lambda"; +import { Construct } from "constructs"; +import { join } from "path"; +import { Esbuild } from "@aligent/esbuild"; + +export interface ErrorResponseFunctionOptions { + pathPrefix?: string; +} + +export class ErrorResponseFunction extends Construct { + readonly edgeFunction: EdgeFunction; + + constructor( + scope: Construct, + id: string, + options: ErrorResponseFunctionOptions + ) { + super(scope, id); + + const command = [ + "sh", + "-c", + 'echo "Docker build not supported. Please install esbuild."', + ]; + + this.edgeFunction = new EdgeFunction(this, `${id}-error-response-fn`, { + code: Code.fromAsset(join(__dirname, "handlers"), { + assetHashType: AssetHashType.OUTPUT, + bundling: { + command, + image: DockerImage.fromRegistry("busybox"), + local: new Esbuild({ + entryPoints: [join(__dirname, "handlers/error-response.ts")], + define: { + "process.env.PATH_PREFIX": options.pathPrefix ?? "", + }, + }), + }, + }), + runtime: Runtime.NODEJS_18_X, + handler: "error-response.handler", + }); + } + + public getFunctionVersion(): IVersion { + return Version.fromVersionArn( + this, + "error-response-fn-version", + this.edgeFunction.currentVersion.edgeArn + ); + } +} diff --git a/packages/prerender-proxy/lib/handlers/cache-control.ts b/packages/prerender-proxy/lib/handlers/cache-control.ts new file mode 100644 index 00000000..8543a6ef --- /dev/null +++ b/packages/prerender-proxy/lib/handlers/cache-control.ts @@ -0,0 +1,20 @@ +import "source-map-support/register"; +import { CloudFrontResponseEvent, CloudFrontResponse } from "aws-lambda"; + +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/prerender-proxy/lib/handlers/error-response.ts b/packages/prerender-proxy/lib/handlers/error-response.ts new file mode 100644 index 00000000..7c95b77a --- /dev/null +++ b/packages/prerender-proxy/lib/handlers/error-response.ts @@ -0,0 +1,61 @@ +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 => { + 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/prerender-proxy/lib/handlers/prerender-check.ts b/packages/prerender-proxy/lib/handlers/prerender-check.ts new file mode 100644 index 00000000..879c1d25 --- /dev/null +++ b/packages/prerender-proxy/lib/handlers/prerender-check.ts @@ -0,0 +1,33 @@ +import "source-map-support/register"; +import { CloudFrontRequest, CloudFrontRequestEvent } from "aws-lambda"; + +const IS_BOT = + /googlebot|Google-InspectionTool|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_BOT.test(request.headers["user-agent"][0].value)) { + if (!IS_FILE.test(request.uri) && !request.headers["x-prerender"]) { + request.headers["x-request-prerender"] = [ + { + key: "x-request-prerender", + value: "true", + }, + ]; + + request.headers["x-prerender-host"] = [ + { key: "X-Prerender-Host", value: request.headers.host[0].value }, + ]; + } + } + + return request; +}; diff --git a/packages/prerender-proxy/lib/handlers/prerender.ts b/packages/prerender-proxy/lib/handlers/prerender.ts new file mode 100644 index 00000000..675fb048 --- /dev/null +++ b/packages/prerender-proxy/lib/handlers/prerender.ts @@ -0,0 +1,48 @@ +import { CloudFrontRequest, CloudFrontRequestEvent, CloudFrontResponse } from "aws-lambda"; +import "source-map-support/register"; + + +const PRERENDER_TOKEN = process.env.PRERENDER_TOKEN; +const PATH_PREFIX = process.env.PATH_PREFIX; +const PRERENDER_URL = process.env.PRERENDER_URL; + +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: 60, + 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/prerender-proxy/lib/prerender-cf-cache-control-construct.ts b/packages/prerender-proxy/lib/prerender-cf-cache-control-construct.ts new file mode 100644 index 00000000..fbc52713 --- /dev/null +++ b/packages/prerender-proxy/lib/prerender-cf-cache-control-construct.ts @@ -0,0 +1,58 @@ +import { AssetHashType, DockerImage } from "aws-cdk-lib"; +import { EdgeFunction } from "aws-cdk-lib/aws-cloudfront/lib/experimental"; +import { Code, IVersion, Runtime, Version } from "aws-cdk-lib/aws-lambda"; +import { Construct } from "constructs"; +import { join } from "path"; +import { Esbuild } from "@aligent/esbuild"; + +export interface CloudFrontCacheControlOptions { + cacheKey?: string; + maxAge?: number; +} + +export class CloudFrontCacheControl extends Construct { + readonly edgeFunction: EdgeFunction; + + constructor( + scope: Construct, + id: string, + options?: CloudFrontCacheControlOptions + ) { + super(scope, id); + + const command = [ + "sh", + "-c", + 'echo "Docker build not supported. Please install esbuild."', + ]; + + this.edgeFunction = new EdgeFunction(this, `${id}-cache-control-fn`, { + code: Code.fromAsset(join(__dirname, "handlers"), { + assetHashType: AssetHashType.OUTPUT, + bundling: { + command, + image: DockerImage.fromRegistry("busybox"), + local: new Esbuild({ + entryPoints: [join(__dirname, "handlers/cache-control.ts")], + define: { + "process.env.PRERENDER_CACHE_KEY": + options?.cacheKey ?? "x-prerender-requestid", + "process.env.PRERENDER_CACHE_MAX_AGE": + String(options?.maxAge) ?? "0", + }, + }), + }, + }), + runtime: Runtime.NODEJS_18_X, + handler: "cache-control.handler", + }); + } + + public getFunctionVersion(): IVersion { + return Version.fromVersionArn( + this, + "cache-control-fn-version", + this.edgeFunction.currentVersion.edgeArn + ); + } +} diff --git a/packages/prerender-proxy/lib/prerender-check-construct.ts b/packages/prerender-proxy/lib/prerender-check-construct.ts new file mode 100644 index 00000000..9d127088 --- /dev/null +++ b/packages/prerender-proxy/lib/prerender-check-construct.ts @@ -0,0 +1,43 @@ +import { AssetHashType, DockerImage } from "aws-cdk-lib"; +import { EdgeFunction } from "aws-cdk-lib/aws-cloudfront/lib/experimental"; +import { Code, IVersion, Runtime, Version } from "aws-cdk-lib/aws-lambda"; +import { Construct } from "constructs"; +import { join } from "path"; +import { Esbuild } from "@aligent/esbuild"; + +export class PrerenderCheckFunction extends Construct { + readonly edgeFunction: EdgeFunction; + + constructor(scope: Construct, id: string) { + super(scope, id); + + const command = [ + "sh", + "-c", + 'echo "Docker build not supported. Please install esbuild."', + ]; + + this.edgeFunction = new EdgeFunction(this, `${id}-prerender-check-fn`, { + code: Code.fromAsset(join(__dirname, "handlers"), { + assetHashType: AssetHashType.OUTPUT, + bundling: { + command, + image: DockerImage.fromRegistry("busybox"), + local: new Esbuild({ + entryPoints: [join(__dirname, "handlers/prerender-check.ts")], + }), + }, + }), + runtime: Runtime.NODEJS_18_X, + handler: "prerender-check.handler", + }); + } + + public getFunctionVersion(): IVersion { + return Version.fromVersionArn( + this, + "prerender-check-fn-version", + this.edgeFunction.currentVersion.edgeArn + ); + } +} diff --git a/packages/prerender-proxy/lib/prerender-construct.ts b/packages/prerender-proxy/lib/prerender-construct.ts new file mode 100644 index 00000000..2707384a --- /dev/null +++ b/packages/prerender-proxy/lib/prerender-construct.ts @@ -0,0 +1,55 @@ +import { AssetHashType, DockerImage } from "aws-cdk-lib"; +import { EdgeFunction } from "aws-cdk-lib/aws-cloudfront/lib/experimental"; +import { Code, IVersion, Runtime, Version } from "aws-cdk-lib/aws-lambda"; +import { Construct } from "constructs"; +import { join } from "path"; +import { Esbuild } from "@aligent/esbuild"; + +export interface PrerenderFunctionOptions { + prerenderToken: string; + prerenderUrl?: string; + pathPrefix?: string; +} + +export class PrerenderFunction extends Construct { + readonly edgeFunction: EdgeFunction; + + constructor(scope: Construct, id: string, options: PrerenderFunctionOptions) { + super(scope, id); + + const command = [ + "sh", + "-c", + 'echo "Docker build not supported. Please install esbuild."', + ]; + + this.edgeFunction = new EdgeFunction(this, `${id}-prerender-fn`, { + code: Code.fromAsset(join(__dirname, "handlers"), { + assetHashType: AssetHashType.OUTPUT, + bundling: { + command, + image: DockerImage.fromRegistry("busybox"), + local: new Esbuild({ + entryPoints: [join(__dirname, "handlers/prerender.ts")], + define: { + "process.env.PRERENDER_TOKEN": options.prerenderToken, + "process.env.PATH_PREFIX": options.pathPrefix ?? "", + "process.env.PRERENDER_URL": + options.prerenderUrl ?? "service.prerender.io", + }, + }), + }, + }), + runtime: Runtime.NODEJS_18_X, + handler: "prerender.handler", + }); + } + + public getFunctionVersion(): IVersion { + return Version.fromVersionArn( + this, + "prerender-fn-version", + this.edgeFunction.currentVersion.edgeArn + ); + } +} diff --git a/packages/prerender-proxy/lib/prerender-lambda-construct.ts b/packages/prerender-proxy/lib/prerender-lambda-construct.ts new file mode 100644 index 00000000..4f0959fe --- /dev/null +++ b/packages/prerender-proxy/lib/prerender-lambda-construct.ts @@ -0,0 +1,48 @@ +import { Construct } from "constructs"; +import { + CloudFrontCacheControl, + CloudFrontCacheControlOptions, +} from "./prerender-cf-cache-control-construct"; +import { PrerenderCheckFunction } from "./prerender-check-construct"; +import { PrerenderFunction } from "./prerender-construct"; +import { ErrorResponseFunction } from "./error-response-construct"; + +export interface PrerenderLambdaProps { + prerenderToken: string; + exclusionExpression?: string; + cacheControlProps?: CloudFrontCacheControlOptions; +} + +export class PrerenderLambda extends Construct { + readonly prerenderCheckFunction: PrerenderCheckFunction; + readonly prerenderFunction: PrerenderFunction; + readonly errorResponseFunction: ErrorResponseFunction; + readonly cacheControlFunction: CloudFrontCacheControl; + + constructor(scope: Construct, id: string, props: PrerenderLambdaProps) { + super(scope, id); + + this.prerenderCheckFunction = new PrerenderCheckFunction( + this, + "PrerenderViewerRequest" + ); + + this.prerenderFunction = new PrerenderFunction( + this, + "PrerenderOriginRequest", + props + ); + + this.errorResponseFunction = new ErrorResponseFunction( + this, + "ErrorResponse", + {} + ); + + this.cacheControlFunction = new CloudFrontCacheControl( + this, + "PrerenderCloudFrontCacheControl", + props.cacheControlProps + ); + } +} diff --git a/packages/prerender-proxy/package.json b/packages/prerender-proxy/package.json new file mode 100644 index 00000000..0d5698cd --- /dev/null +++ b/packages/prerender-proxy/package.json @@ -0,0 +1,36 @@ +{ + "name": "@aligent/cdk-prerender-proxy", + "version": "2.0.0", + "description": "Cloudfront Lambda@Edge constructs for integrating with prerender.io", + "main": "index.js", + "scripts": { + "build": "tsc && cd ./lib/handlers && npm ci", + "prepublish": "tsc && cd ./lib/handlers && npm ci" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/aligent/aws-cdk-prerender-proxy-stack.git" + }, + "license": "GPL-3.0-only", + "bugs": { + "url": "https://github.com/aligent/aws-cdk-prerender-proxy-stack/issues" + }, + "homepage": "https://github.com/aligent/aws-cdk-prerender-proxy-stack#readme", + "devDependencies": { + "@types/jest": "^29.5.5", + "@types/node": "20.6.3", + "aws-cdk": "2.97.0", + "jest": "^29.7.0", + "ts-jest": "^29.1.1", + "ts-node": "^10.9.1", + "typescript": "~5.2.2" + }, + "dependencies": { + "@types/aws-lambda": "^8.10.122", + "aws-cdk-lib": "2.97.0", + "axios": "^1.5.1", + "constructs": "^10.0.0", + "esbuild": "^0.17.0", + "source-map-support": "^0.5.21" + } +} diff --git a/packages/prerender-proxy/tsconfig.json b/packages/prerender-proxy/tsconfig.json new file mode 100644 index 00000000..4082f16a --- /dev/null +++ b/packages/prerender-proxy/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "../../tsconfig.json" +}