Skip to content

Commit

Permalink
perf(http/file_server): read fileinfo in parallel (#3363)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayame113 authored May 6, 2023
1 parent 6e8ee3c commit 163a902
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,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
Expand Down Expand Up @@ -254,7 +252,6 @@ export async function serveFile(
return createCommonResponse(Status.OK, file.readable, { headers });
}

// TODO(bartlomieju): simplify this after deno.stat and deno.readDir are fixed
async function serveDirIndex(
dirPath: string,
options: {
Expand All @@ -264,40 +261,43 @@ async function serveDirIndex(
): Promise<Response> {
const { showDotfiles } = options;
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

0 comments on commit 163a902

Please sign in to comment.