Skip to content
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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,8 +1,8 @@
language: node_js

node_js:
- "0.10"
- "4.0"
- 4
- 5

sudo: false
80 changes: 42 additions & 38 deletions 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({
Expand All @@ -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) {
Copy link
Contributor

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.

let path = paths[i];

if (!Hoek.isAbsolutePath(path)) {
path = Path.join(route.settings.files.relativeTo, path);
Expand All @@ -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);
}
Expand All @@ -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());
Expand All @@ -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] === '/');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can use resource.endsWith('/') for that, @cjihrig @kpdecker any clue about performance on this ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think endsWith is more readable. I'll change it back if needed.

Copy link
Contributor

Choose a reason for hiding this comment

The 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

Expand All @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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

Expand All @@ -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'));
}
Expand All @@ -176,7 +179,7 @@ exports.handler = function (route, options) {
return nextIndex();
});
},
function (/* err */) {
(/* err */) => {

// None of the index files were found

Expand All @@ -188,7 +191,7 @@ exports.handler = function (route, options) {
});
});
},
function (/* err */) {
(/* err */) => {

return reply(Boom.notFound());
});
Expand All @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for length here.

if (settings.showHidden ||
!internals.isFileHidden(files[i])) {

Expand Down
47 changes: 25 additions & 22 deletions lib/etag.js
@@ -1,39 +1,41 @@
'use strict';

// Load modules

var Fs = require('fs');
var Crypto = require('crypto');
var Boom = require('boom');
var Hoek = require('hoek');
var LruCache = require('lru-cache');
const Fs = require('fs');
const Crypto = require('crypto');
const Boom = require('boom');
const Hoek = require('hoek');
const LruCache = require('lru-cache');


// Declare internals

var internals = {};
const internals = {};


internals.computeHashed = function (response, stat, next) {

var etags = response.request.server.plugins.inert._etags;
const etags = response.request.server.plugins.inert._etags;
if (!etags) {
return next(null, null);
}

// Use stat info for an LRU cache key.

var path = response.source.path;
var cachekey = [path, stat.ino, stat.size, stat.mtime.getTime()].join('-');
const path = response.source.path;
const cachekey = [path, stat.ino, stat.size, stat.mtime.getTime()].join('-');

// The etag hashes the file contents in order to be consistent across distributed deployments

var cachedEtag = etags.get(cachekey);
const cachedEtag = etags.get(cachekey);
if (cachedEtag) {
return next(null, cachedEtag);
}

var pendings = response.request.server.plugins.inert._pendings;
var pendingsId = '+' + cachekey; // Prefix to avoid conflicts with JS internals (e.g. __proto__)
var nexts = pendings[pendingsId];
const pendings = response.request.server.plugins.inert._pendings;
const pendingsId = '+' + cachekey; // Prefix to avoid conflicts with JS internals (e.g. __proto__)
let nexts = pendings[pendingsId];
if (nexts) {
return nexts.push(next);
}
Expand All @@ -43,7 +45,7 @@ internals.computeHashed = function (response, stat, next) {
nexts = [next];
pendings[pendingsId] = nexts;

internals.hashFile(response, function (err, hash) {
internals.hashFile(response, (err, hash) => {

if (!err) {
etags.set(cachekey, hash);
Expand All @@ -52,7 +54,8 @@ internals.computeHashed = function (response, stat, next) {
// Call pending callbacks

delete pendings[pendingsId];
for (var i = 0, il = nexts.length; i < il; ++i) {
const il = nexts.length;
for (let i = 0; i < il; ++i) {
Hoek.nextTick(nexts[i])(err, hash);
}
});
Expand All @@ -61,13 +64,13 @@ internals.computeHashed = function (response, stat, next) {

internals.hashFile = function (response, callback) {

var hash = Crypto.createHash('sha1');
const hash = Crypto.createHash('sha1');
hash.setEncoding('hex');

var fileStream = Fs.createReadStream(response.source.path, { fd: response.source.fd, autoClose: false });
const fileStream = Fs.createReadStream(response.source.path, { fd: response.source.fd, autoClose: false });
fileStream.pipe(hash);

var done = function (err) {
let done = function (err) {

if (err) {
return callback(Boom.wrap(err, null, 'Failed to hash file'));
Expand All @@ -85,21 +88,21 @@ internals.hashFile = function (response, callback) {

internals.computeSimple = function (response, stat, next) {

var size = stat.size.toString(16);
var mtime = stat.mtime.getTime().toString(16);
const size = stat.size.toString(16);
const mtime = stat.mtime.getTime().toString(16);

return next(null, size + '-' + mtime);
};


exports.apply = function (response, stat, next) {

var etagMethod = response.source.settings.etagMethod;
const etagMethod = response.source.settings.etagMethod;
if (etagMethod === false) {
return next();
}

var applyEtag = function (err, etag) {
const applyEtag = function (err, etag) {

if (err) {
return next(err);
Expand Down