transport_stream.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. const readers = this._rangeReaders.slice(0);
  140. readers.forEach(function (rangeReader) {
  141. rangeReader.cancel(reason);
  142. });
  143. this._pdfDataRangeTransport.abort();
  144. }
  145. }
  146. exports.PDFDataTransportStream = PDFDataTransportStream;
  147. class PDFDataTransportStreamReader {
  148. constructor(stream, queuedChunks, progressiveDone = false, contentDispositionFilename = null) {
  149. this._stream = stream;
  150. this._done = progressiveDone || false;
  151. this._filename = (0, _display_utils.isPdfFile)(contentDispositionFilename) ? contentDispositionFilename : null;
  152. this._queuedChunks = queuedChunks || [];
  153. this._loaded = 0;
  154. for (const chunk of this._queuedChunks) {
  155. this._loaded += chunk.byteLength;
  156. }
  157. this._requests = [];
  158. this._headersReady = Promise.resolve();
  159. stream._fullRequestReader = this;
  160. this.onProgress = null;
  161. }
  162. _enqueue(chunk) {
  163. if (this._done) {
  164. return;
  165. }
  166. if (this._requests.length > 0) {
  167. const requestCapability = this._requests.shift();
  168. requestCapability.resolve({
  169. value: chunk,
  170. done: false
  171. });
  172. } else {
  173. this._queuedChunks.push(chunk);
  174. }
  175. this._loaded += chunk.byteLength;
  176. }
  177. get headersReady() {
  178. return this._headersReady;
  179. }
  180. get filename() {
  181. return this._filename;
  182. }
  183. get isRangeSupported() {
  184. return this._stream._isRangeSupported;
  185. }
  186. get isStreamingSupported() {
  187. return this._stream._isStreamingSupported;
  188. }
  189. get contentLength() {
  190. return this._stream._contentLength;
  191. }
  192. async read() {
  193. if (this._queuedChunks.length > 0) {
  194. const chunk = this._queuedChunks.shift();
  195. return {
  196. value: chunk,
  197. done: false
  198. };
  199. }
  200. if (this._done) {
  201. return {
  202. value: undefined,
  203. done: true
  204. };
  205. }
  206. const requestCapability = (0, _util.createPromiseCapability)();
  207. this._requests.push(requestCapability);
  208. return requestCapability.promise;
  209. }
  210. cancel(reason) {
  211. this._done = true;
  212. this._requests.forEach(function (requestCapability) {
  213. requestCapability.resolve({
  214. value: undefined,
  215. done: true
  216. });
  217. });
  218. this._requests = [];
  219. }
  220. progressiveDone() {
  221. if (this._done) {
  222. return;
  223. }
  224. this._done = true;
  225. }
  226. }
  227. class PDFDataTransportStreamRangeReader {
  228. constructor(stream, begin, end) {
  229. this._stream = stream;
  230. this._begin = begin;
  231. this._end = end;
  232. this._queuedChunk = null;
  233. this._requests = [];
  234. this._done = false;
  235. this.onProgress = null;
  236. }
  237. _enqueue(chunk) {
  238. if (this._done) {
  239. return;
  240. }
  241. if (this._requests.length === 0) {
  242. this._queuedChunk = chunk;
  243. } else {
  244. const requestsCapability = this._requests.shift();
  245. requestsCapability.resolve({
  246. value: chunk,
  247. done: false
  248. });
  249. this._requests.forEach(function (requestCapability) {
  250. requestCapability.resolve({
  251. value: undefined,
  252. done: true
  253. });
  254. });
  255. this._requests = [];
  256. }
  257. this._done = true;
  258. this._stream._removeRangeReader(this);
  259. }
  260. get isStreamingSupported() {
  261. return false;
  262. }
  263. async read() {
  264. if (this._queuedChunk) {
  265. const chunk = this._queuedChunk;
  266. this._queuedChunk = null;
  267. return {
  268. value: chunk,
  269. done: false
  270. };
  271. }
  272. if (this._done) {
  273. return {
  274. value: undefined,
  275. done: true
  276. };
  277. }
  278. const requestCapability = (0, _util.createPromiseCapability)();
  279. this._requests.push(requestCapability);
  280. return requestCapability.promise;
  281. }
  282. cancel(reason) {
  283. this._done = true;
  284. this._requests.forEach(function (requestCapability) {
  285. requestCapability.resolve({
  286. value: undefined,
  287. done: true
  288. });
  289. });
  290. this._requests = [];
  291. this._stream._removeRangeReader(this);
  292. }
  293. }