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
1719internals . schema = Joi . object ( {
@@ -28,14 +30,14 @@ internals.schema = Joi.object({
2830
2931exports . 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
201203internals . 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
0 commit comments