transport_stream.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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 _util = require("../shared/util.js");
  28. class PDFDataTransportStream {
  29. constructor(params, pdfDataRangeTransport) {
  30. (0, _util.assert)(pdfDataRangeTransport, 'PDFDataTransportStream - missing required "pdfDataRangeTransport" argument.');
  31. this._queuedChunks = [];
  32. this._progressiveDone = params.progressiveDone || false;
  33. const initialData = params.initialData;
  34. if (initialData && initialData.length > 0) {
  35. const buffer = new Uint8Array(initialData).buffer;
  36. this._queuedChunks.push(buffer);
  37. }
  38. this._pdfDataRangeTransport = pdfDataRangeTransport;
  39. this._isStreamingSupported = !params.disableStream;
  40. this._isRangeSupported = !params.disableRange;
  41. this._contentLength = params.length;
  42. this._fullRequestReader = null;
  43. this._rangeReaders = [];
  44. this._pdfDataRangeTransport.addRangeListener((begin, chunk) => {
  45. this._onReceiveData({
  46. begin,
  47. chunk
  48. });
  49. });
  50. this._pdfDataRangeTransport.addProgressListener((loaded, total) => {
  51. this._onProgress({
  52. loaded,
  53. total
  54. });
  55. });
  56. this._pdfDataRangeTransport.addProgressiveReadListener(chunk => {
  57. this._onReceiveData({
  58. chunk
  59. });
  60. });
  61. this._pdfDataRangeTransport.addProgressiveDoneListener(() => {
  62. this._onProgressiveDone();
  63. });
  64. this._pdfDataRangeTransport.transportReady();
  65. }
  66. _onReceiveData(args) {
  67. const buffer = new Uint8Array(args.chunk).buffer;
  68. if (args.begin === undefined) {
  69. if (this._fullRequestReader) {
  70. this._fullRequestReader._enqueue(buffer);
  71. } else {
  72. this._queuedChunks.push(buffer);
  73. }
  74. } else {
  75. const found = this._rangeReaders.some(function (rangeReader) {
  76. if (rangeReader._begin !== args.begin) {
  77. return false;
  78. }
  79. rangeReader._enqueue(buffer);
  80. return true;
  81. });
  82. (0, _util.assert)(found, "_onReceiveData - no `PDFDataTransportStreamRangeReader` instance found.");
  83. }
  84. }
  85. get _progressiveDataLength() {
  86. return this._fullRequestReader ? this._fullRequestReader._loaded : 0;
  87. }
  88. _onProgress(evt) {
  89. if (evt.total === undefined) {
  90. const firstReader = this._rangeReaders[0];
  91. if (firstReader && firstReader.onProgress) {
  92. firstReader.onProgress({
  93. loaded: evt.loaded
  94. });
  95. }
  96. } else {
  97. const fullReader = this._fullRequestReader;
  98. if (fullReader && fullReader.onProgress) {
  99. fullReader.onProgress({
  100. loaded: evt.loaded,
  101. total: evt.total
  102. });
  103. }
  104. }
  105. }
  106. _onProgressiveDone() {
  107. if (this._fullRequestReader) {
  108. this._fullRequestReader.progressiveDone();
  109. }
  110. this._progressiveDone = true;
  111. }
  112. _removeRangeReader(reader) {
  113. const i = this._rangeReaders.indexOf(reader);
  114. if (i >= 0) {
  115. this._rangeReaders.splice(i, 1);
  116. }
  117. }
  118. getFullReader() {
  119. (0, _util.assert)(!this._fullRequestReader, "PDFDataTransportStream.getFullReader can only be called once.");
  120. const queuedChunks = this._queuedChunks;
  121. this._queuedChunks = null;
  122. return new PDFDataTransportStreamReader(this, queuedChunks, this._progressiveDone);
  123. }
  124. getRangeReader(begin, end) {
  125. if (end <= this._progressiveDataLength) {
  126. return null;
  127. }
  128. const reader = new PDFDataTransportStreamRangeReader(this, begin, end);
  129. this._pdfDataRangeTransport.requestDataRange(begin, end);
  130. this._rangeReaders.push(reader);
  131. return reader;
  132. }
  133. cancelAllRequests(reason) {
  134. if (this._fullRequestReader) {
  135. this._fullRequestReader.cancel(reason);
  136. }
  137. const readers = this._rangeReaders.slice(0);
  138. readers.forEach(function (rangeReader) {
  139. rangeReader.cancel(reason);
  140. });
  141. this._pdfDataRangeTransport.abort();
  142. }
  143. }
  144. exports.PDFDataTransportStream = PDFDataTransportStream;
  145. class PDFDataTransportStreamReader {
  146. constructor(stream, queuedChunks, progressiveDone = false) {
  147. this._stream = stream;
  148. this._done = progressiveDone || false;
  149. this._filename = null;
  150. this._queuedChunks = queuedChunks || [];
  151. this._loaded = 0;
  152. for (const chunk of this._queuedChunks) {
  153. this._loaded += chunk.byteLength;
  154. }
  155. this._requests = [];
  156. this._headersReady = Promise.resolve();
  157. stream._fullRequestReader = this;
  158. this.onProgress = null;
  159. }
  160. _enqueue(chunk) {
  161. if (this._done) {
  162. return;
  163. }
  164. if (this._requests.length > 0) {
  165. const requestCapability = this._requests.shift();
  166. requestCapability.resolve({
  167. value: chunk,
  168. done: false
  169. });
  170. } else {
  171. this._queuedChunks.push(chunk);
  172. }
  173. this._loaded += chunk.byteLength;
  174. }
  175. get headersReady() {
  176. return this._headersReady;
  177. }
  178. get filename() {
  179. return this._filename;
  180. }
  181. get isRangeSupported() {
  182. return this._stream._isRangeSupported;
  183. }
  184. get isStreamingSupported() {
  185. return this._stream._isStreamingSupported;
  186. }
  187. get contentLength() {
  188. return this._stream._contentLength;
  189. }
  190. async read() {
  191. if (this._queuedChunks.length > 0) {
  192. const chunk = this._queuedChunks.shift();
  193. return {
  194. value: chunk,
  195. done: false
  196. };
  197. }
  198. if (this._done) {
  199. return {
  200. value: undefined,
  201. done: true
  202. };
  203. }
  204. const requestCapability = (0, _util.createPromiseCapability)();
  205. this._requests.push(requestCapability);
  206. return requestCapability.promise;
  207. }
  208. 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. progressiveDone() {
  219. if (this._done) {
  220. return;
  221. }
  222. this._done = true;
  223. }
  224. }
  225. class PDFDataTransportStreamRangeReader {
  226. constructor(stream, begin, end) {
  227. this._stream = stream;
  228. this._begin = begin;
  229. this._end = end;
  230. this._queuedChunk = null;
  231. this._requests = [];
  232. this._done = false;
  233. this.onProgress = null;
  234. }
  235. _enqueue(chunk) {
  236. if (this._done) {
  237. return;
  238. }
  239. if (this._requests.length === 0) {
  240. this._queuedChunk = chunk;
  241. } else {
  242. const requestsCapability = this._requests.shift();
  243. requestsCapability.resolve({
  244. value: chunk,
  245. done: false
  246. });
  247. this._requests.forEach(function (requestCapability) {
  248. requestCapability.resolve({
  249. value: undefined,
  250. done: true
  251. });
  252. });
  253. this._requests = [];
  254. }
  255. this._done = true;
  256. this._stream._removeRangeReader(this);
  257. }
  258. get isStreamingSupported() {
  259. return false;
  260. }
  261. async read() {
  262. if (this._queuedChunk) {
  263. const chunk = this._queuedChunk;
  264. this._queuedChunk = null;
  265. return {
  266. value: chunk,
  267. done: false
  268. };
  269. }
  270. if (this._done) {
  271. return {
  272. value: undefined,
  273. done: true
  274. };
  275. }
  276. const requestCapability = (0, _util.createPromiseCapability)();
  277. this._requests.push(requestCapability);
  278. return requestCapability.promise;
  279. }
  280. cancel(reason) {
  281. this._done = true;
  282. this._requests.forEach(function (requestCapability) {
  283. requestCapability.resolve({
  284. value: undefined,
  285. done: true
  286. });
  287. });
  288. this._requests = [];
  289. this._stream._removeRangeReader(this);
  290. }
  291. }