Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const asteriskRegex = /\*/gu
const supportedEncodings = ['br', 'gzip', 'deflate']
send.mime.default_type = 'application/octet-stream'

/** @type {import("fastify").FastifyPluginAsync<import("./types").FastifyStaticOptions>} */
async function fastifyStatic (fastify, opts) {
opts.root = normalizeRoot(opts.root)
checkRootPathForErrors(fastify, opts.root)
Expand Down Expand Up @@ -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<string>} [checkedEncodings]
*/
async function pumpSendToReply (
request,
reply,
Expand Down Expand Up @@ -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
Expand All @@ -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')
Expand Down Expand Up @@ -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')
Expand All @@ -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

Expand All @@ -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 => {
Expand All @@ -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<string>} checked
*/
function getEncodingHeader (headers, checked) {
if (!('accept-encoding' in headers)) return

Expand All @@ -507,6 +546,10 @@ function getEncodingHeader (headers, checked) {
)
}

/**
* @param {string} encoding
* @returns {string}
*/
function getEncodingExtension (encoding) {
switch (encoding) {
case 'br':
Expand All @@ -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
Expand Down
Loading