diff --git a/http/file_server.ts b/http/file_server.ts index 19a94c6aaa..659e9ba562 100755 --- a/http/file_server.ts +++ b/http/file_server.ts @@ -61,7 +61,7 @@ if (target) { const addr = `0.0.0.0:${serverArgs[2] || 4500}`; const encoder = new TextEncoder(); -function modeToString(isDir: boolean, maybeMode: number | null) { +function modeToString(isDir: boolean, maybeMode: number | null): string { const modeMap = ["---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"]; if (maybeMode === null) { @@ -83,7 +83,7 @@ function modeToString(isDir: boolean, maybeMode: number | null) { return output; } -function fileLenToString(len: number) { +function fileLenToString(len: number): string { const multipler = 1024; let base = 1; const suffix = ["B", "K", "M", "G", "T"]; @@ -106,7 +106,7 @@ function createDirEntryDisplay( size: number | null, mode: number | null, isDir: boolean -) { +): string { const sizeStr = size === null ? "" : "" + fileLenToString(size!); return ` ${modeToString( @@ -119,8 +119,30 @@ function createDirEntryDisplay( `; } +async function serveFile( + req: ServerRequest, + filename: string +): Promise { + const file = await open(filename); + const fileInfo = await stat(filename); + const headers = new Headers(); + headers.set("content-length", fileInfo.len.toString()); + headers.set("content-type", contentType(extname(filename)) || "text/plain"); + + const res = { + status: 200, + body: file, + headers + }; + return res; +} + // TODO: simplify this after deno.stat and deno.readDir are fixed -async function serveDir(req: ServerRequest, dirPath: string, dirName: string) { +async function serveDir( + req: ServerRequest, + dirPath: string, + dirName: string +): Promise { // dirname has no prefix const listEntry: string[] = []; const fileInfos = await readDir(dirPath); @@ -163,22 +185,7 @@ async function serveDir(req: ServerRequest, dirPath: string, dirName: string) { return res; } -async function serveFile(req: ServerRequest, filename: string) { - const file = await open(filename); - const fileInfo = await stat(filename); - const headers = new Headers(); - headers.set("content-length", fileInfo.len.toString()); - headers.set("content-type", contentType(extname(filename)) || "text/plain"); - - const res = { - status: 200, - body: file, - headers - }; - return res; -} - -async function serveFallback(req: ServerRequest, e: Error) { +async function serveFallback(req: ServerRequest, e: Error): Promise { if ( e instanceof Deno.DenoError && (e as Deno.DenoError).kind === ErrorKind.NotFound @@ -195,14 +202,14 @@ async function serveFallback(req: ServerRequest, e: Error) { } } -function serverLog(req: ServerRequest, res: Response) { +function serverLog(req: ServerRequest, res: Response): void { const d = new Date().toISOString(); const dateFmt = `[${d.slice(0, 10)} ${d.slice(11, 19)}]`; const s = `${dateFmt} "${req.method} ${req.url} ${req.proto}" ${res.status}`; console.log(s); } -function setCORS(res: Response) { +function setCORS(res: Response): void { if (!res.headers) { res.headers = new Headers(); }