transport_stream.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.PDFDataTransportStream = undefined;
  20. var _util = require('../shared/util');
  21. var PDFDataTransportStream = function PDFDataTransportStreamClosure() {
  22. function PDFDataTransportStream(params, pdfDataRangeTransport) {
  23. var _this = this;
  24. (0, _util.assert)(pdfDataRangeTransport);
  25. this._queuedChunks = [];
  26. var initialData = params.initialData;
  27. if (initialData && initialData.length > 0) {
  28. var buffer = new Uint8Array(initialData).buffer;
  29. this._queuedChunks.push(buffer);
  30. }
  31. this._pdfDataRangeTransport = pdfDataRangeTransport;
  32. this._isRangeSupported = !params.disableRange;
  33. this._isStreamingSupported = !params.disableStream;
  34. this._contentLength = params.length;
  35. this._fullRequestReader = null;
  36. this._rangeReaders = [];
  37. this._pdfDataRangeTransport.addRangeListener(function (begin, chunk) {
  38. _this._onReceiveData({
  39. begin: begin,
  40. chunk: chunk
  41. });
  42. });
  43. this._pdfDataRangeTransport.addProgressListener(function (loaded) {
  44. _this._onProgress({ loaded: loaded });
  45. });
  46. this._pdfDataRangeTransport.addProgressiveReadListener(function (chunk) {
  47. _this._onReceiveData({ chunk: chunk });
  48. });
  49. this._pdfDataRangeTransport.transportReady();
  50. }
  51. PDFDataTransportStream.prototype = {
  52. _onReceiveData: function PDFDataTransportStream_onReceiveData(args) {
  53. var buffer = new Uint8Array(args.chunk).buffer;
  54. if (args.begin === undefined) {
  55. if (this._fullRequestReader) {
  56. this._fullRequestReader._enqueue(buffer);
  57. } else {
  58. this._queuedChunks.push(buffer);
  59. }
  60. } else {
  61. var found = this._rangeReaders.some(function (rangeReader) {
  62. if (rangeReader._begin !== args.begin) {
  63. return false;
  64. }
  65. rangeReader._enqueue(buffer);
  66. return true;
  67. });
  68. (0, _util.assert)(found);
  69. }
  70. },
  71. _onProgress: function PDFDataTransportStream_onDataProgress(evt) {
  72. if (this._rangeReaders.length > 0) {
  73. var firstReader = this._rangeReaders[0];
  74. if (firstReader.onProgress) {
  75. firstReader.onProgress({ loaded: evt.loaded });
  76. }
  77. }
  78. },
  79. _removeRangeReader: function PDFDataTransportStream_removeRangeReader(reader) {
  80. var i = this._rangeReaders.indexOf(reader);
  81. if (i >= 0) {
  82. this._rangeReaders.splice(i, 1);
  83. }
  84. },
  85. getFullReader: function PDFDataTransportStream_getFullReader() {
  86. (0, _util.assert)(!this._fullRequestReader);
  87. var queuedChunks = this._queuedChunks;
  88. this._queuedChunks = null;
  89. return new PDFDataTransportStreamReader(this, queuedChunks);
  90. },
  91. getRangeReader: function PDFDataTransportStream_getRangeReader(begin, end) {
  92. var reader = new PDFDataTransportStreamRangeReader(this, begin, end);
  93. this._pdfDataRangeTransport.requestDataRange(begin, end);
  94. this._rangeReaders.push(reader);
  95. return reader;
  96. },
  97. cancelAllRequests: function PDFDataTransportStream_cancelAllRequests(reason) {
  98. if (this._fullRequestReader) {
  99. this._fullRequestReader.cancel(reason);
  100. }
  101. var readers = this._rangeReaders.slice(0);
  102. readers.forEach(function (rangeReader) {
  103. rangeReader.cancel(reason);
  104. });
  105. this._pdfDataRangeTransport.abort();
  106. }
  107. };
  108. function PDFDataTransportStreamReader(stream, queuedChunks) {
  109. this._stream = stream;
  110. this._done = false;
  111. this._queuedChunks = queuedChunks || [];
  112. this._requests = [];
  113. this._headersReady = Promise.resolve();
  114. stream._fullRequestReader = this;
  115. this.onProgress = null;
  116. }
  117. PDFDataTransportStreamReader.prototype = {
  118. _enqueue: function PDFDataTransportStreamReader_enqueue(chunk) {
  119. if (this._done) {
  120. return;
  121. }
  122. if (this._requests.length > 0) {
  123. var requestCapability = this._requests.shift();
  124. requestCapability.resolve({
  125. value: chunk,
  126. done: false
  127. });
  128. return;
  129. }
  130. this._queuedChunks.push(chunk);
  131. },
  132. get headersReady() {
  133. return this._headersReady;
  134. },
  135. get isRangeSupported() {
  136. return this._stream._isRangeSupported;
  137. },
  138. get isStreamingSupported() {
  139. return this._stream._isStreamingSupported;
  140. },
  141. get contentLength() {
  142. return this._stream._contentLength;
  143. },
  144. read: function PDFDataTransportStreamReader_read() {
  145. if (this._queuedChunks.length > 0) {
  146. var chunk = this._queuedChunks.shift();
  147. return Promise.resolve({
  148. value: chunk,
  149. done: false
  150. });
  151. }
  152. if (this._done) {
  153. return Promise.resolve({
  154. value: undefined,
  155. done: true
  156. });
  157. }
  158. var requestCapability = (0, _util.createPromiseCapability)();
  159. this._requests.push(requestCapability);
  160. return requestCapability.promise;
  161. },
  162. cancel: function PDFDataTransportStreamReader_cancel(reason) {
  163. this._done = true;
  164. this._requests.forEach(function (requestCapability) {
  165. requestCapability.resolve({
  166. value: undefined,
  167. done: true
  168. });
  169. });
  170. this._requests = [];
  171. }
  172. };
  173. function PDFDataTransportStreamRangeReader(stream, begin, end) {
  174. this._stream = stream;
  175. this._begin = begin;
  176. this._end = end;
  177. this._queuedChunk = null;
  178. this._requests = [];
  179. this._done = false;
  180. this.onProgress = null;
  181. }
  182. PDFDataTransportStreamRangeReader.prototype = {
  183. _enqueue: function PDFDataTransportStreamRangeReader_enqueue(chunk) {
  184. if (this._done) {
  185. return;
  186. }
  187. if (this._requests.length === 0) {
  188. this._queuedChunk = chunk;
  189. } else {
  190. var requestsCapability = this._requests.shift();
  191. requestsCapability.resolve({
  192. value: chunk,
  193. done: false
  194. });
  195. this._requests.forEach(function (requestCapability) {
  196. requestCapability.resolve({
  197. value: undefined,
  198. done: true
  199. });
  200. });
  201. this._requests = [];
  202. }
  203. this._done = true;
  204. this._stream._removeRangeReader(this);
  205. },
  206. get isStreamingSupported() {
  207. return false;
  208. },
  209. read: function PDFDataTransportStreamRangeReader_read() {
  210. if (this._queuedChunk) {
  211. var chunk = this._queuedChunk;
  212. this._queuedChunk = null;
  213. return Promise.resolve({
  214. value: chunk,
  215. done: false
  216. });
  217. }
  218. if (this._done) {
  219. return Promise.resolve({
  220. value: undefined,
  221. done: true
  222. });
  223. }
  224. var requestCapability = (0, _util.createPromiseCapability)();
  225. this._requests.push(requestCapability);
  226. return requestCapability.promise;
  227. },
  228. cancel: function PDFDataTransportStreamRangeReader_cancel(reason) {
  229. this._done = true;
  230. this._requests.forEach(function (requestCapability) {
  231. requestCapability.resolve({
  232. value: undefined,
  233. done: true
  234. });
  235. });
  236. this._requests = [];
  237. this._stream._removeRangeReader(this);
  238. }
  239. };
  240. return PDFDataTransportStream;
  241. }();
  242. exports.PDFDataTransportStream = PDFDataTransportStream;