worker_stream.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.PDFWorkerStream = void 0;
  27. var _util = require("../shared/util.js");
  28. class PDFWorkerStream {
  29. constructor(msgHandler) {
  30. this._msgHandler = msgHandler;
  31. this._contentLength = null;
  32. this._fullRequestReader = null;
  33. this._rangeRequestReaders = [];
  34. }
  35. getFullReader() {
  36. (0, _util.assert)(!this._fullRequestReader, "PDFWorkerStream.getFullReader can only be called once.");
  37. this._fullRequestReader = new PDFWorkerStreamReader(this._msgHandler);
  38. return this._fullRequestReader;
  39. }
  40. getRangeReader(begin, end) {
  41. const reader = new PDFWorkerStreamRangeReader(begin, end, this._msgHandler);
  42. this._rangeRequestReaders.push(reader);
  43. return reader;
  44. }
  45. cancelAllRequests(reason) {
  46. if (this._fullRequestReader) {
  47. this._fullRequestReader.cancel(reason);
  48. }
  49. const readers = this._rangeRequestReaders.slice(0);
  50. readers.forEach(function (reader) {
  51. reader.cancel(reason);
  52. });
  53. }
  54. }
  55. exports.PDFWorkerStream = PDFWorkerStream;
  56. class PDFWorkerStreamReader {
  57. constructor(msgHandler) {
  58. this._msgHandler = msgHandler;
  59. this.onProgress = null;
  60. this._contentLength = null;
  61. this._isRangeSupported = false;
  62. this._isStreamingSupported = false;
  63. const readableStream = this._msgHandler.sendWithStream("GetReader");
  64. this._reader = readableStream.getReader();
  65. this._headersReady = this._msgHandler.sendWithPromise("ReaderHeadersReady").then(data => {
  66. this._isStreamingSupported = data.isStreamingSupported;
  67. this._isRangeSupported = data.isRangeSupported;
  68. this._contentLength = data.contentLength;
  69. });
  70. }
  71. get headersReady() {
  72. return this._headersReady;
  73. }
  74. get contentLength() {
  75. return this._contentLength;
  76. }
  77. get isStreamingSupported() {
  78. return this._isStreamingSupported;
  79. }
  80. get isRangeSupported() {
  81. return this._isRangeSupported;
  82. }
  83. async read() {
  84. const {
  85. value,
  86. done
  87. } = await this._reader.read();
  88. if (done) {
  89. return {
  90. value: undefined,
  91. done: true
  92. };
  93. }
  94. return {
  95. value: value.buffer,
  96. done: false
  97. };
  98. }
  99. cancel(reason) {
  100. this._reader.cancel(reason);
  101. }
  102. }
  103. class PDFWorkerStreamRangeReader {
  104. constructor(begin, end, msgHandler) {
  105. this._msgHandler = msgHandler;
  106. this.onProgress = null;
  107. const readableStream = this._msgHandler.sendWithStream("GetRangeReader", {
  108. begin,
  109. end
  110. });
  111. this._reader = readableStream.getReader();
  112. }
  113. get isStreamingSupported() {
  114. return false;
  115. }
  116. async read() {
  117. const {
  118. value,
  119. done
  120. } = await this._reader.read();
  121. if (done) {
  122. return {
  123. value: undefined,
  124. done: true
  125. };
  126. }
  127. return {
  128. value: value.buffer,
  129. done: false
  130. };
  131. }
  132. cancel(reason) {
  133. this._reader.cancel(reason);
  134. }
  135. }