mirror of
https://github.com/denoland/deno.git
synced 2024-11-27 16:10:57 -05:00
parent
0434076ea6
commit
3761d41d32
1 changed files with 29 additions and 22 deletions
|
@ -61,7 +61,7 @@ if (target) {
|
||||||
const addr = `0.0.0.0:${serverArgs[2] || 4500}`;
|
const addr = `0.0.0.0:${serverArgs[2] || 4500}`;
|
||||||
const encoder = new TextEncoder();
|
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"];
|
const modeMap = ["---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"];
|
||||||
|
|
||||||
if (maybeMode === null) {
|
if (maybeMode === null) {
|
||||||
|
@ -83,7 +83,7 @@ function modeToString(isDir: boolean, maybeMode: number | null) {
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
function fileLenToString(len: number) {
|
function fileLenToString(len: number): string {
|
||||||
const multipler = 1024;
|
const multipler = 1024;
|
||||||
let base = 1;
|
let base = 1;
|
||||||
const suffix = ["B", "K", "M", "G", "T"];
|
const suffix = ["B", "K", "M", "G", "T"];
|
||||||
|
@ -106,7 +106,7 @@ function createDirEntryDisplay(
|
||||||
size: number | null,
|
size: number | null,
|
||||||
mode: number | null,
|
mode: number | null,
|
||||||
isDir: boolean
|
isDir: boolean
|
||||||
) {
|
): string {
|
||||||
const sizeStr = size === null ? "" : "" + fileLenToString(size!);
|
const sizeStr = size === null ? "" : "" + fileLenToString(size!);
|
||||||
return `
|
return `
|
||||||
<tr><td class="mode">${modeToString(
|
<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
|
// 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
|
// dirname has no prefix
|
||||||
const listEntry: string[] = [];
|
const listEntry: string[] = [];
|
||||||
const fileInfos = await readDir(dirPath);
|
const fileInfos = await readDir(dirPath);
|
||||||
|
@ -163,22 +185,7 @@ async function serveDir(req: ServerRequest, dirPath: string, dirName: string) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function serveFile(req: ServerRequest, filename: string) {
|
async function serveFallback(req: ServerRequest, e: Error): 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function serveFallback(req: ServerRequest, e: Error) {
|
|
||||||
if (
|
if (
|
||||||
e instanceof Deno.DenoError &&
|
e instanceof Deno.DenoError &&
|
||||||
(e as Deno.DenoError<any>).kind === ErrorKind.NotFound
|
(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 d = new Date().toISOString();
|
||||||
const dateFmt = `[${d.slice(0, 10)} ${d.slice(11, 19)}]`;
|
const dateFmt = `[${d.slice(0, 10)} ${d.slice(11, 19)}]`;
|
||||||
const s = `${dateFmt} "${req.method} ${req.url} ${req.proto}" ${res.status}`;
|
const s = `${dateFmt} "${req.method} ${req.url} ${req.proto}" ${res.status}`;
|
||||||
console.log(s);
|
console.log(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setCORS(res: Response) {
|
function setCORS(res: Response): void {
|
||||||
if (!res.headers) {
|
if (!res.headers) {
|
||||||
res.headers = new Headers();
|
res.headers = new Headers();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue