worker_stream.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.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. for (const reader of this._rangeRequestReaders.slice(0)) {
  50. reader.cancel(reason);
  51. }
  52. }
  53. }
  54. exports.PDFWorkerStream = PDFWorkerStream;
  55. class PDFWorkerStreamReader {
  56. constructor(msgHandler) {
  57. this._msgHandler = msgHandler;
  58. this.onProgress = null;
  59. this._contentLength = null;
  60. this._isRangeSupported = false;
  61. this._isStreamingSupported = false;
  62. const readableStream = this._msgHandler.sendWithStream("GetReader");
  63. this._reader = readableStream.getReader();
  64. this._headersReady = this._msgHandler.sendWithPromise("ReaderHeadersReady").then(data => {
  65. this._isStreamingSupported = data.isStreamingSupported;
  66. this._isRangeSupported = data.isRangeSupported;
  67. this._contentLength = data.contentLength;
  68. });
  69. }
  70. get headersReady() {
  71. return this._headersReady;
  72. }
  73. get contentLength() {
  74. return this._contentLength;
  75. }
  76. get isStreamingSupported() {
  77. return this._isStreamingSupported;
  78. }
  79. get isRangeSupported() {
  80. return this._isRangeSupported;
  81. }
  82. async read() {
  83. const {
  84. value,
  85. done
  86. } = await this._reader.read();
  87. if (done) {
  88. return {
  89. value: undefined,
  90. done: true
  91. };
  92. }
  93. return {
  94. value: value.buffer,
  95. done: false
  96. };
  97. }
  98. cancel(reason) {
  99. this._reader.cancel(reason);
  100. }
  101. }
  102. class PDFWorkerStreamRangeReader {
  103. constructor(begin, end, msgHandler) {
  104. this._msgHandler = msgHandler;
  105. this.onProgress = null;
  106. const readableStream = this._msgHandler.sendWithStream("GetRangeReader", {
  107. begin,
  108. end
  109. });
  110. this._reader = readableStream.getReader();
  111. }
  112. get isStreamingSupported() {
  113. return false;
  114. }
  115. async read() {
  116. const {
  117. value,
  118. done
  119. } = await this._reader.read();
  120. if (done) {
  121. return {
  122. value: undefined,
  123. done: true
  124. };
  125. }
  126. return {
  127. value: value.buffer,
  128. done: false
  129. };
  130. }
  131. cancel(reason) {
  132. this._reader.cancel(reason);
  133. }
  134. }