1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.launchStatusDocumentProcessing = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const debug_ = require("debug");
|
6 | const request = require("request");
|
7 | const requestPromise = require("request-promise-native");
|
8 | const BufferUtils_1 = require("r2-utils-js/dist/es7-es2016/src/_utils/stream/BufferUtils");
|
9 | const lsd_1 = require("../parser/epub/lsd");
|
10 | const serializable_1 = require("../serializable");
|
11 | const lcpl_update_1 = require("./lcpl-update");
|
12 | const register_1 = require("./register");
|
13 | const debug = debug_("r2:lcp#lsd/status-document-processing");
|
14 | const IS_DEV = (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "dev");
|
15 | function launchStatusDocumentProcessing(lcp, deviceIDManager, onStatusDocumentProcessingComplete, httpHeaders) {
|
16 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
17 | if (!lcp || !lcp.Links) {
|
18 | if (onStatusDocumentProcessingComplete) {
|
19 | onStatusDocumentProcessingComplete(undefined);
|
20 | }
|
21 | return;
|
22 | }
|
23 | const linkStatus = lcp.Links.find((link) => {
|
24 | return link.Rel === "status";
|
25 | });
|
26 | if (!linkStatus) {
|
27 | if (onStatusDocumentProcessingComplete) {
|
28 | onStatusDocumentProcessingComplete(undefined);
|
29 | }
|
30 | return;
|
31 | }
|
32 | if (IS_DEV) {
|
33 | debug(linkStatus);
|
34 | }
|
35 | const failure = (err) => {
|
36 | debug(err);
|
37 | if (onStatusDocumentProcessingComplete) {
|
38 | onStatusDocumentProcessingComplete(undefined);
|
39 | }
|
40 | };
|
41 | const success = (response) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
42 | if (IS_DEV) {
|
43 | Object.keys(response.headers).forEach((header) => {
|
44 | debug(header + " => " + response.headers[header]);
|
45 | });
|
46 | }
|
47 | if (response.statusCode && (response.statusCode < 200 || response.statusCode >= 300)) {
|
48 | let failBuff;
|
49 | try {
|
50 | failBuff = yield (0, BufferUtils_1.streamToBufferPromise)(response);
|
51 | }
|
52 | catch (buffErr) {
|
53 | if (IS_DEV) {
|
54 | debug(buffErr);
|
55 | }
|
56 | failure(response.statusCode);
|
57 | return;
|
58 | }
|
59 | try {
|
60 | const failStr = failBuff.toString("utf8");
|
61 | if (IS_DEV) {
|
62 | debug(failStr);
|
63 | }
|
64 | try {
|
65 | const failJson = global.JSON.parse(failStr);
|
66 | if (IS_DEV) {
|
67 | debug(failJson);
|
68 | }
|
69 | failJson.httpStatusCode = response.statusCode;
|
70 | failure(failJson);
|
71 | }
|
72 | catch (jsonErr) {
|
73 | if (IS_DEV) {
|
74 | debug(jsonErr);
|
75 | }
|
76 | failure({ httpStatusCode: response.statusCode, httpResponseBody: failStr });
|
77 | }
|
78 | }
|
79 | catch (strErr) {
|
80 | if (IS_DEV) {
|
81 | debug(strErr);
|
82 | }
|
83 | failure(response.statusCode);
|
84 | }
|
85 | return;
|
86 | }
|
87 | let responseData;
|
88 | try {
|
89 | responseData = yield (0, BufferUtils_1.streamToBufferPromise)(response);
|
90 | }
|
91 | catch (err) {
|
92 | debug(err);
|
93 | if (onStatusDocumentProcessingComplete) {
|
94 | onStatusDocumentProcessingComplete(undefined);
|
95 | }
|
96 | return;
|
97 | }
|
98 | const responseStr = responseData.toString("utf8");
|
99 | const mime = "application/vnd.readium.license.status.v1.0+json";
|
100 | if (IS_DEV) {
|
101 | if (response.headers["content-type"] === mime ||
|
102 | response.headers["content-type"] === "application/json") {
|
103 | debug(responseStr);
|
104 | }
|
105 | }
|
106 | const lsdJSON = global.JSON.parse(responseStr);
|
107 | if (IS_DEV) {
|
108 | debug(lsdJSON);
|
109 | }
|
110 | try {
|
111 | lcp.LSD = (0, serializable_1.TaJsonDeserialize)(lsdJSON, lsd_1.LSD);
|
112 | if (IS_DEV) {
|
113 | debug(lcp.LSD);
|
114 | }
|
115 | }
|
116 | catch (err) {
|
117 | debug(err);
|
118 | if (onStatusDocumentProcessingComplete) {
|
119 | onStatusDocumentProcessingComplete(undefined);
|
120 | }
|
121 | return;
|
122 | }
|
123 | let licenseUpdateResponseJson;
|
124 | try {
|
125 | licenseUpdateResponseJson = yield (0, lcpl_update_1.lsdLcpUpdate)(lcp, httpHeaders);
|
126 | }
|
127 | catch (err) {
|
128 | debug(err);
|
129 | }
|
130 | if (licenseUpdateResponseJson) {
|
131 | if (onStatusDocumentProcessingComplete) {
|
132 | onStatusDocumentProcessingComplete(licenseUpdateResponseJson);
|
133 | }
|
134 | return;
|
135 | }
|
136 | if (lcp.LSD.Status === lsd_1.StatusEnum.Revoked
|
137 | || lcp.LSD.Status === lsd_1.StatusEnum.Returned
|
138 | || lcp.LSD.Status === lsd_1.StatusEnum.Cancelled
|
139 | || lcp.LSD.Status === lsd_1.StatusEnum.Expired) {
|
140 | debug("What?! LSD status:" + lcp.LSD.Status);
|
141 | if (onStatusDocumentProcessingComplete) {
|
142 | onStatusDocumentProcessingComplete(undefined);
|
143 | }
|
144 | return;
|
145 | }
|
146 | let registerResponse;
|
147 | try {
|
148 | registerResponse = yield (0, register_1.lsdRegister_)(lcp.LSD, deviceIDManager, httpHeaders);
|
149 | }
|
150 | catch (err) {
|
151 | debug(err);
|
152 | }
|
153 | if (registerResponse) {
|
154 | lcp.LSD = registerResponse;
|
155 | if (IS_DEV) {
|
156 | debug(lcp.LSD);
|
157 | }
|
158 | }
|
159 | if (onStatusDocumentProcessingComplete) {
|
160 | onStatusDocumentProcessingComplete(undefined);
|
161 | }
|
162 | });
|
163 | const headers = Object.assign({
|
164 | "Accept": "application/json,application/xml",
|
165 | "Accept-Language": "en-UK,en-US;q=0.7,en;q=0.5",
|
166 | "User-Agent": "Readium2-LCP",
|
167 | }, httpHeaders ? httpHeaders : {});
|
168 | const needsStreamingResponse = true;
|
169 | if (needsStreamingResponse) {
|
170 | request.get({
|
171 | headers,
|
172 | method: "GET",
|
173 | timeout: 2000,
|
174 | uri: linkStatus.Href,
|
175 | })
|
176 | .on("response", (res) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
177 | try {
|
178 | yield success(res);
|
179 | }
|
180 | catch (successError) {
|
181 | failure(successError);
|
182 | return;
|
183 | }
|
184 | }))
|
185 | .on("error", failure);
|
186 | }
|
187 | else {
|
188 | let response;
|
189 | try {
|
190 | response = yield requestPromise({
|
191 | headers,
|
192 | method: "GET",
|
193 | resolveWithFullResponse: true,
|
194 | uri: linkStatus.Href,
|
195 | });
|
196 | }
|
197 | catch (err) {
|
198 | failure(err);
|
199 | return;
|
200 | }
|
201 | yield success(response);
|
202 | }
|
203 | });
|
204 | }
|
205 | exports.launchStatusDocumentProcessing = launchStatusDocumentProcessing;
|
206 |
|
\ | No newline at end of file |