-
Notifications
You must be signed in to change notification settings - Fork 622
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
perf(http/file_server): read fileinfo in parallel #3363
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,8 +23,6 @@ interface EntryInfo { | |
name: string; | ||
} | ||
|
||
const encoder = new TextEncoder(); | ||
|
||
const ENV_PERM_STATUS = | ||
Deno.permissions.querySync?.({ name: "env", variable: "DENO_DEPLOYMENT_ID" }) | ||
.state ?? "granted"; // for deno deploy | ||
|
@@ -255,7 +253,6 @@ export async function serveFile( | |
return createCommonResponse(Status.OK, file.readable, { headers }); | ||
} | ||
|
||
// TODO(bartlomieju): simplify this after deno.stat and deno.readDir are fixed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: This is an ancient comment by Kevin and this doesn't seem relevant anymore. https://github.com/denoland/deno_std/pull/15/files#diff-106086daad2313990285a9c61085eeb5995d479127db151e613f84ea709450c8R104 |
||
async function serveDirIndex( | ||
dirPath: string, | ||
options: { | ||
|
@@ -265,40 +262,43 @@ async function serveDirIndex( | |
): Promise<Response> { | ||
const showDotfiles = options.dotfiles; | ||
const dirUrl = `/${posix.relative(options.target, dirPath)}`; | ||
const listEntry: EntryInfo[] = []; | ||
const listEntryPromise: Promise<EntryInfo>[] = []; | ||
|
||
// if ".." makes sense | ||
if (dirUrl !== "/") { | ||
const prevPath = posix.join(dirPath, ".."); | ||
const fileInfo = await Deno.stat(prevPath); | ||
listEntry.push({ | ||
const entryInfo = Deno.stat(prevPath).then((fileInfo): EntryInfo => ({ | ||
mode: modeToString(true, fileInfo.mode), | ||
size: "", | ||
name: "../", | ||
url: posix.join(dirUrl, ".."), | ||
}); | ||
})); | ||
listEntryPromise.push(entryInfo); | ||
} | ||
|
||
// Read fileInfo in parallel | ||
for await (const entry of Deno.readDir(dirPath)) { | ||
if (!showDotfiles && entry.name[0] === ".") { | ||
continue; | ||
} | ||
const filePath = posix.join(dirPath, entry.name); | ||
const fileUrl = encodeURIComponent(posix.join(dirUrl, entry.name)) | ||
.replaceAll("%2F", "/"); | ||
const fileInfo = await Deno.stat(filePath); | ||
listEntry.push({ | ||
const entryInfo = Deno.stat(filePath).then((fileInfo): EntryInfo => ({ | ||
mode: modeToString(entry.isDirectory, fileInfo.mode), | ||
size: entry.isFile ? fileLenToString(fileInfo.size ?? 0) : "", | ||
name: `${entry.name}${entry.isDirectory ? "/" : ""}`, | ||
url: `${fileUrl}${entry.isDirectory ? "/" : ""}`, | ||
}); | ||
})); | ||
listEntryPromise.push(entryInfo); | ||
} | ||
|
||
const listEntry = await Promise.all(listEntryPromise); | ||
listEntry.sort((a, b) => | ||
a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1 | ||
); | ||
const formattedDirUrl = `${dirUrl.replace(/\/$/, "")}/`; | ||
const page = encoder.encode(dirViewerTemplate(formattedDirUrl, listEntry)); | ||
const page = dirViewerTemplate(formattedDirUrl, listEntry); | ||
|
||
const headers = setBaseHeaders(); | ||
headers.set("content-type", "text/html"); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Note:
Response
object take care of encoding, and this is unnecessary now. Nice clean up.