UNPKG

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