Skip to content

Commit cbab9fc

Browse files
committed
Merge branch 'escalant3-es6-and-node4'
2 parents 19b5802 + 6614b93 commit cbab9fc

10 files changed

Lines changed: 697 additions & 671 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
language: node_js
22

33
node_js:
4-
- "0.10"
54
- "4.0"
65
- 4
6+
- 5
77

88
sudo: false

lib/directory.js

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
'use strict';
2+
13
// Load modules
24

3-
var Fs = require('fs');
4-
var Path = require('path');
5-
var Boom = require('boom');
6-
var Hoek = require('hoek');
7-
var Items = require('items');
8-
var Joi = require('joi');
9-
var File = require('./file');
5+
const Fs = require('fs');
6+
const Path = require('path');
7+
const Boom = require('boom');
8+
const Hoek = require('hoek');
9+
const Items = require('items');
10+
const Joi = require('joi');
11+
const File = require('./file');
1012

1113

1214
// Declare internals
1315

14-
var internals = {};
16+
const internals = {};
1517

1618

1719
internals.schema = Joi.object({
@@ -28,14 +30,14 @@ internals.schema = Joi.object({
2830

2931
exports.handler = function (route, options) {
3032

31-
var settings = Joi.attempt(options, internals.schema, 'Invalid directory handler options (' + route.path + ')');
33+
const settings = Joi.attempt(options, internals.schema, 'Invalid directory handler options (' + route.path + ')');
3234
Hoek.assert(route.path[route.path.length - 1] === '}', 'The route path must end with a parameter:', route.path);
3335

34-
var normalize = function (paths) {
36+
const normalize = function (paths) {
3537

36-
var normalized = [];
37-
for (var i = 0, il = paths.length; i < il; ++i) {
38-
var path = paths[i];
38+
const normalized = [];
39+
for (let i = 0; i < paths.length; ++i) {
40+
let path = paths[i];
3941

4042
if (!Hoek.isAbsolutePath(path)) {
4143
path = Path.join(route.settings.files.relativeTo, path);
@@ -47,17 +49,17 @@ exports.handler = function (route, options) {
4749
return normalized;
4850
};
4951

50-
var normalized = (Array.isArray(settings.path) ? normalize(settings.path) : []); // Array or function
52+
const normalized = (Array.isArray(settings.path) ? normalize(settings.path) : []); // Array or function
5153

52-
var indexNames = (settings.index === true) ? ['index.html'] : (settings.index || []);
54+
const indexNames = (settings.index === true) ? ['index.html'] : (settings.index || []);
5355

5456
// Declare handler
5557

56-
var handler = function (request, reply) {
58+
const handler = function (request, reply) {
5759

58-
var paths = normalized;
60+
let paths = normalized;
5961
if (typeof settings.path === 'function') {
60-
var result = settings.path.call(null, request);
62+
const result = settings.path.call(null, request);
6163
if (result instanceof Error) {
6264
return reply(result);
6365
}
@@ -75,8 +77,8 @@ exports.handler = function (route, options) {
7577

7678
// Append parameter
7779

78-
var selection = null;
79-
var lastParam = request.paramsArray[request.paramsArray.length - 1];
80+
let selection = null;
81+
const lastParam = request.paramsArray[request.paramsArray.length - 1];
8082
if (lastParam) {
8183
if (lastParam.indexOf('..') !== -1) {
8284
return reply(Boom.forbidden());
@@ -94,15 +96,15 @@ exports.handler = function (route, options) {
9496

9597
// Generate response
9698

97-
var resource = request.path;
98-
var hasTrailingSlash = (resource[resource.length - 1] === '/');
99-
var fileOptions = { lookupCompressed: settings.lookupCompressed, etagMethod: settings.etagMethod };
99+
const resource = request.path;
100+
const hasTrailingSlash = resource.endsWith('/');
101+
const fileOptions = { lookupCompressed: settings.lookupCompressed, etagMethod: settings.etagMethod };
100102

101-
Items.serial(paths, function (path, nextPath) {
103+
Items.serial(paths, (path, nextPath) => {
102104

103105
path = Path.join(path, selection || '');
104106

105-
File.load(path, request, fileOptions, function (response) {
107+
File.load(path, request, fileOptions, (response) => {
106108

107109
// File loaded successfully
108110

@@ -112,7 +114,7 @@ exports.handler = function (route, options) {
112114

113115
// Not found
114116

115-
var err = response;
117+
const err = response;
116118
if (err.output.statusCode === 404) {
117119
if (!settings.defaultExtension) {
118120
return nextPath();
@@ -122,7 +124,7 @@ exports.handler = function (route, options) {
122124
path = path.slice(0, -1);
123125
}
124126

125-
return File.load(path + '.' + settings.defaultExtension, request, fileOptions, function (extResponse) {
127+
return File.load(path + '.' + settings.defaultExtension, request, fileOptions, (extResponse) => {
126128

127129
if (!extResponse.isBoom) {
128130
return reply(extResponse);
@@ -153,10 +155,10 @@ exports.handler = function (route, options) {
153155
return reply.redirect(resource + '/');
154156
}
155157

156-
Items.serial(indexNames, function (indexName, nextIndex) {
158+
Items.serial(indexNames, (indexName, nextIndex) => {
157159

158-
var indexFile = Path.join(path, indexName);
159-
File.load(indexFile, request, fileOptions, function (indexResponse) {
160+
const indexFile = Path.join(path, indexName);
161+
File.load(indexFile, request, fileOptions, (indexResponse) => {
160162

161163
// File loaded successfully
162164

@@ -166,7 +168,7 @@ exports.handler = function (route, options) {
166168

167169
// Directory
168170

169-
var err = indexResponse;
171+
const err = indexResponse;
170172
if (err.output.statusCode !== 404) {
171173
return reply(Boom.badImplementation(indexName + ' is a directory'));
172174
}
@@ -176,7 +178,7 @@ exports.handler = function (route, options) {
176178
return nextIndex();
177179
});
178180
},
179-
function (/* err */) {
181+
(/* err */) => {
180182

181183
// None of the index files were found
182184

@@ -188,7 +190,7 @@ exports.handler = function (route, options) {
188190
});
189191
});
190192
},
191-
function (/* err */) {
193+
(/* err */) => {
192194

193195
return reply(Boom.notFound());
194196
});
@@ -200,22 +202,22 @@ exports.handler = function (route, options) {
200202

201203
internals.generateListing = function (path, resource, selection, hasTrailingSlash, settings, request, reply) {
202204

203-
Fs.readdir(path, function (err, files) {
205+
Fs.readdir(path, (err, files) => {
204206

205207
if (err) {
206208
return reply(Boom.internal('Error accessing directory', err));
207209
}
208210

209211
resource = decodeURIComponent(resource);
210-
var display = Hoek.escapeHtml(resource);
211-
var html = '<html><head><title>' + display + '</title></head><body><h1>Directory: ' + display + '</h1><ul>';
212+
const display = Hoek.escapeHtml(resource);
213+
let html = '<html><head><title>' + display + '</title></head><body><h1>Directory: ' + display + '</h1><ul>';
212214

213215
if (selection) {
214-
var parent = resource.substring(0, resource.lastIndexOf('/', resource.length - (hasTrailingSlash ? 2 : 1))) + '/';
216+
const parent = resource.substring(0, resource.lastIndexOf('/', resource.length - (hasTrailingSlash ? 2 : 1))) + '/';
215217
html += '<li><a href="' + internals.pathEncode(parent) + '">Parent Directory</a></li>';
216218
}
217219

218-
for (var i = 0, il = files.length; i < il; ++i) {
220+
for (let i = 0; i < files.length; ++i) {
219221
if (settings.showHidden ||
220222
!internals.isFileHidden(files[i])) {
221223

lib/etag.js

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
1+
'use strict';
2+
13
// Load modules
24

3-
var Fs = require('fs');
4-
var Crypto = require('crypto');
5-
var Boom = require('boom');
6-
var Hoek = require('hoek');
7-
var LruCache = require('lru-cache');
5+
const Fs = require('fs');
6+
const Crypto = require('crypto');
7+
const Boom = require('boom');
8+
const Hoek = require('hoek');
9+
const LruCache = require('lru-cache');
810

911

1012
// Declare internals
1113

12-
var internals = {};
14+
const internals = {};
1315

1416

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

17-
var etags = response.request.server.plugins.inert._etags;
19+
const etags = response.request.server.plugins.inert._etags;
1820
if (!etags) {
1921
return next(null, null);
2022
}
2123

2224
// Use stat info for an LRU cache key.
2325

24-
var path = response.source.path;
25-
var cachekey = [path, stat.ino, stat.size, stat.mtime.getTime()].join('-');
26+
const path = response.source.path;
27+
const cachekey = [path, stat.ino, stat.size, stat.mtime.getTime()].join('-');
2628

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

29-
var cachedEtag = etags.get(cachekey);
31+
const cachedEtag = etags.get(cachekey);
3032
if (cachedEtag) {
3133
return next(null, cachedEtag);
3234
}
3335

34-
var pendings = response.request.server.plugins.inert._pendings;
35-
var pendingsId = '+' + cachekey; // Prefix to avoid conflicts with JS internals (e.g. __proto__)
36-
var nexts = pendings[pendingsId];
36+
const pendings = response.request.server.plugins.inert._pendings;
37+
const pendingsId = '+' + cachekey; // Prefix to avoid conflicts with JS internals (e.g. __proto__)
38+
let nexts = pendings[pendingsId];
3739
if (nexts) {
3840
return nexts.push(next);
3941
}
@@ -43,7 +45,7 @@ internals.computeHashed = function (response, stat, next) {
4345
nexts = [next];
4446
pendings[pendingsId] = nexts;
4547

46-
internals.hashFile(response, function (err, hash) {
48+
internals.hashFile(response, (err, hash) => {
4749

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

5456
delete pendings[pendingsId];
55-
for (var i = 0, il = nexts.length; i < il; ++i) {
57+
const il = nexts.length;
58+
for (let i = 0; i < il; ++i) {
5659
Hoek.nextTick(nexts[i])(err, hash);
5760
}
5861
});
@@ -61,13 +64,13 @@ internals.computeHashed = function (response, stat, next) {
6164

6265
internals.hashFile = function (response, callback) {
6366

64-
var hash = Crypto.createHash('sha1');
67+
const hash = Crypto.createHash('sha1');
6568
hash.setEncoding('hex');
6669

67-
var fileStream = Fs.createReadStream(response.source.path, { fd: response.source.fd, autoClose: false });
70+
const fileStream = Fs.createReadStream(response.source.path, { fd: response.source.fd, autoClose: false });
6871
fileStream.pipe(hash);
6972

70-
var done = function (err) {
73+
let done = function (err) {
7174

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

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

88-
var size = stat.size.toString(16);
89-
var mtime = stat.mtime.getTime().toString(16);
91+
const size = stat.size.toString(16);
92+
const mtime = stat.mtime.getTime().toString(16);
9093

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

9497

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

97-
var etagMethod = response.source.settings.etagMethod;
100+
const etagMethod = response.source.settings.etagMethod;
98101
if (etagMethod === false) {
99102
return next();
100103
}
101104

102-
var applyEtag = function (err, etag) {
105+
const applyEtag = function (err, etag) {
103106

104107
if (err) {
105108
return next(err);

0 commit comments

Comments
 (0)