From ab0979b958447d4ccfc1a9c70706271e2243fba1 Mon Sep 17 00:00:00 2001 From: Ray Schamp Date: Sat, 16 Apr 2016 13:46:03 -0400 Subject: [PATCH] Add docstrings --- bin/configure-fastly.js | 22 +++++++++++++------ bin/lib/fastly-extended.js | 44 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/bin/configure-fastly.js b/bin/configure-fastly.js index 51592b884..44125ffec 100644 --- a/bin/configure-fastly.js +++ b/bin/configure-fastly.js @@ -24,9 +24,11 @@ var extraAppRoutes = [ '^/[^\/]*\.html' ]; +/* + * Given the relative path to the static directory, return an array of + * patterns matching the files and directories there. + */ 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)); return staticPaths.map( function (pathName) { // 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) { - // 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 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) { - // 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) { var patternCondition = 'req.url ~ "' + pattern + '"'; 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 staticPaths = getStaticPaths(pathToStatic); var viewPaths = getViewPaths(routes); diff --git a/bin/lib/fastly-extended.js b/bin/lib/fastly-extended.js index 8d48e7756..6d9c680f5 100644 --- a/bin/lib/fastly-extended.js +++ b/bin/lib/fastly-extended.js @@ -1,17 +1,45 @@ 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) { var fastly = Fastly(apiKey); fastly.serviceId = serviceId; + /* + * Helper method to NOT a condition statement + * + * @param {string} Statement + * + * @return {string} + */ fastly.negateConditionStatement = function (statement) { return '!(' + statement + ')'; }; + + /* + * Helper method for constructing Fastly API urls + * + * @param {string} Service id + * @param {number} Version + * + * @return {string} + */ fastly.getFastlyAPIPrefix = function (serviceId, 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) { if (!this.serviceId) { 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) { if (!this.serviceId) { console.error('Failed to set condition', condition); @@ -59,6 +95,14 @@ module.exports = function (apiKey, serviceId) { }.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) { if (!this.serviceId) { console.error('Failed to set header', header);