-
-
Notifications
You must be signed in to change notification settings - Fork 106
Extended dir-list information #241
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
Conversation
-added stats and more info to dir list
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 }) |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
-used p-map
There was a problem hiding this 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 => { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 => { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm, thx
There was a problem hiding this 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
?
Lines 145 to 150 in 8da6140
return dirList.send({ | |
reply, | |
dir: path, | |
options: opts.list, | |
route: pathname | |
}) |
Line 164 in 8da6140
return dirList.send({ reply, dir: dirList.path(opts.root, pathname), options: opts.list, route: pathname }) |
@Jelenkee could you add a |
Mmh... When I add the catch method to the calling method, the coverage for the tests is below 100% 😆 |
If we cannot think of a case that will throw for a promise currently. Using It is always good to handle all promise properly (e.g. Remember do not return a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
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
npm run test
andnpm run benchmark
and the Code of conduct