@@ -16,6 +16,10 @@ const asteriskRegex = /\*/gu
1616
1717const supportedEncodings = [ 'br' , 'gzip' , 'deflate' ]
1818send . mime . default_type = 'application/octet-stream'
19+ const encodingExtensionMap = {
20+ br : '.br' ,
21+ gzip : '.gz'
22+ }
1923
2024/** @type {import("fastify").FastifyPluginAsync<import("./types").FastifyStaticOptions> } */
2125async function fastifyStatic ( fastify , opts ) {
@@ -32,9 +36,7 @@ async function fastifyStatic (fastify, opts) {
3236 throw invalidDirListOpts
3337 }
3438
35- if ( opts . dotfiles === undefined ) {
36- opts . dotfiles = 'allow'
37- }
39+ opts . dotfiles ??= 'allow'
3840
3941 const sendOptions = {
4042 root : opts . root ,
@@ -50,7 +52,7 @@ async function fastifyStatic (fastify, opts) {
5052 maxAge : opts . maxAge
5153 }
5254
53- let prefix = opts . prefix ?? ( opts . prefix = '/' )
55+ let prefix = opts . prefix ??= '/'
5456
5557 if ( ! opts . prefixAvoidTrailingSlash ) {
5658 prefix =
@@ -63,7 +65,7 @@ async function fastifyStatic (fastify, opts) {
6365 const routeOpts = {
6466 constraints : opts . constraints ,
6567 schema : {
66- hide : opts . schemaHide !== undefined ? opts . schemaHide : true
68+ hide : opts . schemaHide ?? true
6769 } ,
6870 logLevel : opts . logLevel ,
6971 errorHandler ( error , request , reply ) {
@@ -127,7 +129,7 @@ async function fastifyStatic (fastify, opts) {
127129 } )
128130 }
129131 } else {
130- const indexes = opts . index === undefined ? [ 'index.html' ] : [ ] . concat ( opts . index )
132+ const indexes = new Set ( opts . index === undefined ? [ 'index.html' ] : [ ] . concat ( opts . index ) )
131133 const indexDirs = new Map ( )
132134 const routes = new Set ( )
133135
@@ -152,7 +154,7 @@ async function fastifyStatic (fastify, opts) {
152154 setUpHeadAndGet ( routeOpts , route , `/${ file } ` , rootPath )
153155
154156 const key = path . posix . basename ( route )
155- if ( indexes . includes ( key ) && ! indexDirs . has ( key ) ) {
157+ if ( indexes . has ( key ) && ! indexDirs . has ( key ) ) {
156158 indexDirs . set ( path . posix . dirname ( route ) , rootPath )
157159 }
158160 }
@@ -213,9 +215,7 @@ async function fastifyStatic (fastify, opts) {
213215 * We conditionally create this structure to track our attempts
214216 * at sending pre-compressed assets
215217 */
216- if ( ! checkedEncodings ) {
217- checkedEncodings = new Set ( )
218- }
218+ checkedEncodings ??= new Set ( )
219219
220220 encoding = getEncodingHeader ( request . headers , checkedEncodings )
221221
@@ -225,9 +225,9 @@ async function fastifyStatic (fastify, opts) {
225225 if ( ! pathname ) {
226226 return reply . callNotFound ( )
227227 }
228- pathnameForSend = pathnameForSend + pathname + '.' + getEncodingExtension ( encoding )
228+ pathnameForSend = pathnameForSend + pathname + encodingExtensionMap [ encoding ]
229229 } else {
230- pathnameForSend = pathname + '.' + getEncodingExtension ( encoding )
230+ pathnameForSend = pathname + encodingExtensionMap [ encoding ]
231231 }
232232 }
233233 }
@@ -370,9 +370,7 @@ async function fastifyStatic (fastify, opts) {
370370 // otherwise use send provided status code
371371 const newStatusCode = reply . statusCode !== 200 ? reply . statusCode : statusCode
372372 reply . code ( newStatusCode )
373- if ( setHeaders !== undefined ) {
374- setHeaders ( reply . raw , metadata . path , metadata . stat )
375- }
373+ setHeaders ?. ( reply . raw , metadata . path , metadata . stat )
376374 reply . headers ( headers )
377375 if ( encoding ) {
378376 reply . header ( 'content-type' , getContentType ( pathname ) )
@@ -390,7 +388,7 @@ async function fastifyStatic (fastify, opts) {
390388 url : route ,
391389 handler : serveFileHandler
392390 } )
393- toSetUp . config = toSetUp . config || { }
391+ toSetUp . config ??= { }
394392 toSetUp . config . file = file
395393 toSetUp . config . rootPath = rootPath
396394 fastify . route ( toSetUp )
@@ -546,28 +544,14 @@ function getEncodingHeader (headers, checked) {
546544 )
547545}
548546
549- /**
550- * @param {string } encoding
551- * @returns {string }
552- */
553- function getEncodingExtension ( encoding ) {
554- switch ( encoding ) {
555- case 'br' :
556- return 'br'
557-
558- case 'gzip' :
559- return 'gz'
560- }
561- }
562-
563547/**
564548 * @param {string } url
565549 * @return {string }
566550 */
567551function getRedirectUrl ( url ) {
568552 let i = 0
569553 // we detect how many slash before a valid path
570- for ( ; i < url . length ; ++ i ) {
554+ for ( const ul = url . length ; i < ul ; ++ i ) {
571555 if ( url [ i ] !== '/' && url [ i ] !== '\\' ) break
572556 }
573557 // turns all leading / or \ into a single /
0 commit comments