transport_stream.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2018 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.PDFDataTransportStream = void 0;
  27. var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
  28. var _util = require("../shared/util");
  29. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  30. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  31. function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  32. var PDFDataTransportStream = function PDFDataTransportStreamClosure() {
  33. function PDFDataTransportStream(params, pdfDataRangeTransport) {
  34. var _this = this;
  35. (0, _util.assert)(pdfDataRangeTransport);
  36. this._queuedChunks = [];
  37. var initialData = params.initialData;
  38. if (initialData && initialData.length > 0) {
  39. var buffer = new Uint8Array(initialData).buffer;
  40. this._queuedChunks.push(buffer);
  41. }
  42. this._pdfDataRangeTransport = pdfDataRangeTransport;
  43. this._isStreamingSupported = !params.disableStream;
  44. this._isRangeSupported = !params.disableRange;
  45. this._contentLength = params.length;
  46. this._fullRequestReader = null;
  47. this._rangeReaders = [];
  48. this._pdfDataRangeTransport.addRangeListener(function (begin, chunk) {
  49. _this._onReceiveData({
  50. begin: begin,
  51. chunk: chunk
  52. });
  53. });
  54. this._pdfDataRangeTransport.addProgressListener(function (loaded) {
  55. _this._onProgress({
  56. loaded: loaded
  57. });
  58. });
  59. this._pdfDataRangeTransport.addProgressiveReadListener(function (chunk) {
  60. _this._onReceiveData({
  61. chunk: chunk
  62. });
  63. });
  64. this._pdfDataRangeTransport.transportReady();
  65. }
  66. PDFDataTransportStream.prototype = {
  67. _onReceiveData: function PDFDataTransportStream_onReceiveData(args) {
  68. var buffer = new Uint8Array(args.chunk).buffer;
  69. if (args.begin === undefined) {
  70. if (this._fullRequestReader) {
  71. this._fullRequestReader._enqueue(buffer);
  72. } else {
  73. this._queuedChunks.push(buffer);
  74. }
  75. } else {
  76. var found = this._rangeReaders.some(function (rangeReader) {
  77. if (rangeReader._begin !== args.begin) {
  78. return false;
  79. }
  80. rangeReader._enqueue(buffer);
  81. return true;
  82. });
  83. (0, _util.assert)(found);
  84. }
  85. },
  86. _onProgress: function PDFDataTransportStream_onDataProgress(evt) {
  87. if (this._rangeReaders.length > 0) {
  88. var firstReader = this._rangeReaders[0];
  89. if (firstReader.onProgress) {
  90. firstReader.onProgress({
  91. loaded: evt.loaded
  92. });
  93. }
  94. }
  95. },
  96. _removeRangeReader: function PDFDataTransportStream_removeRangeReader(reader) {
  97. var i = this._rangeReaders.indexOf(reader);
  98. if (i >= 0) {
  99. this._rangeReaders.splice(i, 1);
  100. }
  101. },
  102. getFullReader: function PDFDataTransportStream_getFullReader() {
  103. (0, _util.assert)(!this._fullRequestReader);
  104. var queuedChunks = this._queuedChunks;
  105. this._queuedChunks = null;
  106. return new PDFDataTransportStreamReader(this, queuedChunks);
  107. },
  108. getRangeReader: function PDFDataTransportStream_getRangeReader(begin, end) {
  109. var reader = new PDFDataTransportStreamRangeReader(this, begin, end);
  110. this._pdfDataRangeTransport.requestDataRange(begin, end);
  111. this._rangeReaders.push(reader);
  112. return reader;
  113. },
  114. cancelAllRequests: function PDFDataTransportStream_cancelAllRequests(reason) {
  115. if (this._fullRequestReader) {
  116. this._fullRequestReader.cancel(reason);
  117. }
  118. var readers = this._rangeReaders.slice(0);
  119. readers.forEach(function (rangeReader) {
  120. rangeReader.cancel(reason);
  121. });
  122. this._pdfDataRangeTransport.abort();
  123. }
  124. };
  125. function PDFDataTransportStreamReader(stream, queuedChunks) {
  126. this._stream = stream;
  127. this._done = false;
  128. this._filename = null;
  129. this._queuedChunks = queuedChunks || [];
  130. this._requests = [];
  131. this._headersReady = Promise.resolve();
  132. stream._fullRequestReader = this;
  133. this.onProgress = null;
  134. }
  135. PDFDataTransportStreamReader.prototype = {
  136. _enqueue: function PDFDataTransportStreamReader_enqueue(chunk) {
  137. if (this._done) {
  138. return;
  139. }
  140. if (this._requests.length > 0) {
  141. var requestCapability = this._requests.shift();
  142. requestCapability.resolve({
  143. value: chunk,
  144. done: false
  145. });
  146. return;
  147. }
  148. this._queuedChunks.push(chunk);
  149. },
  150. get headersReady() {
  151. return this._headersReady;
  152. },
  153. get filename() {
  154. return this._filename;
  155. },
  156. get isRangeSupported() {
  157. return this._stream._isRangeSupported;
  158. },
  159. get isStreamingSupported() {
  160. return this._stream._isStreamingSupported;
  161. },
  162. get contentLength() {
  163. return this._stream._contentLength;
  164. },
  165. read: function () {
  166. var _read = _asyncToGenerator(
  167. /*#__PURE__*/
  168. _regenerator.default.mark(function _callee() {
  169. var chunk, requestCapability;
  170. return _regenerator.default.wrap(function _callee$(_context) {
  171. while (1) {
  172. switch (_context.prev = _context.next) {
  173. case 0:
  174. if (!(this._queuedChunks.length > 0)) {
  175. _context.next = 3;
  176. break;
  177. }
  178. chunk = this._queuedChunks.shift();
  179. return _context.abrupt("return", {
  180. value: chunk,
  181. done: false
  182. });
  183. case 3:
  184. if (!this._done) {
  185. _context.next = 5;
  186. break;
  187. }
  188. return _context.abrupt("return", {
  189. value: undefined,
  190. done: true
  191. });
  192. case 5:
  193. requestCapability = (0, _util.createPromiseCapability)();
  194. this._requests.push(requestCapability);
  195. return _context.abrupt("return", requestCapability.promise);
  196. case 8:
  197. case "end":
  198. return _context.stop();
  199. }
  200. }
  201. }, _callee, this);
  202. }));
  203. function read() {
  204. return _read.apply(this, arguments);
  205. }
  206. return read;
  207. }(),
  208. cancel: function PDFDataTransportStreamReader_cancel(reason) {
  209. this._done = true;
  210. this._requests.forEach(function (requestCapability) {
  211. requestCapability.resolve({
  212. value: undefined,
  213. done: true
  214. });
  215. });
  216. this._requests = [];
  217. }
  218. };
  219. function PDFDataTransportStreamRangeReader(stream, begin, end) {
  220. this._stream = stream;
  221. this._begin = begin;
  222. this._end = end;
  223. this._queuedChunk = null;
  224. this._requests = [];
  225. this._done = false;
  226. this.onProgress = null;
  227. }
  228. PDFDataTransportStreamRangeReader.prototype = {
  229. _enqueue: function PDFDataTransportStreamRangeReader_enqueue(chunk) {
  230. if (this._done) {
  231. return;
  232. }
  233. if (this._requests.length === 0) {
  234. this._queuedChunk = chunk;
  235. } else {
  236. var requestsCapability = this._requests.shift();
  237. requestsCapability.resolve({
  238. value: chunk,
  239. done: false
  240. });
  241. this._requests.forEach(function (requestCapability) {
  242. requestCapability.resolve({
  243. value: undefined,
  244. done: true
  245. });
  246. });
  247. this._requests = [];
  248. }
  249. this._done = true;
  250. this._stream._removeRangeReader(this);
  251. },
  252. get isStreamingSupported() {
  253. return false;
  254. },
  255. read: function () {
  256. var _read2 = _asyncToGenerator(
  257. /*#__PURE__*/
  258. _regenerator.default.mark(function _callee2() {
  259. var chunk, requestCapability;
  260. return _regenerator.default.wrap(function _callee2$(_context2) {
  261. while (1) {
  262. switch (_context2.prev = _context2.next) {
  263. case 0:
  264. if (!this._queuedChunk) {
  265. _context2.next = 4;
  266. break;
  267. }
  268. chunk = this._queuedChunk;
  269. this._queuedChunk = null;
  270. return _context2.abrupt("return", {
  271. value: chunk,
  272. done: false
  273. });
  274. case 4:
  275. if (!this._done) {
  276. _context2.next = 6;
  277. break;
  278. }
  279. return _context2.abrupt("return", {
  280. value: undefined,
  281. done: true
  282. });
  283. case 6:
  284. requestCapability = (0, _util.createPromiseCapability)();
  285. this._requests.push(requestCapability);
  286. return _context2.abrupt("return", requestCapability.promise);
  287. case 9:
  288. case "end":
  289. return _context2.stop();
  290. }
  291. }
  292. }, _callee2, this);
  293. }));
  294. function read() {
  295. return _read2.apply(this, arguments);
  296. }
  297. return read;
  298. }(),
  299. cancel: function PDFDataTransportStreamRangeReader_cancel(reason) {
  300. this._done = true;
  301. this._requests.forEach(function (requestCapability) {
  302. requestCapability.resolve({
  303. value: undefined,
  304. done: true
  305. });
  306. });
  307. this._requests = [];
  308. this._stream._removeRangeReader(this);
  309. }
  310. };
  311. return PDFDataTransportStream;
  312. }();
  313. exports.PDFDataTransportStream = PDFDataTransportStream;