transport_stream.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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._queuedChunks = queuedChunks || [];
  119. this._requests = [];
  120. this._headersReady = Promise.resolve();
  121. stream._fullRequestReader = this;
  122. this.onProgress = null;
  123. }
  124. PDFDataTransportStreamReader.prototype = {
  125. _enqueue: function PDFDataTransportStreamReader_enqueue(chunk) {
  126. if (this._done) {
  127. return;
  128. }
  129. if (this._requests.length > 0) {
  130. var requestCapability = this._requests.shift();
  131. requestCapability.resolve({
  132. value: chunk,
  133. done: false
  134. });
  135. return;
  136. }
  137. this._queuedChunks.push(chunk);
  138. },
  139. get headersReady() {
  140. return this._headersReady;
  141. },
  142. get isRangeSupported() {
  143. return this._stream._isRangeSupported;
  144. },
  145. get isStreamingSupported() {
  146. return this._stream._isStreamingSupported;
  147. },
  148. get contentLength() {
  149. return this._stream._contentLength;
  150. },
  151. read: function PDFDataTransportStreamReader_read() {
  152. if (this._queuedChunks.length > 0) {
  153. var chunk = this._queuedChunks.shift();
  154. return Promise.resolve({
  155. value: chunk,
  156. done: false
  157. });
  158. }
  159. if (this._done) {
  160. return Promise.resolve({
  161. value: undefined,
  162. done: true
  163. });
  164. }
  165. var requestCapability = (0, _util.createPromiseCapability)();
  166. this._requests.push(requestCapability);
  167. return requestCapability.promise;
  168. },
  169. cancel: function PDFDataTransportStreamReader_cancel(reason) {
  170. this._done = true;
  171. this._requests.forEach(function (requestCapability) {
  172. requestCapability.resolve({
  173. value: undefined,
  174. done: true
  175. });
  176. });
  177. this._requests = [];
  178. }
  179. };
  180. function PDFDataTransportStreamRangeReader(stream, begin, end) {
  181. this._stream = stream;
  182. this._begin = begin;
  183. this._end = end;
  184. this._queuedChunk = null;
  185. this._requests = [];
  186. this._done = false;
  187. this.onProgress = null;
  188. }
  189. PDFDataTransportStreamRangeReader.prototype = {
  190. _enqueue: function PDFDataTransportStreamRangeReader_enqueue(chunk) {
  191. if (this._done) {
  192. return;
  193. }
  194. if (this._requests.length === 0) {
  195. this._queuedChunk = chunk;
  196. } else {
  197. var requestsCapability = this._requests.shift();
  198. requestsCapability.resolve({
  199. value: chunk,
  200. done: false
  201. });
  202. this._requests.forEach(function (requestCapability) {
  203. requestCapability.resolve({
  204. value: undefined,
  205. done: true
  206. });
  207. });
  208. this._requests = [];
  209. }
  210. this._done = true;
  211. this._stream._removeRangeReader(this);
  212. },
  213. get isStreamingSupported() {
  214. return false;
  215. },
  216. read: function PDFDataTransportStreamRangeReader_read() {
  217. if (this._queuedChunk) {
  218. var chunk = this._queuedChunk;
  219. this._queuedChunk = null;
  220. return Promise.resolve({
  221. value: chunk,
  222. done: false
  223. });
  224. }
  225. if (this._done) {
  226. return Promise.resolve({
  227. value: undefined,
  228. done: true
  229. });
  230. }
  231. var requestCapability = (0, _util.createPromiseCapability)();
  232. this._requests.push(requestCapability);
  233. return requestCapability.promise;
  234. },
  235. cancel: function PDFDataTransportStreamRangeReader_cancel(reason) {
  236. this._done = true;
  237. this._requests.forEach(function (requestCapability) {
  238. requestCapability.resolve({
  239. value: undefined,
  240. done: true
  241. });
  242. });
  243. this._requests = [];
  244. this._stream._removeRangeReader(this);
  245. }
  246. };
  247. return PDFDataTransportStream;
  248. }();
  249. exports.PDFDataTransportStream = PDFDataTransportStream;