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

added some small code improvements #399

Merged
merged 10 commits into from
Sep 6, 2023
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ async function fastifyStatic (fastify, opts) {
}

const errorHandler = (error, request, reply) => {
if (error && error.code === 'ERR_STREAM_PREMATURE_CLOSE') {
if (error?.code === 'ERR_STREAM_PREMATURE_CLOSE') {
reply.request.raw.destroy()
return
}
Expand Down
12 changes: 5 additions & 7 deletions lib/dirList.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const dirList = {
const entries = { dirs: [], files: [] }
let files = await fs.readdir(dir)
if (dotfiles === 'deny' || dotfiles === 'ignore') {
files = files.filter(f => f.charAt(0) !== '.')
files = files.filter(file => file.charAt(0) !== '.')
}
if (files.length < 1) {
return entries
Expand Down Expand Up @@ -92,6 +92,7 @@ const dirList = {

entries.dirs.sort((a, b) => a.name.localeCompare(b.name))
entries.files.sort((a, b) => a.name.localeCompare(b.name))

return entries
},

Expand Down Expand Up @@ -141,7 +142,7 @@ const dirList = {
* @return {ListFile}
*/
htmlInfo: function (entry, route, prefix, options) {
if (options.names && options.names.includes(path.basename(route))) {
if (options.names?.includes(path.basename(route))) {
route = path.normalize(path.join(route, '..'))
}
return {
Expand All @@ -159,12 +160,9 @@ const dirList = {
* @return {boolean}
turnerran marked this conversation as resolved.
Show resolved Hide resolved
*/
handle: function (route, options) {
if (!options.names) {
return false
}
return options.names.includes(path.basename(route)) ||
return options.names?.includes(path.basename(route)) ||
// match trailing slash
(options.names.includes('/') && route[route.length - 1] === '/')
((options.names?.includes('/') && route[route.length - 1] === '/') ?? false)
},

/**
Expand Down