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

Configure fastly cleanup #8507

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,18 +314,18 @@ npm run build && npm run deploy
### Fastly deployment details

When deploying, Fastly's API is used to clone the active VCL configuration, update just the
relevant component with content from this repo's `routes.json` file, and activate the new VCL
relevant component with content from this repo's `routes.js` file, and activate the new VCL
configuration.

#### routes.json
#### `routes.js`

Much of the routes.json file is straightforward, but some fields are not obvious in their purpose.
Much of the `routes.js` file is straightforward, but some fields are not obvious in their purpose.

`routeAlias` helps us keep the overall length and complexity of the regex comparison code in
Fastly from getting too large. There is one large regex which we have Fastly test the incoming
request URL against to know if it can reply with a static file in S3; if no match is found, we
assume we need to pass the request on to scratchr2. We could test every single route `pattern`
regex in `routes.json`, but many are similar, so instead we just take the unique set of all
regex in `routes.js`, but many are similar, so instead we just take the unique set of all
`routeAlias` entries, which is shorter and quicker.

## Windows
Expand Down
30 changes: 13 additions & 17 deletions bin/build-locales
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var localizedUrls = require('./lib/localized-urls');
var merge = require('lodash.merge');
var defaults = require('lodash.defaults');
var path = require('path');
var routes = require('../src/routes.json');
var {pageRoutes} = require('../src/routes.js');

