2020-09-21 08:14:06 +03:00
|
|
|
"use strict";
|
|
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2021-03-31 02:55:25 +03:00
|
|
|
exports.start = exports.getMime = void 0;
|
2020-09-21 08:14:06 +03:00
|
|
|
const fs_1 = __importDefault(require("fs"));
|
|
|
|
const http_1 = require("http");
|
|
|
|
const path_1 = require("path");
|
2020-10-23 03:01:57 +03:00
|
|
|
const path_2 = require("../path");
|
2020-09-21 08:14:06 +03:00
|
|
|
function getMime(filename) {
|
|
|
|
switch (filename.split('.').pop().toLowerCase()) {
|
|
|
|
case 'css': return 'text/css';
|
|
|
|
case 'doctree': return 'application/x-doctree';
|
|
|
|
case 'eot': return 'application/vnd.ms-fontobject';
|
|
|
|
case 'gif': return 'image/gif';
|
|
|
|
case 'html': return 'text/html';
|
2021-02-08 23:27:05 +03:00
|
|
|
case 'ico': return 'image/x-icon';
|
2020-09-21 08:14:06 +03:00
|
|
|
case 'js': return 'application/javascript';
|
|
|
|
case 'jpg': return 'image/jpeg';
|
|
|
|
case 'jpeg': return 'image/jpeg';
|
2020-10-23 03:01:57 +03:00
|
|
|
case 'json': return 'application/json';
|
2020-09-21 08:14:06 +03:00
|
|
|
case 'md': return 'text/markdown';
|
|
|
|
case 'pickle': return 'application/x-pickle';
|
|
|
|
case 'png': return 'image/png';
|
|
|
|
case 'svg': return 'image/svg+xml';
|
|
|
|
case 'ttf': return 'application/x-font-ttf';
|
|
|
|
case 'txt': return 'text/plain';
|
|
|
|
case 'woff': return 'application/font-woff';
|
|
|
|
}
|
|
|
|
console.log('NO MIME', filename);
|
|
|
|
return "application/octet-stream";
|
|
|
|
}
|
|
|
|
exports.getMime = getMime;
|
|
|
|
function start(root, options) {
|
|
|
|
if (root == null) {
|
|
|
|
throw new Error("root required");
|
|
|
|
}
|
|
|
|
if (options == null) {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
if (options.port == null) {
|
|
|
|
options.port = 8000;
|
|
|
|
}
|
2021-10-16 09:29:27 +03:00
|
|
|
root = (0, path_1.resolve)(root);
|
|
|
|
const server = (0, http_1.createServer)((req, resp) => {
|
2020-10-23 03:01:57 +03:00
|
|
|
const url = req.url.split("?")[0];
|
2020-09-21 08:14:06 +03:00
|
|
|
// Follow redirects in options
|
2020-10-23 03:01:57 +03:00
|
|
|
if (options.redirects && options.redirects[url]) {
|
|
|
|
resp.writeHead(301, { Location: options.redirects[url] });
|
2020-09-21 08:14:06 +03:00
|
|
|
resp.end();
|
|
|
|
return;
|
|
|
|
}
|
2021-10-16 09:29:27 +03:00
|
|
|
let filename = (0, path_1.resolve)(root, "." + url);
|
2020-09-21 08:14:06 +03:00
|
|
|
// Make sure we aren't crawling out of our sandbox
|
2020-10-23 03:01:57 +03:00
|
|
|
if (url[0] !== "/" || filename.substring(0, filename.length) !== filename) {
|
2020-09-21 08:14:06 +03:00
|
|
|
resp.writeHead(403);
|
|
|
|
resp.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const stat = fs_1.default.statSync(filename);
|
|
|
|
if (stat.isDirectory()) {
|
|
|
|
// Redirect bare directory to its path (i.e. "/foo" => "/foo/")
|
2020-10-23 03:01:57 +03:00
|
|
|
if (url[url.length - 1] !== "/") {
|
|
|
|
resp.writeHead(301, { Location: url + "/" });
|
2020-09-21 08:14:06 +03:00
|
|
|
resp.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
filename += "/index.html";
|
|
|
|
}
|
|
|
|
const content = fs_1.default.readFileSync(filename);
|
|
|
|
resp.writeHead(200, {
|
|
|
|
"Content-Length": content.length,
|
|
|
|
"Content-Type": getMime(filename)
|
|
|
|
});
|
|
|
|
resp.end(content);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
if (error.code === "ENOENT") {
|
|
|
|
resp.writeHead(404, {});
|
|
|
|
resp.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resp.writeHead(500, {});
|
|
|
|
resp.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
server.listen(options.port, () => {
|
|
|
|
console.log(`Server running on: http://localhost:${options.port}`);
|
|
|
|
});
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
exports.start = start;
|
2021-10-16 09:29:27 +03:00
|
|
|
start((0, path_2.resolve)("docs"), {
|
2020-09-21 08:14:06 +03:00
|
|
|
redirects: {
|
|
|
|
"/": "/v5/"
|
|
|
|
}
|
|
|
|
});
|