1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | return new (P || (P = Promise))(function (resolve, reject) {
|
4 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
7 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8 | });
|
9 | };
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | const path = require("path");
|
12 | const url = require("url");
|
13 | const fs = require("fs");
|
14 | const utils_1 = require("./utils");
|
15 | function serveHtml(wwwDir, scriptLocations) {
|
16 | return function (filePath, req, res) {
|
17 | return __awaiter(this, void 0, void 0, function* () {
|
18 | const indexHtml = yield utils_1.fsReadFilePr(filePath);
|
19 | const appendString = scriptLocations.map(sl => `<script type="text/javascript" src="${sl}" charset="utf-8"></script>`).join('\n');
|
20 | const htmlString = indexHtml.toString()
|
21 | .replace(`</body>`, `${appendString}
|
22 | </body>`);
|
23 | res.writeHead(200, {
|
24 | 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0',
|
25 | 'Expires': '0',
|
26 | 'Content-Type': 'text/html'
|
27 | });
|
28 | res.end(htmlString);
|
29 | });
|
30 | };
|
31 | }
|
32 | exports.serveHtml = serveHtml;
|
33 | function serveDirContents(wwwDir) {
|
34 | return function (dirPath, req, res) {
|
35 | return __awaiter(this, void 0, void 0, function* () {
|
36 | let files;
|
37 | const dirUrl = req.url;
|
38 | if (!dirUrl) {
|
39 | return sendError(500, res, { err: 'Somthing is not right' });
|
40 | }
|
41 | try {
|
42 | files = yield utils_1.fsReadDirPr(dirPath);
|
43 | }
|
44 | catch (err) {
|
45 | return sendError(500, res, { err: err });
|
46 | }
|
47 | const templateSrc = yield utils_1.fsReadFilePr(path.join(__dirname, '..', 'assets', 'index.html'));
|
48 | if (!templateSrc) {
|
49 | throw new Error('wait, where is my template src.');
|
50 | }
|
51 | files = files
|
52 | .filter((fileName) => '.' !== fileName[0])
|
53 | .sort();
|
54 | const fileStats = yield Promise.all(files.map((fileName) => utils_1.fsStatPr(path.join(dirPath, fileName))));
|
55 | if (dirUrl !== '/') {
|
56 | const dirStat = yield utils_1.fsStatPr(dirPath);
|
57 | files.unshift('..');
|
58 | fileStats.unshift(dirStat);
|
59 | }
|
60 | const fileHtml = files
|
61 | .map((fileName, index) => {
|
62 | const isDirectory = fileStats[index].isDirectory();
|
63 | return (`<span class="denote">${isDirectory ? 'd' : '-'}</span> <a class="${isDirectory ? 'directory' : 'file'}" href="${url.resolve(dirUrl, fileName)}">${fileName}</a>`);
|
64 | })
|
65 | .join('<br/>\n');
|
66 | const templateHtml = templateSrc.toString()
|
67 | .replace('{directory}', dirPath)
|
68 | .replace('{files}', fileHtml)
|
69 | .replace('{linked-path}', dirUrl.replace(/\//g, ' / '));
|
70 | res.writeHead(200, {
|
71 | 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0',
|
72 | 'Expires': '0',
|
73 | 'Content-Type': 'text/html'
|
74 | });
|
75 | res.end(templateHtml);
|
76 | });
|
77 | };
|
78 | }
|
79 | exports.serveDirContents = serveDirContents;
|
80 | function sendFile(contentType, filePath, req, res) {
|
81 | return __awaiter(this, void 0, void 0, function* () {
|
82 | const stat = yield utils_1.fsStatPr(filePath);
|
83 | if (!stat.isFile()) {
|
84 | return sendError(404, res, { error: 'File not found' });
|
85 | }
|
86 | res.writeHead(200, {
|
87 | 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0',
|
88 | 'Expires': '0',
|
89 | 'Content-Type': contentType,
|
90 | 'Content-Length': stat.size
|
91 | });
|
92 | fs.createReadStream(filePath)
|
93 | .pipe(res);
|
94 | });
|
95 | }
|
96 | exports.sendFile = sendFile;
|
97 | function sendError(httpStatus, res, content = {}) {
|
98 | res.writeHead(httpStatus, {
|
99 | 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0',
|
100 | 'Expires': '0',
|
101 | 'Content-Type': 'text/plain'
|
102 | });
|
103 | res.write(JSON.stringify(content, null, 2));
|
104 | res.end();
|
105 | }
|
106 | exports.sendError = sendError;
|