UNPKG

2.73 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.serverVersion = exports.serverVersion_PATH = void 0;
4var crypto = require("crypto");
5var css2json = require("css2json");
6var debug_ = require("debug");
7var fs = require("fs");
8var jsonMarkup = require("json-markup");
9var path = require("path");
10var request_ext_1 = require("./request-ext");
11var debug = debug_("r2:streamer#http/server-version");
12var jsonStyle = "\n.json-markup {\n line-height: 17px;\n font-size: 13px;\n font-family: monospace;\n white-space: pre;\n}\n.json-markup-key {\n font-weight: bold;\n}\n.json-markup-bool {\n color: firebrick;\n}\n.json-markup-string {\n color: green;\n}\n.json-markup-null {\n color: gray;\n}\n.json-markup-number {\n color: blue;\n}\n";
13exports.serverVersion_PATH = "/version";
14function serverVersion(server, topRouter) {
15 topRouter.get([exports.serverVersion_PATH, exports.serverVersion_PATH + "/" + request_ext_1._show + "/:" + request_ext_1._jsonPath + "?"], function (req, res) {
16 var reqparams = req.params;
17 var isShow = req.url.indexOf("/show") >= 0 || req.query.show;
18 if (!reqparams.jsonPath && req.query.show) {
19 reqparams.jsonPath = req.query.show;
20 }
21 var gitRevJson = "../../../gitrev.json";
22 if (!fs.existsSync(path.resolve(path.join(__dirname, gitRevJson)))) {
23 var err = "Missing Git rev JSON! ";
24 debug(err + gitRevJson);
25 res.status(500).send("<html><body><p>Internal Server Error</p><p>"
26 + err + "</p></body></html>");
27 return;
28 }
29 var jsonObj = require(gitRevJson);
30 if (isShow) {
31 var jsonPretty = jsonMarkup(jsonObj, css2json(jsonStyle));
32 res.status(200).send("<html><body>" +
33 "<h1>R2-STREAMER-JS VERSION INFO</h1>" +
34 "<hr><p><pre>" + jsonPretty + "</pre></p>" +
35 "</body></html>");
36 }
37 else {
38 server.setResponseCORS(res);
39 res.set("Content-Type", "application/json; charset=utf-8");
40 var jsonStr = JSON.stringify(jsonObj, null, " ");
41 var checkSum = crypto.createHash("sha256");
42 checkSum.update(jsonStr);
43 var hash = checkSum.digest("hex");
44 var match = req.header("If-None-Match");
45 if (match === hash) {
46 debug("publications.json cache");
47 res.status(304);
48 res.end();
49 return;
50 }
51 res.setHeader("ETag", hash);
52 res.status(200).send(jsonStr);
53 }
54 });
55}
56exports.serverVersion = serverVersion;
57//# sourceMappingURL=server-version.js.map
\No newline at end of file