-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simulate content-origin-request (#3047)
* simulate content-origin-request * specifics * oops * eslint * start server in background * polka is smaller * calm down eslint * debugging * node_modules caching * undebugging * add basic unit test start * eslint * querystring * more tests * more tests and stuff * tests updated based on feedback * way more conservative with caching node_modules * all feedback addressed * Update .github/workflows/content-origin-request.yml Co-authored-by: Ryan Johnson <escattone@gmail.com> Co-authored-by: Ryan Johnson <escattone@gmail.com>
- Loading branch information
Showing
7 changed files
with
4,743 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# This starts up a simulator that tries to do what our Lambda@Edge does. | ||
|
||
name: content-origin-request | ||
|
||
on: | ||
pull_request: | ||
branches: | ||
- main | ||
paths: | ||
- deployer/aws-lambda/** | ||
- libs/** | ||
- .github/workflows/content-origin-request.yml | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Setup Node.js environment | ||
uses: actions/setup-node@v2.1.5 | ||
with: | ||
node-version: "12" | ||
|
||
- name: Cache node_modules | ||
uses: actions/cache@v2.1.4 | ||
id: cached-node_modules | ||
with: | ||
path: | | ||
deployer/aws-lambda/content-origin-request/node_modules | ||
key: ${{ runner.os }}-${{ hashFiles('deployer/aws-lambda/content-origin-request/yarn.lock') }}-${{ hashFiles('libs/**/*.js') }} | ||
|
||
- name: Install all yarn packages | ||
if: steps.cached-node_modules.outputs.cache-hit != 'true' | ||
working-directory: deployer/aws-lambda/content-origin-request | ||
run: yarn --frozen-lockfile | ||
|
||
- name: Run test server | ||
working-directory: deployer/aws-lambda/content-origin-request | ||
run: | | ||
yarn serve > /tmp/stdout.log 2> /tmp/stderr.log & | ||
- name: Check that the server started | ||
run: curl --retry-connrefused --retry 5 -I http://localhost:7000/ping | ||
|
||
- name: Preflight the integration tests | ||
run: | | ||
curl -I http://localhost:7000/docs/Web | ||
curl -I http://localhost:7000/en-US/docs/Web/ | ||
- name: Unit test | ||
working-directory: deployer/aws-lambda/content-origin-request | ||
run: | | ||
yarn test-server | ||
- name: Debug any server outputs | ||
run: | | ||
echo "____STDOUT____" | ||
cat /tmp/stdout.log | ||
echo "____STDERR____" | ||
cat /tmp/stderr.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* eslint-disable node/no-unpublished-require */ | ||
/* eslint-disable node/no-missing-require */ | ||
const polka = require("polka"); | ||
const kleur = require("kleur"); | ||
|
||
const { handler } = require("./index"); | ||
|
||
const PORT = parseInt(process.env.PORT || "7000"); | ||
|
||
function ping(req, res) { | ||
res.end("pong\n"); | ||
} | ||
|
||
async function catchall(req, res) { | ||
// Reminder... | ||
// - url: e.g. `/en-US/docs/Foo?key=value` | ||
// - search: e.g. `?key=value` | ||
const { url } = req; | ||
let querystring = ""; | ||
if (req.search) { | ||
querystring = req.search.split("?")[1]; | ||
} | ||
const uri = url.split("?")[0]; | ||
console.log(`Simulating a request for: ${url}`); | ||
|
||
const event = {}; | ||
const origin = {}; | ||
origin.custom = {}; | ||
// This always pretends to proceed and do a S3 lookup | ||
origin.custom.domainName = | ||
req.headers["ORIGIN_DOMAIN_NAME"] || "s3.fakey.fake"; | ||
const cf = {}; | ||
const headers = {}; | ||
headers.host = [{ value: req.hostname }]; | ||
for (const [key, value] of Object.entries(req.headers)) { | ||
headers[key] = [{ value }]; | ||
} | ||
cf.request = { | ||
uri, | ||
querystring, | ||
origin, | ||
headers, | ||
}; | ||
|
||
event.Records = [{ cf }]; | ||
// console.log("EVENT..."); | ||
// console.log(event); | ||
// console.log(JSON.stringify(event, null, 3)); | ||
const handle = await handler(event); | ||
if (handle === cf.request) { | ||
// The request is allowed to pass through. | ||
// The URL might have been mutated. | ||
const msg = `Looking up ${kleur.white().bold(handle.uri)} in S3!`; | ||
console.log(msg); | ||
// This is unrealistic but helpful if you want to write some integration | ||
// tests against this test server if you're interesting in seeing that | ||
// would happen next. | ||
res.setHeader("X-S3-Lookup", handle.uri); | ||
res.end(`${msg}\n`); | ||
} else if (handle.status) { | ||
// It's a redirect. | ||
// console.log(JSON.stringify(handle, null, 3)); | ||
let path = null; | ||
for (const headers of Object.values(handle.headers)) { | ||
for (const header of headers) { | ||
res.setHeader(header.key, header.value); | ||
if (header.key.toLowerCase() === "location") { | ||
path = header.value; | ||
} | ||
} | ||
} | ||
console.log( | ||
`${ | ||
handle.status === 302 | ||
? kleur.green(handle.status) | ||
: kleur.yellow(handle.status) | ||
} redirect to ${kleur.bold(path)}` | ||
); | ||
res.statusCode = handle.status; | ||
res.setHeader("Location", path); | ||
res.end(""); | ||
} else { | ||
console.warn(JSON.stringify(handle, null, 3)); | ||
res.statusCode = 500; | ||
res.end("Unrecognized handler response"); | ||
} | ||
} | ||
|
||
polka() | ||
.get("/ping", ping) | ||
.head("/ping", ping) | ||
.get("/*", catchall) | ||
.head("/*", catchall) | ||
.listen(PORT, (err) => { | ||
if (err) { | ||
throw err; | ||
} | ||
console.log( | ||
`content-origin-request simulator started on http://localhost:${PORT}` | ||
); | ||
}); |
Oops, something went wrong.