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

perf(http/file_server): read fileinfo in parallel #3363

Merged
merged 2 commits into from
May 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ interface EntryInfo {
name: string;
}

const encoder = new TextEncoder();
Copy link
Member

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.


const ENV_PERM_STATUS =
Deno.permissions.querySync?.({ name: "env", variable: "DENO_DEPLOYMENT_ID" })
.state ?? "granted"; // for deno deploy
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

async function serveDirIndex(
dirPath: string,
options: {
Expand All @@ -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");
Expand Down