fetch_stream.js 7.4 KB

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