summaryrefslogtreecommitdiff
path: root/std/http
diff options
context:
space:
mode:
authorBert Belder <bertbelder@gmail.com>2020-04-29 22:00:31 +0200
committerGitHub <noreply@github.com>2020-04-29 16:00:31 -0400
commit3e6ea6284178df0be4982d9775f47b47b14c6139 (patch)
treeed684ea536e32023e72004110556ad8285126676 /std/http
parent721a4ad59d4a8bdd8470d6b98839137f14c84ba9 (diff)
BREAKING: Include limited metadata in 'DirEntry' objects (#4941)
This change is to prevent needed a separate stat syscall for each file when using readdir. For consistency, this PR also modifies std's `WalkEntry` interface to extend `DirEntry` with an additional `path` field.
Diffstat (limited to 'std/http')
-rwxr-xr-xstd/http/file_server.ts22
1 files changed, 11 insertions, 11 deletions
diff --git a/std/http/file_server.ts b/std/http/file_server.ts
index 90f8b8792..c225dbef1 100755
--- a/std/http/file_server.ts
+++ b/std/http/file_server.ts
@@ -140,22 +140,22 @@ async function serveDir(
): Promise<Response> {
const dirUrl = `/${posix.relative(target, dirPath)}`;
const listEntry: EntryInfo[] = [];
- for await (const dirEntry of readdir(dirPath)) {
- const filePath = posix.join(dirPath, dirEntry.name);
- const fileUrl = posix.join(dirUrl, dirEntry.name);
- if (dirEntry.name === "index.html" && dirEntry.isFile) {
+ for await (const entry of readdir(dirPath)) {
+ const filePath = posix.join(dirPath, entry.name);
+ const fileUrl = posix.join(dirUrl, entry.name);
+ if (entry.name === "index.html" && entry.isFile) {
// in case index.html as dir...
return serveFile(req, filePath);
}
// Yuck!
- let mode = null;
+ let fileInfo = null;
try {
- mode = (await stat(filePath)).mode;
+ fileInfo = await stat(filePath);
} catch (e) {}
listEntry.push({
- mode: modeToString(dirEntry.isDirectory, mode),
- size: dirEntry.isFile ? fileLenToString(dirEntry.size) : "",
- name: dirEntry.name,
+ mode: modeToString(entry.isDirectory, fileInfo?.mode ?? null),
+ size: entry.isFile ? fileLenToString(fileInfo?.size ?? 0) : "",
+ name: entry.name,
url: fileUrl,
});
}
@@ -331,8 +331,8 @@ function main(): void {
let response: Response | undefined;
try {
- const info = await stat(fsPath);
- if (info.isDirectory) {
+ const fileInfo = await stat(fsPath);
+ if (fileInfo.isDirectory) {
response = await serveDir(req, fsPath);
} else {
response = await serveFile(req, fsPath);