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
4 changes: 3 additions & 1 deletion example/server-compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ fastify
root: path.join(__dirname, '/public')
})
.listen({ port: 3000 }, err => {
if (err) throw err
if (err) {
throw err
}
})
4 changes: 3 additions & 1 deletion example/server-dir-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@ fastify
}
})
.listen({ port: 3000 }, err => {
if (err) throw err
if (err) {
throw err
}
})
4 changes: 3 additions & 1 deletion example/server-hidden-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ fastify
serveDotFiles: true
})
.listen({ port: 3000 }, err => {
if (err) throw err
if (err) {
throw err
}
})
4 changes: 3 additions & 1 deletion example/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ fastify
root: path.join(__dirname, '/public')
})
.listen({ port: 3000 }, err => {
if (err) throw err
if (err) {
throw err
}
})
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
16 changes: 7 additions & 9 deletions lib/dirList.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ const dirList = {
* @param {string} dotfiles
* note: can't use glob because don't get error on non existing dir
*/
list: async function (dir, options, dotfiles) {
list: async function (dir, options, dotFiles) {
jsumners marked this conversation as resolved.
Show resolved Hide resolved
const entries = { dirs: [], files: [] }
let files = await fs.readdir(dir)
if (dotfiles === 'deny' || dotfiles === 'ignore') {
files = files.filter(f => f.charAt(0) !== '.')
if (dotFiles === 'deny' || dotFiles === 'ignore') {
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,10 +160,7 @@ 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] === '/')
turnerran marked this conversation as resolved.
Show resolved Hide resolved
},
Expand Down Expand Up @@ -198,7 +196,7 @@ const dirList = {
if (options.list.names && !Array.isArray(options.list.names)) {
return new TypeError('The `list.names` option must be an array')
}
if (options.list.jsonFormat != null && options.list.jsonFormat !== 'names' && options.list.jsonFormat !== 'extended') {
if (options.list.jsonFormat && options.list.jsonFormat !== 'names' && options.list.jsonFormat !== 'extended') {
jsumners marked this conversation as resolved.
Show resolved Hide resolved
return new TypeError('The `list.jsonFormat` option must be name or extended')
}
if (options.list.format === 'html' && typeof options.list.render !== 'function') {
Expand Down