UNPKG

7.97 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.serverMediaOverlays = void 0;
4var tslib_1 = require("tslib");
5var crypto = require("crypto");
6var css2json = require("css2json");
7var debug_ = require("debug");
8var express = require("express");
9var jsonMarkup = require("json-markup");
10var path = require("path");
11var serializable_1 = require("r2-lcp-js/dist/es5/src/serializable");
12var epub_1 = require("r2-shared-js/dist/es5/src/parser/epub");
13var UrlUtils_1 = require("r2-utils-js/dist/es5/src/_utils/http/UrlUtils");
14var JsonUtils_1 = require("r2-utils-js/dist/es5/src/_utils/JsonUtils");
15var request_ext_1 = require("./request-ext");
16var debug = debug_("r2:streamer#http/server-mediaoverlays");
17function serverMediaOverlays(server, routerPathBase64) {
18 var _this = this;
19 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";
20 var routerMediaOverlays = express.Router({ strict: false });
21 routerMediaOverlays.get(["/", "/" + request_ext_1._show + "/:" + epub_1.mediaOverlayURLParam + "?"], function (req, res) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
22 function absoluteURL(href) {
23 return rootUrl + "/" + href;
24 }
25 function absolutizeURLs(jsonObject) {
26 JsonUtils_1.traverseJsonObjects(jsonObject, function (obj) {
27 if (obj.text && typeof obj.text === "string"
28 && !UrlUtils_1.isHTTP(obj.text)) {
29 obj.text = absoluteURL(obj.text);
30 }
31 if (obj.audio && typeof obj.audio === "string"
32 && !UrlUtils_1.isHTTP(obj.audio)) {
33 obj.audio = absoluteURL(obj.audio);
34 }
35 });
36 }
37 var reqparams, isShow, isHead, isCanonical, isSecureHttp, pathBase64Str, publication, err_1, rootUrl, objToSerialize, resource, err_2, err_3, jsonObj, jsonPretty, jsonStr, checkSum, hash, match;
38 return tslib_1.__generator(this, function (_a) {
39 switch (_a.label) {
40 case 0:
41 reqparams = req.params;
42 if (!reqparams.pathBase64) {
43 reqparams.pathBase64 = req.pathBase64;
44 }
45 if (!reqparams.lcpPass64) {
46 reqparams.lcpPass64 = req.lcpPass64;
47 }
48 isShow = req.url.indexOf("/show") >= 0 || req.query.show;
49 isHead = req.method.toLowerCase() === "head";
50 if (isHead) {
51 debug("HEAD !!!!!!!!!!!!!!!!!!!");
52 }
53 isCanonical = req.query.canonical &&
54 req.query.canonical === "true";
55 isSecureHttp = req.secure ||
56 req.protocol === "https" ||
57 req.get("X-Forwarded-Proto") === "https";
58 pathBase64Str = Buffer.from(reqparams.pathBase64, "base64").toString("utf8");
59 _a.label = 1;
60 case 1:
61 _a.trys.push([1, 3, , 4]);
62 return [4, server.loadOrGetCachedPublication(pathBase64Str)];
63 case 2:
64 publication = _a.sent();
65 return [3, 4];
66 case 3:
67 err_1 = _a.sent();
68 debug(err_1);
69 res.status(500).send("<html><body><p>Internal Server Error</p><p>"
70 + err_1 + "</p></body></html>");
71 return [2];
72 case 4:
73 rootUrl = (isSecureHttp ? "https://" : "http://")
74 + req.headers.host + "/pub/"
75 + (reqparams.lcpPass64 ?
76 (server.lcpBeginToken + UrlUtils_1.encodeURIComponent_RFC3986(reqparams.lcpPass64) + server.lcpEndToken) :
77 "")
78 + UrlUtils_1.encodeURIComponent_RFC3986(reqparams.pathBase64);
79 objToSerialize = null;
80 resource = isShow ?
81 (req.query.show ?
82 req.query.show :
83 reqparams[epub_1.mediaOverlayURLParam]) :
84 req.query[epub_1.mediaOverlayURLParam];
85 if (!(resource && resource !== "all")) return [3, 9];
86 _a.label = 5;
87 case 5:
88 _a.trys.push([5, 7, , 8]);
89 return [4, epub_1.getMediaOverlay(publication, resource)];
90 case 6:
91 objToSerialize = _a.sent();
92 return [3, 8];
93 case 7:
94 err_2 = _a.sent();
95 debug(err_2);
96 res.status(500).send("<html><body><p>Internal Server Error</p><p>"
97 + err_2 + "</p></body></html>");
98 return [2];
99 case 8: return [3, 12];
100 case 9:
101 _a.trys.push([9, 11, , 12]);
102 return [4, epub_1.getAllMediaOverlays(publication)];
103 case 10:
104 objToSerialize = _a.sent();
105 return [3, 12];
106 case 11:
107 err_3 = _a.sent();
108 debug(err_3);
109 res.status(500).send("<html><body><p>Internal Server Error</p><p>"
110 + err_3 + "</p></body></html>");
111 return [2];
112 case 12:
113 if (!objToSerialize) {
114 objToSerialize = [];
115 }
116 jsonObj = serializable_1.TaJsonSerialize(objToSerialize);
117 if (isShow) {
118 absolutizeURLs(jsonObj);
119 jsonPretty = jsonMarkup(jsonObj, css2json(jsonStyle));
120 res.status(200).send("<html><body>" +
121 "<h1>" + path.basename(pathBase64Str) + "</h1>" +
122 "<p><pre>" + jsonPretty + "</pre></p>" +
123 "</body></html>");
124 }
125 else {
126 server.setResponseCORS(res);
127 res.set("Content-Type", "application/vnd.syncnarr+json; charset=utf-8");
128 jsonStr = isCanonical ?
129 global.JSON.stringify(JsonUtils_1.sortObject(jsonObj), null, "") :
130 global.JSON.stringify(jsonObj, null, " ");
131 checkSum = crypto.createHash("sha256");
132 checkSum.update(jsonStr);
133 hash = checkSum.digest("hex");
134 match = req.header("If-None-Match");
135 if (match === hash) {
136 debug("smil cache");
137 res.status(304);
138 res.end();
139 return [2];
140 }
141 res.setHeader("ETag", hash);
142 res.status(200);
143 if (isHead) {
144 res.end();
145 }
146 else {
147 res.send(jsonStr);
148 }
149 }
150 return [2];
151 }
152 });
153 }); });
154 routerPathBase64.use("/:" + request_ext_1._pathBase64 + "/" + epub_1.mediaOverlayURLPath, routerMediaOverlays);
155}
156exports.serverMediaOverlays = serverMediaOverlays;
157//# sourceMappingURL=server-mediaoverlays.js.map
\No newline at end of file