From 3e5ba006ffae1f281caa6c9519c657a5fbf9d650 Mon Sep 17 00:00:00 2001 From: Jose Bolos Date: Tue, 5 Jan 2021 17:41:18 +0000 Subject: [PATCH] Replace var with const/let, respectively This applies to the main code in * `bin` * `dust` * `filters` * `lib` I'm leaving the tests for another day as those are less important. --- bin/compile.js | 68 +++++++++++----------- bin/serve.js | 30 +++++----- dust/and.js | 16 +++--- dust/date-format.js | 6 +- dust/html.js | 2 +- dust/number-format.js | 2 +- dust/or.js | 16 +++--- filters/input/environment.js | 8 +-- lib/benchmark.js | 6 +- lib/config.js | 24 ++++---- lib/content-type.js | 4 +- lib/dispatch.js | 28 ++++----- lib/dust.js | 12 ++-- lib/error-pages.js | 16 +++--- lib/input-filter.js | 12 ++-- lib/logging.js | 30 +++++----- lib/map-route.js | 12 ++-- lib/output-filter.js | 14 ++--- lib/processor.js | 78 ++++++++++++------------- lib/renderer.js | 108 +++++++++++++++++------------------ lib/router.js | 16 +++--- lib/server.js | 38 ++++++------ lib/statsd.js | 22 +++---- lib/watcher.js | 12 ++-- lib/worker.js | 24 ++++---- 25 files changed, 303 insertions(+), 301 deletions(-) diff --git a/bin/compile.js b/bin/compile.js index e0d2d0df..3b7cca1a 100755 --- a/bin/compile.js +++ b/bin/compile.js @@ -1,15 +1,15 @@ #!/usr/bin/env node 'use strict'; -var fs = require('fs'); -var path = require('path'); -var async = require('async'); -var glob = require('glob'); -var mkdirp = require('mkdirp'); -var uglify = require('uglify-js'); -var yargs = require('yargs'); - -var argv = yargs +const fs = require('fs'); +const path = require('path'); +const async = require('async'); +const glob = require('glob'); +const mkdirp = require('mkdirp'); +const uglify = require('uglify-js'); +const yargs = require('yargs'); + +const argv = yargs .options('x', { alias: 'extra-js', type: 'string' @@ -26,15 +26,15 @@ var argv = yargs }) .argv; -var config = require('../lib/config')('production', null, {}); +const config = require('../lib/config')('production', null, {}); // All files named main.js will be minified, extra files to minify can be specified here -var EXTRA_MINIFY_PATHS = []; +let EXTRA_MINIFY_PATHS = []; if (argv['extra-js']) { EXTRA_MINIFY_PATHS = Array.isArray(argv['extra-js']) ? argv['extra-js'] : [argv['extra-js']]; } -var resourceModules; +let resourceModules; if (argv['resource-module']) { resourceModules = ( Array.isArray(argv['resource-module']) ? @@ -44,10 +44,10 @@ if (argv['resource-module']) { config.modules = resourceModules; } -var renderer = require('../lib/renderer')(config); +const renderer = require('../lib/renderer')(config); -var environment = renderer.environment; -var manifest = renderer.manifest; +const environment = renderer.environment; +const manifest = renderer.manifest; environment.cssCompressor = 'csswring'; @@ -57,22 +57,22 @@ process.on('uncaughtException', function (err) { process.exit(128); }); -var compile = function (data, callback) { - var findAssets = function () { - var pattern = new RegExp('\.(' + [].slice.call(arguments, 0).join('|') + ')$'); +const compile = function (data, callback) { + const findAssets = function () { + const pattern = new RegExp('\.(' + [].slice.call(arguments, 0).join('|') + ')$'); return Object.keys(data.assets).filter(function (key) { return key.match(pattern); }); }; - var deleteAsset = function (name) { + const deleteAsset = function (name) { delete manifest.assets[name]; return null; }; - var stylesheets = findAssets('css').map(function (name) { - var asset = environment.findAsset(name); - var content = asset ? asset.toString() : null; + const stylesheets = findAssets('css').map(function (name) { + const asset = environment.findAsset(name); + const content = asset ? asset.toString() : null; if (!content) { return deleteAsset(name); @@ -86,20 +86,20 @@ var compile = function (data, callback) { return stylesheet !== null; }); - var jsToMinify = ['main'].concat(EXTRA_MINIFY_PATHS); + const jsToMinify = ['main'].concat(EXTRA_MINIFY_PATHS); - var javascripts = findAssets('js').filter(function (name) { - for (var i = 0; jsToMinify[i]; ++i) { + const javascripts = findAssets('js').filter(function (name) { + for (let i = 0; jsToMinify[i]; ++i) { if (name.indexOf(jsToMinify[i]) !== -1) { return true; } } return false; }).map(function (name) { - var asset = environment.findAsset(name); - var content = asset ? asset.toString() : null; - var start; - var end; + const asset = environment.findAsset(name); + let content = asset ? asset.toString() : null; + let start; + let end; if (!content) { return deleteAsset(name); @@ -128,15 +128,15 @@ var compile = function (data, callback) { }); }; -var generate = function (callback) { +const generate = function (callback) { async.waterfall([ function (fn) { mkdirp(config.path.publicResources, fn); }, function (dir, fn) { // Glob returns absolute path and we need to strip that out - var readGlobDir = function (p, cb) { - var pth = p.replace(/\\\?/g, '\/'); // Glob must use / as path separator even on windows + const readGlobDir = function (p, cb) { + const pth = p.replace(/\\\?/g, '\/'); // Glob must use / as path separator even on windows glob(pth + '/**/*.*', function (er, files) { if (er) { return cb(er); @@ -150,7 +150,7 @@ var generate = function (callback) { async.concat(environment.paths, readGlobDir, fn); }, function (files) { - var data = null; + let data = null; try { data = manifest.compile(files.filter(function (file) { return /(?:\.([^.]+))?$/.exec(file) === 'scss'; @@ -163,7 +163,7 @@ var generate = function (callback) { } }, function (files) { - var data = null; + let data = null; try { data = manifest.compile(files.map(function (file) { return file.replace(/\.ejs$/, ''); diff --git a/bin/serve.js b/bin/serve.js index 68f85a19..439ce6b7 100755 --- a/bin/serve.js +++ b/bin/serve.js @@ -1,14 +1,14 @@ #!/usr/bin/env node 'use strict'; -var path = require('path'); -var jserve = require('jserve'); -var query = require('qs-middleware'); -var request = require('request'); -var yargs = require('yargs'); +const path = require('path'); +const jserve = require('jserve'); +const query = require('qs-middleware'); +const request = require('request'); +const yargs = require('yargs'); // Parse command-line arguments -var args = yargs +const args = yargs .options('p', { alias: 'port', nargs: 1, @@ -53,7 +53,7 @@ if (args.data && !/^[\/\~]/.test(args.data)) { // Create a JServe application // See https://github.com/rowanmanning/jserve -var app = jserve({ +const app = jserve({ contentType: 'application/x-shunter+json', log: { debug: console.log.bind(console), @@ -107,11 +107,11 @@ function serveRemoteJson(request, response, next) { if (request.path !== '/remote') { return next(); } - var options = { + const options = { url: request.query.url, headers: request.query.headers }; - var error; + let error; if (!options.url || typeof options.url !== 'string') { error = new Error('Invalid query parameter: url'); @@ -140,11 +140,11 @@ function serveRemoteJson(request, response, next) { // Load remote JSON function loadRemoteJson(options, done) { - var requestOptions = { + const requestOptions = { url: options.url, headers: options.headers }; - var error; + let error; request(requestOptions, function (err, response, body) { if (err) { @@ -166,11 +166,13 @@ function loadRemoteJson(options, done) { // Parse a HTTP header string function parseHeaders(headerString) { - var headers = {}; - var headersArray = headerString.split(/[\r\n]+/); + let headers = {}; + const headersArray = headerString.split(/[\r\n]+/); + headersArray.forEach(function (headerString) { - var headerChunks = headerString.split(':'); + const headerChunks = headerString.split(':'); headers[headerChunks.shift().trim()] = headerChunks.join(':').trim(); }); + return headers; } diff --git a/dust/and.js b/dust/and.js index 7f2f1893..64ee0d1a 100644 --- a/dust/and.js +++ b/dust/and.js @@ -11,15 +11,15 @@ function initHelper(dust) { */ dust.helpers.and = function (chunk, context, bodies, params) { params = params || {}; - var alternate = bodies.else; - var keys = context.resolve(params.keys); - var not = context.resolve(params.not); + const alternate = bodies.else; + const keys = context.resolve(params.keys); + const not = context.resolve(params.not); - var checkContext = function (arr) { - var count = 0; - var item; - var nestedKeys; - for (var i = 0; arr[i]; ++i) { + const checkContext = function (arr) { + let count = 0; + let item; + let nestedKeys; + for (let i = 0; arr[i]; ++i) { nestedKeys = arr[i].split('.'); item = context.get(nestedKeys.shift()); // Handle finding nested properties like foo.bar diff --git a/dust/date-format.js b/dust/date-format.js index bb891af9..b2f80826 100644 --- a/dust/date-format.js +++ b/dust/date-format.js @@ -1,13 +1,13 @@ 'use strict'; -var dateformat = require('dateformat'); +const dateformat = require('dateformat'); module.exports = initHelper; function initHelper(dust, renderer, config) { dust.helpers.dateFormat = function (chunk, context, bodies, params) { - var date = null; - var value = null; + let date = null; + let value = null; params = params || {}; diff --git a/dust/html.js b/dust/html.js index 38c170e8..320d2ac0 100644 --- a/dust/html.js +++ b/dust/html.js @@ -4,7 +4,7 @@ module.exports = initFilter; function initFilter(dust) { dust.filters.html = function (value) { - var escapes = { + const escapes = { '<': '<', '>': '>', '"': '"', diff --git a/dust/number-format.js b/dust/number-format.js index bb61d388..e5d46a8c 100644 --- a/dust/number-format.js +++ b/dust/number-format.js @@ -4,7 +4,7 @@ module.exports = initHelper; function initHelper(dust) { dust.helpers.numberFormat = function (chunk, context, bodies, params) { - var num = context.resolve(params.num); + let num = context.resolve(params.num); if (num) { return chunk.write(num.replace(/\B(?=(\d{3})+(?!\d))/g, ',')); } diff --git a/dust/or.js b/dust/or.js index 005cf12a..d7a6e555 100644 --- a/dust/or.js +++ b/dust/or.js @@ -11,15 +11,15 @@ function initHelper(dust) { */ dust.helpers.or = function (chunk, context, bodies, params) { params = params || {}; - var alternate = bodies.else; - var keys = context.resolve(params.keys); - var not = context.resolve(params.not); + const alternate = bodies.else; + const keys = context.resolve(params.keys); + const not = context.resolve(params.not); - var checkContext = function (arr) { - var count = 0; - var item; - var nestedKeys; - for (var i = 0; arr[i]; ++i) { + const checkContext = function (arr) { + let count = 0; + let item; + let nestedKeys; + for (let i = 0; arr[i]; ++i) { nestedKeys = arr[i].split('.'); item = context.get(nestedKeys.shift()); // Handle finding nested properties like foo.bar diff --git a/filters/input/environment.js b/filters/input/environment.js index 560a7723..20bf7889 100644 --- a/filters/input/environment.js +++ b/filters/input/environment.js @@ -1,14 +1,14 @@ 'use strict'; -var qs = require('qs'); +const qs = require('qs'); module.exports = function (config, req, res, data, next) { - var cast = function (params) { - var output = {}; + const cast = function (params) { + let output = {}; params = params || {}; - var transform = function (value) { + const transform = function (value) { var val = (typeof value === 'string') ? value.toLowerCase() : value; if (val === 'true' || val === 'false') { return val === 'true'; diff --git a/lib/benchmark.js b/lib/benchmark.js index 1b41ae4f..4229fcf6 100644 --- a/lib/benchmark.js +++ b/lib/benchmark.js @@ -1,11 +1,11 @@ 'use strict'; module.exports = function (config) { - var statsd = require('./statsd')(config); + const statsd = require('./statsd')(config); return function (req, res, next) { - var timer = config.timer(); - var end = res.end; + const timer = config.timer(); + const end = res.end; res.end = function () { statsd.classifiedTiming(req.url, 'response_time', timer('Request completed ' + req.url)); diff --git a/lib/config.js b/lib/config.js index 0aae79b8..9507a7c5 100644 --- a/lib/config.js +++ b/lib/config.js @@ -5,12 +5,12 @@ module.exports = function (env, config, args) { env = env || process.env.NODE_ENV || 'development'; - var hostname = require('os').hostname(); - var path = require('path'); - var yargs = require('yargs'); - var extend = require('extend'); - var fs = require('fs'); - var shunterRoot = path.dirname(__dirname); + const hostname = require('os').hostname(); + const path = require('path'); + const yargs = require('yargs'); + const extend = require('extend'); + const fs = require('fs'); + const shunterRoot = path.dirname(__dirname); args = args || yargs .options('c', { @@ -94,7 +94,7 @@ module.exports = function (env, config, args) { return require('../package').version; }) .check(function (argv, args) { - var exclude = ['_', '$0']; + const exclude = ['_', '$0']; Object.keys(argv).forEach(function (key) { if (exclude.indexOf(key) === -1 && !args.hasOwnProperty(key)) { @@ -110,9 +110,9 @@ module.exports = function (env, config, args) { }) .argv; - var appRoot = args['source-directory'] || process.cwd(); + const appRoot = args['source-directory'] || process.cwd(); - var defaultConfig = { + const defaultConfig = { argv: args, env: { host: function () { @@ -168,9 +168,9 @@ module.exports = function (env, config, args) { matchExpression: 'application/x-shunter\\+json' }, timer: function () { - var start = Date.now(); + const start = Date.now(); return function (msg) { - var diff = Date.now() - start; + const diff = Date.now() - start; config.log.debug(msg + ' - ' + diff + 'ms'); return diff; }; @@ -184,7 +184,7 @@ module.exports = function (env, config, args) { }; config = extend(true, {}, defaultConfig, config); - var localConfig = path.join(appRoot, 'config', 'local.json'); + const localConfig = path.join(appRoot, 'config', 'local.json'); if (fs.existsSync(localConfig)) { extend(true, config, require(localConfig)); } diff --git a/lib/content-type.js b/lib/content-type.js index d1e7be53..b24b5abb 100644 --- a/lib/content-type.js +++ b/lib/content-type.js @@ -3,9 +3,9 @@ module.exports = function (url, opts) { opts = opts || {}; - var ext = (url.indexOf('.') === -1) ? null : url.split('.').pop().replace(/\?.*/, ''); + const ext = (url.indexOf('.') === -1) ? null : url.split('.').pop().replace(/\?.*/, ''); - var mapping = { + const mapping = { atom: 'application/atom+xml', json: 'application/json', rss: 'application/rss+xml', diff --git a/lib/dispatch.js b/lib/dispatch.js index d913bf0c..b91c613f 100644 --- a/lib/dispatch.js +++ b/lib/dispatch.js @@ -1,27 +1,27 @@ 'use strict'; module.exports = function (config) { - var statsd = require('./statsd')(config); - var outputFilter = require('./output-filter')(config); - var contentType = require('./content-type'); - var http = require('http'); + const statsd = require('./statsd')(config); + const outputFilter = require('./output-filter')(config); + const contentType = require('./content-type'); + const http = require('http'); - var getErrorMessage = function (err) { + const getErrorMessage = function (err) { return err.message || err.toString(); }; - var api = { + const api = { send: function (err, out, req, res, status) { - var timer = config.timer(); - var mimeType; + const timer = config.timer(); + let mimeType; - var logError = function (err, status) { + const logError = function (err, status) { config.log.error(err.stack ? err.stack : getErrorMessage(err)); statsd.increment('errors_' + status); }; - var doSend = function (content) { - var length = Buffer.byteLength(content); + const doSend = function (content) { + const length = Buffer.byteLength(content); if (!mimeType) { mimeType = contentType(req.url, {charset: 'utf-8'}); } @@ -35,7 +35,7 @@ module.exports = function (config) { res.end(content); }; - var getErrorContent = function (err, req, res) { + const getErrorContent = function (err, req, res) { mimeType = contentType('anything.html', {charset: 'utf-8'}); require('./error-pages')(config).getPage(err, req, res, function (content) { doSend(content || api.error(err, status)); @@ -58,8 +58,8 @@ module.exports = function (config) { } }, error: function (err, status) { - var statusMessage = http.STATUS_CODES[status] || 'Internal server error'; - var out = [ + const statusMessage = http.STATUS_CODES[status] || 'Internal server error'; + const out = [ '' + statusMessage + '', '', '
' + status + ' – ' + statusMessage + '
', diff --git a/lib/dust.js b/lib/dust.js index 09af8b99..72f1cd28 100644 --- a/lib/dust.js +++ b/lib/dust.js @@ -1,12 +1,12 @@ 'use strict'; module.exports = function (dust, renderer, config) { - var compileOnDemandCache = {}; + let compileOnDemandCache = {}; // Override dust load mechanism to support namespaced templates // and to not blow up if the template is not found dust.onLoad = function (name, options, callback) { - var getTemplate = function (namespace) { + const getTemplate = function (namespace) { if (namespace.length === 0) { return dust.cache[name]; } @@ -14,11 +14,11 @@ module.exports = function (dust, renderer, config) { return dust.cache[namespace.join('__')] || getTemplate(namespace.slice(0, -2)); }; - var ns = (options && options.namespace) ? options.namespace : ''; + let ns = (options && options.namespace) ? options.namespace : ''; // DEPRECATED: If the namespace starts with the name of the host application, trim it ns = renderer.TEMPLATE_CACHE_KEY_PREFIX + '__' + ns.replace(/^shunter-[^_]+__/, ''); - var template = getTemplate(ns.split('__')); + const template = getTemplate(ns.split('__')); if (template) { callback(null, template); } else if (config.argv['compile-on-demand'] && !compileOnDemandCache[name]) { @@ -32,7 +32,7 @@ module.exports = function (dust, renderer, config) { }; dust.helpers.assetPath = function (chunk, context, bodies, params) { - var path = renderer.assetPath(context.resolve(params.src)); + const path = renderer.assetPath(context.resolve(params.src)); if (!path) { return chunk; } @@ -40,7 +40,7 @@ module.exports = function (dust, renderer, config) { }; dust.helpers.linkPath = function (chunk, context, bodies, params) { - var path = renderer.linkPath(context.resolve(params.src)); + const path = renderer.linkPath(context.resolve(params.src)); if (!path) { return chunk; } diff --git a/lib/error-pages.js b/lib/error-pages.js index dc301f34..38af13f2 100644 --- a/lib/error-pages.js +++ b/lib/error-pages.js @@ -12,9 +12,9 @@ */ module.exports = function (config) { - var renderer = require('./renderer')(config); + const renderer = require('./renderer')(config); - var renderPageForContext = function (req, res, templateContext, fn) { + const renderPageForContext = function (req, res, templateContext, fn) { renderer.render(req, res, templateContext, function (err, out) { if (err || !out) { config.log.warn('Rendering custom error page failed (misconfiguration?)'); @@ -25,7 +25,7 @@ module.exports = function (config) { }); }; - var getTemplateContext = function (err, req, fn) { + const getTemplateContext = function (err, req, fn) { if (!err) { fn(null); return; @@ -36,11 +36,11 @@ module.exports = function (config) { err.status = 500; } - var key = typeof err.status === 'string' ? err.status : err.status.toString(); - var userData = config.errorPages; - var layout = userData.errorLayouts.hasOwnProperty(key) ? userData.errorLayouts[key] : userData.errorLayouts.default; + const key = typeof err.status === 'string' ? err.status : err.status.toString(); + const userData = config.errorPages; + const layout = userData.errorLayouts.hasOwnProperty(key) ? userData.errorLayouts[key] : userData.errorLayouts.default; - var templateContext = { + const templateContext = { layout: { template: layout, namespace: 'custom-errors' @@ -56,7 +56,7 @@ module.exports = function (config) { }; if (userData.staticData) { - for (var key in userData.staticData) { + for (let key in userData.staticData) { // Prevent the user clobbering required templateContext keys & proto if (userData.staticData.hasOwnProperty(key) && !(key in templateContext)) { templateContext[key] = userData.staticData[key]; diff --git a/lib/input-filter.js b/lib/input-filter.js index de251c50..62ee8694 100644 --- a/lib/input-filter.js +++ b/lib/input-filter.js @@ -19,13 +19,13 @@ module.exports = function (config) { }, run: function (req, res, data, callback) { - var self = this; + const self = this; - var runner = function (filters, data) { - var remain; - var filter; - var arity; - var cb; + const runner = function (filters, data) { + let remain; + let filter; + let arity; + let cb; if (filters.length > 0) { filter = filters[0]; diff --git a/lib/logging.js b/lib/logging.js index d23c935a..54ca2728 100644 --- a/lib/logging.js +++ b/lib/logging.js @@ -1,15 +1,15 @@ 'use strict'; -var path = require('path'); -var eachModule = require('each-module'); -var winston = require('winston'); +const path = require('path'); +const eachModule = require('each-module'); +const winston = require('winston'); module.exports = function (config) { - var moduleLoadErrors = []; + const moduleLoadErrors = []; - var getArrayOfValidModulesByDirName = function (finalDir) { - var modules = []; - var modulePusher = function (moduleName, moduleExports, file) { + const getArrayOfValidModulesByDirName = function (finalDir) { + let modules = []; + const modulePusher = function (moduleName, moduleExports, file) { if (typeof moduleExports === 'function') { modules.push(moduleExports); } else { @@ -18,9 +18,9 @@ module.exports = function (config) { }; // User-defined configurations take priority, but fallback to defaults if all seem invalid - var locations = [config.path.root, config.path.shunterRoot]; // Config.path.root = users files - for (var i = 0; i < locations.length; i++) { - var localPath = path.join(locations[i], config.structure.logging, finalDir); + const locations = [config.path.root, config.path.shunterRoot]; // Config.path.root = users files + for (let i = 0; i < locations.length; i++) { + const localPath = path.join(locations[i], config.structure.logging, finalDir); eachModule(localPath, modulePusher); if (modules.length > 0) { break; @@ -31,7 +31,7 @@ module.exports = function (config) { return { getLogger: function () { - var validateTransports = function (arModules) { + const validateTransports = function (arModules) { return arModules.map(function (fnModule) { return fnModule(config); }).filter(function (obModule) { @@ -39,16 +39,16 @@ module.exports = function (config) { }); }; - var validateFilters = function (arModules) { + const validateFilters = function (arModules) { return arModules.filter(function (fnModule) { return typeof fnModule('debug', 'a message') === 'string'; }); }; - var transports = getArrayOfValidModulesByDirName(config.structure.loggingTransports); - var filters = getArrayOfValidModulesByDirName(config.structure.loggingFilters); + const transports = getArrayOfValidModulesByDirName(config.structure.loggingTransports); + const filters = getArrayOfValidModulesByDirName(config.structure.loggingFilters); - var loggerInstance = new winston.Logger({ + const loggerInstance = new winston.Logger({ transports: validateTransports(transports), filters: validateFilters(filters) }); diff --git a/lib/map-route.js b/lib/map-route.js index 784e1e67..3b810b81 100644 --- a/lib/map-route.js +++ b/lib/map-route.js @@ -1,10 +1,10 @@ 'use strict'; module.exports = function (address) { - var url = require('url'); + const url = require('url'); - var parseUrl = function (address) { - var protocol = url.parse(address).protocol || null; + const parseUrl = function (address) { + const protocol = url.parse(address).protocol || null; if (protocol === 'http:' || protocol === 'https:') { return url.parse(address); @@ -13,9 +13,9 @@ module.exports = function (address) { return url.parse('http://' + address); }; - var map = function (address) { - var mappedRoute = {}; - var route = parseUrl(address); + const map = function (address) { + const route = parseUrl(address); + let mappedRoute = {}; mappedRoute.protocol = route.protocol || null; mappedRoute.host = route.hostname || null; diff --git a/lib/output-filter.js b/lib/output-filter.js index c899066f..34752c34 100644 --- a/lib/output-filter.js +++ b/lib/output-filter.js @@ -1,28 +1,28 @@ 'use strict'; -var path = require('path'); -var eachModule = require('each-module'); +const path = require('path'); +const eachModule = require('each-module'); module.exports = function (config) { - var filters = []; + let filters = []; - var modulePaths = config.modules.map(function (module) { + const modulePaths = config.modules.map(function (module) { return path.join(config.path.root, 'node_modules', module); }); modulePaths.push(config.path.root); modulePaths.unshift(config.path.shunterRoot); modulePaths.forEach(function (modulePath) { - var filterPath = path.join(modulePath, config.structure.filters, config.structure.filtersOutput); + const filterPath = path.join(modulePath, config.structure.filters, config.structure.filtersOutput); eachModule(filterPath, function (name, runFilter) { filters.push(runFilter); }); }); return function (content, contentType, req) { - var output; + let output; - for (var i = 0; filters[i]; ++i) { + for (let i = 0; filters[i]; ++i) { output = filters[i](content, contentType, req, config); if (typeof output === 'string') { content = output; diff --git a/lib/processor.js b/lib/processor.js index 7da02ad7..4ac29233 100644 --- a/lib/processor.js +++ b/lib/processor.js @@ -2,22 +2,22 @@ // Middlewares module.exports = function (config, renderer) { - var pkg = require('../package.json'); - var httpProxy = require('http-proxy'); - var rewriteRedirect = config.argv['rewrite-redirect'] || false; - var protocolRewrite = config.argv['rewrite-protocol'] || null; - var proxy = httpProxy.createProxyServer({ + const pkg = require('../package.json'); + const httpProxy = require('http-proxy'); + const rewriteRedirect = config.argv['rewrite-redirect'] || false; + const protocolRewrite = config.argv['rewrite-protocol'] || null; + const proxy = httpProxy.createProxyServer({ autoRewrite: rewriteRedirect, protocolRewrite: protocolRewrite }); /* eslint-disable import/no-unresolved */ - var deployTimestamp = require('../timestamp').value; + const deployTimestamp = require('../timestamp').value; /* eslint-enable import/no-unresolved */ - var dispatch = require('./dispatch')(config); - var statsd = require('./statsd')(config); - var router = require('./router')(config); + const dispatch = require('./dispatch')(config); + const statsd = require('./statsd')(config); + const router = require('./router')(config); - var proxyErr = false; + let proxyErr = false; proxy.on('proxyRes', function (proxyRes) { if (proxyRes.statusCode >= 400) { proxyErr = new Error(proxyRes.statusMessage); @@ -27,9 +27,9 @@ module.exports = function (config, renderer) { } }); - var parseJson = function (data) { - var json = null; - var err = null; + const parseJson = function (data) { + let json = null; + let err = null; try { json = JSON.parse(data); @@ -43,7 +43,7 @@ module.exports = function (config, renderer) { }; }; - var processor = { + const processor = { timestamp: function (req, res, next) { req.headers['X-Shunter-Deploy-Timestamp'] = config.env.isDevelopment() ? Date.now() : deployTimestamp; next(); @@ -55,43 +55,43 @@ module.exports = function (config, renderer) { }, intercept: function (req, res, next) { - var data = []; - var status = null; + let data = []; + let status = null; - var trigger = config.trigger || {}; + const trigger = config.trigger || {}; - var shouldIntercept = function () { - var method = req.method ? req.method.toUpperCase() : 'GET'; - var acceptedMethod = method === 'GET' || method === 'POST'; + const shouldIntercept = function () { + const method = req.method ? req.method.toUpperCase() : 'GET'; + const acceptedMethod = method === 'GET' || method === 'POST'; - var triggerHeader = trigger.header || 'Content-type'; - var matchExpression = trigger.matchExpression || 'application/x-shunter\\+json'; + const triggerHeader = trigger.header || 'Content-type'; + const matchExpression = trigger.matchExpression || 'application/x-shunter\\+json'; - var headerValue = res.getHeader(triggerHeader); - var acceptedHeader = headerValue && (headerValue.match(new RegExp(matchExpression, 'i'))); + const headerValue = res.getHeader(triggerHeader); + const acceptedHeader = headerValue && (headerValue.match(new RegExp(matchExpression, 'i'))); return acceptedMethod && acceptedHeader; }; - var write = function (chunk) { + const write = function (chunk) { data.push(chunk); }; - var endProxyError = function () { + const endProxyError = function () { res.writeHead = res.__originalWriteHead; res.write = res.__originalWrite; res.end = res.__originalEnd; return dispatch.send(proxyErr, '', req, res); }; - var endIntercept = function () { - var timer = config.timer(); + const endIntercept = function () { + let timer = config.timer(); if (req.__proxyTimingFunctionBodyReceived) { statsd.classifiedTiming(req.url, 'proxy_body_received', req.__proxyTimingFunctionBodyReceived('Received body ' + req.url)); } - var rawJson = Buffer.concat(data).toString('utf8'); - var json = parseJson(rawJson); + const rawJson = Buffer.concat(data).toString('utf8'); + const json = parseJson(rawJson); statsd.classifiedGauge(req.url, 'json_size', Buffer.byteLength(rawJson)); statsd.classifiedTiming(req.url, 'parsing', timer('Parsing JSON ' + req.url)); @@ -105,7 +105,7 @@ module.exports = function (config, renderer) { } else { if (config.jsonViewParameter && req.query[config.jsonViewParameter]) { req.isJson = true; - var jsonOutput = JSON.stringify(json.data, null, '\t'); + const jsonOutput = JSON.stringify(json.data, null, '\t'); return dispatch.send(null, jsonOutput, req, res, status); } @@ -117,7 +117,7 @@ module.exports = function (config, renderer) { } }; - var writeHead = function (code) { + const writeHead = function (code) { statsd.increment('total_requests'); if (shouldIntercept()) { @@ -151,9 +151,9 @@ module.exports = function (config, renderer) { }, api: function (req, res) { - var name = req.url.replace(/^\/+/, '').replace(/\?.*/, '').replace(/\/+/g, '__'); - var body = req.body; - var err = null; + let name = req.url.replace(/^\/+/, '').replace(/\?.*/, '').replace(/\/+/g, '__'); + const body = req.body; + let err = null; if (!name && body.layout && body.layout.template) { name = body.layout.template; @@ -173,11 +173,11 @@ module.exports = function (config, renderer) { proxy: function (req, res) { req.headers = req.headers || {}; - var host = req.headers.host || ''; - var route = router.map(host, req.url); - var err = null; + let host = req.headers.host || ''; + const route = router.map(host, req.url); + let err = null; - var rewriteRequestHeaders = function () { + const rewriteRequestHeaders = function () { if (route.changeOrigin) { req.headers['X-Orig-Host'] = host; } diff --git a/lib/renderer.js b/lib/renderer.js index fd67a160..4c2fe60b 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -1,29 +1,29 @@ 'use strict'; module.exports = function (config) { - var dust = require('dustjs-helpers'); - var mincer = require('mincer'); - var fs = require('fs'); - var path = require('path'); - var glob = require('glob'); - var inputFilters = require('./input-filter')(config); - var eachModule = require('each-module'); - - var hostAppDir = config.path.root; - var modulesPaths = config.modules.map(function (module) { + const dust = require('dustjs-helpers'); + const mincer = require('mincer'); + const fs = require('fs'); + const path = require('path'); + const glob = require('glob'); + const inputFilters = require('./input-filter')(config); + const eachModule = require('each-module'); + + const hostAppDir = config.path.root; + const modulesPaths = config.modules.map(function (module) { return path.join(hostAppDir, 'node_modules', module); }); mincer.logger.use(config.log); - var initExtensions = function () { - var paths = [].slice.call(arguments, 0, -1); - var callback = arguments[arguments.length - 1]; - var locations = [config.path.shunterRoot].concat(modulesPaths, hostAppDir); + const initExtensions = function () { + const paths = [].slice.call(arguments, 0, -1); + const callback = arguments[arguments.length - 1]; + const locations = [config.path.shunterRoot].concat(modulesPaths, hostAppDir); if (typeof callback === 'function') { locations.forEach(function (dir) { - var extensionPath = path.join.apply(path, [dir].concat(paths)); + const extensionPath = path.join.apply(path, [dir].concat(paths)); eachModule(extensionPath, callback); }); } @@ -43,17 +43,17 @@ module.exports = function (config) { } }); - var environment = new mincer.Environment(); - var manifest = new mincer.Manifest(environment, config.path.publicResources); + const environment = new mincer.Environment(); + const manifest = new mincer.Manifest(environment, config.path.publicResources); // Host app can be shunter-based app or manifest, so rely on root - var assetPath = function (name) { - var isProduction = config.env.isProduction(); - var asset = (isProduction) ? manifest.assets[name] : environment.findAsset(name); + const assetPath = function (name) { + const isProduction = config.env.isProduction(); + const asset = (isProduction) ? manifest.assets[name] : environment.findAsset(name); if (!asset) { return ''; } - var mountPath = config.argv && (config.argv['mount-path'] || ''); + const mountPath = config.argv && (config.argv['mount-path'] || ''); return ( isProduction ? @@ -62,25 +62,25 @@ module.exports = function (config) { ); }; - var linkPath = function (link) { - var mountPath = config.argv && (config.argv['mount-path'] || ''); + const linkPath = function (link) { + const mountPath = config.argv && (config.argv['mount-path'] || ''); return path.join(mountPath, link); }; environment.registerHelper('asset_path', assetPath); environment.registerHelper('link_path', linkPath); // Assets must be loaded in order (e.g. styles relies on images already being available) - var assetTypes = [config.structure.fonts, config.structure.images, config.structure.styles, config.structure.scripts]; - var themeResourcesPath = config.path.resources; + const assetTypes = [config.structure.fonts, config.structure.images, config.structure.styles, config.structure.scripts]; + const themeResourcesPath = config.path.resources; // NB: risk of mincer clashes until stuff is moved out of proxy // for each asset type, add host then module. this order is important assetTypes.forEach(function (assetType) { - var newPath = path.join(themeResourcesPath, assetType); + const newPath = path.join(themeResourcesPath, assetType); if (fs.existsSync(newPath)) { environment.appendPath(newPath); } modulesPaths.reverse().forEach(function (modulePath) { - var newPath = path.join(modulePath, 'resources', assetType); + const newPath = path.join(modulePath, 'resources', assetType); if (fs.existsSync(newPath)) { environment.appendPath(newPath); } @@ -117,12 +117,12 @@ module.exports = function (config) { }, compileFile: function (fp) { - var ext = config.structure.templateExt; - var id; - var compiled; - var sandboxNS; - var splitPath; - var timer; + const ext = config.structure.templateExt; + let id; + let compiled; + let sandboxNS; + let splitPath; + let timer; if (path.extname(fp) === ext) { sandboxNS = fp; @@ -152,7 +152,7 @@ module.exports = function (config) { // Just used for testing? compilePaths: function (paths) { - var self = this; + const self = this; if (typeof paths === 'string') { paths = [].slice.call(arguments, 0); } @@ -166,24 +166,24 @@ module.exports = function (config) { }, compileTemplates: function (forTests) { - var fullFiles = []; + let fullFiles = []; // Get all defined modules templates first (in order defined by the host app) config.modules.forEach(function (module) { - var moduleResourcesPath = (forTests) ? forTests : path.join(hostAppDir, 'node_modules', module); + const moduleResourcesPath = (forTests) ? forTests : path.join(hostAppDir, 'node_modules', module); // Must use / for glob even with windows - var templates = [moduleResourcesPath, config.structure.templates, '**', ('*' + config.structure.templateExt)].join('/'); + const templates = [moduleResourcesPath, config.structure.templates, '**', ('*' + config.structure.templateExt)].join('/'); fullFiles = fullFiles.concat(glob.sync(templates, {})); }); // Then get the app's templates // (must use / for glob even with windows) - var templates = [hostAppDir, config.structure.templates, '**', ('*' + config.structure.templateExt)].join('/'); + const templates = [hostAppDir, config.structure.templates, '**', ('*' + config.structure.templateExt)].join('/'); fullFiles = fullFiles.concat(glob.sync(templates, {})); this.compileFileList(fullFiles); }, compileOnDemand: function (name) { - var self = this; - var localPath = path.join(config.structure.templates, name.split('__').join(path.sep) + config.structure.templateExt); + const self = this; + const localPath = path.join(config.structure.templates, name.split('__').join(path.sep) + config.structure.templateExt); config.modules.map(function (module) { return path.join(hostAppDir, 'node_modules', module, localPath); }).concat([ @@ -197,19 +197,19 @@ module.exports = function (config) { // Accepts an array of files with full paths, sends each to compile compileFileList: function (fileArr) { - var self = this; + const self = this; fileArr.forEach(function (file) { self.compileFile(file); }); }, watchTemplates: function () { - var watchTree; - var watcher; - var folders = [config.structure.templates]; - var self = this; + let watchTree; + let watcher; + let folders = [config.structure.templates]; + let self = this; - var compile = function (fp) { + const compile = function (fp) { self.compileFile(fp); }; @@ -224,12 +224,12 @@ module.exports = function (config) { }, watchDustExtensions: function () { - var watchTree; - var watcher; - var folders = [config.path.dust]; - var self = this; + let watchTree; + let watcher; + let folders = [config.path.dust]; + let self = this; - var compile = function (fp) { + const compile = function (fp) { config.log.info('Loading Dust extension ' + fp); delete require.cache[require.resolve(fp)]; require(fp)(dust, self, config); @@ -246,14 +246,14 @@ module.exports = function (config) { }, render: function (req, res, data, callback) { - var name = (data && data.layout && data.layout.template) ? data.layout.template : 'layout'; + const name = (data && data.layout && data.layout.template) ? data.layout.template : 'layout'; this.renderPartial(name, req, res, data, callback); }, renderPartial: function (partial, req, res, data, callback) { inputFilters.run(req, res, data, function (data) { - var ns = (data && data.layout && data.layout.namespace) ? data.layout.namespace : null; - var base = dust.makeBase({ + const ns = (data && data.layout && data.layout.namespace) ? data.layout.namespace : null; + const base = dust.makeBase({ namespace: ns }, { namespace: ns diff --git a/lib/router.js b/lib/router.js index 90dfee26..9ba2d95d 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1,12 +1,12 @@ 'use strict'; module.exports = function (config) { - var extend = require('extend'); - var mapRoute = require('./map-route'); - var routes = config.routes; - var defaultRoute = config.argv['route-config'] || 'default'; - var localhost = {}; - var override; + const extend = require('extend'); + const mapRoute = require('./map-route'); + let routes = config.routes; + let defaultRoute = config.argv['route-config'] || 'default'; + let localhost = {}; + let override; if (config.argv['origin-override']) { localhost.changeOrigin = true; @@ -21,7 +21,7 @@ module.exports = function (config) { } } - var matchRoute = function (pattern, url) { + const matchRoute = function (pattern, url) { if (pattern.match(/^\/.+?\/$/)) { return url.match(new RegExp(pattern.replace(/^\//, '').replace(/\/$/, ''), 'i')); } @@ -37,7 +37,7 @@ module.exports = function (config) { if (!routes.hasOwnProperty(domain)) { return null; } - for (var pattern in routes[domain]) { + for (let pattern in routes[domain]) { if (pattern !== defaultRoute && routes[domain].hasOwnProperty(pattern)) { if (matchRoute(pattern, url)) { return routes[domain][pattern]; diff --git a/lib/server.js b/lib/server.js index dcc5aeb9..7749c853 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,14 +1,14 @@ 'use strict'; module.exports = function (config) { - var cluster = require('cluster'); - var fs = require('fs'); - var path = require('path'); + const cluster = require('cluster'); + const fs = require('fs'); + const path = require('path'); var config = require('./config')(process.env.NODE_ENV, config); - var SHUTDOWN_TIMEOUT = 10000; + const SHUTDOWN_TIMEOUT = 10000; - var init = function (count, callback) { + const init = function (count, callback) { cluster.on('exit', function (worker, code) { if (code !== 0) { config.log.error(worker.process.pid + ' died with error code ' + code + ', starting new worker...'); @@ -16,29 +16,29 @@ module.exports = function (config) { } }); - var ready = 0; - var listening = function () { + let ready = 0; + const listening = function () { if (++ready === count) { callback(); } }; - for (var i = 0; i < count; ++i) { + for (let i = 0; i < count; ++i) { cluster.fork().on('listening', listening); } }; - var restart = function () { - var replace = function (workers) { + const restart = function () { + const replace = function (workers) { if (workers.length === 0) { config.log.info('All replacement workers now running'); return; } - var parasite = cluster.workers[workers[0]]; - var worker = cluster.fork(); - var pid = parasite.process.pid; - var timeout; + const parasite = cluster.workers[workers[0]]; + const worker = cluster.fork(); + const pid = parasite.process.pid; + let timeout; parasite.on('disconnect', function () { config.log.info('Shutdown complete for ' + pid); @@ -59,8 +59,8 @@ module.exports = function (config) { replace(Object.keys(cluster.workers)); }; - var saveProcessId = function () { - var pid = process.pid; + const saveProcessId = function () { + const pid = process.pid; fs.writeFile(path.join(config.path.root, 'shunter.pid'), pid, function (err) { if (err) { config.log.error('Error saving shunter.pid file for process ' + pid + ' ' + (err.message || err.toString())); @@ -70,12 +70,12 @@ module.exports = function (config) { }); }; - var clearProcessId = function () { + const clearProcessId = function () { config.log.debug('Deleting old shunter.pid file'); fs.unlinkSync(path.join(config.path.root, 'shunter.pid')); }; - var saveTimestamp = function () { + const saveTimestamp = function () { fs.writeFileSync(path.join(config.path.shunterRoot, 'timestamp.json'), '{"value":' + Date.now() + '}'); }; @@ -85,7 +85,7 @@ module.exports = function (config) { }, start: function () { if (cluster.isMaster) { - var childProcesses = Math.min( + const childProcesses = Math.min( require('os').cpus().length, config.argv['max-child-processes'] ); diff --git a/lib/statsd.js b/lib/statsd.js index b0f228de..2486ae62 100644 --- a/lib/statsd.js +++ b/lib/statsd.js @@ -1,17 +1,17 @@ 'use strict'; module.exports = function (config) { - var SDC = require('statsd-client'); - var client = new SDC(config.statsd); + const SDC = require('statsd-client'); + const client = new SDC(config.statsd); - var mappings = (config.statsd && config.statsd.mappings ? config.statsd.mappings : []).map(function (item) { + const mappings = (config.statsd && config.statsd.mappings ? config.statsd.mappings : []).map(function (item) { if (item.pattern && typeof item.pattern === 'string') { item.pattern = new RegExp(item.pattern); } return item; }); - var methods = [ + const methods = [ 'timing', 'increment', 'decrement', @@ -21,12 +21,12 @@ module.exports = function (config) { 'set' ]; - var obj = { + const obj = { buildMetricNameForUrl: function (url, name) { if (mappings.length === 0) { return name; } - for (var i = 0; mappings[i]; ++i) { + for (let i = 0; mappings[i]; ++i) { if (url.match(mappings[i].pattern)) { return name + '_' + mappings[i].name; } @@ -34,17 +34,17 @@ module.exports = function (config) { return name; } }; - var slice = Array.prototype.slice; + const slice = Array.prototype.slice; - var noop = function () { }; - var mock = config && config.statsd ? config.statsd.mock : true; + const noop = function () { }; + const mock = config && config.statsd ? config.statsd.mock : true; methods.forEach(function (method) { - var prefixedMethod = 'classified' + method.charAt(0).toUpperCase() + method.substring(1); + const prefixedMethod = 'classified' + method.charAt(0).toUpperCase() + method.substring(1); obj[method] = (mock) ? noop : client[method].bind(client); obj[prefixedMethod] = (mock) ? noop : function (url, name) { - var args = slice.call(arguments, 1); + const args = slice.call(arguments, 1); args[0] = obj.buildMetricNameForUrl(url, name); client[method].apply(client, args); }; diff --git a/lib/watcher.js b/lib/watcher.js index a90a52bb..85b69f64 100644 --- a/lib/watcher.js +++ b/lib/watcher.js @@ -2,15 +2,15 @@ // Watch a file tree for changes using Gaze module.exports = function (extension) { - var constructGlob = function (dirpath) { - var glob = '/**/*' + extension; + const constructGlob = function (dirpath) { + const glob = '/**/*' + extension; return dirpath + glob; }; - var watchTree = function (directories, log) { - var Gaze = require('gaze'); - var watchedDirs = (typeof directories === 'string') ? constructGlob(directories) : directories.map(constructGlob); - var watcher = new Gaze(watchedDirs); + const watchTree = function (directories, log) { + const Gaze = require('gaze'); + const watchedDirs = (typeof directories === 'string') ? constructGlob(directories) : directories.map(constructGlob); + const watcher = new Gaze(watchedDirs); watcher.on('added', function (path) { watcher.emit('fileCreated', path); diff --git a/lib/worker.js b/lib/worker.js index 522906d5..854cf04b 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -1,18 +1,18 @@ 'use strict'; -var path = require('path'); +const path = require('path'); module.exports = function (config) { - var connect = require('connect'); - var bodyParser = require('body-parser'); - var cookieParser = require('cookie-parser'); - var serveStatic = require('serve-static'); - var query = require('qs-middleware'); - var app = connect(); + const connect = require('connect'); + const bodyParser = require('body-parser'); + const cookieParser = require('cookie-parser'); + const serveStatic = require('serve-static'); + const query = require('qs-middleware'); + const app = connect(); - var benchmark = require('./benchmark')(config); - var renderer = require('./renderer')(config); - var processor = require('./processor')(config, renderer); + const benchmark = require('./benchmark')(config); + const renderer = require('./renderer')(config); + const processor = require('./processor')(config, renderer); renderer.initDustExtensions(); if (!config.argv['compile-on-demand']) { @@ -31,8 +31,8 @@ module.exports = function (config) { app.use(cookieParser()); app.use(config.web.tests, serveStatic(config.path.tests)); - var assetMountPath = config.argv && (config.argv['mount-path'] || ''); - var endpointsMountPath = assetMountPath || '/'; + const assetMountPath = config.argv && (config.argv['mount-path'] || ''); + const endpointsMountPath = assetMountPath || '/'; if (config.env.isProduction()) { app.use(path.join(assetMountPath, config.web.public), serveStatic(config.path.public, {