diff --git a/.travis.yml b/.travis.yml index 06ee278..8f94e9a 100755 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ language: node_js node_js: - - "0.10" - "4.0" - 4 + - 5 sudo: false diff --git a/lib/directory.js b/lib/directory.js index 507af80..a5f4ded 100755 --- a/lib/directory.js +++ b/lib/directory.js @@ -1,17 +1,19 @@ +'use strict'; + // Load modules -var Fs = require('fs'); -var Path = require('path'); -var Boom = require('boom'); -var Hoek = require('hoek'); -var Items = require('items'); -var Joi = require('joi'); -var File = require('./file'); +const Fs = require('fs'); +const Path = require('path'); +const Boom = require('boom'); +const Hoek = require('hoek'); +const Items = require('items'); +const Joi = require('joi'); +const File = require('./file'); // Declare internals -var internals = {}; +const internals = {}; internals.schema = Joi.object({ @@ -28,14 +30,14 @@ internals.schema = Joi.object({ exports.handler = function (route, options) { - var settings = Joi.attempt(options, internals.schema, 'Invalid directory handler options (' + route.path + ')'); + const settings = Joi.attempt(options, internals.schema, 'Invalid directory handler options (' + route.path + ')'); Hoek.assert(route.path[route.path.length - 1] === '}', 'The route path must end with a parameter:', route.path); - var normalize = function (paths) { + const normalize = function (paths) { - var normalized = []; - for (var i = 0, il = paths.length; i < il; ++i) { - var path = paths[i]; + const normalized = []; + for (let i = 0; i < paths.length; ++i) { + let path = paths[i]; if (!Hoek.isAbsolutePath(path)) { path = Path.join(route.settings.files.relativeTo, path); @@ -47,17 +49,17 @@ exports.handler = function (route, options) { return normalized; }; - var normalized = (Array.isArray(settings.path) ? normalize(settings.path) : []); // Array or function + const normalized = (Array.isArray(settings.path) ? normalize(settings.path) : []); // Array or function - var indexNames = (settings.index === true) ? ['index.html'] : (settings.index || []); + const indexNames = (settings.index === true) ? ['index.html'] : (settings.index || []); // Declare handler - var handler = function (request, reply) { + const handler = function (request, reply) { - var paths = normalized; + let paths = normalized; if (typeof settings.path === 'function') { - var result = settings.path.call(null, request); + const result = settings.path.call(null, request); if (result instanceof Error) { return reply(result); } @@ -75,8 +77,8 @@ exports.handler = function (route, options) { // Append parameter - var selection = null; - var lastParam = request.paramsArray[request.paramsArray.length - 1]; + let selection = null; + const lastParam = request.paramsArray[request.paramsArray.length - 1]; if (lastParam) { if (lastParam.indexOf('..') !== -1) { return reply(Boom.forbidden()); @@ -94,15 +96,15 @@ exports.handler = function (route, options) { // Generate response - var resource = request.path; - var hasTrailingSlash = (resource[resource.length - 1] === '/'); - var fileOptions = { lookupCompressed: settings.lookupCompressed, etagMethod: settings.etagMethod }; + const resource = request.path; + const hasTrailingSlash = resource.endsWith('/'); + const fileOptions = { lookupCompressed: settings.lookupCompressed, etagMethod: settings.etagMethod }; - Items.serial(paths, function (path, nextPath) { + Items.serial(paths, (path, nextPath) => { path = Path.join(path, selection || ''); - File.load(path, request, fileOptions, function (response) { + File.load(path, request, fileOptions, (response) => { // File loaded successfully @@ -112,7 +114,7 @@ exports.handler = function (route, options) { // Not found - var err = response; + const err = response; if (err.output.statusCode === 404) { if (!settings.defaultExtension) { return nextPath(); @@ -122,7 +124,7 @@ exports.handler = function (route, options) { path = path.slice(0, -1); } - return File.load(path + '.' + settings.defaultExtension, request, fileOptions, function (extResponse) { + return File.load(path + '.' + settings.defaultExtension, request, fileOptions, (extResponse) => { if (!extResponse.isBoom) { return reply(extResponse); @@ -153,10 +155,10 @@ exports.handler = function (route, options) { return reply.redirect(resource + '/'); } - Items.serial(indexNames, function (indexName, nextIndex) { + Items.serial(indexNames, (indexName, nextIndex) => { - var indexFile = Path.join(path, indexName); - File.load(indexFile, request, fileOptions, function (indexResponse) { + const indexFile = Path.join(path, indexName); + File.load(indexFile, request, fileOptions, (indexResponse) => { // File loaded successfully @@ -166,7 +168,7 @@ exports.handler = function (route, options) { // Directory - var err = indexResponse; + const err = indexResponse; if (err.output.statusCode !== 404) { return reply(Boom.badImplementation(indexName + ' is a directory')); } @@ -176,7 +178,7 @@ exports.handler = function (route, options) { return nextIndex(); }); }, - function (/* err */) { + (/* err */) => { // None of the index files were found @@ -188,7 +190,7 @@ exports.handler = function (route, options) { }); }); }, - function (/* err */) { + (/* err */) => { return reply(Boom.notFound()); }); @@ -200,22 +202,22 @@ exports.handler = function (route, options) { internals.generateListing = function (path, resource, selection, hasTrailingSlash, settings, request, reply) { - Fs.readdir(path, function (err, files) { + Fs.readdir(path, (err, files) => { if (err) { return reply(Boom.internal('Error accessing directory', err)); } resource = decodeURIComponent(resource); - var display = Hoek.escapeHtml(resource); - var html = '' + display + '

Directory: ' + display + '