fetch_stream.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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.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 && 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 ? 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. const readers = this._rangeRequestReaders.slice(0);
  80. readers.forEach(function (reader) {
  81. reader.cancel(reason);
  82. });
  83. }
  84. }
  85. exports.PDFFetchStream = PDFFetchStream;
  86. class PDFFetchStreamReader {
  87. constructor(stream) {
  88. this._stream = stream;
  89. this._reader = null;
  90. this._loaded = 0;
  91. this._filename = null;
  92. const source = stream.source;
  93. this._withCredentials = source.withCredentials || false;
  94. this._contentLength = source.length;
  95. this._headersCapability = (0, _util.createPromiseCapability)();
  96. this._disableRange = source.disableRange || false;
  97. this._rangeChunkSize = source.rangeChunkSize;
  98. if (!this._rangeChunkSize && !this._disableRange) {
  99. this._disableRange = true;
  100. }
  101. if (typeof AbortController !== "undefined") {
  102. this._abortController = new AbortController();
  103. }
  104. this._isStreamingSupported = !source.disableStream;
  105. this._isRangeSupported = !source.disableRange;
  106. this._headers = createHeaders(this._stream.httpHeaders);
  107. const url = source.url;
  108. fetch(url, createFetchOptions(this._headers, this._withCredentials, this._abortController)).then(response => {
  109. if (!(0, _network_utils.validateResponseStatus)(response.status)) {
  110. throw (0, _network_utils.createResponseStatusError)(response.status, url);
  111. }
  112. this._reader = response.body.getReader();
  113. this._headersCapability.resolve();
  114. const getResponseHeader = name => {
  115. return response.headers.get(name);
  116. };
  117. const {
  118. allowRangeRequests,
  119. suggestedLength
  120. } = (0, _network_utils.validateRangeRequestCapabilities)({
  121. getResponseHeader,
  122. isHttp: this._stream.isHttp,
  123. rangeChunkSize: this._rangeChunkSize,
  124. disableRange: this._disableRange
  125. });
  126. this._isRangeSupported = allowRangeRequests;
  127. this._contentLength = suggestedLength || this._contentLength;
  128. this._filename = (0, _network_utils.extractFilenameFromHeader)(getResponseHeader);
  129. if (!this._isStreamingSupported && this._isRangeSupported) {
  130. this.cancel(new _util.AbortException("Streaming is disabled."));
  131. }
  132. }).catch(this._headersCapability.reject);
  133. this.onProgress = null;
  134. }
  135. get headersReady() {
  136. return this._headersCapability.promise;
  137. }
  138. get filename() {
  139. return this._filename;
  140. }
  141. get contentLength() {
  142. return this._contentLength;
  143. }
  144. get isRangeSupported() {
  145. return this._isRangeSupported;
  146. }
  147. get isStreamingSupported() {
  148. return this._isStreamingSupported;
  149. }
  150. async read() {
  151. await this._headersCapability.promise;
  152. const {
  153. value,
  154. done
  155. } = await this._reader.read();
  156. if (done) {
  157. return {
  158. value,
  159. done
  160. };
  161. }
  162. this._loaded += value.byteLength;
  163. if (this.onProgress) {
  164. this.onProgress({
  165. loaded: this._loaded,
  166. total: this._contentLength
  167. });
  168. }
  169. const buffer = new Uint8Array(value).buffer;
  170. return {
  171. value: buffer,
  172. done: false
  173. };
  174. }
  175. cancel(reason) {
  176. if (this._reader) {
  177. this._reader.cancel(reason);
  178. }
  179. if (this._abortController) {
  180. this._abortController.abort();
  181. }
  182. }
  183. }
  184. class PDFFetchStreamRangeReader {
  185. constructor(stream, begin, end) {
  186. this._stream = stream;
  187. this._reader = null;
  188. this._loaded = 0;
  189. const source = stream.source;
  190. this._withCredentials = source.withCredentials || false;
  191. this._readCapability = (0, _util.createPromiseCapability)();
  192. this._isStreamingSupported = !source.disableStream;
  193. if (typeof AbortController !== "undefined") {
  194. this._abortController = new AbortController();
  195. }
  196. this._headers = createHeaders(this._stream.httpHeaders);
  197. this._headers.append("Range", `bytes=${begin}-${end - 1}`);
  198. const url = source.url;
  199. fetch(url, createFetchOptions(this._headers, this._withCredentials, this._abortController)).then(response => {
  200. if (!(0, _network_utils.validateResponseStatus)(response.status)) {
  201. throw (0, _network_utils.createResponseStatusError)(response.status, url);
  202. }
  203. this._readCapability.resolve();
  204. this._reader = response.body.getReader();
  205. }).catch(reason => {
  206. if (reason && reason.name === "AbortError") {
  207. return;
  208. }
  209. throw reason;
  210. });
  211. this.onProgress = null;
  212. }
  213. get isStreamingSupported() {
  214. return this._isStreamingSupported;
  215. }
  216. async read() {
  217. await this._readCapability.promise;
  218. const {
  219. value,
  220. done
  221. } = await this._reader.read();
  222. if (done) {
  223. return {
  224. value,
  225. done
  226. };
  227. }
  228. this._loaded += value.byteLength;
  229. if (this.onProgress) {
  230. this.onProgress({
  231. loaded: this._loaded
  232. });
  233. }
  234. const buffer = new Uint8Array(value).buffer;
  235. return {
  236. value: buffer,
  237. done: false
  238. };
  239. }
  240. cancel(reason) {
  241. if (this._reader) {
  242. this._reader.cancel(reason);
  243. }
  244. if (this._abortController) {
  245. this._abortController.abort();
  246. }
  247. }
  248. }