-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
211 lines (184 loc) · 5.53 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env -S deno run --allow-net --allow-read
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
// This program serves files in the current directory over HTTP.
// TODO Stream responses instead of reading them into memory.
// TODO Add tests like these:
// https://github.com/indexzero/http-server/blob/master/test/http-server-test.js
import { posix, extname } from "https://deno.land/std/path/mod.ts";
import { listenAndServe, ServerRequest, Response } from "https://deno.land/std/http/server.ts";
import { parse } from "https://deno.land/std/flags/mod.ts";
import { assert } from "https://deno.land/std/_util/assert.ts";
import { existsSync } from "https://deno.land/std/fs/mod.ts";
interface EntryInfo {
mode: string;
size: string;
url: string;
name: string;
}
interface FileServerArgs {
_: string[];
// -p --port
p: number;
port: number;
// --cors
cors: boolean;
// -h --help
h: boolean;
help: boolean;
}
const encoder = new TextEncoder();
const serverArgs = parse(Deno.args) as FileServerArgs;
const mainFile: string = posix.resolve(serverArgs._[0] ?? "./index.html");
const target = posix.resolve(serverArgs._[1] ?? "");
const MEDIA_TYPES: Record<string, string> = {
".md": "text/markdown",
".html": "text/html",
".htm": "text/html",
".json": "application/json",
".map": "application/json",
".txt": "text/plain",
".ts": "text/typescript",
".tsx": "text/tsx",
".js": "application/javascript",
".jsx": "text/jsx",
".gz": "application/gzip",
".css": "text/css",
".wasm": "application/wasm",
};
/** Returns the content-type based on the extension of a path. */
function contentType(path: string): string | undefined {
return MEDIA_TYPES[extname(path)];
}
function modeToString(isDir: boolean, maybeMode: number | null): string {
const modeMap = ["---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"];
if (maybeMode === null) {
return "(unknown mode)";
}
const mode = maybeMode.toString(8);
if (mode.length < 3) {
return "(unknown mode)";
}
let output = "";
mode
.split("")
.reverse()
.slice(0, 3)
.forEach((v): void => {
output = modeMap[+v] + output;
});
output = `(${isDir ? "d" : "-"}${output})`;
return output;
}
function fileLenToString(len: number): string {
const multiplier = 1024;
let base = 1;
const suffix = ["B", "K", "M", "G", "T"];
let suffixIndex = 0;
while (base * multiplier < len) {
if (suffixIndex >= suffix.length - 1) {
break;
}
base *= multiplier;
suffixIndex++;
}
return `${(len / base).toFixed(2)}${suffix[suffixIndex]}`;
}
export async function serveFile(
req: ServerRequest,
filePath: string,
): Promise<Response> {
const [file, fileInfo] = await Promise.all([
Deno.open(filePath),
Deno.stat(filePath),
]);
const headers = new Headers();
headers.set("content-length", fileInfo.size.toString());
const contentTypeValue = contentType(filePath);
if (contentTypeValue) {
headers.set("content-type", contentTypeValue);
}
req.done.then(() => {
file.close();
});
return {
status: 200,
body: file,
headers,
};
}
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): void {
if (!res.headers) {
res.headers = new Headers();
}
res.headers.append("access-control-allow-origin", "*");
res.headers.append(
"access-control-allow-headers",
"Origin, X-Requested-With, Content-Type, Accept, Range",
);
}
function main(): void {
const CORSEnabled = serverArgs.cors ? true : false;
const addr = `0.0.0.0:${serverArgs.port ?? serverArgs.p ?? 4507}`;
if (serverArgs.h ?? serverArgs.help) {
console.log(`Default File Server
Serves a local directory in HTTP, but serves the specified main file when the file is not found
USAGE:
file_server [mainFile] [path] [options]
OPTIONS:
path The directory to serve
mainFile The main file to serve, this file is also served during 404's
-h, --help Prints help information
-p, --port <PORT> Set port
--cors Enable CORS via the "Access-Control-Allow-Origin" header`);
Deno.exit();
}
if (!existsSync(mainFile)){
console.log(`mainFile ${mainFile} not found, ensure you set the mainFile correctly when calling the script !`)
}
listenAndServe(
addr,
async (req): Promise<void> => {
let normalizedUrl = posix.normalize(req.url);
try {
normalizedUrl = decodeURIComponent(normalizedUrl);
} catch (e) {
if (!(e instanceof URIError)) {
throw e;
}
}
let fsPath = posix.join(target, normalizedUrl);
if(fsPath.endsWith("/")){
fsPath += "index.html"
}
console.log('asds', fsPath);
let response: Response | undefined;
try{
const fileInfo = await Deno.stat(fsPath);
response = await serveFile(req, fsPath);
} catch(e) {
const fileInfo = await Deno.stat(mainFile);
response = await serveFile(req, mainFile);
}
if (CORSEnabled) {
assert(response);
setCORS(response);
}
serverLog(req, response!);
try {
await req.respond(response!);
} catch (e) {
console.error(e.message);
}
},
);
console.log(`HTTP server listening on http://${addr}/`);
}
if (import.meta.main) {
main();
}