chunked_stream.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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. if (pos >= this.progressiveDataLength) {
  165. this.ensureByte(pos);
  166. }
  167. return this.bytes[this.pos++];
  168. }
  169. }, {
  170. key: "getUint16",
  171. value: function getUint16() {
  172. var b0 = this.getByte();
  173. var b1 = this.getByte();
  174. if (b0 === -1 || b1 === -1) {
  175. return -1;
  176. }
  177. return (b0 << 8) + b1;
  178. }
  179. }, {
  180. key: "getInt32",
  181. value: function getInt32() {
  182. var b0 = this.getByte();
  183. var b1 = this.getByte();
  184. var b2 = this.getByte();
  185. var b3 = this.getByte();
  186. return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
  187. }
  188. }, {
  189. key: "getBytes",
  190. value: function getBytes(length) {
  191. var forceClamped = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  192. var bytes = this.bytes;
  193. var pos = this.pos;
  194. var strEnd = this.end;
  195. if (!length) {
  196. if (strEnd > this.progressiveDataLength) {
  197. this.ensureRange(pos, strEnd);
  198. }
  199. var _subarray = bytes.subarray(pos, strEnd);
  200. return forceClamped ? new Uint8ClampedArray(_subarray) : _subarray;
  201. }
  202. var end = pos + length;
  203. if (end > strEnd) {
  204. end = strEnd;
  205. }
  206. if (end > this.progressiveDataLength) {
  207. this.ensureRange(pos, end);
  208. }
  209. this.pos = end;
  210. var subarray = bytes.subarray(pos, end);
  211. return forceClamped ? new Uint8ClampedArray(subarray) : subarray;
  212. }
  213. }, {
  214. key: "peekByte",
  215. value: function peekByte() {
  216. var peekedByte = this.getByte();
  217. this.pos--;
  218. return peekedByte;
  219. }
  220. }, {
  221. key: "peekBytes",
  222. value: function peekBytes(length) {
  223. var forceClamped = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  224. var bytes = this.getBytes(length, forceClamped);
  225. this.pos -= bytes.length;
  226. return bytes;
  227. }
  228. }, {
  229. key: "getByteRange",
  230. value: function getByteRange(begin, end) {
  231. if (begin < 0) {
  232. begin = 0;
  233. }
  234. if (end > this.end) {
  235. end = this.end;
  236. }
  237. if (end > this.progressiveDataLength) {
  238. this.ensureRange(begin, end);
  239. }
  240. return this.bytes.subarray(begin, end);
  241. }
  242. }, {
  243. key: "skip",
  244. value: function skip(n) {
  245. if (!n) {
  246. n = 1;
  247. }
  248. this.pos += n;
  249. }
  250. }, {
  251. key: "reset",
  252. value: function reset() {
  253. this.pos = this.start;
  254. }
  255. }, {
  256. key: "moveStart",
  257. value: function moveStart() {
  258. this.start = this.pos;
  259. }
  260. }, {
  261. key: "makeSubStream",
  262. value: function makeSubStream(start, length, dict) {
  263. if (length) {
  264. if (start + length > this.progressiveDataLength) {
  265. this.ensureRange(start, start + length);
  266. }
  267. } else {
  268. if (start >= this.progressiveDataLength) {
  269. this.ensureByte(start);
  270. }
  271. }
  272. function ChunkedStreamSubstream() {}
  273. ChunkedStreamSubstream.prototype = Object.create(this);
  274. ChunkedStreamSubstream.prototype.getMissingChunks = function () {
  275. var chunkSize = this.chunkSize;
  276. var beginChunk = Math.floor(this.start / chunkSize);
  277. var endChunk = Math.floor((this.end - 1) / chunkSize) + 1;
  278. var missingChunks = [];
  279. for (var chunk = beginChunk; chunk < endChunk; ++chunk) {
  280. if (!this.loadedChunks[chunk]) {
  281. missingChunks.push(chunk);
  282. }
  283. }
  284. return missingChunks;
  285. };
  286. var subStream = new ChunkedStreamSubstream();
  287. subStream.pos = subStream.start = start;
  288. subStream.end = start + length || this.end;
  289. subStream.dict = dict;
  290. return subStream;
  291. }
  292. }, {
  293. key: "length",
  294. get: function get() {
  295. return this.end - this.start;
  296. }
  297. }, {
  298. key: "isEmpty",
  299. get: function get() {
  300. return this.length === 0;
  301. }
  302. }]);
  303. return ChunkedStream;
  304. }();
  305. exports.ChunkedStream = ChunkedStream;
  306. var ChunkedStreamManager =
  307. /*#__PURE__*/
  308. function () {
  309. function ChunkedStreamManager(pdfNetworkStream, args) {
  310. _classCallCheck(this, ChunkedStreamManager);
  311. this.length = args.length;
  312. this.chunkSize = args.rangeChunkSize;
  313. this.stream = new ChunkedStream(this.length, this.chunkSize, this);
  314. this.pdfNetworkStream = pdfNetworkStream;
  315. this.disableAutoFetch = args.disableAutoFetch;
  316. this.msgHandler = args.msgHandler;
  317. this.currRequestId = 0;
  318. this.chunksNeededByRequest = Object.create(null);
  319. this.requestsByChunk = Object.create(null);
  320. this.promisesByRequest = Object.create(null);
  321. this.progressiveDataLength = 0;
  322. this.aborted = false;
  323. this._loadedStreamCapability = (0, _util.createPromiseCapability)();
  324. }
  325. _createClass(ChunkedStreamManager, [{
  326. key: "onLoadedStream",
  327. value: function onLoadedStream() {
  328. return this._loadedStreamCapability.promise;
  329. }
  330. }, {
  331. key: "sendRequest",
  332. value: function sendRequest(begin, end) {
  333. var _this = this;
  334. var rangeReader = this.pdfNetworkStream.getRangeReader(begin, end);
  335. if (!rangeReader.isStreamingSupported) {
  336. rangeReader.onProgress = this.onProgress.bind(this);
  337. }
  338. var chunks = [],
  339. loaded = 0;
  340. var promise = new Promise(function (resolve, reject) {
  341. var readChunk = function readChunk(chunk) {
  342. try {
  343. if (!chunk.done) {
  344. var data = chunk.value;
  345. chunks.push(data);
  346. loaded += (0, _util.arrayByteLength)(data);
  347. if (rangeReader.isStreamingSupported) {
  348. _this.onProgress({
  349. loaded: loaded
  350. });
  351. }
  352. rangeReader.read().then(readChunk, reject);
  353. return;
  354. }
  355. var chunkData = (0, _util.arraysToBytes)(chunks);
  356. chunks = null;
  357. resolve(chunkData);
  358. } catch (e) {
  359. reject(e);
  360. }
  361. };
  362. rangeReader.read().then(readChunk, reject);
  363. });
  364. promise.then(function (data) {
  365. if (_this.aborted) {
  366. return;
  367. }
  368. _this.onReceiveData({
  369. chunk: data,
  370. begin: begin
  371. });
  372. });
  373. }
  374. }, {
  375. key: "requestAllChunks",
  376. value: function requestAllChunks() {
  377. var missingChunks = this.stream.getMissingChunks();
  378. this._requestChunks(missingChunks);
  379. return this._loadedStreamCapability.promise;
  380. }
  381. }, {
  382. key: "_requestChunks",
  383. value: function _requestChunks(chunks) {
  384. var requestId = this.currRequestId++;
  385. var chunksNeeded = Object.create(null);
  386. this.chunksNeededByRequest[requestId] = chunksNeeded;
  387. var _iteratorNormalCompletion = true;
  388. var _didIteratorError = false;
  389. var _iteratorError = undefined;
  390. try {
  391. for (var _iterator = chunks[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  392. var _chunk = _step.value;
  393. if (!this.stream.hasChunk(_chunk)) {
  394. chunksNeeded[_chunk] = true;
  395. }
  396. }
  397. } catch (err) {
  398. _didIteratorError = true;
  399. _iteratorError = err;
  400. } finally {
  401. try {
  402. if (!_iteratorNormalCompletion && _iterator["return"] != null) {
  403. _iterator["return"]();
  404. }
  405. } finally {
  406. if (_didIteratorError) {
  407. throw _iteratorError;
  408. }
  409. }
  410. }
  411. if ((0, _util.isEmptyObj)(chunksNeeded)) {
  412. return Promise.resolve();
  413. }
  414. var capability = (0, _util.createPromiseCapability)();
  415. this.promisesByRequest[requestId] = capability;
  416. var chunksToRequest = [];
  417. for (var chunk in chunksNeeded) {
  418. chunk = chunk | 0;
  419. if (!(chunk in this.requestsByChunk)) {
  420. this.requestsByChunk[chunk] = [];
  421. chunksToRequest.push(chunk);
  422. }
  423. this.requestsByChunk[chunk].push(requestId);
  424. }
  425. if (!chunksToRequest.length) {
  426. return capability.promise;
  427. }
  428. var groupedChunksToRequest = this.groupChunks(chunksToRequest);
  429. var _iteratorNormalCompletion2 = true;
  430. var _didIteratorError2 = false;
  431. var _iteratorError2 = undefined;
  432. try {
  433. for (var _iterator2 = groupedChunksToRequest[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
  434. var groupedChunk = _step2.value;
  435. var begin = groupedChunk.beginChunk * this.chunkSize;
  436. var end = Math.min(groupedChunk.endChunk * this.chunkSize, this.length);
  437. this.sendRequest(begin, end);
  438. }
  439. } catch (err) {
  440. _didIteratorError2 = true;
  441. _iteratorError2 = err;
  442. } finally {
  443. try {
  444. if (!_iteratorNormalCompletion2 && _iterator2["return"] != null) {
  445. _iterator2["return"]();
  446. }
  447. } finally {
  448. if (_didIteratorError2) {
  449. throw _iteratorError2;
  450. }
  451. }
  452. }
  453. return capability.promise;
  454. }
  455. }, {
  456. key: "getStream",
  457. value: function getStream() {
  458. return this.stream;
  459. }
  460. }, {
  461. key: "requestRange",
  462. value: function requestRange(begin, end) {
  463. end = Math.min(end, this.length);
  464. var beginChunk = this.getBeginChunk(begin);
  465. var endChunk = this.getEndChunk(end);
  466. var chunks = [];
  467. for (var chunk = beginChunk; chunk < endChunk; ++chunk) {
  468. chunks.push(chunk);
  469. }
  470. return this._requestChunks(chunks);
  471. }
  472. }, {
  473. key: "requestRanges",
  474. value: function requestRanges() {
  475. var ranges = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  476. var chunksToRequest = [];
  477. var _iteratorNormalCompletion3 = true;
  478. var _didIteratorError3 = false;
  479. var _iteratorError3 = undefined;
  480. try {
  481. for (var _iterator3 = ranges[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
  482. var range = _step3.value;
  483. var beginChunk = this.getBeginChunk(range.begin);
  484. var endChunk = this.getEndChunk(range.end);
  485. for (var chunk = beginChunk; chunk < endChunk; ++chunk) {
  486. if (!chunksToRequest.includes(chunk)) {
  487. chunksToRequest.push(chunk);
  488. }
  489. }
  490. }
  491. } catch (err) {
  492. _didIteratorError3 = true;
  493. _iteratorError3 = err;
  494. } finally {
  495. try {
  496. if (!_iteratorNormalCompletion3 && _iterator3["return"] != null) {
  497. _iterator3["return"]();
  498. }
  499. } finally {
  500. if (_didIteratorError3) {
  501. throw _iteratorError3;
  502. }
  503. }
  504. }
  505. chunksToRequest.sort(function (a, b) {
  506. return a - b;
  507. });
  508. return this._requestChunks(chunksToRequest);
  509. }
  510. }, {
  511. key: "groupChunks",
  512. value: function groupChunks(chunks) {
  513. var groupedChunks = [];
  514. var beginChunk = -1;
  515. var prevChunk = -1;
  516. for (var i = 0, ii = chunks.length; i < ii; ++i) {
  517. var chunk = chunks[i];
  518. if (beginChunk < 0) {
  519. beginChunk = chunk;
  520. }
  521. if (prevChunk >= 0 && prevChunk + 1 !== chunk) {
  522. groupedChunks.push({
  523. beginChunk: beginChunk,
  524. endChunk: prevChunk + 1
  525. });
  526. beginChunk = chunk;
  527. }
  528. if (i + 1 === chunks.length) {
  529. groupedChunks.push({
  530. beginChunk: beginChunk,
  531. endChunk: chunk + 1
  532. });
  533. }
  534. prevChunk = chunk;
  535. }
  536. return groupedChunks;
  537. }
  538. }, {
  539. key: "onProgress",
  540. value: function onProgress(args) {
  541. this.msgHandler.send('DocProgress', {
  542. loaded: this.stream.numChunksLoaded * this.chunkSize + args.loaded,
  543. total: this.length
  544. });
  545. }
  546. }, {
  547. key: "onReceiveData",
  548. value: function onReceiveData(args) {
  549. var chunk = args.chunk;
  550. var isProgressive = args.begin === undefined;
  551. var begin = isProgressive ? this.progressiveDataLength : args.begin;
  552. var end = begin + chunk.byteLength;
  553. var beginChunk = Math.floor(begin / this.chunkSize);
  554. var endChunk = end < this.length ? Math.floor(end / this.chunkSize) : Math.ceil(end / this.chunkSize);
  555. if (isProgressive) {
  556. this.stream.onReceiveProgressiveData(chunk);
  557. this.progressiveDataLength = end;
  558. } else {
  559. this.stream.onReceiveData(begin, chunk);
  560. }
  561. if (this.stream.allChunksLoaded()) {
  562. this._loadedStreamCapability.resolve(this.stream);
  563. }
  564. var loadedRequests = [];
  565. for (var _chunk2 = beginChunk; _chunk2 < endChunk; ++_chunk2) {
  566. var requestIds = this.requestsByChunk[_chunk2] || [];
  567. delete this.requestsByChunk[_chunk2];
  568. var _iteratorNormalCompletion4 = true;
  569. var _didIteratorError4 = false;
  570. var _iteratorError4 = undefined;
  571. try {
  572. for (var _iterator4 = requestIds[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
  573. var requestId = _step4.value;
  574. var chunksNeeded = this.chunksNeededByRequest[requestId];
  575. if (_chunk2 in chunksNeeded) {
  576. delete chunksNeeded[_chunk2];
  577. }
  578. if (!(0, _util.isEmptyObj)(chunksNeeded)) {
  579. continue;
  580. }
  581. loadedRequests.push(requestId);
  582. }
  583. } catch (err) {
  584. _didIteratorError4 = true;
  585. _iteratorError4 = err;
  586. } finally {
  587. try {
  588. if (!_iteratorNormalCompletion4 && _iterator4["return"] != null) {
  589. _iterator4["return"]();
  590. }
  591. } finally {
  592. if (_didIteratorError4) {
  593. throw _iteratorError4;
  594. }
  595. }
  596. }
  597. }
  598. if (!this.disableAutoFetch && (0, _util.isEmptyObj)(this.requestsByChunk)) {
  599. var nextEmptyChunk;
  600. if (this.stream.numChunksLoaded === 1) {
  601. var lastChunk = this.stream.numChunks - 1;
  602. if (!this.stream.hasChunk(lastChunk)) {
  603. nextEmptyChunk = lastChunk;
  604. }
  605. } else {
  606. nextEmptyChunk = this.stream.nextEmptyChunk(endChunk);
  607. }
  608. if (Number.isInteger(nextEmptyChunk)) {
  609. this._requestChunks([nextEmptyChunk]);
  610. }
  611. }
  612. for (var _i = 0, _loadedRequests = loadedRequests; _i < _loadedRequests.length; _i++) {
  613. var _requestId = _loadedRequests[_i];
  614. var capability = this.promisesByRequest[_requestId];
  615. delete this.promisesByRequest[_requestId];
  616. capability.resolve();
  617. }
  618. this.msgHandler.send('DocProgress', {
  619. loaded: this.stream.numChunksLoaded * this.chunkSize,
  620. total: this.length
  621. });
  622. }
  623. }, {
  624. key: "onError",
  625. value: function onError(err) {
  626. this._loadedStreamCapability.reject(err);
  627. }
  628. }, {
  629. key: "getBeginChunk",
  630. value: function getBeginChunk(begin) {
  631. return Math.floor(begin / this.chunkSize);
  632. }
  633. }, {
  634. key: "getEndChunk",
  635. value: function getEndChunk(end) {
  636. return Math.floor((end - 1) / this.chunkSize) + 1;
  637. }
  638. }, {
  639. key: "abort",
  640. value: function abort(reason) {
  641. this.aborted = true;
  642. if (this.pdfNetworkStream) {
  643. this.pdfNetworkStream.cancelAllRequests(reason);
  644. }
  645. for (var requestId in this.promisesByRequest) {
  646. this.promisesByRequest[requestId].reject(reason);
  647. }
  648. }
  649. }]);
  650. return ChunkedStreamManager;
  651. }();
  652. exports.ChunkedStreamManager = ChunkedStreamManager;