interfaces.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* Copyright 2018 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. /**
  16. * Interface that represents PDF data transport. If possible, it allows
  17. * progressively load entire or fragment of the PDF binary data.
  18. *
  19. * @interface
  20. */
  21. class IPDFStream {
  22. /**
  23. * Gets a reader for the entire PDF data.
  24. * @returns {IPDFStreamReader}
  25. */
  26. getFullReader() {
  27. return null;
  28. }
  29. /**
  30. * Gets a reader for the range of the PDF data.
  31. * @param {number} begin - the start offset of the data.
  32. * @param {number} end - the end offset of the data.
  33. * @returns {IPDFStreamRangeReader}
  34. */
  35. getRangeReader(begin, end) {
  36. return null;
  37. }
  38. /**
  39. * Cancels all opened reader and closes all their opened requests.
  40. * @param {Object} reason - the reason for cancelling
  41. */
  42. cancelAllRequests(reason) {}
  43. }
  44. /**
  45. * Interface for a PDF binary data reader.
  46. *
  47. * @interface
  48. */
  49. class IPDFStreamReader {
  50. constructor() {
  51. /**
  52. * Sets or gets the progress callback. The callback can be useful when the
  53. * isStreamingSupported property of the object is defined as false.
  54. * The callback is called with one parameter: an object with the loaded and
  55. * total properties.
  56. */
  57. this.onProgress = null;
  58. }
  59. /**
  60. * Gets a promise that is resolved when the headers and other metadata of
  61. * the PDF data stream are available.
  62. * @type {Promise}
  63. */
  64. get headersReady() {
  65. return Promise.resolve();
  66. }
  67. /**
  68. * Gets the Content-Disposition filename. It is defined after the headersReady
  69. * promise is resolved.
  70. * @type {string|null} The filename, or `null` if the Content-Disposition
  71. * header is missing/invalid.
  72. */
  73. get filename() {
  74. return null;
  75. }
  76. /**
  77. * Gets PDF binary data length. It is defined after the headersReady promise
  78. * is resolved.
  79. * @type {number} The data length (or 0 if unknown).
  80. */
  81. get contentLength() {
  82. return 0;
  83. }
  84. /**
  85. * Gets ability of the stream to handle range requests. It is defined after
  86. * the headersReady promise is resolved. Rejected when the reader is cancelled
  87. * or an error occurs.
  88. * @type {boolean}
  89. */
  90. get isRangeSupported() {
  91. return false;
  92. }
  93. /**
  94. * Gets ability of the stream to progressively load binary data. It is defined
  95. * after the headersReady promise is resolved.
  96. * @type {boolean}
  97. */
  98. get isStreamingSupported() {
  99. return false;
  100. }
  101. /**
  102. * Requests a chunk of the binary data. The method returns the promise, which
  103. * is resolved into object with properties "value" and "done". If the done
  104. * is set to true, then the stream has reached its end, otherwise the value
  105. * contains binary data. Cancelled requests will be resolved with the done is
  106. * set to true.
  107. * @returns {Promise}
  108. */
  109. async read() {}
  110. /**
  111. * Cancels all pending read requests and closes the stream.
  112. * @param {Object} reason
  113. */
  114. cancel(reason) {}
  115. }
  116. /**
  117. * Interface for a PDF binary data fragment reader.
  118. *
  119. * @interface
  120. */
  121. class IPDFStreamRangeReader {
  122. constructor() {
  123. /**
  124. * Sets or gets the progress callback. The callback can be useful when the
  125. * isStreamingSupported property of the object is defined as false.
  126. * The callback is called with one parameter: an object with the loaded
  127. * property.
  128. */
  129. this.onProgress = null;
  130. }
  131. /**
  132. * Gets ability of the stream to progressively load binary data.
  133. * @type {boolean}
  134. */
  135. get isStreamingSupported() {
  136. return false;
  137. }
  138. /**
  139. * Requests a chunk of the binary data. The method returns the promise, which
  140. * is resolved into object with properties "value" and "done". If the done
  141. * is set to true, then the stream has reached its end, otherwise the value
  142. * contains binary data. Cancelled requests will be resolved with the done is
  143. * set to true.
  144. * @returns {Promise}
  145. */
  146. async read() {}
  147. /**
  148. * Cancels all pending read requests and closes the stream.
  149. * @param {Object} reason
  150. */
  151. cancel(reason) {}
  152. }
  153. export { IPDFStream, IPDFStreamRangeReader, IPDFStreamReader };