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