New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update dependencies. Update codebase to ES6/Node4 #51
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
language: node_js | ||
|
||
node_js: | ||
- "0.10" | ||
- "4.0" | ||
- 4 | ||
- 5 | ||
|
||
sudo: false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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,15 @@ 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 = []; | ||
const il = paths.length; | ||
for (let i = 0; i < il; ++i) { | ||
let path = paths[i]; | ||
|
||
if (!Hoek.isAbsolutePath(path)) { | ||
path = Path.join(route.settings.files.relativeTo, path); | ||
|
@@ -47,17 +50,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 +78,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 +97,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[resource.length - 1] === '/'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 to going for readability. |
||
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 +115,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 +125,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 +156,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 +169,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 +179,7 @@ exports.handler = function (route, options) { | |
return nextIndex(); | ||
}); | ||
}, | ||
function (/* err */) { | ||
(/* err */) => { | ||
|
||
// None of the index files were found | ||
|
||
|
@@ -188,7 +191,7 @@ exports.handler = function (route, options) { | |
}); | ||
}); | ||
}, | ||
function (/* err */) { | ||
(/* err */) => { | ||
|
||
return reply(Boom.notFound()); | ||
}); | ||
|
@@ -200,22 +203,23 @@ 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 = '<html><head><title>' + display + '</title></head><body><h1>Directory: ' + display + '</h1><ul>'; | ||
const display = Hoek.escapeHtml(resource); | ||
let html = '<html><head><title>' + display + '</title></head><body><h1>Directory: ' + display + '</h1><ul>'; | ||
|
||
if (selection) { | ||
var parent = resource.substring(0, resource.lastIndexOf('/', resource.length - (hasTrailingSlash ? 2 : 1))) + '/'; | ||
const parent = resource.substring(0, resource.lastIndexOf('/', resource.length - (hasTrailingSlash ? 2 : 1))) + '/'; | ||
html += '<li><a href="' + internals.pathEncode(parent) + '">Parent Directory</a></li>'; | ||
} | ||
|
||
for (var i = 0, il = files.length; i < il; ++i) { | ||
const il = files.length; | ||
for (let i = 0; i < il; ++i) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for length here. |
||
if (settings.showHidden || | ||
!internals.isFileHidden(files[i])) { | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're also not memoizing the length anymore unless necessary.