Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: file not found errors when the root has a trailing slash #436

Merged
merged 8 commits into from
Feb 7, 2024
Merged
Changes from 4 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
37 changes: 20 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const contentDisposition = require('content-disposition')
const dirList = require('./lib/dirList')

const endForwardSlashRegex = /\/$/u
const doubleForwardSlashRegex = /\/\//gu
const asteriskRegex = /\*/gu

const supportedEncodings = ['br', 'gzip', 'deflate']
Expand Down Expand Up @@ -112,11 +111,14 @@ async function fastifyStatic (fastify, opts) {
throw new Error('"wildcard" option must be a boolean')
}
if (opts.wildcard === undefined || opts.wildcard === true) {
fastify.head(prefix + '*', routeOpts, function (req, reply) {
pumpSendToReply(req, reply, '/' + req.params['*'], sendOptions.root)
})
fastify.get(prefix + '*', routeOpts, function (req, reply) {
pumpSendToReply(req, reply, '/' + req.params['*'], sendOptions.root)
fastify.route({
...routeOpts,
exposeHeadRoute: true,
method: 'GET',
url: `${prefix}*`,
handler (req, reply) {
pumpSendToReply(req, reply, `/${req.params['*']}`, sendOptions.root)
}
})
if (opts.redirect === true && prefix !== opts.prefix) {
fastify.get(opts.prefix, routeOpts, function (req, reply) {
Expand All @@ -127,18 +129,18 @@ async function fastifyStatic (fastify, opts) {
const indexes = opts.index === undefined ? ['index.html'] : [].concat(opts.index)
const indexDirs = new Map()
const routes = new Set()
const globPattern = '**/**'

const roots = Array.isArray(sendOptions.root) ? sendOptions.root : [sendOptions.root]
for (let i = 0; i < roots.length; ++i) {
const rootPath = roots[i]
const posixRootPath = rootPath.split(path.win32.sep).join(path.posix.sep)
const files = await glob(`${posixRootPath}/${globPattern}`, { follow: true, nodir: true, dot: opts.serveDotFiles })
for (let rootPath of roots) {
rootPath = rootPath.split(path.win32.sep).join(path.posix.sep)
!rootPath.endsWith('/') && (rootPath += '/')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
!rootPath.endsWith('/') && (rootPath += '/')
if(!rootPath.endsWith(path.posix.sep)){
rootPath += path.posix.sep
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Istanbul complains

const files = await glob('**/**', {
cwd: rootPath, absolute: false, follow: true, nodir: true, dot: opts.serveDotFiles
})

for (let i = 0; i < files.length; ++i) {
const file = files[i].split(path.win32.sep).join(path.posix.sep)
.replace(`${posixRootPath}/`, '')
const route = (prefix + file).replace(doubleForwardSlashRegex, '/')
for (let file of files) {
file = file.split(path.win32.sep).join(path.posix.sep)
const route = prefix + file

if (routes.has(route)) {
continue
Expand Down Expand Up @@ -175,7 +177,7 @@ async function fastifyStatic (fastify, opts) {
pathname,
rootPath,
rootPathOffset = 0,
pumpOptions = {},
pumpOptions,
checkedEncodings
) {
const options = Object.assign({}, sendOptions, pumpOptions)
Expand Down Expand Up @@ -381,7 +383,8 @@ async function fastifyStatic (fastify, opts) {

function setUpHeadAndGet (routeOpts, route, file, rootPath) {
const toSetUp = Object.assign({}, routeOpts, {
method: ['HEAD', 'GET'],
exposeHeadRoute: true,
method: 'GET',
gurgunday marked this conversation as resolved.
Show resolved Hide resolved
url: route,
handler: serveFileHandler
})
Expand Down