2020-05-27 15:24:57 -04:00
|
|
|
#!/usr/bin/env -S deno run --allow-net --allow-read
|
2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2018-12-09 15:35:26 -05:00
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2020-09-27 06:22:32 -04:00
|
|
|
import { extname, posix } from "../path/mod.ts";
|
2020-08-12 06:55:38 -04:00
|
|
|
import {
|
2020-09-27 06:22:32 -04:00
|
|
|
HTTPSOptions,
|
2020-08-12 06:55:38 -04:00
|
|
|
listenAndServe,
|
|
|
|
listenAndServeTLS,
|
|
|
|
Response,
|
2020-09-27 06:22:32 -04:00
|
|
|
ServerRequest,
|
2020-08-12 06:55:38 -04:00
|
|
|
} from "./server.ts";
|
2019-12-13 21:01:32 -05:00
|
|
|
import { parse } from "../flags/mod.ts";
|
2020-06-07 09:20:33 -04:00
|
|
|
import { assert } from "../_util/assert.ts";
|
2018-12-11 17:56:32 -05:00
|
|
|
|
2019-12-02 19:14:25 -05:00
|
|
|
interface EntryInfo {
|
|
|
|
mode: string;
|
|
|
|
size: string;
|
|
|
|
url: string;
|
|
|
|
name: string;
|
|
|
|
}
|
2018-11-07 13:16:07 -05:00
|
|
|
|
2020-08-12 11:38:25 -04:00
|
|
|
export interface FileServerArgs {
|
2019-12-13 21:01:32 -05:00
|
|
|
_: string[];
|
|
|
|
// -p --port
|
2020-08-12 11:38:25 -04:00
|
|
|
p?: number;
|
|
|
|
port?: number;
|
2019-12-13 21:01:32 -05:00
|
|
|
// --cors
|
2020-08-12 11:38:25 -04:00
|
|
|
cors?: boolean;
|
|
|
|
// --no-dir-listing
|
|
|
|
"dir-listing"?: boolean;
|
2020-08-12 06:55:38 -04:00
|
|
|
// --host
|
2020-08-12 11:38:25 -04:00
|
|
|
host?: string;
|
2020-08-12 06:55:38 -04:00
|
|
|
// -c --cert
|
2020-08-12 11:38:25 -04:00
|
|
|
c?: string;
|
|
|
|
cert?: string;
|
2020-08-12 06:55:38 -04:00
|
|
|
// -k --key
|
2020-08-12 11:38:25 -04:00
|
|
|
k?: string;
|
|
|
|
key?: string;
|
|
|
|
// -h --help
|
|
|
|
h?: boolean;
|
|
|
|
help?: boolean;
|
2019-12-13 21:01:32 -05:00
|
|
|
}
|
|
|
|
|
2019-12-02 19:14:25 -05:00
|
|
|
const encoder = new TextEncoder();
|
2019-12-13 21:01:32 -05:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
const serverArgs = parse(Deno.args) as FileServerArgs;
|
2020-05-21 13:55:18 -04:00
|
|
|
const target = posix.resolve(serverArgs._[0] ?? "");
|
2019-12-13 21:01:32 -05:00
|
|
|
|
2020-04-03 12:11:52 -04:00
|
|
|
const MEDIA_TYPES: Record<string, string> = {
|
|
|
|
".md": "text/markdown",
|
|
|
|
".html": "text/html",
|
|
|
|
".htm": "text/html",
|
|
|
|
".json": "application/json",
|
|
|
|
".map": "application/json",
|
|
|
|
".txt": "text/plain",
|
2020-04-07 10:06:22 -04:00
|
|
|
".ts": "text/typescript",
|
|
|
|
".tsx": "text/tsx",
|
2020-04-03 12:11:52 -04:00
|
|
|
".js": "application/javascript",
|
2020-04-07 10:06:22 -04:00
|
|
|
".jsx": "text/jsx",
|
2020-04-03 12:11:52 -04:00
|
|
|
".gz": "application/gzip",
|
2020-05-14 11:56:29 -04:00
|
|
|
".css": "text/css",
|
2020-05-27 09:35:03 -04:00
|
|
|
".wasm": "application/wasm",
|
2020-08-12 06:55:38 -04:00
|
|
|
".mjs": "application/javascript",
|
2020-04-03 12:11:52 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Returns the content-type based on the extension of a path. */
|
|
|
|
function contentType(path: string): string | undefined {
|
|
|
|
return MEDIA_TYPES[extname(path)];
|
|
|
|
}
|
|
|
|
|
2019-03-08 13:04:02 -05:00
|
|
|
function modeToString(isDir: boolean, maybeMode: number | null): string {
|
2018-12-11 17:56:32 -05:00
|
|
|
const modeMap = ["---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"];
|
2018-11-07 13:16:07 -05:00
|
|
|
|
2018-12-11 17:56:32 -05:00
|
|
|
if (maybeMode === null) {
|
|
|
|
return "(unknown mode)";
|
|
|
|
}
|
2020-02-07 02:23:38 -05:00
|
|
|
const mode = maybeMode.toString(8);
|
2018-12-11 17:56:32 -05:00
|
|
|
if (mode.length < 3) {
|
|
|
|
return "(unknown mode)";
|
|
|
|
}
|
|
|
|
let output = "";
|
|
|
|
mode
|
|
|
|
.split("")
|
|
|
|
.reverse()
|
|
|
|
.slice(0, 3)
|
2019-11-13 13:42:34 -05:00
|
|
|
.forEach((v): void => {
|
|
|
|
output = modeMap[+v] + output;
|
|
|
|
});
|
2018-12-11 17:56:32 -05:00
|
|
|
output = `(${isDir ? "d" : "-"}${output})`;
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2019-03-08 13:04:02 -05:00
|
|
|
function fileLenToString(len: number): string {
|
2019-05-31 08:39:05 -04:00
|
|
|
const multiplier = 1024;
|
2018-12-11 17:56:32 -05:00
|
|
|
let base = 1;
|
|
|
|
const suffix = ["B", "K", "M", "G", "T"];
|
|
|
|
let suffixIndex = 0;
|
|
|
|
|
2019-05-31 08:39:05 -04:00
|
|
|
while (base * multiplier < len) {
|
2018-12-11 17:56:32 -05:00
|
|
|
if (suffixIndex >= suffix.length - 1) {
|
|
|
|
break;
|
2018-12-09 15:35:26 -05:00
|
|
|
}
|
2019-05-31 08:39:05 -04:00
|
|
|
base *= multiplier;
|
2018-12-11 17:56:32 -05:00
|
|
|
suffixIndex++;
|
2018-12-09 15:35:26 -05:00
|
|
|
}
|
2018-12-11 17:56:32 -05:00
|
|
|
|
|
|
|
return `${(len / base).toFixed(2)}${suffix[suffixIndex]}`;
|
|
|
|
}
|
|
|
|
|
2020-11-19 21:59:45 -05:00
|
|
|
/**
|
|
|
|
* Returns an HTTP Response with the requested file as the body
|
|
|
|
* @param req The server request context used to cleanup the file handle
|
|
|
|
* @param filePath Path of the file to serve
|
|
|
|
*/
|
2020-04-01 12:51:01 -04:00
|
|
|
export async function serveFile(
|
2019-03-08 13:04:02 -05:00
|
|
|
req: ServerRequest,
|
2020-07-14 15:24:17 -04:00
|
|
|
filePath: string,
|
2019-03-08 13:04:02 -05:00
|
|
|
): Promise<Response> {
|
2020-06-12 15:23:38 -04:00
|
|
|
const [file, fileInfo] = await Promise.all([
|
|
|
|
Deno.open(filePath),
|
|
|
|
Deno.stat(filePath),
|
|
|
|
]);
|
2019-03-08 13:04:02 -05:00
|
|
|
const headers = new Headers();
|
2020-03-14 22:57:42 -04:00
|
|
|
headers.set("content-length", fileInfo.size.toString());
|
2020-04-03 12:11:52 -04:00
|
|
|
const contentTypeValue = contentType(filePath);
|
2020-04-01 12:51:01 -04:00
|
|
|
if (contentTypeValue) {
|
|
|
|
headers.set("content-type", contentTypeValue);
|
|
|
|
}
|
2020-06-01 18:45:42 -04:00
|
|
|
req.done.then(() => {
|
|
|
|
file.close();
|
|
|
|
});
|
2020-04-01 12:51:01 -04:00
|
|
|
return {
|
2019-03-08 13:04:02 -05:00
|
|
|
status: 200,
|
|
|
|
body: file,
|
2020-03-28 13:03:49 -04:00
|
|
|
headers,
|
2019-03-08 13:04:02 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-29 16:39:37 -04:00
|
|
|
// TODO: simplify this after deno.stat and deno.readDir are fixed
|
2019-03-08 13:04:02 -05:00
|
|
|
async function serveDir(
|
|
|
|
req: ServerRequest,
|
2020-07-14 15:24:17 -04:00
|
|
|
dirPath: string,
|
2019-03-08 13:04:02 -05:00
|
|
|
): Promise<Response> {
|
2019-08-15 11:59:43 -04:00
|
|
|
const dirUrl = `/${posix.relative(target, dirPath)}`;
|
2019-12-02 19:14:25 -05:00
|
|
|
const listEntry: EntryInfo[] = [];
|
2020-06-12 15:23:38 -04:00
|
|
|
for await (const entry of Deno.readDir(dirPath)) {
|
2020-04-29 16:00:31 -04:00
|
|
|
const filePath = posix.join(dirPath, entry.name);
|
|
|
|
const fileUrl = posix.join(dirUrl, entry.name);
|
|
|
|
if (entry.name === "index.html" && entry.isFile) {
|
2018-12-11 17:56:32 -05:00
|
|
|
// in case index.html as dir...
|
2020-03-16 05:22:16 -04:00
|
|
|
return serveFile(req, filePath);
|
2018-12-11 17:56:32 -05:00
|
|
|
}
|
|
|
|
// Yuck!
|
2020-04-29 16:00:31 -04:00
|
|
|
let fileInfo = null;
|
2018-12-11 17:56:32 -05:00
|
|
|
try {
|
2020-06-12 15:23:38 -04:00
|
|
|
fileInfo = await Deno.stat(filePath);
|
2020-06-12 09:19:29 -04:00
|
|
|
} catch (e) {
|
|
|
|
// Pass
|
|
|
|
}
|
2019-06-22 10:52:56 -04:00
|
|
|
listEntry.push({
|
2020-04-29 16:00:31 -04:00
|
|
|
mode: modeToString(entry.isDirectory, fileInfo?.mode ?? null),
|
|
|
|
size: entry.isFile ? fileLenToString(fileInfo?.size ?? 0) : "",
|
|
|
|
name: entry.name,
|
2020-03-28 13:03:49 -04:00
|
|
|
url: fileUrl,
|
2019-06-22 10:52:56 -04:00
|
|
|
});
|
2018-12-11 17:56:32 -05:00
|
|
|
}
|
2019-12-02 19:14:25 -05:00
|
|
|
listEntry.sort((a, b) =>
|
|
|
|
a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1
|
2018-12-11 17:56:32 -05:00
|
|
|
);
|
2019-12-02 19:14:25 -05:00
|
|
|
const formattedDirUrl = `${dirUrl.replace(/\/$/, "")}/`;
|
|
|
|
const page = encoder.encode(dirViewerTemplate(formattedDirUrl, listEntry));
|
2018-12-11 17:56:32 -05:00
|
|
|
|
2018-12-09 15:35:26 -05:00
|
|
|
const headers = new Headers();
|
2018-12-11 17:56:32 -05:00
|
|
|
headers.set("content-type", "text/html");
|
|
|
|
|
|
|
|
const res = {
|
|
|
|
status: 200,
|
|
|
|
body: page,
|
2020-03-28 13:03:49 -04:00
|
|
|
headers,
|
2018-12-11 17:56:32 -05:00
|
|
|
};
|
2018-12-12 04:38:46 -05:00
|
|
|
return res;
|
2018-12-11 17:56:32 -05:00
|
|
|
}
|
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
function serveFallback(req: ServerRequest, e: Error): Promise<Response> {
|
2020-02-24 15:48:35 -05:00
|
|
|
if (e instanceof Deno.errors.NotFound) {
|
2020-03-20 09:38:34 -04:00
|
|
|
return Promise.resolve({
|
2018-12-17 11:49:10 -05:00
|
|
|
status: 404,
|
2020-03-28 13:03:49 -04:00
|
|
|
body: encoder.encode("Not found"),
|
2020-03-20 09:38:34 -04:00
|
|
|
});
|
2018-12-11 17:56:32 -05:00
|
|
|
} else {
|
2020-03-20 09:38:34 -04:00
|
|
|
return Promise.resolve({
|
2018-12-11 17:56:32 -05:00
|
|
|
status: 500,
|
2020-03-28 13:03:49 -04:00
|
|
|
body: encoder.encode("Internal server error"),
|
2020-03-20 09:38:34 -04:00
|
|
|
});
|
2018-12-11 17:56:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-08 13:04:02 -05:00
|
|
|
function serverLog(req: ServerRequest, res: Response): void {
|
2018-12-12 04:38:46 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-03-08 13:04:02 -05:00
|
|
|
function setCORS(res: Response): void {
|
2018-12-23 22:50:49 -05:00
|
|
|
if (!res.headers) {
|
|
|
|
res.headers = new Headers();
|
|
|
|
}
|
2020-02-07 02:23:38 -05:00
|
|
|
res.headers.append("access-control-allow-origin", "*");
|
|
|
|
res.headers.append(
|
2018-12-23 22:50:49 -05:00
|
|
|
"access-control-allow-headers",
|
2020-07-14 15:24:17 -04:00
|
|
|
"Origin, X-Requested-With, Content-Type, Accept, Range",
|
2018-12-23 22:50:49 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-02 19:14:25 -05:00
|
|
|
function dirViewerTemplate(dirname: string, entries: EntryInfo[]): string {
|
|
|
|
return html`
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8" />
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
|
|
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
|
|
|
|
<title>Deno File Server</title>
|
|
|
|
<style>
|
|
|
|
:root {
|
|
|
|
--background-color: #fafafa;
|
|
|
|
--color: rgba(0, 0, 0, 0.87);
|
|
|
|
}
|
|
|
|
@media (prefers-color-scheme: dark) {
|
|
|
|
:root {
|
|
|
|
--background-color: #303030;
|
|
|
|
--color: #fff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@media (min-width: 960px) {
|
|
|
|
main {
|
|
|
|
max-width: 960px;
|
|
|
|
}
|
|
|
|
body {
|
|
|
|
padding-left: 32px;
|
|
|
|
padding-right: 32px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@media (min-width: 600px) {
|
|
|
|
main {
|
|
|
|
padding-left: 24px;
|
|
|
|
padding-right: 24px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
body {
|
|
|
|
background: var(--background-color);
|
|
|
|
color: var(--color);
|
|
|
|
font-family: "Roboto", "Helvetica", "Arial", sans-serif;
|
|
|
|
font-weight: 400;
|
|
|
|
line-height: 1.43;
|
|
|
|
font-size: 0.875rem;
|
|
|
|
}
|
|
|
|
a {
|
|
|
|
color: #2196f3;
|
|
|
|
text-decoration: none;
|
|
|
|
}
|
|
|
|
a:hover {
|
|
|
|
text-decoration: underline;
|
|
|
|
}
|
|
|
|
table th {
|
|
|
|
text-align: left;
|
|
|
|
}
|
|
|
|
table td {
|
|
|
|
padding: 12px 24px 0 0;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<main>
|
|
|
|
<h1>Index of ${dirname}</h1>
|
|
|
|
<table>
|
|
|
|
<tr>
|
|
|
|
<th>Mode</th>
|
|
|
|
<th>Size</th>
|
|
|
|
<th>Name</th>
|
|
|
|
</tr>
|
2020-07-14 15:24:17 -04:00
|
|
|
${
|
|
|
|
entries.map(
|
|
|
|
(entry) =>
|
|
|
|
html`
|
2020-03-28 13:03:49 -04:00
|
|
|
<tr>
|
|
|
|
<td class="mode">
|
|
|
|
${entry.mode}
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
${entry.size}
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<a href="${entry.url}">${entry.name}</a>
|
|
|
|
</td>
|
|
|
|
</tr>
|
2020-07-14 15:24:17 -04:00
|
|
|
`,
|
|
|
|
)
|
|
|
|
}
|
2019-12-02 19:14:25 -05:00
|
|
|
</table>
|
|
|
|
</main>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function html(strings: TemplateStringsArray, ...values: unknown[]): string {
|
|
|
|
const l = strings.length - 1;
|
|
|
|
let html = "";
|
|
|
|
|
|
|
|
for (let i = 0; i < l; i++) {
|
|
|
|
let v = values[i];
|
|
|
|
if (v instanceof Array) {
|
|
|
|
v = v.join("");
|
|
|
|
}
|
|
|
|
const s = strings[i] + v;
|
|
|
|
html += s;
|
|
|
|
}
|
|
|
|
html += strings[l];
|
|
|
|
return html;
|
|
|
|
}
|
|
|
|
|
2020-10-26 09:55:26 -04:00
|
|
|
function normalizeURL(url: string): string {
|
2020-10-27 06:48:45 -04:00
|
|
|
let normalizedUrl = url;
|
2020-10-26 09:55:26 -04:00
|
|
|
try {
|
2020-10-27 06:48:45 -04:00
|
|
|
normalizedUrl = decodeURI(normalizedUrl);
|
2020-10-26 09:55:26 -04:00
|
|
|
} catch (e) {
|
|
|
|
if (!(e instanceof URIError)) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2020-10-27 06:48:45 -04:00
|
|
|
normalizedUrl = posix.normalize(normalizedUrl);
|
2020-10-26 09:55:26 -04:00
|
|
|
const startOfParams = normalizedUrl.indexOf("?");
|
|
|
|
return startOfParams > -1
|
|
|
|
? normalizedUrl.slice(0, startOfParams)
|
|
|
|
: normalizedUrl;
|
|
|
|
}
|
|
|
|
|
2020-04-01 12:51:01 -04:00
|
|
|
function main(): void {
|
2020-06-03 13:48:03 -04:00
|
|
|
const CORSEnabled = serverArgs.cors ? true : false;
|
2020-08-12 06:55:38 -04:00
|
|
|
const port = serverArgs.port ?? serverArgs.p ?? 4507;
|
|
|
|
const host = serverArgs.host ?? "0.0.0.0";
|
|
|
|
const addr = `${host}:${port}`;
|
|
|
|
const tlsOpts = {} as HTTPSOptions;
|
|
|
|
tlsOpts.certFile = serverArgs.cert ?? serverArgs.c ?? "";
|
|
|
|
tlsOpts.keyFile = serverArgs.key ?? serverArgs.k ?? "";
|
2020-08-12 11:38:25 -04:00
|
|
|
const dirListingEnabled = serverArgs["dir-listing"] ?? true;
|
2020-08-12 06:55:38 -04:00
|
|
|
|
|
|
|
if (tlsOpts.keyFile || tlsOpts.certFile) {
|
|
|
|
if (tlsOpts.keyFile === "" || tlsOpts.certFile === "") {
|
|
|
|
console.log("--key and --cert are required for TLS");
|
|
|
|
serverArgs.h = true;
|
|
|
|
}
|
|
|
|
}
|
2020-06-03 13:48:03 -04:00
|
|
|
|
|
|
|
if (serverArgs.h ?? serverArgs.help) {
|
|
|
|
console.log(`Deno File Server
|
|
|
|
Serves a local directory in HTTP.
|
2020-06-12 15:23:38 -04:00
|
|
|
|
2020-06-03 13:48:03 -04:00
|
|
|
INSTALL:
|
|
|
|
deno install --allow-net --allow-read https://deno.land/std/http/file_server.ts
|
2020-06-12 15:23:38 -04:00
|
|
|
|
2020-06-03 13:48:03 -04:00
|
|
|
USAGE:
|
|
|
|
file_server [path] [options]
|
2020-06-12 15:23:38 -04:00
|
|
|
|
2020-06-03 13:48:03 -04:00
|
|
|
OPTIONS:
|
|
|
|
-h, --help Prints help information
|
|
|
|
-p, --port <PORT> Set port
|
2020-08-12 06:55:38 -04:00
|
|
|
--cors Enable CORS via the "Access-Control-Allow-Origin" header
|
|
|
|
--host <HOST> Hostname (default is 0.0.0.0)
|
|
|
|
-c, --cert <FILE> TLS certificate file (enables TLS)
|
|
|
|
-k, --key <FILE> TLS key file (enables TLS)
|
2020-08-12 11:38:25 -04:00
|
|
|
--no-dir-listing Disable directory listing
|
2020-08-12 06:55:38 -04:00
|
|
|
|
|
|
|
All TLS options are required when one is provided.`);
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.exit();
|
2020-06-03 13:48:03 -04:00
|
|
|
}
|
|
|
|
|
2020-08-12 06:55:38 -04:00
|
|
|
const handler = async (req: ServerRequest): Promise<void> => {
|
2020-10-26 09:55:26 -04:00
|
|
|
const normalizedUrl = normalizeURL(req.url);
|
2020-08-12 06:55:38 -04:00
|
|
|
const fsPath = posix.join(target, normalizedUrl);
|
2018-12-11 17:56:32 -05:00
|
|
|
|
2020-08-12 06:55:38 -04:00
|
|
|
let response: Response | undefined;
|
|
|
|
try {
|
|
|
|
const fileInfo = await Deno.stat(fsPath);
|
|
|
|
if (fileInfo.isDirectory) {
|
2020-08-12 11:38:25 -04:00
|
|
|
if (dirListingEnabled) {
|
|
|
|
response = await serveDir(req, fsPath);
|
|
|
|
} else {
|
|
|
|
throw new Deno.errors.NotFound();
|
|
|
|
}
|
2020-08-12 06:55:38 -04:00
|
|
|
} else {
|
|
|
|
response = await serveFile(req, fsPath);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e.message);
|
|
|
|
response = await serveFallback(req, e);
|
|
|
|
} finally {
|
|
|
|
if (CORSEnabled) {
|
|
|
|
assert(response);
|
|
|
|
setCORS(response);
|
|
|
|
}
|
|
|
|
serverLog(req, response!);
|
2020-04-01 12:51:01 -04:00
|
|
|
try {
|
2020-08-12 06:55:38 -04:00
|
|
|
await req.respond(response!);
|
2020-04-01 12:51:01 -04:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e.message);
|
2019-04-24 07:41:23 -04:00
|
|
|
}
|
2020-08-12 06:55:38 -04:00
|
|
|
}
|
|
|
|
};
|
2018-11-07 13:16:07 -05:00
|
|
|
|
2020-08-12 06:55:38 -04:00
|
|
|
let proto = "http";
|
|
|
|
if (tlsOpts.keyFile || tlsOpts.certFile) {
|
|
|
|
proto += "s";
|
|
|
|
tlsOpts.hostname = host;
|
|
|
|
tlsOpts.port = port;
|
|
|
|
listenAndServeTLS(tlsOpts, handler);
|
|
|
|
} else {
|
|
|
|
listenAndServe(addr, handler);
|
|
|
|
}
|
|
|
|
console.log(`${proto.toUpperCase()} server listening on ${proto}://${addr}/`);
|
2020-04-01 12:51:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (import.meta.main) {
|
|
|
|
main();
|
|
|
|
}
|