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

Extended dir-list information #241

Merged
merged 10 commits into from
Oct 7, 2021
Merged

Extended dir-list information #241

merged 10 commits into from
Oct 7, 2021

Conversation

Jelenkee
Copy link
Contributor

@Jelenkee Jelenkee commented Oct 4, 2021

I added some features to the dirlist.
Additionally to the names I added the stats of the dirs/files and some extended information for the dirs.
You can change the format (html/json) via the URL-Parameter.

Furthermore I replaced the callbacks with promises.

Checklist

index.js Outdated
@@ -164,7 +165,7 @@ async function fastifyStatic (fastify, opts) {
if (err.code === 'ENOENT') {
// if file exists, send real file, otherwise send dir list if name match
if (opts.list && dirList.handle(pathname, opts.list)) {
return dirList.send({ reply, dir: dirList.path(opts.root, pathname), options: opts.list, route: pathname })
return dirList.send({ request, reply, dir: dirList.path(opts.root, pathname), options: opts.list, route: pathname })
Copy link
Member

Choose a reason for hiding this comment

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

There is no need to add request, it's available on reply.request.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

lib/dirList.js Outdated

async function walk (dir) {
const files = await fs.readdir(dir)
await Promise.all(files
Copy link
Member

Choose a reason for hiding this comment

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

This is going to become problematic on very large folders. You should use p-map instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What is a very large folder?
I tested it with my home folder (91593 files, 19091 folders)
Promise.all: ~1.7s
p-map: ~1.2s

So there is an improvement

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

I'm sorry about the back-and-forth, the files.map() throw me off-guard and made me assume we needed to use p-map. We can just use p-limit as there is no need to "map" things.

lib/dirList.js Outdated
return entries
}

await Promise.all(files.map(async filename => {
Copy link
Member

Choose a reason for hiding this comment

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

please use p-limit here. We are not really interested in the result of all the promises.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

lib/dirList.js Outdated

async function walk (dir) {
const files = await fs.readdir(dir)
await pMap(files, async filename => {
Copy link
Member

Choose a reason for hiding this comment

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

If we are not interested in the result, maybe we could just use p-limit instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

mcollina
mcollina previously approved these changes Oct 5, 2021
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm, thx

Copy link
Member

@climba03003 climba03003 left a comment

Choose a reason for hiding this comment

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

Will it has any Unhandled Exception when we change the interface from callback to async-await?

fastify-static/index.js

Lines 145 to 150 in 8da6140

return dirList.send({
reply,
dir: path,
options: opts.list,
route: pathname
})

return dirList.send({ reply, dir: dirList.path(opts.root, pathname), options: opts.list, route: pathname })

@mcollina mcollina dismissed their stale review October 6, 2021 10:58

Fixes are needed

@mcollina
Copy link
Member

mcollina commented Oct 6, 2021

@Jelenkee could you add a .catch((err) => { reply.send(err) }) in those places? Also avoid to return that promise to the caller.

@Jelenkee
Copy link
Contributor Author

Jelenkee commented Oct 6, 2021

Mmh... When I add the catch method to the calling method, the coverage for the tests is below 100% 😆
And I cannot add a test for this because the only error that can occur is already catched and replied with 404 to the client

@climba03003
Copy link
Member

climba03003 commented Oct 6, 2021

Mmh... When I add the catch method to the calling method, the coverage for the tests is below 100% 😆 And I cannot add a test for this because the only error that can occur is already catched and replied with 404 to the client

If we cannot think of a case that will throw for a promise currently. Using t.mock for dirList.send is acceptable to simulate that error.

It is always good to handle all promise properly (e.g. .catch or .finally), even we cannot think a error at this stage.

Remember do not return a promise inside a stream event callback.

Copy link
Member

@climba03003 climba03003 left a comment

Choose a reason for hiding this comment

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

LGTM.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina merged commit 9e3286c into fastify:master Oct 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants