From 2516703141b52a7f635110cec2b1a496e2a54785 Mon Sep 17 00:00:00 2001 From: turnerran Date: Sat, 2 Sep 2023 20:46:08 +0200 Subject: [PATCH 1/9] added some code improvements --- example/server-compress.js | 4 +++- example/server-dir-list.js | 4 +++- example/server-hidden-file.js | 4 +++- example/server.js | 4 +++- lib/dirList.js | 16 +++++++--------- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/example/server-compress.js b/example/server-compress.js index 2956087..6305fd8 100644 --- a/example/server-compress.js +++ b/example/server-compress.js @@ -11,5 +11,7 @@ fastify root: path.join(__dirname, '/public') }) .listen({ port: 3000 }, err => { - if (err) throw err + if (err) { + throw err + } }) diff --git a/example/server-dir-list.js b/example/server-dir-list.js index 0f38047..b4ca49e 100644 --- a/example/server-dir-list.js +++ b/example/server-dir-list.js @@ -45,5 +45,7 @@ fastify } }) .listen({ port: 3000 }, err => { - if (err) throw err + if (err) { + throw err + } }) diff --git a/example/server-hidden-file.js b/example/server-hidden-file.js index 4c9470b..ccf5eaf 100644 --- a/example/server-hidden-file.js +++ b/example/server-hidden-file.js @@ -11,5 +11,7 @@ fastify serveDotFiles: true }) .listen({ port: 3000 }, err => { - if (err) throw err + if (err) { + throw err + } }) diff --git a/example/server.js b/example/server.js index 243f305..ea66318 100644 --- a/example/server.js +++ b/example/server.js @@ -9,5 +9,7 @@ fastify root: path.join(__dirname, '/public') }) .listen({ port: 3000 }, err => { - if (err) throw err + if (err) { + throw err + } }) diff --git a/lib/dirList.js b/lib/dirList.js index 7aec88b..7c062a5 100644 --- a/lib/dirList.js +++ b/lib/dirList.js @@ -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) { 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 @@ -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 }, @@ -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 { @@ -159,10 +160,7 @@ const dirList = { * @return {boolean} */ 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] === '/') }, @@ -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') { return new TypeError('The `list.jsonFormat` option must be name or extended') } if (options.list.format === 'html' && typeof options.list.render !== 'function') { From 0c6ce577ce8e497024a888b2399a15d5f42ecd73 Mon Sep 17 00:00:00 2001 From: turnerran Date: Sat, 2 Sep 2023 20:55:09 +0200 Subject: [PATCH 2/9] removed some redundant assertion --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index eba832f..76ce3b3 100644 --- a/index.js +++ b/index.js @@ -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 } From 164e1ec247e3c31bf26588ae68b49c67cf2e8975 Mon Sep 17 00:00:00 2001 From: turnerran Date: Sat, 2 Sep 2023 22:58:16 +0200 Subject: [PATCH 3/9] reverted != assertion --- lib/dirList.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dirList.js b/lib/dirList.js index 7c062a5..e8b9f1d 100644 --- a/lib/dirList.js +++ b/lib/dirList.js @@ -196,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 && options.list.jsonFormat !== 'names' && options.list.jsonFormat !== 'extended') { + if (options.list.jsonFormat != null && options.list.jsonFormat !== 'names' && options.list.jsonFormat !== 'extended') { return new TypeError('The `list.jsonFormat` option must be name or extended') } if (options.list.format === 'html' && typeof options.list.render !== 'function') { From cd1011c7e56df38402f2f4a56efe72e7167f498b Mon Sep 17 00:00:00 2001 From: turnerran Date: Sun, 3 Sep 2023 16:04:57 +0200 Subject: [PATCH 4/9] fixed pr comments --- example/server-compress.js | 4 +--- example/server-dir-list.js | 4 +--- example/server-hidden-file.js | 4 +--- example/server.js | 4 +--- lib/dirList.js | 4 ++-- 5 files changed, 6 insertions(+), 14 deletions(-) diff --git a/example/server-compress.js b/example/server-compress.js index 6305fd8..2956087 100644 --- a/example/server-compress.js +++ b/example/server-compress.js @@ -11,7 +11,5 @@ fastify root: path.join(__dirname, '/public') }) .listen({ port: 3000 }, err => { - if (err) { - throw err - } + if (err) throw err }) diff --git a/example/server-dir-list.js b/example/server-dir-list.js index b4ca49e..0f38047 100644 --- a/example/server-dir-list.js +++ b/example/server-dir-list.js @@ -45,7 +45,5 @@ fastify } }) .listen({ port: 3000 }, err => { - if (err) { - throw err - } + if (err) throw err }) diff --git a/example/server-hidden-file.js b/example/server-hidden-file.js index ccf5eaf..4c9470b 100644 --- a/example/server-hidden-file.js +++ b/example/server-hidden-file.js @@ -11,7 +11,5 @@ fastify serveDotFiles: true }) .listen({ port: 3000 }, err => { - if (err) { - throw err - } + if (err) throw err }) diff --git a/example/server.js b/example/server.js index ea66318..243f305 100644 --- a/example/server.js +++ b/example/server.js @@ -9,7 +9,5 @@ fastify root: path.join(__dirname, '/public') }) .listen({ port: 3000 }, err => { - if (err) { - throw err - } + if (err) throw err }) diff --git a/lib/dirList.js b/lib/dirList.js index e8b9f1d..1821e10 100644 --- a/lib/dirList.js +++ b/lib/dirList.js @@ -12,10 +12,10 @@ 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) { const entries = { dirs: [], files: [] } let files = await fs.readdir(dir) - if (dotFiles === 'deny' || dotFiles === 'ignore') { + if (dotfiles === 'deny' || dotfiles === 'ignore') { files = files.filter(file => file.charAt(0) !== '.') } if (files.length < 1) { From 1dfcfddc35cce5e0c3ec163dce4a87701eb0292c Mon Sep 17 00:00:00 2001 From: turnerran <55016001+turnerran@users.noreply.github.com> Date: Sun, 3 Sep 2023 17:53:46 +0200 Subject: [PATCH 5/9] Updated second condition MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Gürgün Dayıoğlu --- lib/dirList.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dirList.js b/lib/dirList.js index 1821e10..3a7116c 100644 --- a/lib/dirList.js +++ b/lib/dirList.js @@ -162,7 +162,7 @@ const dirList = { handle: function (route, options) { return options.names?.includes(path.basename(route)) || // match trailing slash - (options.names.includes('/') && route[route.length - 1] === '/') + (options.names?.includes('/') && route[route.length - 1] === '/') }, /** From e701c97284ef3cfda9791466144dd24e882413f6 Mon Sep 17 00:00:00 2001 From: turnerran <55016001+turnerran@users.noreply.github.com> Date: Sun, 3 Sep 2023 20:34:01 +0200 Subject: [PATCH 6/9] Updated return value MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Gürgün Dayıoğlu --- lib/dirList.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dirList.js b/lib/dirList.js index 3a7116c..44d35a2 100644 --- a/lib/dirList.js +++ b/lib/dirList.js @@ -157,7 +157,7 @@ const dirList = { * say if the route can be handled by dir list or not * @param {string} route request route * @param {(boolean | ListOptionsJsonFormat | ListOptionsHtmlFormat)} options - * @return {boolean} + * @return {boolean | undefined} */ handle: function (route, options) { return options.names?.includes(path.basename(route)) || From e2ba99a6e1e07f2312ddbff40491c91aefb37bba Mon Sep 17 00:00:00 2001 From: turnerran <55016001+turnerran@users.noreply.github.com> Date: Tue, 5 Sep 2023 21:40:10 +0200 Subject: [PATCH 7/9] Updated condition to return boolean Co-authored-by: Uzlopak --- lib/dirList.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dirList.js b/lib/dirList.js index 44d35a2..be9444c 100644 --- a/lib/dirList.js +++ b/lib/dirList.js @@ -162,7 +162,7 @@ const dirList = { handle: function (route, options) { 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 }, /** From 6108eae1c1ed06e9d15b9e6e59003113742dee88 Mon Sep 17 00:00:00 2001 From: turnerran <55016001+turnerran@users.noreply.github.com> Date: Wed, 6 Sep 2023 14:03:29 +0200 Subject: [PATCH 8/9] Removed redundant return type Co-authored-by: Uzlopak --- lib/dirList.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dirList.js b/lib/dirList.js index be9444c..44b5260 100644 --- a/lib/dirList.js +++ b/lib/dirList.js @@ -157,7 +157,7 @@ const dirList = { * say if the route can be handled by dir list or not * @param {string} route request route * @param {(boolean | ListOptionsJsonFormat | ListOptionsHtmlFormat)} options - * @return {boolean | undefined} + * @return {boolean} */ handle: function (route, options) { return options.names?.includes(path.basename(route)) || From 59da0f0175d1164add5fa81261ad3dbc28cf723c Mon Sep 17 00:00:00 2001 From: turnerran <55016001+turnerran@users.noreply.github.com> Date: Wed, 6 Sep 2023 14:54:05 +0200 Subject: [PATCH 9/9] Added nullish operator for performance optimization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Gürgün Dayıoğlu --- lib/dirList.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dirList.js b/lib/dirList.js index 44b5260..0e61ac8 100644 --- a/lib/dirList.js +++ b/lib/dirList.js @@ -162,7 +162,7 @@ const dirList = { handle: function (route, options) { return options.names?.includes(path.basename(route)) || // match trailing slash - (options.names?.includes('/') && route[route.length - 1] === '/') || false + ((options.names?.includes('/') && route[route.length - 1] === '/') ?? false) }, /**