Add docstrings

This commit is contained in:
Ray Schamp 2016-04-16 13:46:03 -04:00
parent 5846dcacc8
commit ab0979b958
2 changed files with 60 additions and 6 deletions

View file

@ -24,9 +24,11 @@ var extraAppRoutes = [
'^/[^\/]*\.html' '^/[^\/]*\.html'
]; ];
/*
* Given the relative path to the static directory, return an array of
* patterns matching the files and directories there.
*/
var getStaticPaths = function (pathToStatic) { var getStaticPaths = function (pathToStatic) {
// Given the relative path to the static directory, return an array of
// patterns matching the files and directories there.
var staticPaths = glob.sync(path.resolve(__dirname, pathToStatic)); var staticPaths = glob.sync(path.resolve(__dirname, pathToStatic));
return staticPaths.map( function (pathName) { return staticPaths.map( function (pathName) {
// Reduce absolute path to relative paths like '/js' // Reduce absolute path to relative paths like '/js'
@ -35,23 +37,31 @@ var getStaticPaths = function (pathToStatic) {
}); });
} }
/*
* 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
*/
var getViewPaths = function (routes) { var getViewPaths = function (routes) {
// 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
return routes.map(function (route) { return routes.map(function (route) {
return route.pattern; return route.pattern;
}); });
} }
/*
* Given a list of patterns for paths, OR all of them together into one
* string suitable for a Fastly condition
*/
var pathsToCondition = function (paths) { var pathsToCondition = function (paths) {
// Given a list of patterns for paths, OR all of them together into one
// string suitable for a Fastly condition
return paths.reduce(function(conditionString, pattern) { return paths.reduce(function(conditionString, pattern) {
var patternCondition = 'req.url ~ "' + pattern + '"'; var patternCondition = 'req.url ~ "' + pattern + '"';
return conditionString + (conditionString ? ' || ' : '') + patternCondition; return conditionString + (conditionString ? ' || ' : '') + patternCondition;
}, ''); }, '');
} }
/*
* Combine static paths, routes, and any additional paths to a single
* fastly condition to match req.url
*/
var getAppRouteCondition = function (pathToStatic, routes, additionalPaths) { var getAppRouteCondition = function (pathToStatic, routes, additionalPaths) {
var staticPaths = getStaticPaths(pathToStatic); var staticPaths = getStaticPaths(pathToStatic);
var viewPaths = getViewPaths(routes); var viewPaths = getViewPaths(routes);

View file

@ -1,17 +1,45 @@
var Fastly = require('fastly'); var Fastly = require('fastly');
/*
* Fastly library extended to allow configuration for a particular service
* and some helper methods.
*
* @param {string} API key
* @param {string} Service id
*/
module.exports = function (apiKey, serviceId) { module.exports = function (apiKey, serviceId) {
var fastly = Fastly(apiKey); var fastly = Fastly(apiKey);
fastly.serviceId = serviceId; fastly.serviceId = serviceId;
/*
* Helper method to NOT a condition statement
*
* @param {string} Statement
*
* @return {string}
*/
fastly.negateConditionStatement = function (statement) { fastly.negateConditionStatement = function (statement) {
return '!(' + statement + ')'; return '!(' + statement + ')';
}; };
/*
* Helper method for constructing Fastly API urls
*
* @param {string} Service id
* @param {number} Version
*
* @return {string}
*/
fastly.getFastlyAPIPrefix = function (serviceId, version) { fastly.getFastlyAPIPrefix = function (serviceId, version) {
return '/service/' + encodeURIComponent(serviceId) + '/version/' + version; return '/service/' + encodeURIComponent(serviceId) + '/version/' + version;
}; };
/*
* getLatestVersion: Get the most recent version for the configured service
*
* @param {callback} Callback with signature *err, latestVersion)
*/
fastly.getLatestVersion = function (cb) { fastly.getLatestVersion = function (cb) {
if (!this.serviceId) { if (!this.serviceId) {
console.error('Failed to get latest version.'); console.error('Failed to get latest version.');
@ -32,6 +60,14 @@ module.exports = function (apiKey, serviceId) {
}); });
}; };
/*
* setCondition: Upsert a Fastly condition entry
* Attempts to PUT and POSTs if the PUT request is a 404
*
* @param {number} Version number
* @param {object} Condition object sent to the API
* @param {callback} Callback for fastly.request
*/
fastly.setCondition = function (version, condition, cb) { fastly.setCondition = function (version, condition, cb) {
if (!this.serviceId) { if (!this.serviceId) {
console.error('Failed to set condition', condition); console.error('Failed to set condition', condition);
@ -59,6 +95,14 @@ module.exports = function (apiKey, serviceId) {
}.bind(this)); }.bind(this));
}; };
/*
* setFastlyHeader: Upsert a Fastly header entry
* Attempts to PUT and POSTs if the PUT request is a 404
*
* @param {number} Version number
* @param {object} Header object sent to the API
* @param {callback} Callback for fastly.request
*/
fastly.setFastlyHeader = function (version, header, cb) { fastly.setFastlyHeader = function (version, header, cb) {
if (!this.serviceId) { if (!this.serviceId) {
console.error('Failed to set header', header); console.error('Failed to set header', header);