transport_stream.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 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 = undefined;
  27. var _util = require('../shared/util');
  28. var PDFDataTransportStream = function PDFDataTransportStreamClosure() {
  29. function PDFDataTransportStream(params, pdfDataRangeTransport) {
  30. var _this = this;
  31. (0, _util.assert)(pdfDataRangeTransport);
  32. this._queuedChunks = [];
  33. var initialData = params.initialData;
  34. if (initialData && initialData.length > 0) {
  35. var buffer = new Uint8Array(initialData).buffer;
  36. this._queuedChunks.push(buffer);
  37. }
  38. this._pdfDataRangeTransport = pdfDataRangeTransport;
  39. this._isRangeSupported = !params.disableRange;
  40. this._isStreamingSupported = !params.disableStream;
  41. this._contentLength = params.length;
  42. this._fullRequestReader = null;
  43. this._rangeReaders = [];
  44. this._pdfDataRangeTransport.addRangeListener(function (begin, chunk) {
  45. _this._onReceiveData({
  46. begin: begin,
  47. chunk: chunk
  48. });
  49. });
  50. this._pdfDataRangeTransport.addProgressListener(function (loaded) {
  51. _this._onProgress({ loaded: loaded });
  52. });
  53. this._pdfDataRangeTransport.addProgressiveReadListener(function (chunk) {
  54. _this._onReceiveData({ chunk: chunk });
  55. });
  56. this._pdfDataRangeTransport.transportReady();
  57. }
  58. PDFDataTransportStream.prototype = {
  59. _onReceiveData: function PDFDataTransportStream_onReceiveData(args) {
  60. var buffer = new Uint8Array(args.chunk).buffer;
  61. if (args.begin === undefined) {
  62. if (this._fullRequestReader) {
  63. this._fullRequestReader._enqueue(buffer);
  64. } else {
  65. this._queuedChunks.push(buffer);
  66. }
  67. } else {
  68. var found = this._rangeReaders.some(function (rangeReader) {
  69. if (rangeReader._begin !== args.begin) {
  70. return false;
  71. }
  72. rangeReader._enqueue(buffer);
  73. return true;
  74. });
  75. (0, _util.assert)(found);
  76. }
  77. },
  78. _onProgress: function PDFDataTransportStream_onDataProgress(evt) {
  79. if (this._rangeReaders.length > 0) {
  80. var firstReader = this._rangeReaders[0];
  81. if (firstReader.onProgress) {
  82. firstReader.onProgress({ loaded: evt.loaded });
  83. }
  84. }
  85. },
  86. _removeRangeReader: function PDFDataTransportStream_removeRangeReader(reader) {
  87. var i = this._rangeReaders.indexOf(reader);
  88. if (i >= 0) {
  89. this._rangeReaders.splice(i, 1);
  90. }
  91. },
  92. getFullReader: function PDFDataTransportStream_getFullReader() {
  93. (0, _util.assert)(!this._fullRequestReader);
  94. var queuedChunks = this._queuedChunks;
  95. this._queuedChunks = null;
  96. return new PDFDataTransportStreamReader(this, queuedChunks);
  97. },
  98. getRangeReader: function PDFDataTransportStream_getRangeReader(begin, end) {
  99. var reader = new PDFDataTransportStreamRangeReader(this, begin, end);
  100. this._pdfDataRangeTransport.requestDataRange(begin, end);
  101. this._rangeReaders.push(reader);
  102. return reader;
  103. },
  104. cancelAllRequests: function PDFDataTransportStream_cancelAllRequests(reason) {
  105. if (this._fullRequestReader) {
  106. this._fullRequestReader.cancel(reason);
  107. }
  108. var readers = this._rangeReaders.slice(0);
  109. readers.forEach(function (rangeReader) {
  110. rangeReader.cancel(reason);
  111. });
  112. this._pdfDataRangeTransport.abort();
  113. }
  114. };
  115. function PDFDataTransportStreamReader(stream, queuedChunks) {
  116. this._stream = stream;
  117. this._done = false;
  118. this._filename = null;
  119. this._queuedChunks = queuedChunks || [];
  120. this._requests = [];
  121. this._headersReady = Promise.resolve();
  122. stream._fullRequestReader = this;
  123. this.onProgress = null;
  124. }
  125. PDFDataTransportStreamReader.prototype = {
  126. _enqueue: function PDFDataTransportStreamReader_enqueue(chunk) {
  127. if (this._done) {
  128. return;
  129. }
  130. if (this._requests.length > 0) {
  131. var requestCapability = this._requests.shift();
  132. requestCapability.resolve({
  133. value: chunk,
  134. done: false
  135. });
  136. return;
  137. }
  138. this._queuedChunks.push(chunk);
  139. },
  140. get headersReady() {
  141. return this._headersReady;
  142. },
  143. get filename() {
  144. return this._filename;
  145. },
  146. get isRangeSupported() {
  147. return this._stream._isRangeSupported;
  148. },
  149. get isStreamingSupported() {
  150. return this._stream._isStreamingSupported;
  151. },
  152. get contentLength() {
  153. return this._stream._contentLength;
  154. },
  155. read: function PDFDataTransportStreamReader_read() {
  156. if (this._queuedChunks.length > 0) {
  157. var chunk = this._queuedChunks.shift();
  158. return Promise.resolve({
  159. value: chunk,
  160. done: false
  161. });
  162. }
  163. if (this._done) {
  164. return Promise.resolve({
  165. value: undefined,
  166. done: true
  167. });
  168. }
  169. var requestCapability = (0, _util.createPromiseCapability)();
  170. this._requests.push(requestCapability);
  171. return requestCapability.promise;
  172. },
  173. cancel: function PDFDataTransportStreamReader_cancel(reason) {
  174. this._done = true;
  175. this._requests.forEach(function (requestCapability) {
  176. requestCapability.resolve({
  177. value: undefined,
  178. done: true
  179. });
  180. });
  181. this._requests = [];
  182. }
  183. };
  184. function PDFDataTransportStreamRangeReader(stream, begin, end) {
  185. this._stream = stream;
  186. this._begin = begin;
  187. this._end = end;
  188. this._queuedChunk = null;
  189. this._requests = [];
  190. this._done = false;
  191. this.onProgress = null;
  192. }
  193. PDFDataTransportStreamRangeReader.prototype = {
  194. _enqueue: function PDFDataTransportStreamRangeReader_enqueue(chunk) {
  195. if (this._done) {
  196. return;
  197. }
  198. if (this._requests.length === 0) {
  199. this._queuedChunk = chunk;
  200. } else {
  201. var requestsCapability = this._requests.shift();
  202. requestsCapability.resolve({
  203. value: chunk,
  204. done: false
  205. });
  206. this._requests.forEach(function (requestCapability) {
  207. requestCapability.resolve({
  208. value: undefined,
  209. done: true
  210. });
  211. });
  212. this._requests = [];
  213. }
  214. this._done = true;
  215. this._stream._removeRangeReader(this);
  216. },
  217. get isStreamingSupported() {
  218. return false;
  219. },
  220. read: function PDFDataTransportStreamRangeReader_read() {
  221. if (this._queuedChunk) {
  222. var chunk = this._queuedChunk;
  223. this._queuedChunk = null;
  224. return Promise.resolve({
  225. value: chunk,
  226. done: false
  227. });
  228. }
  229. if (this._done) {
  230. return Promise.resolve({
  231. value: undefined,
  232. done: true
  233. });
  234. }
  235. var requestCapability = (0, _util.createPromiseCapability)();
  236. this._requests.push(requestCapability);
  237. return requestCapability.promise;
  238. },
  239. cancel: function PDFDataTransportStreamRangeReader_cancel(reason) {
  240. this._done = true;
  241. this._requests.forEach(function (requestCapability) {
  242. requestCapability.resolve({
  243. value: undefined,
  244. done: true
  245. });
  246. });
  247. this._requests = [];
  248. this._stream._removeRangeReader(this);
  249. }
  250. };
  251. return PDFDataTransportStream;
  252. }();
  253. exports.PDFDataTransportStream = PDFDataTransportStream;