Merge pull request #498 from rschamp/bugfix/pattern-routes

Always use regex form of route patterns
This commit is contained in:
Ray Schamp 2016-05-20 10:33:54 -04:00
commit 51e491bf44

View file

@ -3,7 +3,7 @@ var defaults = require('lodash.defaults');
var glob = require('glob');
var path = require('path');
var routes = require('../src/routes.json');
var route_json = require('../src/routes.json');
const FASTLY_SERVICE_ID = process.env.FASTLY_SERVICE_ID || '';
const S3_BUCKET_NAME = process.env.S3_BUCKET_NAME || '';
@ -49,13 +49,20 @@ var getViewPaths = function (routes) {
});
};
/*
* Translate an express-style pattern e.g. /path/:arg/ to a regex
* all :arguments become .+?
*/
var expressPatternToRegex = function (pattern) {
return pattern.replace(/(:[^/]+)\//gi, '.+?/');
};
/*
* Given a list of patterns for paths, OR all of them together into one
* string suitable for a Fastly condition
*/
var pathsToCondition = function (paths) {
return 'req.url~"' + paths.reduce(function (conditionString, pattern) {
pattern = pattern.replace(/(:[^/]+)\//gi, '.+?/');
return conditionString + (conditionString ? '|' : '') + pattern;
}, '') + '"';
};
@ -91,6 +98,10 @@ var getResponseNameForRoute = function (route) {
return 'redirects/' + route.pattern;
};
var routes = route_json.map(function (route) {
return defaults({}, {pattern: expressPatternToRegex(route.pattern)}, route);
});
async.auto({
version: function (cb) {
fastly.getLatestVersion(function (err, response) {