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 6 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
21 changes: 10 additions & 11 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 @@ -127,18 +126,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 +174,7 @@ async function fastifyStatic (fastify, opts) {
pathname,
rootPath,
rootPathOffset = 0,
pumpOptions = {},
pumpOptions,
checkedEncodings
) {
const options = Object.assign({}, sendOptions, pumpOptions)
Expand Down