@@ -17,6 +17,7 @@ const asteriskRegex = /\*/gu
1717const supportedEncodings = [ 'br' , 'gzip' , 'deflate' ]
1818send . mime . default_type = 'application/octet-stream'
1919
20+ /** @type {import("fastify").FastifyPluginAsync<import("./types").FastifyStaticOptions> } */
2021async function fastifyStatic ( fastify , opts ) {
2122 opts . root = normalizeRoot ( opts . root )
2223 checkRootPathForErrors ( fastify , opts . root )
@@ -171,6 +172,15 @@ async function fastifyStatic (fastify, opts) {
171172
172173 const allowedPath = opts . allowedPath
173174
175+ /**
176+ * @param {import("fastify").FastifyRequest } request
177+ * @param {import("fastify").FastifyReply } reply
178+ * @param {string } pathname
179+ * @param {import("./types").FastifyStaticOptions['root'] } rootPath
180+ * @param {number } [rootPathOffset]
181+ * @param {import("@fastify/send").SendOptions } [pumpOptions]
182+ * @param {Set<string> } [checkedEncodings]
183+ */
174184 async function pumpSendToReply (
175185 request ,
176186 reply ,
@@ -386,12 +396,17 @@ async function fastifyStatic (fastify, opts) {
386396 fastify . route ( toSetUp )
387397 }
388398
399+ /** @type {import("fastify").RouteHandlerMethod } */
389400 async function serveFileHandler ( req , reply ) {
390401 const routeConfig = req . routeOptions ?. config
391402 return pumpSendToReply ( req , reply , routeConfig . file , routeConfig . rootPath )
392403 }
393404}
394405
406+ /**
407+ * @param {import("./types").FastifyStaticOptions['root'] } root
408+ * @returns {import("./types").FastifyStaticOptions['root'] }
409+ */
395410function normalizeRoot ( root ) {
396411 if ( root === undefined ) {
397412 return root
@@ -415,6 +430,11 @@ function normalizeRoot (root) {
415430 return root
416431}
417432
433+ /**
434+ * @param {import("fastify").FastifyInstance } fastify
435+ * @param {import("./types").FastifyStaticOptions['root'] } rootPath
436+ * @returns {void }
437+ */
418438function checkRootPathForErrors ( fastify , rootPath ) {
419439 if ( rootPath === undefined ) {
420440 throw new Error ( '"root" option is required' )
@@ -443,6 +463,11 @@ function checkRootPathForErrors (fastify, rootPath) {
443463 throw new Error ( '"root" option must be a string or array of strings' )
444464}
445465
466+ /**
467+ * @param {import("fastify").FastifyInstance } fastify
468+ * @param {import("./types").FastifyStaticOptions['root'] } rootPath
469+ * @returns {void }
470+ */
446471function checkPath ( fastify , rootPath ) {
447472 if ( typeof rootPath !== 'string' ) {
448473 throw new TypeError ( '"root" option must be a string' )
@@ -469,6 +494,10 @@ function checkPath (fastify, rootPath) {
469494 }
470495}
471496
497+ /**
498+ * @param {string } path
499+ * @return {string }
500+ */
472501function getContentType ( path ) {
473502 const type = send . mime . getType ( path ) || send . mime . default_type
474503
@@ -478,6 +507,12 @@ function getContentType (path) {
478507 return `${ type } ; charset=utf-8`
479508}
480509
510+ /**
511+ * @param {string } pathname
512+ * @param {* } root
513+ * @param {import("./types").FastifyStaticOptions['index'] } [indexFiles]
514+ * @return {string|boolean }
515+ */
481516function findIndexFile ( pathname , root , indexFiles = [ 'index.html' ] ) {
482517 if ( Array . isArray ( indexFiles ) ) {
483518 return indexFiles . find ( filename => {
@@ -494,7 +529,11 @@ function findIndexFile (pathname, root, indexFiles = ['index.html']) {
494529 return false
495530}
496531
497- // Adapted from https://github.com/fastify/fastify-compress/blob/665e132fa63d3bf05ad37df3c20346660b71a857/index.js#L451
532+ /**
533+ * Adapted from https://github.com/fastify/fastify-compress/blob/665e132fa63d3bf05ad37df3c20346660b71a857/index.js#L451
534+ * @param {import('fastify').FastifyRequest['headers'] } headers
535+ * @param {Set<string> } checked
536+ */
498537function getEncodingHeader ( headers , checked ) {
499538 if ( ! ( 'accept-encoding' in headers ) ) return
500539
@@ -507,6 +546,10 @@ function getEncodingHeader (headers, checked) {
507546 )
508547}
509548
549+ /**
550+ * @param {string } encoding
551+ * @returns {string }
552+ */
510553function getEncodingExtension ( encoding ) {
511554 switch ( encoding ) {
512555 case 'br' :
@@ -517,6 +560,10 @@ function getEncodingExtension (encoding) {
517560 }
518561}
519562
563+ /**
564+ * @param {string } url
565+ * @return {string }
566+ */
520567function getRedirectUrl ( url ) {
521568 let i = 0
522569 // we detect how many slash before a valid path
0 commit comments