2
0

fetch_stream.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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.PDFFetchStream = void 0;
  27. var _util = require("../shared/util.js");
  28. var _network_utils = require("./network_utils.js");
  29. ;
  30. function createFetchOptions(headers, withCredentials, abortController) {
  31. return {
  32. method: "GET",
  33. headers,
  34. signal: abortController.signal,
  35. mode: "cors",
  36. credentials: withCredentials ? "include" : "same-origin",
  37. redirect: "follow"
  38. };
  39. }
  40. function createHeaders(httpHeaders) {
  41. const headers = new Headers();
  42. for (const property in httpHeaders) {
  43. const value = httpHeaders[property];
  44. if (typeof value === "undefined") {
  45. continue;
  46. }
  47. headers.append(property, value);
  48. }
  49. return headers;
  50. }
  51. class PDFFetchStream {
  52. constructor(source) {
  53. this.source = source;
  54. this.isHttp = /^https?:/i.test(source.url);
  55. this.httpHeaders = this.isHttp && source.httpHeaders || {};
  56. this._fullRequestReader = null;
  57. this._rangeRequestReaders = [];
  58. }
  59. get _progressiveDataLength() {
  60. return this._fullRequestReader?._loaded ?? 0;
  61. }
  62. getFullReader() {
  63. (0, _util.assert)(!this._fullRequestReader, "PDFFetchStream.getFullReader can only be called once.");
  64. this._fullRequestReader = new PDFFetchStreamReader(this);
  65. return this._fullRequestReader;
  66. }
  67. getRangeReader(begin, end) {
  68. if (end <= this._progressiveDataLength) {
  69. return null;
  70. }
  71. const reader = new PDFFetchStreamRangeReader(this, begin, end);
  72. this._rangeRequestReaders.push(reader);
  73. return reader;
  74. }
  75. cancelAllRequests(reason) {
  76. if (this._fullRequestReader) {
  77. this._fullRequestReader.cancel(reason);
  78. }
  79. for (const reader of this._rangeRequestReaders.slice(0)) {
  80. reader.cancel(reason);
  81. }
  82. }
  83. }
  84. exports.PDFFetchStream = PDFFetchStream;
  85. class PDFFetchStreamReader {
  86. constructor(stream) {
  87. this._stream = stream;
  88. this._reader = null;
  89. this._loaded = 0;
  90. this._filename = null;
  91. const source = stream.source;
  92. this._withCredentials = source.withCredentials || false;
  93. this._contentLength = source.length;
  94. this._headersCapability = (0, _util.createPromiseCapability)();
  95. this._disableRange = source.disableRange || false;
  96. this._rangeChunkSize = source.rangeChunkSize;
  97. if (!this._rangeChunkSize && !this._disableRange) {
  98. this._disableRange = true;
  99. }
  100. this._abortController = new AbortController();
  101. this._isStreamingSupported = !source.disableStream;
  102. this._isRangeSupported = !source.disableRange;
  103. this._headers = createHeaders(this._stream.httpHeaders);
  104. const url = source.url;
  105. fetch(url, createFetchOptions(this._headers, this._withCredentials, this._abortController)).then(response => {
  106. if (!(0, _network_utils.validateResponseStatus)(response.status)) {
  107. throw (0, _network_utils.createResponseStatusError)(response.status, url);
  108. }
  109. this._reader = response.body.getReader();
  110. this._headersCapability.resolve();
  111. const getResponseHeader = name => {
  112. return response.headers.get(name);
  113. };
  114. const {
  115. allowRangeRequests,
  116. suggestedLength
  117. } = (0, _network_utils.validateRangeRequestCapabilities)({
  118. getResponseHeader,
  119. isHttp: this._stream.isHttp,
  120. rangeChunkSize: this._rangeChunkSize,
  121. disableRange: this._disableRange
  122. });
  123. this._isRangeSupported = allowRangeRequests;
  124. this._contentLength = suggestedLength || this._contentLength;
  125. this._filename = (0, _network_utils.extractFilenameFromHeader)(getResponseHeader);
  126. if (!this._isStreamingSupported && this._isRangeSupported) {
  127. this.cancel(new _util.AbortException("Streaming is disabled."));
  128. }
  129. }).catch(this._headersCapability.reject);
  130. this.onProgress = null;
  131. }
  132. get headersReady() {
  133. return this._headersCapability.promise;
  134. }
  135. get filename() {
  136. return this._filename;
  137. }
  138. get contentLength() {
  139. return this._contentLength;
  140. }
  141. get isRangeSupported() {
  142. return this._isRangeSupported;
  143. }
  144. get isStreamingSupported() {
  145. return this._isStreamingSupported;
  146. }
  147. async read() {
  148. await this._headersCapability.promise;
  149. const {
  150. value,
  151. done
  152. } = await this._reader.read();
  153. if (done) {
  154. return {
  155. value,
  156. done
  157. };
  158. }
  159. this._loaded += value.byteLength;
  160. if (this.onProgress) {
  161. this.onProgress({
  162. loaded: this._loaded,
  163. total: this._contentLength
  164. });
  165. }
  166. const buffer = new Uint8Array(value).buffer;
  167. return {
  168. value: buffer,
  169. done: false
  170. };
  171. }
  172. cancel(reason) {
  173. if (this._reader) {
  174. this._reader.cancel(reason);
  175. }
  176. this._abortController.abort();
  177. }
  178. }
  179. class PDFFetchStreamRangeReader {
  180. constructor(stream, begin, end) {
  181. this._stream = stream;
  182. this._reader = null;
  183. this._loaded = 0;
  184. const source = stream.source;
  185. this._withCredentials = source.withCredentials || false;
  186. this._readCapability = (0, _util.createPromiseCapability)();
  187. this._isStreamingSupported = !source.disableStream;
  188. this._abortController = new AbortController();
  189. this._headers = createHeaders(this._stream.httpHeaders);
  190. this._headers.append("Range", `bytes=${begin}-${end - 1}`);
  191. const url = source.url;
  192. fetch(url, createFetchOptions(this._headers, this._withCredentials, this._abortController)).then(response => {
  193. if (!(0, _network_utils.validateResponseStatus)(response.status)) {
  194. throw (0, _network_utils.createResponseStatusError)(response.status, url);
  195. }
  196. this._readCapability.resolve();
  197. this._reader = response.body.getReader();
  198. }).catch(this._readCapability.reject);
  199. this.onProgress = null;
  200. }
  201. get isStreamingSupported() {
  202. return this._isStreamingSupported;
  203. }
  204. async read() {
  205. await this._readCapability.promise;
  206. const {
  207. value,
  208. done
  209. } = await this._reader.read();
  210. if (done) {
  211. return {
  212. value,
  213. done
  214. };
  215. }
  216. this._loaded += value.byteLength;
  217. if (this.onProgress) {
  218. this.onProgress({
  219. loaded: this._loaded
  220. });
  221. }
  222. const buffer = new Uint8Array(value).buffer;
  223. return {
  224. value: buffer,
  225. done: false
  226. };
  227. }
  228. cancel(reason) {
  229. if (this._reader) {
  230. this._reader.cancel(reason);
  231. }
  232. this._abortController.abort();
  233. }
  234. }