2
0

chunked_stream.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.ChunkedStreamManager = exports.ChunkedStream = void 0;
  27. var _util = require("../shared/util");
  28. var _core_utils = require("./core_utils");
  29. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  30. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  31. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  32. var ChunkedStream =
  33. /*#__PURE__*/
  34. function () {
  35. function ChunkedStream(length, chunkSize, manager) {
  36. _classCallCheck(this, ChunkedStream);
  37. this.bytes = new Uint8Array(length);
  38. this.start = 0;
  39. this.pos = 0;
  40. this.end = length;
  41. this.chunkSize = chunkSize;
  42. this.loadedChunks = [];
  43. this.numChunksLoaded = 0;
  44. this.numChunks = Math.ceil(length / chunkSize);
  45. this.manager = manager;
  46. this.progressiveDataLength = 0;
  47. this.lastSuccessfulEnsureByteChunk = -1;
  48. }
  49. _createClass(ChunkedStream, [{
  50. key: "getMissingChunks",
  51. value: function getMissingChunks() {
  52. var chunks = [];
  53. for (var chunk = 0, n = this.numChunks; chunk < n; ++chunk) {
  54. if (!this.loadedChunks[chunk]) {
  55. chunks.push(chunk);
  56. }
  57. }
  58. return chunks;
  59. }
  60. }, {
  61. key: "getBaseStreams",
  62. value: function getBaseStreams() {
  63. return [this];
  64. }
  65. }, {
  66. key: "allChunksLoaded",
  67. value: function allChunksLoaded() {
  68. return this.numChunksLoaded === this.numChunks;
  69. }
  70. }, {
  71. key: "onReceiveData",
  72. value: function onReceiveData(begin, chunk) {
  73. var chunkSize = this.chunkSize;
  74. if (begin % chunkSize !== 0) {
  75. throw new Error("Bad begin offset: ".concat(begin));
  76. }
  77. var end = begin + chunk.byteLength;
  78. if (end % chunkSize !== 0 && end !== this.bytes.length) {
  79. throw new Error("Bad end offset: ".concat(end));
  80. }
  81. this.bytes.set(new Uint8Array(chunk), begin);
  82. var beginChunk = Math.floor(begin / chunkSize);
  83. var endChunk = Math.floor((end - 1) / chunkSize) + 1;
  84. for (var curChunk = beginChunk; curChunk < endChunk; ++curChunk) {
  85. if (!this.loadedChunks[curChunk]) {
  86. this.loadedChunks[curChunk] = true;
  87. ++this.numChunksLoaded;
  88. }
  89. }
  90. }
  91. }, {
  92. key: "onReceiveProgressiveData",
  93. value: function onReceiveProgressiveData(data) {
  94. var position = this.progressiveDataLength;
  95. var beginChunk = Math.floor(position / this.chunkSize);
  96. this.bytes.set(new Uint8Array(data), position);
  97. position += data.byteLength;
  98. this.progressiveDataLength = position;
  99. var endChunk = position >= this.end ? this.numChunks : Math.floor(position / this.chunkSize);
  100. for (var curChunk = beginChunk; curChunk < endChunk; ++curChunk) {
  101. if (!this.loadedChunks[curChunk]) {
  102. this.loadedChunks[curChunk] = true;
  103. ++this.numChunksLoaded;
  104. }
  105. }
  106. }
  107. }, {
  108. key: "ensureByte",
  109. value: function ensureByte(pos) {
  110. if (pos < this.progressiveDataLength) {
  111. return;
  112. }
  113. var chunk = Math.floor(pos / this.chunkSize);
  114. if (chunk === this.lastSuccessfulEnsureByteChunk) {
  115. return;
  116. }
  117. if (!this.loadedChunks[chunk]) {
  118. throw new _core_utils.MissingDataException(pos, pos + 1);
  119. }
  120. this.lastSuccessfulEnsureByteChunk = chunk;
  121. }
  122. }, {
  123. key: "ensureRange",
  124. value: function ensureRange(begin, end) {
  125. if (begin >= end) {
  126. return;
  127. }
  128. if (end <= this.progressiveDataLength) {
  129. return;
  130. }
  131. var chunkSize = this.chunkSize;
  132. var beginChunk = Math.floor(begin / chunkSize);
  133. var endChunk = Math.floor((end - 1) / chunkSize) + 1;
  134. for (var chunk = beginChunk; chunk < endChunk; ++chunk) {
  135. if (!this.loadedChunks[chunk]) {
  136. throw new _core_utils.MissingDataException(begin, end);
  137. }
  138. }
  139. }
  140. }, {
  141. key: "nextEmptyChunk",
  142. value: function nextEmptyChunk(beginChunk) {
  143. var numChunks = this.numChunks;
  144. for (var i = 0; i < numChunks; ++i) {
  145. var chunk = (beginChunk + i) % numChunks;
  146. if (!this.loadedChunks[chunk]) {
  147. return chunk;
  148. }
  149. }
  150. return null;
  151. }
  152. }, {
  153. key: "hasChunk",
  154. value: function hasChunk(chunk) {
  155. return !!this.loadedChunks[chunk];
  156. }
  157. }, {
  158. key: "getByte",
  159. value: function getByte() {
  160. var pos = this.pos;
  161. if (pos >= this.end) {
  162. return -1;
  163. }
  164. this.ensureByte(pos);
  165. return this.bytes[this.pos++];
  166. }
  167. }, {
  168. key: "getUint16",
  169. value: function getUint16() {
  170. var b0 = this.getByte();
  171. var b1 = this.getByte();
  172. if (b0 === -1 || b1 === -1) {
  173. return -1;
  174. }
  175. return (b0 << 8) + b1;
  176. }
  177. }, {
  178. key: "getInt32",
  179. value: function getInt32() {
  180. var b0 = this.getByte();
  181. var b1 = this.getByte();
  182. var b2 = this.getByte();
  183. var b3 = this.getByte();
  184. return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
  185. }
  186. }, {
  187. key: "getBytes",
  188. value: function getBytes(length) {
  189. var forceClamped = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  190. var bytes = this.bytes;
  191. var pos = this.pos;
  192. var strEnd = this.end;
  193. if (!length) {
  194. this.ensureRange(pos, strEnd);
  195. var _subarray = bytes.subarray(pos, strEnd);
  196. return forceClamped ? new Uint8ClampedArray(_subarray) : _subarray;
  197. }
  198. var end = pos + length;
  199. if (end > strEnd) {
  200. end = strEnd;
  201. }
  202. this.ensureRange(pos, end);
  203. this.pos = end;
  204. var subarray = bytes.subarray(pos, end);
  205. return forceClamped ? new Uint8ClampedArray(subarray) : subarray;
  206. }
  207. }, {
  208. key: "peekByte",
  209. value: function peekByte() {
  210. var peekedByte = this.getByte();
  211. this.pos--;
  212. return peekedByte;
  213. }
  214. }, {
  215. key: "peekBytes",
  216. value: function peekBytes(length) {
  217. var forceClamped = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  218. var bytes = this.getBytes(length, forceClamped);
  219. this.pos -= bytes.length;
  220. return bytes;
  221. }
  222. }, {
  223. key: "getByteRange",
  224. value: function getByteRange(begin, end) {
  225. this.ensureRange(begin, end);
  226. return this.bytes.subarray(begin, end);
  227. }
  228. }, {
  229. key: "skip",
  230. value: function skip(n) {
  231. if (!n) {
  232. n = 1;
  233. }
  234. this.pos += n;
  235. }
  236. }, {
  237. key: "reset",
  238. value: function reset() {
  239. this.pos = this.start;
  240. }
  241. }, {
  242. key: "moveStart",
  243. value: function moveStart() {
  244. this.start = this.pos;
  245. }
  246. }, {
  247. key: "makeSubStream",
  248. value: function makeSubStream(start, length, dict) {
  249. if (length) {
  250. this.ensureRange(start, start + length);
  251. } else {
  252. this.ensureByte(start);
  253. }
  254. function ChunkedStreamSubstream() {}
  255. ChunkedStreamSubstream.prototype = Object.create(this);
  256. ChunkedStreamSubstream.prototype.getMissingChunks = function () {
  257. var chunkSize = this.chunkSize;
  258. var beginChunk = Math.floor(this.start / chunkSize);
  259. var endChunk = Math.floor((this.end - 1) / chunkSize) + 1;
  260. var missingChunks = [];
  261. for (var chunk = beginChunk; chunk < endChunk; ++chunk) {
  262. if (!this.loadedChunks[chunk]) {
  263. missingChunks.push(chunk);
  264. }
  265. }
  266. return missingChunks;
  267. };
  268. var subStream = new ChunkedStreamSubstream();
  269. subStream.pos = subStream.start = start;
  270. subStream.end = start + length || this.end;
  271. subStream.dict = dict;
  272. return subStream;
  273. }
  274. }, {
  275. key: "length",
  276. get: function get() {
  277. return this.end - this.start;
  278. }
  279. }, {
  280. key: "isEmpty",
  281. get: function get() {
  282. return this.length === 0;
  283. }
  284. }]);
  285. return ChunkedStream;
  286. }();
  287. exports.ChunkedStream = ChunkedStream;
  288. var ChunkedStreamManager =
  289. /*#__PURE__*/
  290. function () {
  291. function ChunkedStreamManager(pdfNetworkStream, args) {
  292. _classCallCheck(this, ChunkedStreamManager);
  293. this.length = args.length;
  294. this.chunkSize = args.rangeChunkSize;
  295. this.stream = new ChunkedStream(this.length, this.chunkSize, this);
  296. this.pdfNetworkStream = pdfNetworkStream;
  297. this.disableAutoFetch = args.disableAutoFetch;
  298. this.msgHandler = args.msgHandler;
  299. this.currRequestId = 0;
  300. this.chunksNeededByRequest = Object.create(null);
  301. this.requestsByChunk = Object.create(null);
  302. this.promisesByRequest = Object.create(null);
  303. this.progressiveDataLength = 0;
  304. this.aborted = false;
  305. this._loadedStreamCapability = (0, _util.createPromiseCapability)();
  306. }
  307. _createClass(ChunkedStreamManager, [{
  308. key: "onLoadedStream",
  309. value: function onLoadedStream() {
  310. return this._loadedStreamCapability.promise;
  311. }
  312. }, {
  313. key: "sendRequest",
  314. value: function sendRequest(begin, end) {
  315. var _this = this;
  316. var rangeReader = this.pdfNetworkStream.getRangeReader(begin, end);
  317. if (!rangeReader.isStreamingSupported) {
  318. rangeReader.onProgress = this.onProgress.bind(this);
  319. }
  320. var chunks = [],
  321. loaded = 0;
  322. var promise = new Promise(function (resolve, reject) {
  323. var readChunk = function readChunk(chunk) {
  324. try {
  325. if (!chunk.done) {
  326. var data = chunk.value;
  327. chunks.push(data);
  328. loaded += (0, _util.arrayByteLength)(data);
  329. if (rangeReader.isStreamingSupported) {
  330. _this.onProgress({
  331. loaded: loaded
  332. });
  333. }
  334. rangeReader.read().then(readChunk, reject);
  335. return;
  336. }
  337. var chunkData = (0, _util.arraysToBytes)(chunks);
  338. chunks = null;
  339. resolve(chunkData);
  340. } catch (e) {
  341. reject(e);
  342. }
  343. };
  344. rangeReader.read().then(readChunk, reject);
  345. });
  346. promise.then(function (data) {
  347. if (_this.aborted) {
  348. return;
  349. }
  350. _this.onReceiveData({
  351. chunk: data,
  352. begin: begin
  353. });
  354. });
  355. }
  356. }, {
  357. key: "requestAllChunks",
  358. value: function requestAllChunks() {
  359. var missingChunks = this.stream.getMissingChunks();
  360. this._requestChunks(missingChunks);
  361. return this._loadedStreamCapability.promise;
  362. }
  363. }, {
  364. key: "_requestChunks",
  365. value: function _requestChunks(chunks) {
  366. var requestId = this.currRequestId++;
  367. var chunksNeeded = Object.create(null);
  368. this.chunksNeededByRequest[requestId] = chunksNeeded;
  369. var _iteratorNormalCompletion = true;
  370. var _didIteratorError = false;
  371. var _iteratorError = undefined;
  372. try {
  373. for (var _iterator = chunks[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  374. var _chunk = _step.value;
  375. if (!this.stream.hasChunk(_chunk)) {
  376. chunksNeeded[_chunk] = true;
  377. }
  378. }
  379. } catch (err) {
  380. _didIteratorError = true;
  381. _iteratorError = err;
  382. } finally {
  383. try {
  384. if (!_iteratorNormalCompletion && _iterator["return"] != null) {
  385. _iterator["return"]();
  386. }
  387. } finally {
  388. if (_didIteratorError) {
  389. throw _iteratorError;
  390. }
  391. }
  392. }
  393. if ((0, _util.isEmptyObj)(chunksNeeded)) {
  394. return Promise.resolve();
  395. }
  396. var capability = (0, _util.createPromiseCapability)();
  397. this.promisesByRequest[requestId] = capability;
  398. var chunksToRequest = [];
  399. for (var chunk in chunksNeeded) {
  400. chunk = chunk | 0;
  401. if (!(chunk in this.requestsByChunk)) {
  402. this.requestsByChunk[chunk] = [];
  403. chunksToRequest.push(chunk);
  404. }
  405. this.requestsByChunk[chunk].push(requestId);
  406. }
  407. if (!chunksToRequest.length) {
  408. return capability.promise;
  409. }
  410. var groupedChunksToRequest = this.groupChunks(chunksToRequest);
  411. var _iteratorNormalCompletion2 = true;
  412. var _didIteratorError2 = false;
  413. var _iteratorError2 = undefined;
  414. try {
  415. for (var _iterator2 = groupedChunksToRequest[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
  416. var groupedChunk = _step2.value;
  417. var begin = groupedChunk.beginChunk * this.chunkSize;
  418. var end = Math.min(groupedChunk.endChunk * this.chunkSize, this.length);
  419. this.sendRequest(begin, end);
  420. }
  421. } catch (err) {
  422. _didIteratorError2 = true;
  423. _iteratorError2 = err;
  424. } finally {
  425. try {
  426. if (!_iteratorNormalCompletion2 && _iterator2["return"] != null) {
  427. _iterator2["return"]();
  428. }
  429. } finally {
  430. if (_didIteratorError2) {
  431. throw _iteratorError2;
  432. }
  433. }
  434. }
  435. return capability.promise;
  436. }
  437. }, {
  438. key: "getStream",
  439. value: function getStream() {
  440. return this.stream;
  441. }
  442. }, {
  443. key: "requestRange",
  444. value: function requestRange(begin, end) {
  445. end = Math.min(end, this.length);
  446. var beginChunk = this.getBeginChunk(begin);
  447. var endChunk = this.getEndChunk(end);
  448. var chunks = [];
  449. for (var chunk = beginChunk; chunk < endChunk; ++chunk) {
  450. chunks.push(chunk);
  451. }
  452. return this._requestChunks(chunks);
  453. }
  454. }, {
  455. key: "requestRanges",
  456. value: function requestRanges() {
  457. var ranges = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  458. var chunksToRequest = [];
  459. var _iteratorNormalCompletion3 = true;
  460. var _didIteratorError3 = false;
  461. var _iteratorError3 = undefined;
  462. try {
  463. for (var _iterator3 = ranges[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
  464. var range = _step3.value;
  465. var beginChunk = this.getBeginChunk(range.begin);
  466. var endChunk = this.getEndChunk(range.end);
  467. for (var chunk = beginChunk; chunk < endChunk; ++chunk) {
  468. if (!chunksToRequest.includes(chunk)) {
  469. chunksToRequest.push(chunk);
  470. }
  471. }
  472. }
  473. } catch (err) {
  474. _didIteratorError3 = true;
  475. _iteratorError3 = err;
  476. } finally {
  477. try {
  478. if (!_iteratorNormalCompletion3 && _iterator3["return"] != null) {
  479. _iterator3["return"]();
  480. }
  481. } finally {
  482. if (_didIteratorError3) {
  483. throw _iteratorError3;
  484. }
  485. }
  486. }
  487. chunksToRequest.sort(function (a, b) {
  488. return a - b;
  489. });
  490. return this._requestChunks(chunksToRequest);
  491. }
  492. }, {
  493. key: "groupChunks",
  494. value: function groupChunks(chunks) {
  495. var groupedChunks = [];
  496. var beginChunk = -1;
  497. var prevChunk = -1;
  498. for (var i = 0, ii = chunks.length; i < ii; ++i) {
  499. var chunk = chunks[i];
  500. if (beginChunk < 0) {
  501. beginChunk = chunk;
  502. }
  503. if (prevChunk >= 0 && prevChunk + 1 !== chunk) {
  504. groupedChunks.push({
  505. beginChunk: beginChunk,
  506. endChunk: prevChunk + 1
  507. });
  508. beginChunk = chunk;
  509. }
  510. if (i + 1 === chunks.length) {
  511. groupedChunks.push({
  512. beginChunk: beginChunk,
  513. endChunk: chunk + 1
  514. });
  515. }
  516. prevChunk = chunk;
  517. }
  518. return groupedChunks;
  519. }
  520. }, {
  521. key: "onProgress",
  522. value: function onProgress(args) {
  523. this.msgHandler.send('DocProgress', {
  524. loaded: this.stream.numChunksLoaded * this.chunkSize + args.loaded,
  525. total: this.length
  526. });
  527. }
  528. }, {
  529. key: "onReceiveData",
  530. value: function onReceiveData(args) {
  531. var chunk = args.chunk;
  532. var isProgressive = args.begin === undefined;
  533. var begin = isProgressive ? this.progressiveDataLength : args.begin;
  534. var end = begin + chunk.byteLength;
  535. var beginChunk = Math.floor(begin / this.chunkSize);
  536. var endChunk = end < this.length ? Math.floor(end / this.chunkSize) : Math.ceil(end / this.chunkSize);
  537. if (isProgressive) {
  538. this.stream.onReceiveProgressiveData(chunk);
  539. this.progressiveDataLength = end;
  540. } else {
  541. this.stream.onReceiveData(begin, chunk);
  542. }
  543. if (this.stream.allChunksLoaded()) {
  544. this._loadedStreamCapability.resolve(this.stream);
  545. }
  546. var loadedRequests = [];
  547. for (var _chunk2 = beginChunk; _chunk2 < endChunk; ++_chunk2) {
  548. var requestIds = this.requestsByChunk[_chunk2] || [];
  549. delete this.requestsByChunk[_chunk2];
  550. var _iteratorNormalCompletion4 = true;
  551. var _didIteratorError4 = false;
  552. var _iteratorError4 = undefined;
  553. try {
  554. for (var _iterator4 = requestIds[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
  555. var requestId = _step4.value;
  556. var chunksNeeded = this.chunksNeededByRequest[requestId];
  557. if (_chunk2 in chunksNeeded) {
  558. delete chunksNeeded[_chunk2];
  559. }
  560. if (!(0, _util.isEmptyObj)(chunksNeeded)) {
  561. continue;
  562. }
  563. loadedRequests.push(requestId);
  564. }
  565. } catch (err) {
  566. _didIteratorError4 = true;
  567. _iteratorError4 = err;
  568. } finally {
  569. try {
  570. if (!_iteratorNormalCompletion4 && _iterator4["return"] != null) {
  571. _iterator4["return"]();
  572. }
  573. } finally {
  574. if (_didIteratorError4) {
  575. throw _iteratorError4;
  576. }
  577. }
  578. }
  579. }
  580. if (!this.disableAutoFetch && (0, _util.isEmptyObj)(this.requestsByChunk)) {
  581. var nextEmptyChunk;
  582. if (this.stream.numChunksLoaded === 1) {
  583. var lastChunk = this.stream.numChunks - 1;
  584. if (!this.stream.hasChunk(lastChunk)) {
  585. nextEmptyChunk = lastChunk;
  586. }
  587. } else {
  588. nextEmptyChunk = this.stream.nextEmptyChunk(endChunk);
  589. }
  590. if (Number.isInteger(nextEmptyChunk)) {
  591. this._requestChunks([nextEmptyChunk]);
  592. }
  593. }
  594. for (var _i = 0, _loadedRequests = loadedRequests; _i < _loadedRequests.length; _i++) {
  595. var _requestId = _loadedRequests[_i];
  596. var capability = this.promisesByRequest[_requestId];
  597. delete this.promisesByRequest[_requestId];
  598. capability.resolve();
  599. }
  600. this.msgHandler.send('DocProgress', {
  601. loaded: this.stream.numChunksLoaded * this.chunkSize,
  602. total: this.length
  603. });
  604. }
  605. }, {
  606. key: "onError",
  607. value: function onError(err) {
  608. this._loadedStreamCapability.reject(err);
  609. }
  610. }, {
  611. key: "getBeginChunk",
  612. value: function getBeginChunk(begin) {
  613. return Math.floor(begin / this.chunkSize);
  614. }
  615. }, {
  616. key: "getEndChunk",
  617. value: function getEndChunk(end) {
  618. return Math.floor((end - 1) / this.chunkSize) + 1;
  619. }
  620. }, {
  621. key: "abort",
  622. value: function abort() {
  623. this.aborted = true;
  624. if (this.pdfNetworkStream) {
  625. this.pdfNetworkStream.cancelAllRequests('abort');
  626. }
  627. for (var requestId in this.promisesByRequest) {
  628. this.promisesByRequest[requestId].reject(new Error('Request was aborted'));
  629. }
  630. }
  631. }]);
  632. return ChunkedStreamManager;
  633. }();
  634. exports.ChunkedStreamManager = ChunkedStreamManager;