Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New deployer #1284

Merged
merged 7 commits into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
initial lambda@edge for content origin-request events
  • Loading branch information
escattone committed Sep 18, 2020
commit 713e6cea1b2d0b5b02ed7a12560237182f66b43b
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 68,4 @@ yarn-error.log*
/deployer/src/deployer.egg-info/
fake-v1-api/
mdn-yari-*.tgz
lambda/**/*.zip
6 changes: 6 additions & 0 deletions content/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 8,12 @@ function buildURL(locale, slug) {
return `/${locale}/docs/${slug}`.toLowerCase();
}

/*
* NOTE: A nearly identical copy of this function is used within
* ./lambda/content-origin-request/index.js. If you make a
* change to this function, you must replicate the change
* there as well.
*/
function slugToFolder(slug) {
return (
slug
Expand Down
50 changes: 50 additions & 0 deletions lambda/content-origin-request/index.js
Original file line number Diff line number Diff line change
@@ -0,0 1,50 @@
const sanitizeFilename = require("sanitize-filename");

const CONTENT_DEVELOPMENT_DOMAIN = ".content.dev.mdn.mozit.cloud";

/*
* NOTE: This function is derived from the function of the same name within
* ../../content/utils.js. It differs only in its final "join", which
* uses "/", as required by S3 keys, rather than "path.sep".
*/
function slugToFolder(slug) {
return slug
.replace(/\*/g, "_star_")
.replace(/::/g, "_doublecolon_")
.replace(/:/g, "_colon_")
.replace(/\?/g, "_question_")
.toLowerCase()
.split("/")
.map(sanitizeFilename)
.join("/");
}

exports.handler = async (event, context) => {
/*
* Modify the request before it's passed to the S3 origin.
*/
const request = event.Records[0].cf.request;
const host = request.headers.host[0].value.toLowerCase();
// Rewrite the URI to match the keys in S3.
// NOTE: The incoming URI should remain URI-encoded.
let newURI = slugToFolder(request.uri);
if (newURI.includes("/docs/") && !newURI.endsWith("/index.json")) {
if (!newURI.endsWith("/")) {
newURI = "/";
}
newURI = "index.html";
}
request.uri = newURI;
// Rewrite the HOST header to match the S3 bucket website domain.
// This is required only because we're using S3 as a website, which
// we need in order to do redirects from S3. NOTE: The origin is
// considered a "custom" origin because we're using S3 as a website.
request.headers.host[0].value = request.origin.custom.domainName;
// Conditionally rewrite the path (prefix) of the origin.
if (host.endsWith(CONTENT_DEVELOPMENT_DOMAIN)) {
// When reviewing PR's, each PR gets its own subdomain, and
// all of its content is prefixed with that subdomain in S3.
request.origin.custom.path = `/${host.split(".")[0]}`;
}
return request;
};
14 changes: 14 additions & 0 deletions lambda/content-origin-request/package.json
Original file line number Diff line number Diff line change
@@ -0,0 1,14 @@
{
"name": "content-origin-request-handler",
"version": "1.0.0",
"description": "For creating the AWS Lambda deployment package for the Lambda@Edge content-origin-request handler.",
"private": true,
"main": "index.js",
"license": "MPL-2.0",
"scripts": {
"make-zip": "yarn install && zip -r mdn-content-origin-request.zip ."
},
"dependencies": {
"sanitize-filename": "^1.6.3"
}
}
22 changes: 22 additions & 0 deletions lambda/content-origin-request/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 1,22 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


sanitize-filename@^1.6.3:
version "1.6.3"
resolved "https://registry.yarnpkg.com/sanitize-filename/-/sanitize-filename-1.6.3.tgz#755ebd752045931977e30b2025d340d7c9090378"
integrity sha512-y/52Mcy7aw3gRm7IrcGDFx/bCk4AhRh2eI9luHOQM86nZsqwiRkkq2GekHXBBD SmPidc8i2PqtYZl pWJ8Oeg==
dependencies:
truncate-utf8-bytes "^1.0.0"

truncate-utf8-bytes@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/truncate-utf8-bytes/-/truncate-utf8-bytes-1.0.2.tgz#405923909592d56f78a5818434b0b78489ca5f2b"
integrity sha1-QFkjkJWS1W94pYGENLC3hInKXys=
dependencies:
utf8-byte-length "^1.0.1"

utf8-byte-length@^1.0.1:
version "1.0.4"
resolved "https://registry.yarnpkg.com/utf8-byte-length/-/utf8-byte-length-1.0.4.tgz#f45f150c4c66eee968186505ab93fcbb8ad6bf61"
integrity sha1-9F8VDExm7uloGGUFq5P8u4rWv2E=