1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.serverLCPLSD_show = exports.serverLCPLSD_show_PATH = void 0;
|
4 | var tslib_1 = require("tslib");
|
5 | var css2json = require("css2json");
|
6 | var debug_ = require("debug");
|
7 | var DotProp = require("dot-prop");
|
8 | var express = require("express");
|
9 | var jsonMarkup = require("json-markup");
|
10 | var morgan = require("morgan");
|
11 | var path = require("path");
|
12 | var request = require("request");
|
13 | var requestPromise = require("request-promise-native");
|
14 | var lcp_1 = require("r2-lcp-js/dist/es5/src/parser/epub/lcp");
|
15 | var lsd_1 = require("r2-lcp-js/dist/es5/src/parser/epub/lsd");
|
16 | var serializable_1 = require("r2-lcp-js/dist/es5/src/serializable");
|
17 | var UrlUtils_1 = require("r2-utils-js/dist/es5/src/_utils/http/UrlUtils");
|
18 | var JsonUtils_1 = require("r2-utils-js/dist/es5/src/_utils/JsonUtils");
|
19 | var BufferUtils_1 = require("r2-utils-js/dist/es5/src/_utils/stream/BufferUtils");
|
20 | var json_schema_validate_1 = require("../utils/json-schema-validate");
|
21 | var request_ext_1 = require("./request-ext");
|
22 | var server_trailing_slash_redirect_1 = require("./server-trailing-slash-redirect");
|
23 | var server_url_1 = require("./server-url");
|
24 | var debug = debug_("r2:streamer#http/lcp-lsd-show");
|
25 | exports.serverLCPLSD_show_PATH = "/lcp-lsd-show";
|
26 | function serverLCPLSD_show(_server, topRouter) {
|
27 | var _this = this;
|
28 | var 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";
|
29 | var routerLCPLSD_show = express.Router({ strict: false });
|
30 | routerLCPLSD_show.use(morgan("combined", { stream: { write: function (msg) { return debug(msg); } } }));
|
31 | routerLCPLSD_show.use(server_trailing_slash_redirect_1.trailingSlashRedirect);
|
32 | routerLCPLSD_show.get("/", function (_req, res) {
|
33 | var html = "<html><head>";
|
34 | html += "<script type=\"text/javascript\">function encodeURIComponent_RFC3986(str) { " +
|
35 | "return encodeURIComponent(str).replace(/[!'()*]/g, (c) => { " +
|
36 | "return \"%\" + c.charCodeAt(0).toString(16); }); }" +
|
37 | "function go(evt) {" +
|
38 | "if (evt) { evt.preventDefault(); } var url = " +
|
39 | "location.origin +" +
|
40 | (" '" + exports.serverLCPLSD_show_PATH + "/' +") +
|
41 | " encodeURIComponent_RFC3986(document.getElementById(\"url\").value);" +
|
42 | "location.href = url;}</script>";
|
43 | html += "</head>";
|
44 | html += "<body><h1>LCP / LSD examiner</h1>";
|
45 | html += "<form onsubmit=\"go();return false;\">" +
|
46 | "<input type=\"text\" name=\"url\" id=\"url\" size=\"80\">" +
|
47 | "<input type=\"submit\" value=\"Go!\"></form>";
|
48 | html += "</body></html>";
|
49 | res.status(200).send(html);
|
50 | });
|
51 | routerLCPLSD_show.param("urlEncoded", function (req, _res, next, value, _name) {
|
52 | req.urlEncoded = value;
|
53 | next();
|
54 | });
|
55 | routerLCPLSD_show.get("/:" + request_ext_1._urlEncoded + "(*)", function (req, res) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
56 | var reqparams, urlDecoded, isSecureHttp, rootUrl, failure, success, headers, needsStreamingResponse, response, err_1;
|
57 | var _this = this;
|
58 | return tslib_1.__generator(this, function (_a) {
|
59 | switch (_a.label) {
|
60 | case 0:
|
61 | reqparams = req.params;
|
62 | if (!reqparams.urlEncoded) {
|
63 | reqparams.urlEncoded = req.urlEncoded;
|
64 | }
|
65 | urlDecoded = reqparams.urlEncoded;
|
66 | debug(urlDecoded);
|
67 | isSecureHttp = req.secure ||
|
68 | req.protocol === "https" ||
|
69 | req.get("X-Forwarded-Proto") === "https";
|
70 | rootUrl = (isSecureHttp ? "https://" : "http://")
|
71 | + req.headers.host;
|
72 | failure = function (err) {
|
73 | debug(err);
|
74 | res.status(500).send("<html><body><p>Internal Server Error</p><p>"
|
75 | + err + "</p></body></html>");
|
76 | };
|
77 | success = function (response) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
78 | var isBadStatusCode, responseData, err_2, responseStr, responseJson, isStatusDoc, lcpOrLsd, lcpOrLsdJson, validationStr, doValidate, jsonSchemasRootpath, jsonSchemasNames, validationErrors, _i, validationErrors_1, err, val, valueStr, funk, css, jsonPretty;
|
79 | return tslib_1.__generator(this, function (_a) {
|
80 | switch (_a.label) {
|
81 | case 0:
|
82 | isBadStatusCode = response.statusCode && (response.statusCode < 200 || response.statusCode >= 300);
|
83 | if (isBadStatusCode) {
|
84 | failure("HTTP CODE " + response.statusCode);
|
85 | return [2];
|
86 | }
|
87 | _a.label = 1;
|
88 | case 1:
|
89 | _a.trys.push([1, 3, , 4]);
|
90 | return [4, BufferUtils_1.streamToBufferPromise(response)];
|
91 | case 2:
|
92 | responseData = _a.sent();
|
93 | return [3, 4];
|
94 | case 3:
|
95 | err_2 = _a.sent();
|
96 | debug(err_2);
|
97 | res.status(500).send("<html><body><p>Internal Server Error</p><p>" + err_2 + "</p></body></html>");
|
98 | return [2];
|
99 | case 4:
|
100 | responseStr = responseData.toString("utf8");
|
101 | responseJson = JSON.parse(responseStr);
|
102 | isStatusDoc = responseJson.id &&
|
103 | responseJson.status &&
|
104 | responseJson.updated &&
|
105 | responseJson.links;
|
106 | lcpOrLsd = isStatusDoc ?
|
107 | serializable_1.TaJsonDeserialize(responseJson, lsd_1.LSD) :
|
108 | serializable_1.TaJsonDeserialize(responseJson, lcp_1.LCP);
|
109 | lcpOrLsdJson = serializable_1.TaJsonSerialize(lcpOrLsd);
|
110 | doValidate = !reqparams.jsonPath || reqparams.jsonPath === "all";
|
111 | if (doValidate) {
|
112 | jsonSchemasRootpath = path.join(process.cwd(), "misc", "json-schema");
|
113 | jsonSchemasNames = [
|
114 | isStatusDoc ? "lcp/status" : "lcp/license",
|
115 | "lcp/link",
|
116 | ];
|
117 | validationErrors = json_schema_validate_1.jsonSchemaValidate(jsonSchemasRootpath, jsonSchemasNames, lcpOrLsdJson);
|
118 | if (validationErrors) {
|
119 | validationStr = "";
|
120 | for (_i = 0, validationErrors_1 = validationErrors; _i < validationErrors_1.length; _i++) {
|
121 | err = validationErrors_1[_i];
|
122 | debug("JSON Schema validation FAIL.");
|
123 | debug(err);
|
124 | val = DotProp.get(lcpOrLsdJson, err.jsonPath);
|
125 | valueStr = (typeof val === "string") ?
|
126 | "" + val :
|
127 | ((val instanceof Array || typeof val === "object") ?
|
128 | "" + JSON.stringify(val) :
|
129 | "");
|
130 | debug(valueStr);
|
131 | validationStr +=
|
132 | "\n" + err.ajvMessage + ": " + valueStr + "\n\n'" + err.ajvDataPath.replace(/^\./, "") + "' (" + err.ajvSchemaPath + ")\n\n";
|
133 | }
|
134 | }
|
135 | }
|
136 | funk = function (obj) {
|
137 | if ((obj.href && typeof obj.href === "string") ||
|
138 | (obj.Href && typeof obj.Href === "string")) {
|
139 | var fullHref = obj.href ? obj.href : obj.Href;
|
140 | var isDataUrl = /^data:/.test(fullHref);
|
141 | var isMailUrl = /^mailto:/.test(fullHref);
|
142 | var notFull = !isDataUrl && !isMailUrl && !UrlUtils_1.isHTTP(fullHref);
|
143 | if (notFull) {
|
144 | fullHref = UrlUtils_1.ensureAbsolute(urlDecoded, fullHref);
|
145 | }
|
146 | if ((obj.type === "application/vnd.readium.license.status.v1.0+json" && obj.rel === "status") ||
|
147 | (obj.type === "application/vnd.readium.lcp.license.v1.0+json" && obj.rel === "license")) {
|
148 | obj.__href__ = rootUrl + req.originalUrl.substr(0, req.originalUrl.indexOf(exports.serverLCPLSD_show_PATH + "/")) +
|
149 | exports.serverLCPLSD_show_PATH + "/" + UrlUtils_1.encodeURIComponent_RFC3986(fullHref);
|
150 | }
|
151 | else if (obj.type === "application/epub+zip" && obj.rel === "publication") {
|
152 | obj.__href__ = rootUrl + req.originalUrl.substr(0, req.originalUrl.indexOf(exports.serverLCPLSD_show_PATH + "/")) +
|
153 | server_url_1.serverRemotePub_PATH + "/" + UrlUtils_1.encodeURIComponent_RFC3986(fullHref);
|
154 | }
|
155 | else if (isDataUrl) {
|
156 | }
|
157 | else if (notFull && !isMailUrl) {
|
158 | obj.__href__ = fullHref;
|
159 | }
|
160 | }
|
161 | };
|
162 | JsonUtils_1.traverseJsonObjects(lcpOrLsdJson, funk);
|
163 | css = css2json(jsonStyle);
|
164 | jsonPretty = jsonMarkup(lcpOrLsdJson, css);
|
165 | res.status(200).send("<html><body>" +
|
166 | "<h1>" +
|
167 | (isStatusDoc ? "LSD" : "LCP") +
|
168 | " JSON" + "</h1>" +
|
169 | "<h2><a href=\"" + urlDecoded + "\">" + urlDecoded + "</a></h2>" +
|
170 | "<hr>" +
|
171 | "<div style=\"overflow-x: auto;margin:0;padding:0;width:100%;height:auto;\">" +
|
172 | jsonPretty + "</div>" +
|
173 | (doValidate ? (validationStr ? ("<hr><p><pre>" + validationStr + "</pre></p>") : ("<hr><p>JSON SCHEMA OK.</p>")) : "") +
|
174 | "</body></html>");
|
175 | return [2];
|
176 | }
|
177 | });
|
178 | }); };
|
179 | headers = {
|
180 | "Accept": "application/json,application/xml",
|
181 | "Accept-Language": "en-UK,en-US;q=0.7,en;q=0.5",
|
182 | "User-Agent": "READIUM2",
|
183 | };
|
184 | needsStreamingResponse = true;
|
185 | if (!needsStreamingResponse) return [3, 1];
|
186 | request.get({
|
187 | headers: headers,
|
188 | method: "GET",
|
189 | uri: urlDecoded,
|
190 | })
|
191 | .on("response", success)
|
192 | .on("error", failure);
|
193 | return [3, 7];
|
194 | case 1:
|
195 | response = void 0;
|
196 | _a.label = 2;
|
197 | case 2:
|
198 | _a.trys.push([2, 4, , 5]);
|
199 | return [4, requestPromise({
|
200 | headers: headers,
|
201 | method: "GET",
|
202 | resolveWithFullResponse: true,
|
203 | uri: urlDecoded,
|
204 | })];
|
205 | case 3:
|
206 | response = _a.sent();
|
207 | return [3, 5];
|
208 | case 4:
|
209 | err_1 = _a.sent();
|
210 | failure(err_1);
|
211 | return [2];
|
212 | case 5: return [4, success(response)];
|
213 | case 6:
|
214 | _a.sent();
|
215 | _a.label = 7;
|
216 | case 7: return [2];
|
217 | }
|
218 | });
|
219 | }); });
|
220 | topRouter.use(exports.serverLCPLSD_show_PATH, routerLCPLSD_show);
|
221 | }
|
222 | exports.serverLCPLSD_show = serverLCPLSD_show;
|
223 |
|
\ | No newline at end of file |