Skip to content

Commit cec0bef

Browse files
authored
chore(index): add jsdoc comments for static code analysis (#535)
* chore(index): add jsdoc comments for static code analysis * chore(index): add some returns
1 parent 8cc6ca2 commit cec0bef

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

index.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const asteriskRegex = /\*/gu
1717
const supportedEncodings = ['br', 'gzip', 'deflate']
1818
send.mime.default_type = 'application/octet-stream'
1919

20+
/** @type {import("fastify").FastifyPluginAsync<import("./types").FastifyStaticOptions>} */
2021
async 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+
*/
395410
function 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+
*/
418438
function 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+
*/
446471
function 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+
*/
472501
function 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+
*/
481516
function 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+
*/
498537
function 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+
*/
510553
function 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+
*/
520567
function getRedirectUrl (url) {
521568
let i = 0
522569
// we detect how many slash before a valid path

0 commit comments

Comments
 (0)