transport_stream.js 8.3 KB

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