2016-04-16 12:49:48 -04:00
|
|
|
var async = require('async');
|
2016-04-15 19:42:57 -04:00
|
|
|
var defaults = require('lodash.defaults');
|
|
|
|
var glob = require('glob');
|
|
|
|
var path = require('path');
|
|
|
|
|
2016-05-20 10:33:13 -04:00
|
|
|
var route_json = require('../src/routes.json');
|
2016-04-16 10:17:20 -04:00
|
|
|
|
2016-04-18 12:32:20 -04:00
|
|
|
const FASTLY_SERVICE_ID = process.env.FASTLY_SERVICE_ID || '';
|
|
|
|
const S3_BUCKET_NAME = process.env.S3_BUCKET_NAME || '';
|
2016-04-15 19:42:57 -04:00
|
|
|
|
2016-04-16 13:01:39 -04:00
|
|
|
const PASS_REQUEST_CONDITION_NAME = 'Pass';
|
2016-04-18 14:07:27 -04:00
|
|
|
const NOT_PASS_REQUEST_CONDITION_NAME = '!(Pass)';
|
2016-04-16 13:01:39 -04:00
|
|
|
const PASS_CACHE_CONDITION_NAME = 'Cache Pass';
|
|
|
|
const BUCKET_NAME_HEADER_NAME = 'Bucket name';
|
|
|
|
|
2016-04-18 12:32:20 -04:00
|
|
|
var fastly = require('./lib/fastly-extended')(process.env.FASTLY_API_KEY, FASTLY_SERVICE_ID);
|
|
|
|
|
2016-04-15 19:42:57 -04:00
|
|
|
var extraAppRoutes = [
|
|
|
|
// Homepage with querystring.
|
|
|
|
// TODO: Should this be added for every route?
|
|
|
|
'^/\\?',
|
|
|
|
// View html
|
|
|
|
'^/[^\/]*\.html'
|
2016-04-16 10:17:20 -04:00
|
|
|
];
|
2016-04-15 19:42:57 -04:00
|
|
|
|
2016-04-16 13:46:03 -04:00
|
|
|
/*
|
|
|
|
* Given the relative path to the static directory, return an array of
|
|
|
|
* patterns matching the files and directories there.
|
|
|
|
*/
|
2016-04-15 19:42:57 -04:00
|
|
|
var getStaticPaths = function (pathToStatic) {
|
|
|
|
var staticPaths = glob.sync(path.resolve(__dirname, pathToStatic));
|
2016-05-17 13:49:03 -04:00
|
|
|
return staticPaths.filter(function (pathName) {
|
2016-05-17 12:20:45 -04:00
|
|
|
// Exclude view html, resolve everything else in the build
|
2016-05-17 13:49:03 -04:00
|
|
|
return path.extname(pathName) !== '.html';
|
|
|
|
}).map(function (pathName) {
|
2016-04-15 19:42:57 -04:00
|
|
|
// Reduce absolute path to relative paths like '/js'
|
|
|
|
var base = path.dirname(path.resolve(__dirname, pathToStatic));
|
2016-04-25 14:32:52 -04:00
|
|
|
return '^' + pathName.replace(base, '') + (path.extname(pathName) ? '' : '/');
|
2016-04-15 19:42:57 -04:00
|
|
|
});
|
2016-04-18 14:07:27 -04:00
|
|
|
};
|
2016-04-15 19:42:57 -04:00
|
|
|
|
2016-04-16 13:46:03 -04:00
|
|
|
/*
|
|
|
|
* Given a list of express routes, return a list of patterns to match
|
|
|
|
* the express route and a static view file associated with the route
|
|
|
|
*/
|
2016-04-15 19:42:57 -04:00
|
|
|
var getViewPaths = function (routes) {
|
|
|
|
return routes.map(function (route) {
|
|
|
|
return route.pattern;
|
|
|
|
});
|
2016-04-18 14:07:27 -04:00
|
|
|
};
|
2016-04-15 19:42:57 -04:00
|
|
|
|
2016-05-20 10:33:13 -04:00
|
|
|
/*
|
|
|
|
* Translate an express-style pattern e.g. /path/:arg/ to a regex
|
|
|
|
* all :arguments become .+?
|
|
|
|
*/
|
|
|
|
var expressPatternToRegex = function (pattern) {
|
|
|
|
return pattern.replace(/(:[^/]+)\//gi, '.+?/');
|
|
|
|
};
|
|
|
|
|
2016-04-16 13:46:03 -04:00
|
|
|
/*
|
|
|
|
* Given a list of patterns for paths, OR all of them together into one
|
|
|
|
* string suitable for a Fastly condition
|
|
|
|
*/
|
2016-04-15 19:42:57 -04:00
|
|
|
var pathsToCondition = function (paths) {
|
2016-04-28 14:18:42 -04:00
|
|
|
return 'req.url~"' + paths.reduce(function (conditionString, pattern) {
|
|
|
|
return conditionString + (conditionString ? '|' : '') + pattern;
|
|
|
|
}, '') + '"';
|
2016-04-18 14:07:27 -04:00
|
|
|
};
|
2016-04-15 19:42:57 -04:00
|
|
|
|
2016-04-19 10:47:33 -04:00
|
|
|
/*
|
|
|
|
* Helper method to NOT a condition statement
|
|
|
|
*/
|
|
|
|
var negateConditionStatement = function (statement) {
|
|
|
|
return '!(' + statement + ')';
|
|
|
|
};
|
|
|
|
|
2016-04-16 13:46:03 -04:00
|
|
|
/*
|
|
|
|
* Combine static paths, routes, and any additional paths to a single
|
|
|
|
* fastly condition to match req.url
|
|
|
|
*/
|
2016-04-15 19:42:57 -04:00
|
|
|
var getAppRouteCondition = function (pathToStatic, routes, additionalPaths) {
|
|
|
|
var staticPaths = getStaticPaths(pathToStatic);
|
|
|
|
var viewPaths = getViewPaths(routes);
|
|
|
|
var allPaths = [].concat(staticPaths, viewPaths, additionalPaths);
|
|
|
|
return pathsToCondition(allPaths);
|
2016-04-18 14:07:27 -04:00
|
|
|
};
|
2016-04-15 19:42:57 -04:00
|
|
|
|
2016-04-19 18:42:03 -04:00
|
|
|
var getConditionNameForRoute = function (route, type) {
|
|
|
|
return 'routes/' + route.pattern + ' (' + type + ')';
|
2016-04-15 19:42:57 -04:00
|
|
|
};
|
|
|
|
|
2016-04-19 18:42:03 -04:00
|
|
|
var getHeaderNameForRoute = function (route) {
|
2016-04-21 18:16:49 -04:00
|
|
|
if (route.name) return 'rewrites/' + route.name;
|
2016-04-19 18:42:03 -04:00
|
|
|
if (route.redirect) return 'redirects/' + route.pattern;
|
|
|
|
};
|
|
|
|
|
|
|
|
var getResponseNameForRoute = function (route) {
|
|
|
|
return 'redirects/' + route.pattern;
|
2016-04-16 12:49:48 -04:00
|
|
|
};
|
2016-04-15 19:42:57 -04:00
|
|
|
|
2016-05-20 10:33:13 -04:00
|
|
|
var routes = route_json.map(function (route) {
|
|
|
|
return defaults({}, {pattern: expressPatternToRegex(route.pattern)}, route);
|
|
|
|
});
|
|
|
|
|
2016-04-18 12:32:20 -04:00
|
|
|
async.auto({
|
2016-04-18 14:07:27 -04:00
|
|
|
version: function (cb) {
|
2016-04-18 12:32:20 -04:00
|
|
|
fastly.getLatestVersion(function (err, response) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
// Validate latest version before continuing
|
2016-04-19 13:04:45 -04:00
|
|
|
if (response.active || response.locked) {
|
|
|
|
fastly.cloneVersion(response.number, function (err, response) {
|
|
|
|
if (err) return cb('Failed to clone latest version: ' + err);
|
|
|
|
cb(null, response.number);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
cb(null, response.number);
|
|
|
|
}
|
2016-04-18 12:32:20 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
notPassRequestCondition: ['version', function (cb, results) {
|
2016-05-17 12:20:45 -04:00
|
|
|
var statement = getAppRouteCondition('../build/*', routes, extraAppRoutes);
|
2016-04-18 12:32:20 -04:00
|
|
|
var condition = {
|
|
|
|
name: NOT_PASS_REQUEST_CONDITION_NAME,
|
|
|
|
statement: statement,
|
|
|
|
type: 'REQUEST',
|
|
|
|
priority: 10
|
|
|
|
};
|
|
|
|
fastly.setCondition(results.version, condition, cb);
|
|
|
|
}],
|
|
|
|
setBucketNameHeader: ['version', 'notPassRequestCondition', function (cb, results) {
|
|
|
|
var header = {
|
|
|
|
name: BUCKET_NAME_HEADER_NAME,
|
|
|
|
action: 'set',
|
|
|
|
ignore_if_set: 0,
|
|
|
|
type: 'REQUEST',
|
|
|
|
dst: 'http.host',
|
|
|
|
src: '"' + S3_BUCKET_NAME + '"',
|
|
|
|
request_condition: results.notPassRequestCondition.name,
|
|
|
|
priority: 1
|
|
|
|
};
|
|
|
|
fastly.setFastlyHeader(results.version, header, cb);
|
|
|
|
}],
|
|
|
|
passRequestCondition: ['version', 'notPassRequestCondition', function (cb, results) {
|
|
|
|
var condition = {
|
|
|
|
name: PASS_REQUEST_CONDITION_NAME,
|
2016-04-19 10:47:33 -04:00
|
|
|
statement: negateConditionStatement(results.notPassRequestCondition.statement),
|
2016-04-18 12:32:20 -04:00
|
|
|
type: 'REQUEST',
|
|
|
|
priority: 10
|
|
|
|
};
|
|
|
|
fastly.setCondition(results.version, condition, cb);
|
|
|
|
}],
|
|
|
|
passRequestSettingsCondition: ['version', 'passRequestCondition', function (cb, results) {
|
|
|
|
fastly.request(
|
|
|
|
'PUT',
|
|
|
|
fastly.getFastlyAPIPrefix(FASTLY_SERVICE_ID, results.version) + '/request_settings/Pass',
|
|
|
|
{request_condition: results.passRequestCondition.name},
|
|
|
|
cb
|
|
|
|
);
|
|
|
|
}],
|
2016-04-19 10:50:57 -04:00
|
|
|
backendCondition: ['version', 'notPassRequestCondition', function (cb, results) {
|
2016-04-18 12:32:20 -04:00
|
|
|
fastly.request(
|
|
|
|
'PUT',
|
2016-04-19 10:50:57 -04:00
|
|
|
fastly.getFastlyAPIPrefix(FASTLY_SERVICE_ID, results.version) + '/backend/s3',
|
|
|
|
{request_condition: results.notPassRequestCondition.name},
|
2016-04-18 12:32:20 -04:00
|
|
|
cb
|
|
|
|
);
|
|
|
|
}],
|
|
|
|
passCacheCondition: ['version', 'passRequestCondition', function (cb, results) {
|
|
|
|
var condition = defaults(
|
|
|
|
{name: PASS_CACHE_CONDITION_NAME, type: 'CACHE'},
|
|
|
|
results.passRequestCondition
|
|
|
|
);
|
|
|
|
fastly.setCondition(results.version, condition, cb);
|
|
|
|
}],
|
|
|
|
passCacheSettingsCondition: ['version', 'passCacheCondition', function (cb, results) {
|
|
|
|
fastly.request(
|
|
|
|
'PUT',
|
|
|
|
fastly.getFastlyAPIPrefix(FASTLY_SERVICE_ID, results.version) + '/cache_settings/Pass',
|
|
|
|
{cache_condition: results.passCacheCondition.name},
|
|
|
|
cb
|
|
|
|
);
|
|
|
|
}],
|
|
|
|
appRouteRequestConditions: ['version', function (cb, results) {
|
|
|
|
var conditions = {};
|
|
|
|
async.forEachOf(routes, function (route, id, cb2) {
|
|
|
|
var condition = {
|
2016-04-19 18:42:03 -04:00
|
|
|
name: getConditionNameForRoute(route, 'request'),
|
2016-04-18 12:32:20 -04:00
|
|
|
statement: 'req.url ~ "' + route.pattern + '"',
|
|
|
|
type: 'REQUEST',
|
2016-04-27 15:27:45 -04:00
|
|
|
// Priority needs to be > 1 to not interact with http->https redirect
|
|
|
|
priority: 10 + id
|
2016-04-18 12:32:20 -04:00
|
|
|
};
|
|
|
|
fastly.setCondition(results.version, condition, function (err, response) {
|
|
|
|
if (err) return cb2(err);
|
|
|
|
conditions[id] = response;
|
|
|
|
cb2(null, response);
|
|
|
|
});
|
|
|
|
}, function (err) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
cb(null, conditions);
|
|
|
|
});
|
|
|
|
}],
|
|
|
|
appRouteHeaders: ['version', 'appRouteRequestConditions', function (cb, results) {
|
|
|
|
var headers = {};
|
|
|
|
async.forEachOf(routes, function (route, id, cb2) {
|
2016-04-19 18:42:03 -04:00
|
|
|
if (route.redirect) {
|
|
|
|
async.auto({
|
|
|
|
responseCondition: function (cb3) {
|
|
|
|
var condition = {
|
|
|
|
name: getConditionNameForRoute(route, 'response'),
|
|
|
|
statement: 'req.url ~ "' + route.pattern + '"',
|
|
|
|
type: 'RESPONSE',
|
|
|
|
priority: id
|
|
|
|
};
|
|
|
|
fastly.setCondition(results.version, condition, cb3);
|
|
|
|
},
|
|
|
|
responseObject: function (cb3) {
|
|
|
|
var responseObject = {
|
|
|
|
name: getResponseNameForRoute(route),
|
|
|
|
status: 301,
|
|
|
|
response: 'Moved Permanently',
|
|
|
|
request_condition: getConditionNameForRoute(route, 'request')
|
|
|
|
};
|
|
|
|
fastly.setResponseObject(results.version, responseObject, cb3);
|
|
|
|
},
|
|
|
|
redirectHeader: ['responseCondition', function (cb3, redirectResults) {
|
|
|
|
var header = {
|
|
|
|
name: getHeaderNameForRoute(route),
|
|
|
|
action: 'set',
|
|
|
|
ignore_if_set: 0,
|
|
|
|
type: 'RESPONSE',
|
|
|
|
dst: 'http.Location',
|
|
|
|
src: '"' + route.redirect + '"',
|
|
|
|
response_condition: redirectResults.responseCondition.name
|
|
|
|
};
|
|
|
|
fastly.setFastlyHeader(results.version, header, cb3);
|
|
|
|
}]
|
|
|
|
}, function (err, redirectResults) {
|
|
|
|
if (err) return cb2(err);
|
|
|
|
headers[id] = redirectResults.redirectHeader;
|
|
|
|
cb2(null, redirectResults);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
var header = {
|
|
|
|
name: getHeaderNameForRoute(route, 'request'),
|
|
|
|
action: 'set',
|
|
|
|
ignore_if_set: 0,
|
|
|
|
type: 'REQUEST',
|
|
|
|
dst: 'url',
|
2016-04-21 18:16:49 -04:00
|
|
|
src: '"/' + route.name + '.html"',
|
2016-04-19 18:42:03 -04:00
|
|
|
request_condition: results.appRouteRequestConditions[id].name,
|
|
|
|
priority: 10
|
|
|
|
};
|
|
|
|
fastly.setFastlyHeader(results.version, header, function (err, response) {
|
|
|
|
if (err) return cb2(err);
|
|
|
|
headers[id] = response;
|
|
|
|
cb2(null, response);
|
|
|
|
});
|
|
|
|
}
|
2016-04-18 12:32:20 -04:00
|
|
|
}, function (err) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
cb(null, headers);
|
2016-04-18 14:07:27 -04:00
|
|
|
});
|
2016-04-18 12:32:20 -04:00
|
|
|
}]},
|
2016-04-19 13:04:45 -04:00
|
|
|
function (err, results) {
|
2016-04-18 12:32:20 -04:00
|
|
|
if (err) throw new Error(err);
|
2016-04-19 13:04:45 -04:00
|
|
|
if (process.env.FASTLY_ACTIVATE_CHANGES) {
|
|
|
|
fastly.activateVersion(results.version, function (err, response) {
|
|
|
|
if (err) throw new Error(err);
|
|
|
|
process.stdout.write('Successfully configured and activated version ' + response.number + '\n');
|
2016-04-29 15:40:44 -04:00
|
|
|
fastly.purgeAll(FASTLY_SERVICE_ID, function (err) {
|
|
|
|
if (err) throw new Error(err);
|
|
|
|
process.stdout.write('Purged all.\n');
|
|
|
|
});
|
2016-04-19 13:04:45 -04:00
|
|
|
});
|
|
|
|
}
|
2016-04-16 12:49:48 -04:00
|
|
|
}
|
2016-04-18 12:32:20 -04:00
|
|
|
);
|