Skip to content
This repository has been archived by the owner on Nov 14, 2022. It is now read-only.

Add redirector worker #1

Merged
merged 2 commits into from Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions .editorconfig
@@ -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
19 changes: 19 additions & 0 deletions .eslintrc.js
@@ -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": {
}
};
4 changes: 4 additions & 0 deletions .travis.yml
@@ -0,0 +1,4 @@
language: node_js
node_js:
- "10"
cache: yarn
17 changes: 17 additions & 0 deletions README.md
@@ -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
```
26 changes: 26 additions & 0 deletions package.json
@@ -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",

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 :-)

"chai": "^4.2.0",
"eslint": "^6.5.1",
"mocha": "^6.2.1",
"sinon": "^7.5.0"
}
}
53 changes: 53 additions & 0 deletions test/redirector-test.js
@@ -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/');
});
});

60 changes: 60 additions & 0 deletions workers/redirector.js
@@ -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))
});