1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 08:09:08 -05:00

http: add return types (denoland/deno_std#250)

Original: 84f9fe0649
This commit is contained in:
Eugen Cazacu 2019-03-08 19:04:02 +01:00 committed by Ryan Dahl
parent 0434076ea6
commit 3761d41d32

View file

@ -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 `
<tr><td class="mode">${modeToString(
@ -119,8 +119,30 @@ function createDirEntryDisplay(
`;
}
async function serveFile(
req: ServerRequest,
filename: string
): Promise<Response> {
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<Response> {
// 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<Response> {
if (
e instanceof Deno.DenoError &&
(e as Deno.DenoError<any>).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();
}