// -----------------------------------------------------------------------------
// Utility function
Expand Down Expand Up @@ -138,25 +138,21 @@ var txMapping = {
'scratch_1.4': 'scratch_14' // transifex doesn't allow dots in resource names
};
// start with english default for all views
for (var v in routes) {
if (typeof routes[v].redirect !== 'undefined') {
continue;
}

views.push(routes[v].name);
for (var v in pageRoutes) {
views.push(pageRoutes[v].name);
try {
var subdir = routes[v].view.split('/');
var subdir = pageRoutes[v].view.split('/');
subdir.pop();
var l10n = path.resolve(__dirname, '../src/views/' + subdir.join('/') + '/l10n.json');
var viewIds = JSON.parse(fs.readFileSync(l10n, 'utf8'));
defaultLocales[routes[v].name] = viewIds;
defaultLocales[pageRoutes[v].name] = viewIds;
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}

try {
fs.accessSync(path.resolve(__dirname, '../src/views/' + routes[v].view + '.jsx'));
fs.accessSync(path.resolve(__dirname, '../src/views/' + pageRoutes[v].view + '.jsx'));
} catch (err) {
// the config for the view is not set up correctly, so throw the error.
throw err;
Expand All @@ -165,22 +161,22 @@ for (var v in routes) {

// get asset url translations
try {
subdir = routes[v].view.split('/');
subdir = pageRoutes[v].view.split('/');
subdir.pop();
var l10nStatic = path.resolve(__dirname, '../src/views/' + subdir.join('/') + '/l10n-static.json');
localizedAssetUrls[routes[v].name] = {};
localizedAssetUrls[pageRoutes[v].name] = {};

var viewUrls = require(l10nStatic);
localizedAssetUrls[routes[v].name]['en'] = viewUrls;
localizedAssetUrls[pageRoutes[v].name]['en'] = viewUrls;
var defaultUrls = localizedUrls['en'];
for (var lang in languages) {
localizedAssetUrls[routes[v].name][lang] = {};
localizedAssetUrls[pageRoutes[v].name][lang] = {};
var langUrls = localizedUrls[lang] || {};
for (var key in viewUrls) {
if (langUrls.hasOwnProperty(key)) {
localizedAssetUrls[routes[v].name][lang][key] = langUrls[key];
localizedAssetUrls[pageRoutes[v].name][lang][key] = langUrls[key];
} else {
localizedAssetUrls[routes[v].name][lang][key] = defaultUrls[key];
localizedAssetUrls[pageRoutes[v].name][lang][key] = defaultUrls[key];
}
}
}
Expand All @@ -190,7 +186,7 @@ for (var v in routes) {
}

try {
fs.accessSync(path.resolve(__dirname, '../src/views/' + routes[v].view + '.jsx'));
fs.accessSync(path.resolve(__dirname, '../src/views/' + pageRoutes[v].view + '.jsx'));
} catch (err) {
// the config for the view is not set up correctly, so throw the error.
throw err;
Expand Down
35 changes: 12 additions & 23 deletions bin/configure-fastly.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ const defaults = require('lodash.defaults');
const fastlyConfig = require('./lib/fastly-config-methods');
const languages = require('scratch-l10n').default;

const routeJson = require('../src/routes.json');
const {routes: rawRoutes} = require('../src/routes.js');

const FASTLY_SERVICE_ID = process.env.FASTLY_SERVICE_ID || '';
const S3_BUCKET_NAME = process.env.S3_BUCKET_NAME || '';
const RADISH_URL = process.env.RADISH_URL || '';

const fastly = require('./lib/fastly-extended')(process.env.FASTLY_API_KEY, FASTLY_SERVICE_ID);

Expand All @@ -19,17 +18,7 @@ const extraAppRoutes = [
'/[^/]*.html$'
];

const routeJsonPreProcessed = routeJson.map(
route => {
if (route.redirect) {
process.stdout.write(`Updating: ${route.redirect} to `);
route.redirect = route.redirect.replace('RADISH_URL', RADISH_URL);
process.stdout.write(`${route.redirect}\n`);
}
return route;
}
);
const routes = routeJsonPreProcessed.map(
const routes = rawRoutes.map(
route => defaults({}, {pattern: fastlyConfig.expressPatternToRegex(route.pattern)}, route)
);

Expand All @@ -40,7 +29,7 @@ async.auto({
// Validate latest version before continuing
if (response.active || response.locked) {
fastly.cloneVersion(response.number, (e, resp) => {
if (e) return cb(`Failed to clone latest version: ${e}`);
if (e) return cb(new Error('Failed to clone latest version', {cause: e}));
cb(null, resp.number);
});
} else {
Expand All @@ -49,7 +38,7 @@ async.auto({
});
},
recvCustomVCL: ['version', function (results, cb) {
// For all the routes in routes.json, construct a varnish-style regex that matches
// For all the routes in routes.js, construct a varnish-style regex that matches
// on any of those route conditions.
const notPassStatement = fastlyConfig.getAppRouteCondition('../build/*', routes, extraAppRoutes, __dirname);

Expand Down Expand Up @@ -103,7 +92,7 @@ async.auto({
}],
appRouteRequestConditions: ['version', function (results, cb) {
const conditions = {};
async.forEachOf(routes, (route, id, cb2) => {
async.forEachOf(routes, (route, /** @type {number} */ id, cb2) => {
const condition = {
name: fastlyConfig.getConditionNameForRoute(route, 'request'),
statement: `req.url.path ~ "${route.pattern}"`,
Expand All @@ -114,7 +103,7 @@ async.auto({
fastly.setCondition(results.version, condition, (err, response) => {
if (err) return cb2(err);
conditions[id] = response;
cb2(null, response);
cb2(/* no error */);
});
}, err => {
if (err) return cb(err);
Expand Down Expand Up @@ -159,11 +148,11 @@ async.auto({
}, (err, redirectResults) => {
if (err) return cb2(err);
headers[id] = redirectResults.redirectHeader;
cb2(null, redirectResults);
cb2(/* no error */);
});
} else {
const header = {
name: fastlyConfig.getHeaderNameForRoute(route, 'request'),
name: fastlyConfig.getHeaderNameForRoute(route),
action: 'set',
ignore_if_set: 0,
type: 'REQUEST',
Expand All @@ -175,7 +164,7 @@ async.auto({
fastly.setFastlyHeader(results.version, header, (err, response) => {
if (err) return cb2(err);
headers[id] = response;
cb2(null, response);
cb2(/* no error */);
});
}
}, err => {
Expand Down Expand Up @@ -276,14 +265,14 @@ async.auto({
});
}]
}, (err, results) => {
if (err) throw new Error(err);
if (err) throw err;
if (process.env.FASTLY_ACTIVATE_CHANGES) {
fastly.activateVersion(results.version, (e, resp) => {
if (e) throw new Error(e);
if (e) throw e;
process.stdout.write(`Successfully configured and activated version ${resp.number}\n`);
// purge static-assets using surrogate key
fastly.purgeKey(FASTLY_SERVICE_ID, 'static-assets', error => {
if (error) throw new Error(error);
if (error) throw error;
process.stdout.write('Purged static assets.\n');
});
});
Expand Down
2 changes: 1 addition & 1 deletion bin/init-l10n-src
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ to ES6. Leaving template string versions as comments.
var execSync = require('child_process').execSync;
var fs = require('fs');
var path = require('path');
var routes = require('../src/routes.json');
var routes = require('../src/routes.js');

var cmd = '';
var execute = '';
Expand Down
9 changes: 9 additions & 0 deletions bin/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"moduleResolution": "Node16",
"module": "Node16",
"target": "ES2022", // like Node 16+, supports Error.cause
}
}
Loading