This repository has been archived by the owner on Nov 14, 2022. It is now read-only.
Add redirector worker #1
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,26 @@ | ||
# EditorConfig is awesome: http://EditorConfig.org | ||
|
||
# top-most EditorConfig file | ||
root = true | ||
|
||
# Unix-style newlines with a newline ending every file | ||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
indent_style = space | ||
|
||
[*.js] | ||
indent_size = 4 | ||
|
||
[*.json] | ||
indent_size = 4 | ||
|
||
[*.css] | ||
indent_size = 4 | ||
|
||
[*.html] | ||
indent_size = 2 | ||
|
||
[package.json] | ||
indent_size = 2 |
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,19 @@ | ||
module.exports = { | ||
"env": { | ||
"browser": false, | ||
"commonjs": true, | ||
"es6": true, | ||
"mocha": true, | ||
"worker": true | ||
}, | ||
"extends": "eslint:recommended", | ||
"globals": { | ||
"Atomics": "readonly", | ||
"SharedArrayBuffer": "readonly" | ||
}, | ||
"parserOptions": { | ||
"ecmaVersion": 2018 | ||
}, | ||
"rules": { | ||
} | ||
}; |
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,4 @@ | ||
language: node_js | ||
node_js: | ||
- "10" | ||
cache: yarn |
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 |
---|---|---|
@@ -1,2 +1,19 @@ | ||
# www-workers | ||
|
||
Cloudflare Workers for www.mozilla.org | ||
|
||
## Install | ||
|
||
First make sure you have [Node](https://nodejs.org/) and [Yarn](https://yarnpkg.com/) installed. Then run: | ||
|
||
``` | ||
yarn | ||
``` | ||
|
||
## Test | ||
|
||
To run unit tests: | ||
|
||
``` | ||
npm test | ||
``` |
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,26 @@ | ||
{ | ||
"name": "www-workers", | ||
"private": true, | ||
"version": "1.0.0", | ||
"description": "Cloudflare Workers for www.mozilla.org", | ||
"scripts": { | ||
"pretest": "./node_modules/.bin/eslint .", | ||
"test": "mocha --timeout 20000" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/mozmeao/www-workers.git" | ||
}, | ||
"author": "Mozilla", | ||
"license": "MPL-2.0", | ||
"bugs": { | ||
"url": "https://github.com/mozmeao/www-workers/issues" | ||
}, | ||
"devDependencies": { | ||
"@dollarshaveclub/cloudworker": "^0.1.1", | ||
"chai": "^4.2.0", | ||
"eslint": "^6.5.1", | ||
"mocha": "^6.2.1", | ||
"sinon": "^7.5.0" | ||
} | ||
} |
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,53 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
const expect = require('chai').expect; | ||
const sinon = require('sinon'); | ||
|
||
|
||
before(async function () { | ||
const context = new (require('@dollarshaveclub/cloudworker'))(require('fs').readFileSync('workers/redirector.js', 'utf8')).context; | ||
global.Request = context.Request | ||
global.URL = context.URL | ||
global.handleRequest = context.handleRequest; | ||
}); | ||
|
||
describe('Redirector Worker', function() { | ||
|
||
it('should return a 200 for requests that have a matching response, but are not within sample rate', async function () { | ||
Math.random = sinon.stub().returns(0.8534); | ||
const url = new URL('https://bedrock-stage.gcp.moz.works/en-US/firefox/new/'); | ||
const req = new Request(url); | ||
const res = await global.handleRequest(req); | ||
expect(res.status).to.equal(200); | ||
expect(res.url).to.equal('https://bedrock-stage.gcp.moz.works/en-US/firefox/new/'); | ||
}); | ||
|
||
it('should return a 302 for requests that have a matching response, and are within sample rate', async function () { | ||
Math.random = sinon.stub().returns(0.0001); | ||
const url = new URL('https://bedrock-stage.gcp.moz.works/en-US/firefox/new/'); | ||
const req = new Request(url); | ||
const res = await global.handleRequest(req); | ||
expect(res.status).to.equal(302); | ||
expect(res.headers.get('location')).to.equal('https://bedrock-stage.gcp.moz.works/en-US/exp/firefox/new/'); | ||
}); | ||
|
||
it('should preserve query string parameters when redirecting the URL', async function() { | ||
Math.random = sinon.stub().returns(0.0001); | ||
const url = new URL('https://bedrock-stage.gcp.moz.works/en-US/firefox/new/?foo=bar'); | ||
const req = new Request(url); | ||
const res = await global.handleRequest(req); | ||
expect(res.status).to.equal(302); | ||
expect(res.headers.get('location')).to.equal('https://bedrock-stage.gcp.moz.works/en-US/exp/firefox/new/?foo=bar'); | ||
}); | ||
|
||
it('should return a 200 if the request does not have a matching redirect', async function() { | ||
const url = new URL('https://bedrock-stage.gcp.moz.works/en-US/firefox/'); | ||
const req = new Request(url); | ||
const res = await global.handleRequest(req); | ||
expect(res.status).to.equal(200); | ||
expect(res.url).to.equal('https://bedrock-stage.gcp.moz.works/en-US/firefox/'); | ||
}); | ||
}); | ||
|
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,60 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
/** | ||
* This is a custom URL redirector implemented as a CloudFlare Worker. | ||
* The role of the worker is to redirect a predefined % of traffic | ||
* to a different URL (used for A/B testing). | ||
* API documentation can be found at https://developers.cloudflare.com/workers/ | ||
*/ | ||
|
||
/** | ||
* `targetPath` is the target pathname that the worker looks to match against. | ||
* `sandboxPath` is the sandbox experiment pathname to redirect to. | ||
* `sampleRate` is the proporation of traffic that should be redirected (a value of 0.1 equates to 10%). | ||
*/ | ||
const experimentPages = [ | ||
{ | ||
'targetPath': `/en-US/firefox/new/`, | ||
'sandboxPath': `/en-US/exp/firefox/new/`, | ||
'sampleRate': 0.06 | ||
} | ||
]; | ||
|
||
function isWithinSampleRate(SAMPLE_RATE) { | ||
return Math.random() < SAMPLE_RATE; | ||
} | ||
|
||
async function handleRequest(request) { | ||
// Get the current request URL. | ||
const requestURL = new URL(request.url); | ||
|
||
// Split out search params from the origin and pathname. | ||
const origin = requestURL.origin; | ||
const pathname = requestURL.pathname; | ||
const search = requestURL.search; | ||
|
||
// Check to see if the URL matches a route. | ||
const match = experimentPages.filter(page => pathname === page.targetPath); | ||
|
||
if (match.length > 0) { | ||
// Assume only the first match found will be processed. | ||
const sandbox = match[0]; | ||
|
||
// Get the experimental URL to redirect to. | ||
const experimentURL = search ? `${origin}${sandbox.sandboxPath}${search}` : `${origin}${sandbox.sandboxPath}`; | ||
|
||
// Only redirect a pre-defined % of requests per-page. | ||
if (experimentURL && isWithinSampleRate(sandbox.sampleRate)) { | ||
return Response.redirect(experimentURL, 302); | ||
} | ||
} | ||
|
||
// Fetch the original request if no redirect was fulfilled. | ||
return await fetch(request); | ||
} | ||
|
||
addEventListener('fetch', event => { | ||
event.respondWith(handleRequest(event.request)) | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow, never thought I'd see dollarshaveclub in a PR :-)