transport_stream.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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. this._rangeReaders[0]?.onProgress?.({
  93. loaded: evt.loaded
  94. });
  95. } else {
  96. this._fullRequestReader?.onProgress?.({
  97. loaded: evt.loaded,
  98. total: evt.total
  99. });
  100. }
  101. }
  102. _onProgressiveDone() {
  103. this._fullRequestReader?.progressiveDone();
  104. this._progressiveDone = true;
  105. }
  106. _removeRangeReader(reader) {
  107. const i = this._rangeReaders.indexOf(reader);
  108. if (i >= 0) {
  109. this._rangeReaders.splice(i, 1);
  110. }
  111. }
  112. getFullReader() {
  113. (0, _util.assert)(!this._fullRequestReader, "PDFDataTransportStream.getFullReader can only be called once.");
  114. const queuedChunks = this._queuedChunks;
  115. this._queuedChunks = null;
  116. return new PDFDataTransportStreamReader(this, queuedChunks, this._progressiveDone, this._contentDispositionFilename);
  117. }
  118. getRangeReader(begin, end) {
  119. if (end <= this._progressiveDataLength) {
  120. return null;
  121. }
  122. const reader = new PDFDataTransportStreamRangeReader(this, begin, end);
  123. this._pdfDataRangeTransport.requestDataRange(begin, end);
  124. this._rangeReaders.push(reader);
  125. return reader;
  126. }
  127. cancelAllRequests(reason) {
  128. this._fullRequestReader?.cancel(reason);
  129. for (const reader of this._rangeReaders.slice(0)) {
  130. reader.cancel(reason);
  131. }
  132. this._pdfDataRangeTransport.abort();
  133. }
  134. }
  135. exports.PDFDataTransportStream = PDFDataTransportStream;
  136. class PDFDataTransportStreamReader {
  137. constructor(stream, queuedChunks, progressiveDone = false, contentDispositionFilename = null) {
  138. this._stream = stream;
  139. this._done = progressiveDone || false;
  140. this._filename = (0, _display_utils.isPdfFile)(contentDispositionFilename) ? contentDispositionFilename : null;
  141. this._queuedChunks = queuedChunks || [];
  142. this._loaded = 0;
  143. for (const chunk of this._queuedChunks) {
  144. this._loaded += chunk.byteLength;
  145. }
  146. this._requests = [];
  147. this._headersReady = Promise.resolve();
  148. stream._fullRequestReader = this;
  149. this.onProgress = null;
  150. }
  151. _enqueue(chunk) {
  152. if (this._done) {
  153. return;
  154. }
  155. if (this._requests.length > 0) {
  156. const requestCapability = this._requests.shift();
  157. requestCapability.resolve({
  158. value: chunk,
  159. done: false
  160. });
  161. } else {
  162. this._queuedChunks.push(chunk);
  163. }
  164. this._loaded += chunk.byteLength;
  165. }
  166. get headersReady() {
  167. return this._headersReady;
  168. }
  169. get filename() {
  170. return this._filename;
  171. }
  172. get isRangeSupported() {
  173. return this._stream._isRangeSupported;
  174. }
  175. get isStreamingSupported() {
  176. return this._stream._isStreamingSupported;
  177. }
  178. get contentLength() {
  179. return this._stream._contentLength;
  180. }
  181. async read() {
  182. if (this._queuedChunks.length > 0) {
  183. const chunk = this._queuedChunks.shift();
  184. return {
  185. value: chunk,
  186. done: false
  187. };
  188. }
  189. if (this._done) {
  190. return {
  191. value: undefined,
  192. done: true
  193. };
  194. }
  195. const requestCapability = (0, _util.createPromiseCapability)();
  196. this._requests.push(requestCapability);
  197. return requestCapability.promise;
  198. }
  199. cancel(reason) {
  200. this._done = true;
  201. for (const requestCapability of this._requests) {
  202. requestCapability.resolve({
  203. value: undefined,
  204. done: true
  205. });
  206. }
  207. this._requests.length = 0;
  208. }
  209. progressiveDone() {
  210. if (this._done) {
  211. return;
  212. }
  213. this._done = true;
  214. }
  215. }
  216. class PDFDataTransportStreamRangeReader {
  217. constructor(stream, begin, end) {
  218. this._stream = stream;
  219. this._begin = begin;
  220. this._end = end;
  221. this._queuedChunk = null;
  222. this._requests = [];
  223. this._done = false;
  224. this.onProgress = null;
  225. }
  226. _enqueue(chunk) {
  227. if (this._done) {
  228. return;
  229. }
  230. if (this._requests.length === 0) {
  231. this._queuedChunk = chunk;
  232. } else {
  233. const requestsCapability = this._requests.shift();
  234. requestsCapability.resolve({
  235. value: chunk,
  236. done: false
  237. });
  238. for (const requestCapability of this._requests) {
  239. requestCapability.resolve({
  240. value: undefined,
  241. done: true
  242. });
  243. }
  244. this._requests.length = 0;
  245. }
  246. this._done = true;
  247. this._stream._removeRangeReader(this);
  248. }
  249. get isStreamingSupported() {
  250. return false;
  251. }
  252. async read() {
  253. if (this._queuedChunk) {
  254. const chunk = this._queuedChunk;
  255. this._queuedChunk = null;
  256. return {
  257. value: chunk,
  258. done: false
  259. };
  260. }
  261. if (this._done) {
  262. return {
  263. value: undefined,
  264. done: true
  265. };
  266. }
  267. const requestCapability = (0, _util.createPromiseCapability)();
  268. this._requests.push(requestCapability);
  269. return requestCapability.promise;
  270. }
  271. cancel(reason) {
  272. this._done = true;
  273. for (const requestCapability of this._requests) {
  274. requestCapability.resolve({
  275. value: undefined,
  276. done: true
  277. });
  278. }
  279. this._requests.length = 0;
  280. this._stream._removeRangeReader(this);
  281. }
  282. }