From 3761d41d32d6e6baffa56adbda380d04ad10e710 Mon Sep 17 00:00:00 2001 From: Eugen Cazacu <32613393+oygen87@users.noreply.github.com> Date: Fri, 8 Mar 2019 19:04:02 +0100 Subject: [PATCH] http: add return types (denoland/deno_std#250) Original: https://github.com/denoland/deno_std/commit/84f9fe064984276781e7f9deb03544024d81c1a6 --- http/file_server.ts | 51 ++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 22 deletions(-) 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(); }