diff --git a/index.js b/index.js index d5ab197..ee79120 100644 --- a/index.js +++ b/index.js @@ -17,6 +17,7 @@ const asteriskRegex = /\*/gu const supportedEncodings = ['br', 'gzip', 'deflate'] send.mime.default_type = 'application/octet-stream' +/** @type {import("fastify").FastifyPluginAsync} */ async function fastifyStatic (fastify, opts) { opts.root = normalizeRoot(opts.root) checkRootPathForErrors(fastify, opts.root) @@ -171,6 +172,15 @@ async function fastifyStatic (fastify, opts) { const allowedPath = opts.allowedPath + /** + * @param {import("fastify").FastifyRequest} request + * @param {import("fastify").FastifyReply} reply + * @param {string} pathname + * @param {import("./types").FastifyStaticOptions['root']} rootPath + * @param {number} [rootPathOffset] + * @param {import("@fastify/send").SendOptions} [pumpOptions] + * @param {Set} [checkedEncodings] + */ async function pumpSendToReply ( request, reply, @@ -386,12 +396,17 @@ async function fastifyStatic (fastify, opts) { fastify.route(toSetUp) } + /** @type {import("fastify").RouteHandlerMethod} */ async function serveFileHandler (req, reply) { const routeConfig = req.routeOptions?.config return pumpSendToReply(req, reply, routeConfig.file, routeConfig.rootPath) } } +/** + * @param {import("./types").FastifyStaticOptions['root']} root + * @returns {import("./types").FastifyStaticOptions['root']} + */ function normalizeRoot (root) { if (root === undefined) { return root @@ -415,6 +430,11 @@ function normalizeRoot (root) { return root } +/** + * @param {import("fastify").FastifyInstance} fastify + * @param {import("./types").FastifyStaticOptions['root']} rootPath + * @returns {void} + */ function checkRootPathForErrors (fastify, rootPath) { if (rootPath === undefined) { throw new Error('"root" option is required') @@ -443,6 +463,11 @@ function checkRootPathForErrors (fastify, rootPath) { throw new Error('"root" option must be a string or array of strings') } +/** + * @param {import("fastify").FastifyInstance} fastify + * @param {import("./types").FastifyStaticOptions['root']} rootPath + * @returns {void} + */ function checkPath (fastify, rootPath) { if (typeof rootPath !== 'string') { throw new TypeError('"root" option must be a string') @@ -469,6 +494,10 @@ function checkPath (fastify, rootPath) { } } +/** + * @param {string} path + * @return {string} + */ function getContentType (path) { const type = send.mime.getType(path) || send.mime.default_type @@ -478,6 +507,12 @@ function getContentType (path) { return `${type}; charset=utf-8` } +/** + * @param {string} pathname + * @param {*} root + * @param {import("./types").FastifyStaticOptions['index']} [indexFiles] + * @return {string|boolean} + */ function findIndexFile (pathname, root, indexFiles = ['index.html']) { if (Array.isArray(indexFiles)) { return indexFiles.find(filename => { @@ -494,7 +529,11 @@ function findIndexFile (pathname, root, indexFiles = ['index.html']) { return false } -// Adapted from https://github.com/fastify/fastify-compress/blob/665e132fa63d3bf05ad37df3c20346660b71a857/index.js#L451 +/** + * Adapted from https://github.com/fastify/fastify-compress/blob/665e132fa63d3bf05ad37df3c20346660b71a857/index.js#L451 + * @param {import('fastify').FastifyRequest['headers']} headers + * @param {Set} checked + */ function getEncodingHeader (headers, checked) { if (!('accept-encoding' in headers)) return @@ -507,6 +546,10 @@ function getEncodingHeader (headers, checked) { ) } +/** + * @param {string} encoding + * @returns {string} + */ function getEncodingExtension (encoding) { switch (encoding) { case 'br': @@ -517,6 +560,10 @@ function getEncodingExtension (encoding) { } } +/** + * @param {string} url + * @return {string} + */ function getRedirectUrl (url) { let i = 0 // we detect how many slash before a valid path