@@ -13,7 +13,6 @@ const contentDisposition = require('content-disposition')
1313const dirList = require ( './lib/dirList' )
1414
1515const endForwardSlashRegex = / \/ $ / u
16- const doubleForwardSlashRegex = / \/ \/ / gu
1716const asteriskRegex = / \* / gu
1817
1918const supportedEncodings = [ 'br' , 'gzip' , 'deflate' ]
@@ -112,33 +111,30 @@ async function fastifyStatic (fastify, opts) {
112111 throw new Error ( '"wildcard" option must be a boolean' )
113112 }
114113 if ( opts . wildcard === undefined || opts . wildcard === true ) {
115- fastify . head ( prefix + '*' , routeOpts , function ( req , reply ) {
116- pumpSendToReply ( req , reply , '/' + req . params [ '*' ] , sendOptions . root )
117- } )
118- fastify . get ( prefix + '*' , routeOpts , function ( req , reply ) {
114+ fastify . get ( prefix + '*' , { ...routeOpts , exposeHeadRoute : true } , ( req , reply ) => {
119115 pumpSendToReply ( req , reply , '/' + req . params [ '*' ] , sendOptions . root )
120116 } )
121117 if ( opts . redirect === true && prefix !== opts . prefix ) {
122- fastify . get ( opts . prefix , routeOpts , function ( req , reply ) {
118+ fastify . get ( opts . prefix , routeOpts , ( req , reply ) => {
123119 reply . redirect ( 301 , getRedirectUrl ( req . raw . url ) )
124120 } )
125121 }
126122 } else {
127123 const indexes = opts . index === undefined ? [ 'index.html' ] : [ ] . concat ( opts . index )
128124 const indexDirs = new Map ( )
129125 const routes = new Set ( )
130- const globPattern = '**/**'
131126
132127 const roots = Array . isArray ( sendOptions . root ) ? sendOptions . root : [ sendOptions . root ]
133- for ( let i = 0 ; i < roots . length ; ++ i ) {
134- const rootPath = roots [ i ]
135- const posixRootPath = rootPath . split ( path . win32 . sep ) . join ( path . posix . sep )
136- const files = await glob ( `${ posixRootPath } /${ globPattern } ` , { follow : true , nodir : true , dot : opts . serveDotFiles } )
128+ for ( let rootPath of roots ) {
129+ rootPath = rootPath . split ( path . win32 . sep ) . join ( path . posix . sep )
130+ ! rootPath . endsWith ( '/' ) && ( rootPath += '/' )
131+ const files = await glob ( '**/**' , {
132+ cwd : rootPath , absolute : false , follow : true , nodir : true , dot : opts . serveDotFiles
133+ } )
137134
138- for ( let i = 0 ; i < files . length ; ++ i ) {
139- const file = files [ i ] . split ( path . win32 . sep ) . join ( path . posix . sep )
140- . replace ( `${ posixRootPath } /` , '' )
141- const route = ( prefix + file ) . replace ( doubleForwardSlashRegex , '/' )
135+ for ( let file of files ) {
136+ file = file . split ( path . win32 . sep ) . join ( path . posix . sep )
137+ const route = prefix + file
142138
143139 if ( routes . has ( route ) ) {
144140 continue
@@ -175,7 +171,7 @@ async function fastifyStatic (fastify, opts) {
175171 pathname ,
176172 rootPath ,
177173 rootPathOffset = 0 ,
178- pumpOptions = { } ,
174+ pumpOptions ,
179175 checkedEncodings
180176 ) {
181177 const options = Object . assign ( { } , sendOptions , pumpOptions )
0 commit comments